-
Notifications
You must be signed in to change notification settings - Fork 1
/
zm-amcrest-trigger.py
192 lines (168 loc) · 5.92 KB
/
zm-amcrest-trigger.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
import time
import sys
import signal
from configparser import ConfigParser
import threading
import socket
from amcrest import Http
from datetime import datetime
import pytz
import os
import urllib
class ThreadExited(Exception):
pass
def signal_handler(signal, frame):
raise ThreadExited()
def lines(ret, monitor):
global tz
line = ''
try:
for char in ret.iter_content(decode_unicode=True):
line = line + char
if line.endswith('\r\n'):
yield line.strip()
line = ''
except:
print(str(datetime.now(tz)) + f" - ERROR - Issue with {monitor}. Hopefully recovering...")
line = ''
yield line
# os.kill(os.getpid(), signal.SIGUSR1)
def startrec(monid):
global host, port
# print("start")
zmip = host
zmport = int(port)
onvifmid = monid
onvifcause = 'Motion'
onviftext1 = 'CamTriggered'
onvifscore = '1'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((zmip, zmport))
triggeron = onvifmid + '|on|' + onvifscore + '|' + onvifcause + '|' + onviftext1
s.sendall(triggeron.encode('utf-8'))
s.close()
except socket.error as exc:
print(" - ERROR - There was an error sending Start-Recording to zoneminder.")
print(f" - socket error: {exc}")
return
except:
print(" - ERROR - There was an error sending Start-Recording to zoneminder.")
return
def stoprec(monid):
global host, port
# print("Stop")
zmip = host
zmport = int(port)
onvifmid = monid
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((zmip, zmport))
triggeron = onvifmid + '|cancel|0||'
s.sendall(triggeron.encode('utf-8'))
s.close()
except socket.error as exc:
print(" - ERROR - There was an error sending Stop-Recording to zoneminder.")
print(f" - socket error: {exc}")
return
except:
print(" - ERROR - There was an error sending Stop-Recording to zoneminder.")
return
def camtrigger(monitor, ip, monid, moncodes):
global host, port, username, password, tz
if moncodes != 'VideoMotion':
print(f" - Subscribing to event codes: {moncodes}")
moncodes = urllib.parse.quote_plus(moncodes)
cam = Http(ip, 80, username, password, retries_connection=3, timeout_protocol=3.05)
try:
ret = cam.command('eventManager.cgi?action=attach&codes=[' + moncodes + ']', timeout_cmd=(3.05, None), stream=True)
ret.encoding = 'utf-8'
except:
print(f" - ERROR - There was an error starting {monitor}.\n")
# time.sleep(30)
return
print(str(datetime.now(tz)) + f" - Firing up: {monitor}")
try:
for line in lines(ret, monitor):
timenow = datetime.now(tz)
if line.lower().startswith('content-length:'):
chunk_size = int(line.split(':')[1])
action = repr(next(ret.iter_content(chunk_size=chunk_size, decode_unicode=True)))
if "Start" in action:
startrec(monid)
print(str(timenow) + f" - {monitor} - Start")
elif "Stop" in action:
stoprec(monid)
print(str(timenow) + f" - {monitor} - Stop")
else:
continue
except KeyboardInterrupt:
ret.close()
def camthread(monitor):
global parser
print(str(datetime.now(tz)) + f" - Starting thread for {monitor}")
# Get cam config items
try:
ip = parser.get(monitor, "ip")
if debug: print(f" - IP address: {ip}")
except:
print(f" - ERROR - IP address is required")
return
try:
monid = parser.get(monitor, "monid")
if debug: print(f" - ZM monid: {monid}")
except:
print(f" - ERROR - ZM monid is required")
return
moncodes = parser.get(monitor, "moncodes", fallback="VideoMotion")
# Init cam thread
t = threading.Thread(target=camtrigger, args=(monitor, ip, monid, moncodes))
t.name = monitor
t.setDaemon(True)
t.start()
def main():
global parser
main_thread = threading.current_thread()
# Register the signal handlers
signal.signal(signal.SIGUSR1, signal_handler)
# signal.signal(signal.SIGTERM, service_shutdown)
# signal.signal(signal.SIGINT, service_shutdown)
# Generate set of defined cameras
camera_set = set()
for section in parser.sections():
if (str(section) == 'zm' or str(section) == 'zmat'):
continue
else:
camera_set.add(section)
while True:
# Check defined cameras against current threads
current_threads = set()
for thread in threading.enumerate():
if thread is main_thread:
continue
current_threads.add(thread.getName())
for camera in camera_set:
if debug: print(str(datetime.now(tz)) + f" - Checking for {camera}.")
if camera in current_threads:
if debug: print(f" - thread found.")
# Check for more than it just existing?
else:
if debug: print(" - thread not found!")
# Try to start a thread for this camera
camthread(camera)
time.sleep(int(retry))
if __name__ == '__main__':
global parser, debug, retry, host, port, username, password, tz
parser = ConfigParser()
parser.read("zm-amcrest-trigger.conf")
timezone = parser.get("zm", "tz")
tz = pytz.timezone(timezone)
print(str(datetime.now(tz)) + f" - Start Up.")
debug = parser.getboolean("zmat", "debug", fallback="false")
retry = parser.get("zmat", "retry", fallback="30")
host = parser.get("zm", "host")
port = parser.get("zm", "port")
if debug: print(f" - Zoneminder IP: {host}:{port}")
username = parser.get("zm", "defusername")
password = parser.get("zm", "defpassword")
main()