-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
109 lines (87 loc) · 2.55 KB
/
run.py
File metadata and controls
109 lines (87 loc) · 2.55 KB
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
import os
import signal
import shutil
import psutil
import subprocess
import datetime
import time
import argparse
import json
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--conf", required=True, help="path to the JSON configuration file")
args = vars(ap.parse_args())
conf = json.load(open(args["conf"]))
tokenpath = conf["tokenpath"]
tmptokenpath = conf["tmptokenpath"]
tmptimepath = conf["tmptimepath"]
#if os.path.exists(tmptokenpath):
#os.chmod(tmptokenpath, 0666)
pid = 0
def start_surveillance():
print datetime.datetime.now(), "Starting surveillance..."
surveillance = subprocess.Popen(["/home/pi/.virtualenvs/cv/bin/python", "/home/pi/SPi/pi_surveillance.py", "--conf", "/home/pi/SPi/conf.json"])
return surveillance.pid
def check_camera():
if os.path.exists("/dev/video0"):
print "Camera connected."
else:
print "No camera found! Wait..."
time.sleep(30)
check_camera()
def check_token():
if os.path.isfile(tokenpath):
if not os.path.exists(tmptokenpath):
shutil.copy(tokenpath, tmptokenpath)
os.chmod(tmptokenpath, 0666)
else:
print "No token."
time.sleep(30)
check_token()
if __name__ == "__main__":
if conf["use_dropbox"]:
check_token()
check_camera()
pid = start_surveillance()
time.sleep(conf["camera_warmup_time"]+conf["read_seconds"])
# print "Process PID: ", pid
while True:
if os.path.isfile(tmptimepath):
with open(tmptimepath, "r") as timefile:
line = timefile.readline()
if not line:
pass
else:
lasttime = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S.%f')
thistime = datetime.datetime.now()
## check if the script has been writing regularly to a file
if (thistime - lasttime).seconds > conf["read_seconds"]:
print "Idle > ", conf["read_seconds"], "PID: ", pid
p = psutil.Process(pid)
try:
p.terminate()
p.wait(timeout=1)
#os.kill(pid, signal.SIGTERM)
#os.kill(pid, signal.SIGKILL)
except OSError:
raise
if pid not in psutil.pids():
pid = start_surveillance()
else:
p.kill()
p.wait(timeout=1)
continue
#elif (thistime - lasttime).seconds < conf["read_seconds"]:
#print "< ", conf["read_seconds"]
#pass
#else:
#print "Time difference: ", (thistime - lasttime).seconds
#pass
time.sleep(conf["read_seconds"])
else:
time.sleep(conf["read_seconds"])
print "No time file. Restart surveillance."
psutil.Process(pid).terminate()
psutil.Process(pid).wait(timeout=1)
if not pid in psutil.pids():
pid = start_surveillance()
continue