forked from xyyangkun/python-dvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
executable file
·121 lines (105 loc) · 3.17 KB
/
monitor.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
#! /usr/bin/python3
from dvrip import DVRIPCam, SomethingIsWrongWithCamera
from signal import signal, SIGINT, SIGTERM
from sys import argv, stdout, exit
from datetime import datetime
from pathlib import Path
from time import sleep, time
import logging
baseDir = argv[3]
retryIn = 5
rebootWait = 10
camIp = argv[1]
camName = argv[2]
cam = None
isShuttingDown = False
chunkSize = 600 # new file every 10 minutes
logFile = baseDir + '/' + camName + '/log.log'
def log(str):
logging.info(str)
def mkpath():
path = baseDir + '/' + camName + "/" + datetime.today().strftime('%Y/%m/%d/%H.%M.%S')
Path(path).parent.mkdir(parents=True, exist_ok=True)
return path
def shutDown():
global isShuttingDown
isShuttingDown = True
log('Shutting down...')
try:
cam.stop_monitor()
close()
except (RuntimeError, TypeError, NameError, Exception):
pass
log('done')
exit(0)
def handler(signum, b):
log('Signal ' + str(signum) + ' received')
shutDown()
signal(SIGINT, handler)
signal(SIGTERM, handler)
def close():
cam.close()
def theActualJob():
prevtime = 0
video = None
audio = None
def receiver(frame, meta, user):
nonlocal prevtime, video, audio
if frame is None:
log('Empty frame')
else:
tn = time()
if tn - prevtime >= chunkSize:
if video != None:
video.close()
audio.close()
prevtime = tn
path = mkpath()
log('Starting files: ' + path)
video = open(path + '.video', "wb")
audio = open(path + '.audio', "wb")
if 'type' in meta and meta["type"] == "g711a": audio.write(frame)
elif 'frame' in meta: video.write(frame)
log('Starting to grab streams...')
cam.start_monitor(receiver)
def syncTime():
log('Synching time...')
cam.set_time()
log('done')
def jobWrapper():
global cam
log('Logging in to camera ' + camIp + '...')
cam = DVRIPCam(camIp)
if cam.login():
log('done')
else:
raise SomethingIsWrongWithCamera('Cannot login')
syncTime()
theActualJob()
def theJob():
while True:
try:
jobWrapper()
except (TypeError, ValueError) as err:
if isShuttingDown:
exit(0)
else:
try:
log('Error. Attempting to reboot camera...')
cam.reboot()
log('Waiting for ' + str(rebootWait) + 's for reboot...')
sleep(rebootWait)
except (UnicodeDecodeError, ValueError, TypeError):
raise SomethingIsWrongWithCamera('Failed to reboot')
def main():
Path(logFile).parent.mkdir(parents=True, exist_ok=True)
logging.basicConfig(filename=logFile, level=logging.INFO, format='[%(asctime)s] %(message)s')
while True:
try:
theJob()
except SomethingIsWrongWithCamera as err:
close()
log(str(err) + '. Waiting for ' + str(retryIn) + ' seconds before trying again...')
sleep(retryIn)
if __name__ == "__main__":
main()