package onvif import ( "bytes" "regexp" "time" ) const ServiceGetServiceCapabilities = "GetServiceCapabilities" const ( DeviceGetCapabilities = "GetCapabilities" DeviceGetDeviceInformation = "GetDeviceInformation" DeviceGetDiscoveryMode = "GetDiscoveryMode" DeviceGetDNS = "GetDNS" DeviceGetHostname = "GetHostname" DeviceGetNetworkDefaultGateway = "GetNetworkDefaultGateway" DeviceGetNetworkInterfaces = "GetNetworkInterfaces" DeviceGetNetworkProtocols = "GetNetworkProtocols" DeviceGetNTP = "GetNTP" DeviceGetScopes = "GetScopes" DeviceGetServices = "GetServices" DeviceGetSystemDateAndTime = "GetSystemDateAndTime" DeviceSystemReboot = "SystemReboot" ) const ( MediaGetAudioEncoderConfigurations = "GetAudioEncoderConfigurations" MediaGetAudioSources = "GetAudioSources" MediaGetAudioSourceConfigurations = "GetAudioSourceConfigurations" MediaGetProfile = "GetProfile" MediaGetProfiles = "GetProfiles" MediaGetSnapshotUri = "GetSnapshotUri" MediaGetStreamUri = "GetStreamUri" MediaGetVideoEncoderConfigurations = "GetVideoEncoderConfigurations" MediaGetVideoSources = "GetVideoSources" MediaGetVideoSourceConfiguration = "GetVideoSourceConfiguration" MediaGetVideoSourceConfigurations = "GetVideoSourceConfigurations" ) func GetRequestAction(b []byte) string { // // re := regexp.MustCompile(`Body[^<]+<([^ />]+)`) m := re.FindSubmatch(b) if len(m) != 2 { return "" } if i := bytes.IndexByte(m[1], ':'); i > 0 { return string(m[1][i+1:]) } return string(m[1]) } func GetCapabilitiesResponse(host string) []byte { e := NewEnvelope() e.Append(` http://`, host, `/onvif/device_service http://`, host, `/onvif/media_service false false true `) return e.Bytes() } func GetServicesResponse(host string) []byte { e := NewEnvelope() e.Append(` http://www.onvif.org/ver10/device/wsdl http://`, host, `/onvif/device_service 25 http://www.onvif.org/ver10/media/wsdl http://`, host, `/onvif/media_service 25 `) return e.Bytes() } func GetSystemDateAndTimeResponse() []byte { loc := time.Now() utc := loc.UTC() e := NewEnvelope() e.Appendf(` NTP true %s %d%d%d %d%d%d %d%d%d %d%d%d `, GetPosixTZ(loc), utc.Hour(), utc.Minute(), utc.Second(), utc.Year(), utc.Month(), utc.Day(), loc.Hour(), loc.Minute(), loc.Second(), loc.Year(), loc.Month(), loc.Day(), ) return e.Bytes() } func GetDeviceInformationResponse(manuf, model, firmware, serial string) []byte { e := NewEnvelope() e.Append(` `, manuf, ` `, model, ` `, firmware, ` `, serial, ` 1.00 `) return e.Bytes() } func GetMediaServiceCapabilitiesResponse() []byte { e := NewEnvelope() e.Append(` `) return e.Bytes() } func GetProfilesResponse(names []string) []byte { e := NewEnvelope() e.Append(` `) for _, name := range names { appendProfile(e, "Profiles", name) } e.Append(``) return e.Bytes() } func GetProfileResponse(name string) []byte { e := NewEnvelope() e.Append(` `) appendProfile(e, "Profile", name) e.Append(``) return e.Bytes() } func appendProfile(e *Envelope, tag, name string) { e.Append(` `, name, ` VSC `, name, ` VEC H264 19201080 `) } func GetVideoSourceConfigurationResponse(name string) []byte { e := NewEnvelope() e.Append(` VSC `, name, ` `) return e.Bytes() } func GetVideoSourcesResponse(names []string) []byte { e := NewEnvelope() e.Append(` `) for _, name := range names { e.Append(` 30.000000 19201080 `) } e.Append(``) return e.Bytes() } func GetStreamUriResponse(uri string) []byte { e := NewEnvelope() e.Append(``, uri, ``) return e.Bytes() } func GetSnapshotUriResponse(uri string) []byte { e := NewEnvelope() e.Append(``, uri, ``) return e.Bytes() } func StaticResponse(operation string) []byte { switch operation { case DeviceGetSystemDateAndTime: return GetSystemDateAndTimeResponse() } e := NewEnvelope() e.Append(responses[operation]) b := e.Bytes() if operation == DeviceGetNetworkInterfaces { println() } return b } var responses = map[string]string{ DeviceGetDiscoveryMode: `Discoverable`, DeviceGetDNS: ``, DeviceGetHostname: ``, DeviceGetNetworkDefaultGateway: ``, DeviceGetNTP: ``, DeviceSystemReboot: `OK`, DeviceGetNetworkInterfaces: ``, DeviceGetNetworkProtocols: ``, DeviceGetScopes: ` Fixedonvif://www.onvif.org/name/go2rtc Fixedonvif://www.onvif.org/location/github Fixedonvif://www.onvif.org/Profile/Streaming Fixedonvif://www.onvif.org/type/Network_Video_Transmitter `, }