-
Notifications
You must be signed in to change notification settings - Fork 10
/
onvifdevice.go
141 lines (122 loc) · 4.31 KB
/
onvifdevice.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
package onvif4go
import (
"errors"
"net/url"
"reflect"
"strings"
"time"
"github.com/faceterteam/onvif4go/onvif"
"github.com/faceterteam/onvif4go/xsd"
)
type OnvifDevice struct {
xaddr string
auth onvifAuth
Capabilities onvif.Capabilities
endpoints map[string]onvifCaller
Device *DeviceService
Media *MediaService
Events *EventsService
}
func (onvifDevice *OnvifDevice) Call(request, response interface{}) error {
pkgPath := strings.Split(reflect.TypeOf(request).PkgPath(), "/")
pkg := pkgPath[len(pkgPath)-1]
client, ok := onvifDevice.On(strings.ToLower(pkg))
if !ok {
return errors.New("no endpoint")
}
return client.Call(request, response)
}
func (onvifDevice *OnvifDevice) On(endpoint string) (caller onvifCaller, ok bool) {
caller, ok = onvifDevice.endpoints[endpoint]
return
}
func NewOnvifDevice(xaddr string) *OnvifDevice {
return &OnvifDevice{
xaddr: xaddr,
endpoints: make(map[string]onvifCaller),
}
}
func (onvifDevice *OnvifDevice) Auth(login, password string) {
onvifDevice.auth.login = login
onvifDevice.auth.password = password
}
func fixXAddr(uri xsd.AnyURI, xaddr string) string {
u, _ := url.Parse(string(uri))
u.Host = xaddr
return u.String()
}
func (onvifDevice *OnvifDevice) Initialize() error {
deviceService := NewDeviceService(onvifDevice)
currentTime := time.Now().UTC()
systemDateAndTimeResponse, err := deviceService.WithoutAuth().GetSystemDateAndTime()
if err == nil {
//TODO: warn if GetSystemDateAndTime return err
deviceTime, _ := systemDateAndTimeResponse.SystemDateAndTime.GetUTCTime()
//TODO: error mean parse error of Timezone. deviceTime equal time.Now().UTC()
onvifDevice.auth.timeDiff = deviceTime.Sub(currentTime)
}
capabilitiesResponse, err := deviceService.GetCapabilities()
if err != nil {
return err
}
capabilities := capabilitiesResponse.Capabilities
if capabilities.Device != nil {
onvifDevice.endpoints["device"] = deviceService.Client
onvifDevice.Device = deviceService
}
if capabilities.Events != nil {
xaddr := fixXAddr(capabilities.Events.XAddr, onvifDevice.xaddr)
onvifDevice.Events = NewEventsService(xaddr, onvifDevice)
onvifDevice.endpoints["events"] = onvifDevice.Events.Client
}
if capabilities.Media != nil {
mediaURI := fixXAddr(capabilities.Media.XAddr, onvifDevice.xaddr)
onvifDevice.Media = NewMediaService(mediaURI, onvifDevice)
onvifDevice.endpoints["media"] = onvifDevice.Media.Client
}
if capabilities.Imaging != nil {
xaddr := fixXAddr(capabilities.Imaging.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["imaging"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if capabilities.Analytics != nil {
xaddr := fixXAddr(capabilities.Analytics.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["analytics"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if capabilities.PTZ != nil {
xaddr := fixXAddr(capabilities.PTZ.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["ptz"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if capabilities.Extension != nil {
extension := capabilities.Extension
if extension.AnalyticsDevice != nil {
xaddr := fixXAddr(extension.AnalyticsDevice.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["analyticsdevice"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.DeviceIO != nil {
xaddr := fixXAddr(extension.DeviceIO.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["deviceio"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.Display != nil {
xaddr := fixXAddr(extension.Display.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["display"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.Receiver != nil {
xaddr := fixXAddr(extension.Receiver.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["receiver"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.Recording != nil {
xaddr := fixXAddr(extension.Recording.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["recording"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.Replay != nil {
xaddr := fixXAddr(extension.Replay.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["replay"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
if extension.Search != nil {
xaddr := fixXAddr(extension.Search.XAddr, onvifDevice.xaddr)
onvifDevice.endpoints["search"] = NewOnvifClient(xaddr, &onvifDevice.auth)
}
}
onvifDevice.Capabilities = capabilities
return nil
}