diff --git a/TODO.md b/TODO.md index 7634ab2..2ff9208 100644 --- a/TODO.md +++ b/TODO.md @@ -3,18 +3,17 @@ - [X] getDeviceInformation - [X] getSystemDateAndTime - [X] getCapabilities - - [ ] getWsdlUrl - - [ ] getDiscoveryMode - - [ ] getScopes + - [X] getDiscoveryMode + - [X] getScopes + - [X] getHostname + - [X] getDNS + - [ ] getNetworkInterfaces + - [ ] getNetworkProtocols - [ ] setScopes - [ ] addScopes - [ ] removeScopes - - [ ] getHostname - [ ] setHostname - - [ ] getDNS - [ ] setDNS - - [ ] getNetworkInterfaces - - [ ] getNetworkProtocols - [ ] setNetworkProtocols - [ ] getNetworkDefaultGateway - [ ] setNetworkDefaultGateway diff --git a/device.go b/device.go index ea43c5c..678f932 100644 --- a/device.go +++ b/device.go @@ -2,6 +2,7 @@ package onvif import ( "encoding/json" + "fmt" "strings" ) @@ -145,6 +146,132 @@ func (device Device) GetCapabilities() (DeviceCapabilities, error) { return deviceCapabilities, nil } +// GetDiscoveryMode fetch network discovery mode of an ONVIF camera +func (device Device) GetDiscoveryMode() (string, error) { + // Create SOAP + soap := SOAP{ + Body: "", + XMLNs: deviceXMLNs, + } + + // Send SOAP request + response, err := soap.SendRequest(device.XAddr) + if err != nil { + return "", err + } + + // Parse response + discoveryMode, _ := response.ValueForPathString("Envelope.Body.GetDiscoveryModeResponse.DiscoveryMode") + return discoveryMode, nil +} + +// GetScopes fetch scopes of an ONVIF camera +func (device Device) GetScopes() ([]string, error) { + // Create SOAP + soap := SOAP{ + Body: "", + XMLNs: deviceXMLNs, + } + + // Send SOAP request + response, err := soap.SendRequest(device.XAddr) + if err != nil { + return nil, err + } + + // Parse response to interface + ifaceScopes, err := response.ValuesForPath("Envelope.Body.GetScopesResponse.Scopes") + if err != nil { + return nil, err + } + + // Convert interface to array of scope + scopes := []string{} + for _, ifaceScope := range ifaceScopes { + if mapScope, ok := ifaceScope.(map[string]interface{}); ok { + scope := interfaceToString(mapScope["ScopeItem"]) + scopes = append(scopes, scope) + } + } + + return scopes, nil +} + +// GetHostname fetch hostname of an ONVIF camera +func (device Device) GetHostname() (HostnameInformation, error) { + // Create SOAP + soap := SOAP{ + Body: "", + XMLNs: deviceXMLNs, + } + + // Send SOAP request + response, err := soap.SendRequest(device.XAddr) + if err != nil { + return HostnameInformation{}, err + } + + // Parse response to interface + ifaceHostInfo, err := response.ValueForPath("Envelope.Body.GetHostnameResponse.HostnameInformation") + if err != nil { + return HostnameInformation{}, err + } + + // Parse interface to struct + hostnameInfo := HostnameInformation{} + if mapHostInfo, ok := ifaceHostInfo.(map[string]interface{}); ok { + hostnameInfo.Name = interfaceToString(mapHostInfo["Name"]) + hostnameInfo.FromDHCP = interfaceToBool(mapHostInfo["FromDHCP"]) + hostnameInfo.Extension = interfaceToString(mapHostInfo["Extension"]) + } + + return hostnameInfo, nil +} + +// GetDNS fetch DNS of an ONVIF camera +func (device Device) GetDNS() (string, error) { + // Create SOAP + soap := SOAP{ + Body: "", + XMLNs: deviceXMLNs, + } + + // Send SOAP request + response, err := soap.SendRequest(device.XAddr) + if err != nil { + return "", err + } + + bt, _ := response.JsonIndent("", " ") + fmt.Println(string(bt)) + + // Parse response + DNS, _ := response.ValueForPathString("Envelope.Body.GetDNSResponse.DNSInformation") + return DNS, nil +} + +// GetDNS fetch DNS of an ONVIF camera +func (device Device) GetNetworkInterfaces() (string, error) { + // Create SOAP + soap := SOAP{ + Body: "", + XMLNs: deviceXMLNs, + } + + // Send SOAP request + response, err := soap.SendRequest(device.XAddr) + if err != nil { + return "", err + } + + bt, _ := response.JsonIndent("", " ") + fmt.Println(string(bt)) + + // Parse response + DNS, _ := response.ValueForPathString("Envelope.Body.GetDNSResponse.DNSInformation") + return DNS, nil +} + func interfaceToStruct(src, dst interface{}) error { bt, err := json.Marshal(&src) if err != nil { diff --git a/model.go b/model.go index fa5b4a1..ca9eb30 100644 --- a/model.go +++ b/model.go @@ -34,3 +34,10 @@ type DeviceCapabilities struct { Streaming map[string]bool PTZ bool } + +// HostnameInformation contains hostname info of an ONVIF camera +type HostnameInformation struct { + Name string + FromDHCP bool + Extension string +}