/
SmartDoorHost.py
58 lines (48 loc) · 1.8 KB
/
SmartDoorHost.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
import sys
import ptvsd
import signal
import SmartDoor
import SmartDoorConfig
import RPi.GPIO as GPIO
ptvsd.enable_attach(None)
should_read_sensor = True
def read_sensor():
""" Reads the specified GPIO pins via the config
file and processes them via the SmartDoor script.
"""
config = SmartDoorConfig.read_config()
led_pin = int(config['gpio_pin_led'])
sensor_pin = int(config['gpio_pin_sensor'])
flip_pic_h = str2bool(config['flip_pic_h'])
flip_pic_v = str2bool(config['flip_pic_v'])
pushbullet_auth_key = config['pushbullet_auth_key']
pushbullet_device_names = config['pushbullet_device_names']
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT) # LED
GPIO.setup(sensor_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Sensor
try:
print("{0} started and waiting for sensor input".format(__file__))
while should_read_sensor:
button_pressed = GPIO.input(sensor_pin)
if button_pressed:
GPIO.output(led_pin, True)
SmartDoor.take_photo_and_push(flip_pic_h, flip_pic_v, pushbullet_auth_key, pushbullet_device_names)
while button_pressed:
button_pressed = GPIO.input(sensor_pin)
GPIO.output(led_pin, False)
else:
GPIO.output(led_pin, False)
finally:
GPIO.cleanup()
sys.exit()
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
def handler(signum=None, frame=None):
print("{0} exiting gracefully".format(__file__))
global should_read_sensor
should_read_sensor = False
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, handler)
if __name__ == '__main__':
# Being executed as a script
read_sensor()