-
Notifications
You must be signed in to change notification settings - Fork 0
/
onvif.go
190 lines (157 loc) · 4.58 KB
/
onvif.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
package hikrec
import (
"strconv"
"time"
)
type envelope struct {
Header header `xml:"Header"`
Body body `xml:"Body"`
}
type header struct {
Action string `xml:"Action"`
}
type body struct {
CreatePullPointSubscriptionResponse createPullPointSubscriptionResponse `xml:"CreatePullPointSubscriptionResponse"`
PullMessagesResponse pullMessagesResponse `xml:"PullMessagesResponse"`
}
type createPullPointSubscriptionResponse struct {
SubscriptionReference subscriptionReference `xml:"SubscriptionReference"`
CurrentTime string `xml:"CurrentTime"`
TerminationTime string `xml:"TerminationTime"`
}
type subscriptionReference struct {
Address string `xml:"Address"`
}
type pullMessagesResponse struct {
CurrentTime string `xml:"CurrentTime"`
TerminationTime string `xml:"TerminationTime"`
NotificationMessage notificationMessage `xml:"NotificationMessage"`
}
type notificationMessage struct {
Topic string `xml:"Topic"`
Messages messages `xml:"Message"`
}
type messages struct {
Messages []message `xml:"Message"`
}
type message struct {
Source source `xml:"Source"`
Data data `xml:"Data"`
}
type source struct {
Items []simpleItem `xml:"SimpleItem"`
}
type data struct {
Items []simpleItem `xml:"SimpleItem"`
}
type simpleItem struct {
Name string `xml:"Name,attr"`
Value string `xml:"Value,attr"`
}
type Device struct {
ID string
Name string
Url string
Username string
Password string
}
func NewDevice(url, username, password string) *Device {
return &Device{
Url: url,
Username: username,
Password: password,
}
}
func (device *Device) createPullPointSubscription() (createPullPointSubscriptionResponse, error) {
soap := soap{
User: device.Username,
Password: device.Password,
Action: "http://www.onvif.org/ver10/events/wsdl/EventPortType/CreatePullPointSubscriptionRequest",
Body: "<CreatePullPointSubscription xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><InitialTerminationTime>PT180S</InitialTerminationTime></CreatePullPointSubscription>",
}
envelope, err := soap.sendRequest(device.Url, "")
if err != nil {
return createPullPointSubscriptionResponse{}, err
}
return envelope.Body.CreatePullPointSubscriptionResponse, nil
}
func (device *Device) pullMessage(address string, to string) ([]message, error) {
soap := soap{
User: device.Username,
Password: device.Password,
Action: "http://www.onvif.org/ver10/events/wsdl/PullPointSubscription/PullMessagesRequest",
Body: "<PullMessages xmlns=\"http://www.onvif.org/ver10/events/wsdl\"><Timeout>PT3S</Timeout><MessageLimit>10</MessageLimit></PullMessages>",
}
envelope, err := soap.sendRequest(address, to)
if err != nil {
return nil, err
}
messages := envelope.Body.PullMessagesResponse.NotificationMessage.Messages.Messages
if len(messages) == 0 {
return nil, nil
}
return messages, nil
}
func (device *Device) PullRecognitions() (chan Recognition, error) {
res, err := device.createPullPointSubscription()
if err != nil {
return nil, err
}
pullAddress := res.SubscriptionReference.Address
outgoing := make(chan Recognition)
go func() {
for {
start := time.Now()
messages, err := device.pullMessage(device.Url, pullAddress)
if err != nil {
res, err := device.createPullPointSubscription()
for err != nil {
time.Sleep(time.Second)
res, err = device.createPullPointSubscription()
}
pullAddress = res.SubscriptionReference.Address
continue
}
if messages == nil {
continue
}
for _, message := range messages {
recognition := Recognition{}
for _, item := range message.Data.Items {
switch item.Name {
case "PlateNumber":
recognition.Plate = item.Value
case "Likelihood":
intValue, err := strconv.Atoi(item.Value)
if err == nil {
if intValue > 100 {
recognition.Confidence = intValue / 10
} else {
recognition.Confidence = intValue
}
}
case "Nation":
recognition.Nation = item.Value
case "Country":
recognition.Country = item.Value
case "VehicleDirection":
switch item.Value {
case "reverse":
recognition.Direction = Leaving
case "forward":
recognition.Direction = Approaching
default:
recognition.Direction = Unknown
}
}
}
if recognition.Plate == "" || recognition.Plate == "unknown" {
continue
}
recognition.CameraResponseDuration = time.Since(start)
outgoing <- recognition
}
}
}()
return outgoing, nil
}