Implement some new API

This commit is contained in:
Radhi Fadlillah
2017-05-22 12:18:02 +07:00
parent 05cac4b759
commit 2e984a0379
3 changed files with 140 additions and 7 deletions

13
TODO.md
View File

@@ -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

127
device.go
View File

@@ -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: "<tds:GetDiscoveryMode/>",
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: "<tds:GetScopes/>",
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: "<tds:GetHostname/>",
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: "<tds:GetDNS/>",
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: "<tds:GetNetworkInterfaces/>",
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 {

View File

@@ -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
}