forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeGuard.py
65 lines (53 loc) · 1.56 KB
/
HomeGuard.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
#!/usr/bin/env python
import os
from subprocess import check_output
import subprocess
homeDir = "/home/pi"
sensorDir = "/home/pi/Sensors"
emailDir = "/home/pi/Email"
cameraDir = "/home/pi/Camera"
snifferProcName = "RFSniffer"
sensorOutputFile = "sniff"
sensorAlertCodes = {'5707088': 'Door Open', '5592405': 'Motion 1', '16752505': 'Motion 2'}
def checkSensors():
os.chdir(sensorDir)
with open(sensorOutputFile) as f:
content = f.readlines()
content = [x.strip() for x in content]
sensors = set()
for i in content:
i = i.replace("Received ", "")
sensors.add(i)
return sensors
def clearSensorData():
os.chdir(sensorDir)
os.system("cat /dev/null > %s" % sensorOutputFile)
def checkRFSniffer():
try:
# Everything is good ie. Sniffer process running
pid = check_output(["pidof", snifferProcName])
except subprocess.CalledProcessError:
# Sniffer process has died
os.chdir(sensorDir)
subprocess.Popen(["nohup","./start.sh"])
def sendAlert(subject):
os.chdir(emailDir)
os.system("./sendmail.py \"%s\"" % subject)
def formatAlertSubject(sensorData):
subject = ""
for code, text in sensorAlertCodes.items():
if (code in sensorData):
subject = subject + text + ","
print "Subj: %s" % subject
return subject
def takePicture():
os.chdir(cameraDir)
os.system("./capture.sh")
if __name__ == '__main__':
checkRFSniffer()
s = checkSensors()
print s
if len(s) > 0:
takePicture()
sendAlert("[Home Guard] %s" % formatAlertSubject(s))
clearSensorData()