-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcam.py
59 lines (50 loc) · 1.81 KB
/
ipcam.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
#!/usr/bin/env python3
##########################
PIN_sensorA = 0 # Sensor A. Activated => 1
PIN_bell = 1 # Bell. Press => 1
PIN_door = 2 # Door. Closed => 1
PIN_sensorB = 3 # Sensor B. Activated => 1
#SETTLE_TIME = 5.0 # Wait 5 sec between reqs
##########################
import sys, time, datetime
from subprocess import call
from sys import exit
import pifacedigitalio
pifacedigitalio.init()
def log(txt, die = False):
t = datetime.datetime.now().strftime("%Y/%b/%d %H:%M:%S")
print("[%s] %s" % (t,txt))
if die: sys.exit()
def get_val(pin):
global pifacedigitalio
return pifacedigitalio.digital_read(pin)
def get_event():
if get_val(PIN_bell) == 1:
return (True, 4, "Door bell")
if get_val(PIN_door) == 1:
# These rules apply, if door is closed (PIN_door == 1)
if get_val(PIN_sensorA) == 1:
return (True, 3, "Sensor A")
if get_val(PIN_sensorB) == 1:
return (True, 5, "Sensor B")
return (False, 0, "Nothing")
#pfd = pifacedigitalio.PiFaceDigital()
#listener = pifacedigitalio.InputEventListener(chip=pifacedigitalio)
#for i in range(8): listener.register(i, pifacedigitalio.IODIR_BOTH, iodir_on_pin), SETTLE_TIME)
#listener.activate()
try:
log("Process is active.")
while True:
alarm, eventId, eventName = get_event()
if alarm:
log("ALERT: %s." % eventName)
call(["./trigger.py", str(eventId)])
#sys.stdout.flush() # Not needed when using tmux
time.sleep(0.1) # busy-waiting is stupid, but InputEventListener does not work on my PiFace
except KeyboardInterrupt:
log("KeyboardInterrupt!")
except:
log("Got unknown exception!?")
log("Ending.")
#listener.deactivate()
pifacedigitalio.deinit() # disables interrupts and closes the file