-
Notifications
You must be signed in to change notification settings - Fork 5
/
watch.py
252 lines (202 loc) · 6.32 KB
/
watch.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import socket
import pycurl
import time
import shlex
import subprocess
import paho.mqtt.client as mqtt
ALARM_DELAY = 30
#PLAY_TEMPLATE = "gst-launch-1.0 playbin uri=\"rtsp://{user}:{pass}@{host}:554/cam/realmonitor?channel=1&subtype=2\" latency=300000000 audio-sink=\"autoaudiosink sync=false\""
URL_TEMPLATE = "http://{host}:{port}/cgi-bin/eventManager.cgi?action=attach&codes=%5B{events}%5D"
CAMERAS = [
{
"host": "CAM_1_IP",
"port": 80,
"user": "USERNAME",
"pass": "PASSWORD",
"events": "CrossLineDetection"
},
{
"host": "CAM_2_IP",
"port": 80,
"user": "USER",
"pass": "PASSWORD",
"events": "CrossLineDetection"
},
{
"host": "CAM_3_IP",
"port": 80,
"user": "USER",
"pass": "PASSWORD",
"events": "CrossLineDetection"
}
]
class DahuaCamera():
def __init__(self, master, index, camera):
self.Master = master
self.Index = index
self.Camera = camera
self.CurlObj = None
self.Connected = None
self.Reconnect = None
self.Alarm = dict({
"Active": None,
"Last": None
})
#self.Player = None
#def StartPlayer(self):
# if self.Player:
# return
# print("[{0}] StartPlayer()".format(self.Index))
# self.Master.OnStartPlayer()
# Command = PLAY_TEMPLATE.format(**self.Camera)
# Args = shlex.split(Command)
# self.Player = subprocess.Popen(Args,
# stdin = subprocess.DEVNULL,
# stdout = subprocess.DEVNULL,
# stderr = subprocess.DEVNULL)
#def StopPlayer(self):
# if not self.Player:
# return
# print("[{0}] StopPlayer()".format(self.Index))
# self.Player.kill()
# self.Player.wait()
# self.Player = None
# self.Master.OnStopPlayer()
def SensorOn(self):
sensorurl = ("home-assistant/cameras/{0}/IVS").format(self.Index);
client = mqtt.Client()
client.connect("MQTT_BROKER_IP",1883,60)
#client.publish("home-assistant/cameras/garage/motion", "ON");
#client.publish("home-assistant/cameras/garage/IVS", "ON");
client.publish(sensorurl, "ON");
client.disconnect();
def SensorOff(self):
sensorurl = ("home-assistant/cameras/{0}/IVS").format(self.Index);
client = mqtt.Client()
client.connect("MQTT_BROKER_IP",1883,60)
#client.publish("home-assistant/cameras/garage/motion", "OFF");
#client.publish("home-assistant/cameras/garage/IVS", "OFF");
client.publish(sensorurl, "OFF");
client.disconnect();
def OnAlarm(self, State):
#print("[{0}] Alarm triggered! -> {1}".format(self.Index, "ON" if State else "OFF"))
if State:
self.SensorOn()
print("Motion Detected")
else:
self.SensorOff()
print("Motion Stopped")
def OnConnect(self):
print("[{0}] OnConnect()".format(self.Index))
self.Connected = True
def OnDisconnect(self, reason):
print("[{0}] OnDisconnect({1})".format(self.Index, reason))
self.Connected = False
# self.StopPlayer()
def OnTimer(self):
#if self.Player:
# self.Player.poll()
# if self.Player.returncode != None:
# self.StopPlayer()
if self.Alarm["Active"] == False and time.time() - self.Alarm["Last"] > ALARM_DELAY:
self.Alarm["Active"] = None
self.Alarm["Last"] = None
self.OnAlarm(False)
def OnReceive(self, data):
Data = data.decode("utf-8", errors="ignore")
#print("[{0}]: {1}".format(self.Index, Data))
for Line in Data.split("\r\n"):
if Line == "HTTP/1.1 200 OK":
self.OnConnect()
if not Line.startswith("Code="):
continue
Alarm = dict()
for KeyValue in Line.split(';'):
Key, Value = KeyValue.split('=')
Alarm[Key] = Value
self.ParseAlarm(Alarm)
def ParseAlarm(self, Alarm):
print("[{0}] ParseAlarm({1})".format(self.Index, Alarm))
if Alarm["Code"] not in self.Camera["events"].split(','):
return
if Alarm["action"] == "Start":
if self.Alarm["Active"] == None:
self.OnAlarm(True)
self.Alarm["Active"] = True
elif Alarm["action"] == "Stop":
self.Alarm["Active"] = False
self.Alarm["Last"] = time.time()
class DahuaMaster():
def __init__(self):
self.Cameras = []
self.NumActivePlayers = 0
self.CurlMultiObj = pycurl.CurlMulti()
self.NumCurlObjs = 0
for Index, Camera in enumerate(CAMERAS):
DahuaCam = DahuaCamera(self, Index, Camera)
self.Cameras.append(DahuaCam)
Url = URL_TEMPLATE.format(**Camera)
CurlObj = pycurl.Curl()
DahuaCam.CurlObj = CurlObj
CurlObj.setopt(pycurl.URL, Url)
CurlObj.setopt(pycurl.CONNECTTIMEOUT, 30)
CurlObj.setopt(pycurl.TCP_KEEPALIVE, 1)
CurlObj.setopt(pycurl.TCP_KEEPIDLE, 30)
CurlObj.setopt(pycurl.TCP_KEEPINTVL, 15)
CurlObj.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
CurlObj.setopt(pycurl.USERPWD, "%s:%s" % (Camera["user"], Camera["pass"]))
CurlObj.setopt(pycurl.WRITEFUNCTION, DahuaCam.OnReceive)
self.CurlMultiObj.add_handle(CurlObj)
self.NumCurlObjs += 1
#def OnStartPlayer(self):
# self.NumActivePlayers += 1
# if self.NumActivePlayers == 1:
# subprocess.run(["xset", "dpms", "force", "on"])
#def OnStopPlayer(self):
# self.NumActivePlayers -= 1
# if self.NumActivePlayers == 0:
# subprocess.run(["xset", "dpms", "force", "off"])
def OnTimer(self):
for Camera in self.Cameras:
Camera.OnTimer()
def Run(self, timeout = 1.0):
while 1:
Ret, NumHandles = self.CurlMultiObj.perform()
if Ret != pycurl.E_CALL_MULTI_PERFORM:
break
while 1:
Ret = self.CurlMultiObj.select(timeout)
if Ret == -1:
self.OnTimer()
continue
while 1:
Ret, NumHandles = self.CurlMultiObj.perform()
if NumHandles != self.NumCurlObjs:
_, Success, Error = self.CurlMultiObj.info_read()
for CurlObj in Success:
Camera = next(filter(lambda x: x.CurlObj == CurlObj, self.Cameras))
if Camera.Reconnect:
continue
Camera.OnDisconnect("Success")
Camera.Reconnect = time.time() + 5
for CurlObj, ErrorNo, ErrorStr in Error:
Camera = next(filter(lambda x: x.CurlObj == CurlObj, self.Cameras))
if Camera.Reconnect:
continue
Camera.OnDisconnect("{0} ({1})".format(ErrorStr, ErrorNo))
Camera.Reconnect = time.time() + 5
for Camera in self.Cameras:
if Camera.Reconnect and Camera.Reconnect < time.time():
self.CurlMultiObj.remove_handle(Camera.CurlObj)
self.CurlMultiObj.add_handle(Camera.CurlObj)
Camera.Reconnect = None
if Ret != pycurl.E_CALL_MULTI_PERFORM:
break
self.OnTimer()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
Master = DahuaMaster()
Master.Run()