2021-11-07 18:46:13 +00:00
|
|
|
#!/usr/bin/env python2
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import stat
|
|
|
|
import time
|
|
|
|
import pifacedigitalio
|
2021-11-07 18:46:13 +00:00
|
|
|
import urllib2
|
2021-01-19 17:47:19 +00:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import grp
|
|
|
|
from ConfigParser import SafeConfigParser
|
|
|
|
|
|
|
|
#Read config
|
|
|
|
parser = SafeConfigParser()
|
|
|
|
parser.read('/etc/foodoord.conf')
|
|
|
|
|
2021-11-07 18:46:13 +00:00
|
|
|
url = parser.get('door_firstlevel', 'status_url')
|
|
|
|
old_api = parser.get('door_firstlevel_old', 'status_url')
|
|
|
|
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
#Definitions for output
|
|
|
|
LED_RED=6
|
|
|
|
LED_GREEN=7
|
|
|
|
RELAYS_LOCK=0
|
|
|
|
RELAYS_UNLOCK=1
|
|
|
|
|
|
|
|
#Definitions for input
|
|
|
|
DOOR_BELL=0
|
|
|
|
REED_RELAYS=1 #not implementet yet
|
|
|
|
|
|
|
|
#Definitions for LEDcolor
|
|
|
|
RED=1
|
|
|
|
GREEN=2
|
|
|
|
ORANGE=3
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def doorbell(event):
|
|
|
|
if (STATUS):
|
|
|
|
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
2021-11-07 18:46:13 +00:00
|
|
|
time.sleep(2)
|
2021-01-19 17:47:19 +00:00
|
|
|
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
2021-11-07 18:46:13 +00:00
|
|
|
#print 'got doorbell'
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
def close_button(event):
|
|
|
|
global STATUS
|
|
|
|
STATUS = False
|
2021-11-07 18:46:13 +00:00
|
|
|
try:
|
|
|
|
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
|
|
|
|
urllib2.urlopen(old_api+'&status=closed', timeout=2)
|
|
|
|
except:
|
|
|
|
pass
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
set_led(RED)
|
|
|
|
|
|
|
|
|
|
|
|
listener = pifacedigitalio.InputEventListener()
|
|
|
|
listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, doorbell, settle_time=10)
|
|
|
|
listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, close_button, settle_time=5)
|
|
|
|
listener.activate()
|
|
|
|
|
|
|
|
def signal_handler(signal, frame):
|
|
|
|
listener.deactivate()
|
|
|
|
os.remove("/var/run/foodoord.pipe")
|
|
|
|
|
2021-11-07 18:46:13 +00:00
|
|
|
try:
|
|
|
|
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
|
|
|
|
urllib2.urlopen(old_api+'&status=closed', timeout=2)
|
|
|
|
except:
|
|
|
|
pass
|
2021-01-19 17:47:19 +00:00
|
|
|
|
|
|
|
set_led(RED)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def set_led(color):
|
|
|
|
if (color==RED):
|
|
|
|
pifacedigital.leds[LED_RED].turn_on()
|
|
|
|
pifacedigital.leds[LED_GREEN].turn_off()
|
|
|
|
|
|
|
|
elif (color==GREEN):
|
|
|
|
pifacedigital.leds[LED_GREEN].turn_on()
|
|
|
|
pifacedigital.leds[LED_RED].turn_off()
|
|
|
|
|
|
|
|
elif (color==ORANGE):
|
|
|
|
pifacedigital.leds[LED_RED].turn_on()
|
|
|
|
pifacedigital.leds[LED_GREEN].turn_on()
|
|
|
|
|
|
|
|
pifacedigital = pifacedigitalio.PiFaceDigital()
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
|
|
|
|
#Startsettings
|
|
|
|
STATUS = False
|
|
|
|
pifacedigital.leds[LED_RED].turn_on()
|
|
|
|
|
|
|
|
#Setting up FiFo to get sshd-output
|
|
|
|
try:
|
|
|
|
os.mkfifo("/var/run/foodoord.pipe")
|
|
|
|
os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2])
|
|
|
|
os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
with open("/var/run/foodoord.pipe", "r") as ssh_input:
|
|
|
|
while 1:
|
|
|
|
#Read sshd-output from pipe
|
|
|
|
Pipe = ssh_input.readline()[:-1]
|
|
|
|
|
|
|
|
if (Pipe == "close" and STATUS):
|
|
|
|
pifacedigital.relays[RELAYS_LOCK].toggle()
|
|
|
|
time.sleep(1)
|
|
|
|
pifacedigital.relays[RELAYS_LOCK].toggle()
|
|
|
|
STATUS = False
|
|
|
|
|
|
|
|
try:
|
2021-11-07 18:46:13 +00:00
|
|
|
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
|
|
|
|
urllib2.urlopen(old_api+'&status=closed', timeout=2)
|
2021-01-19 17:47:19 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
set_led(RED)
|
|
|
|
|
|
|
|
elif (Pipe == "open"):
|
|
|
|
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
2021-11-07 18:46:13 +00:00
|
|
|
time.sleep(2)
|
2021-01-19 17:47:19 +00:00
|
|
|
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
|
|
|
|
|
|
|
if (STATUS==False):
|
|
|
|
|
|
|
|
try:
|
2021-11-07 18:46:13 +00:00
|
|
|
urllib2.urlopen(url+'&door=aerie&locked=0', timeout=2)
|
|
|
|
urllib2.urlopen(old_api+'&status=open', timeout=2)
|
2021-01-19 17:47:19 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
STATUS = True
|
|
|
|
|
|
|
|
set_led(GREEN)
|
|
|
|
time.sleep(0.1)
|