forked from deepch/go-onvif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
234 lines (200 loc) · 6.07 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package onvif
import (
"strings"
)
var deviceXMLNs = []string{
`xmlns:tds="http://www.onvif.org/ver10/device/wsdl"`,
`xmlns:tt="http://www.onvif.org/ver10/schema"`,
`xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"`,
}
// GetInformation fetch information of ONVIF camera
func (device Device) GetInformation() (DeviceInformation, error) {
// Create SOAP
soap := SOAP{
Body: "<tds:GetDeviceInformation/>",
XMLNs: deviceXMLNs,
}
// Send SOAP request
response, err := soap.SendRequest(device.XAddr)
if err != nil {
return DeviceInformation{}, err
}
// Parse response to interface
deviceInfo, err := response.ValueForPath("Envelope.Body.GetDeviceInformationResponse")
if err != nil {
return DeviceInformation{}, err
}
// Parse interface to struct
result := DeviceInformation{}
if mapInfo, ok := deviceInfo.(map[string]interface{}); ok {
result.Manufacturer = interfaceToString(mapInfo["Manufacturer"])
result.Model = interfaceToString(mapInfo["Model"])
result.FirmwareVersion = interfaceToString(mapInfo["FirmwareVersion"])
result.SerialNumber = interfaceToString(mapInfo["SerialNumber"])
result.HardwareID = interfaceToString(mapInfo["HardwareId"])
}
return result, nil
}
// GetCapabilities fetch info of ONVIF camera's capabilities
func (device Device) GetCapabilities() (DeviceCapabilities, error) {
// Create SOAP
soap := SOAP{
XMLNs: deviceXMLNs,
Body: `<tds:GetCapabilities>
<tds:Category>All</tds:Category>
</tds:GetCapabilities>`,
}
// Send SOAP request
response, err := soap.SendRequest(device.XAddr)
if err != nil {
return DeviceCapabilities{}, err
}
// Get network capabilities
envelopeBodyPath := "Envelope.Body.GetCapabilitiesResponse.Capabilities"
ifaceNetCap, err := response.ValueForPath(envelopeBodyPath + ".Device.Network")
if err != nil {
return DeviceCapabilities{}, err
}
netCap := NetworkCapabilities{}
if mapNetCap, ok := ifaceNetCap.(map[string]interface{}); ok {
netCap.DynDNS = interfaceToBool(mapNetCap["DynDNS"])
netCap.IPFilter = interfaceToBool(mapNetCap["IPFilter"])
netCap.IPVersion6 = interfaceToBool(mapNetCap["IPVersion6"])
netCap.ZeroConfig = interfaceToBool(mapNetCap["ZeroConfiguration"])
}
// Get events capabilities
ifaceEventsCap, err := response.ValueForPath(envelopeBodyPath + ".Events")
if err != nil {
return DeviceCapabilities{}, err
}
eventsCap := make(map[string]bool)
if mapEventsCap, ok := ifaceEventsCap.(map[string]interface{}); ok {
for key, value := range mapEventsCap {
if strings.ToLower(key) == "xaddr" {
continue
}
key = strings.Replace(key, "WS", "", 1)
eventsCap[key] = interfaceToBool(value)
}
}
// Get streaming capabilities
ifaceStreamingCap, err := response.ValueForPath(envelopeBodyPath + ".Media.StreamingCapabilities")
if err != nil {
return DeviceCapabilities{}, err
}
streamingCap := make(map[string]bool)
if mapStreamingCap, ok := ifaceStreamingCap.(map[string]interface{}); ok {
for key, value := range mapStreamingCap {
key = strings.Replace(key, "_", " ", -1)
streamingCap[key] = interfaceToBool(value)
}
}
// Create final result
deviceCapabilities := DeviceCapabilities{
Network: netCap,
Events: eventsCap,
Streaming: streamingCap,
}
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) Ptz(Token, x, y, z string) error {
// Create SOAP
soap := SOAP{
Body: `<tptz:ContinuousMove>
<tptz:ProfileToken>` + Token + `</tptz:ProfileToken>
<tptz:Velocity>
<tt:PanTilt x="` + x + `" y="` + y + `" space="">
</tt:PanTilt>
<tt:Zoom x="` + z + `" space="">
</tt:Zoom>
</tptz:Velocity>
</tptz:ContinuousMove>`,
XMLNs: deviceXMLNs,
}
// Send SOAP request
_, err := soap.SendRequest(device.XAddr)
return err
}
func (device Device) PtzStop(Token, x, y, z string) error {
// Create SOAP
soap := SOAP{
Body: `<tptz:Stop>
<tptz:ProfileToken>` + Token + `</tptz:ProfileToken>
<tptz:PanTilt>false</tptz:PanTilt>
<tptz:Zoom>false</tptz:Zoom>
</tptz:Stop>`,
XMLNs: deviceXMLNs,
}
// Send SOAP request
_, err := soap.SendRequest(device.XAddr)
return err
}
// 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"])
}
return hostnameInfo, nil
}