Intendation; foodoord_oben - right API™

This commit is contained in:
Bandie 2021-11-13 14:27:33 +01:00
parent 72b2276603
commit ce451daef1
Signed by: Bandie
GPG Key ID: 843D7FA93BA46312
2 changed files with 137 additions and 132 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim: ts=2 sw=2 et
import os import os
import stat import stat
@ -14,8 +15,9 @@ from ConfigParser import SafeConfigParser
parser = SafeConfigParser() parser = SafeConfigParser()
parser.read('/etc/foodoord.conf') parser.read('/etc/foodoord.conf')
url = parser.get('door_firstlevel', 'status_url') doorapi = parser.get('doorstatus', 'status_url')
old_api = parser.get('door_firstlevel_old', 'status_url') consumerkey = parser.get('doorstatus', 'key')
consumersecret = parser.get('doorstatus', 'secret')
#Definitions for output #Definitions for output
@ -33,108 +35,110 @@ RED=1
GREEN=2 GREEN=2
ORANGE=3 ORANGE=3
def update_api(locked):
try:
os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"aerie\": " + str(locked).lower() + " }' '" + doorapi + "' ")
except:
pass
if __name__ == "__main__": if __name__ == "__main__":
def doorbell(event):
if (STATUS):
pifacedigital.relays[RELAYS_UNLOCK].toggle()
time.sleep(2)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
#print 'got doorbell'
def close_button(event):
global STATUS
STATUS = False
try:
update_api(True)
except:
pass
set_led(RED)
def doorbell(event): listener = pifacedigitalio.InputEventListener()
if (STATUS): listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, doorbell, settle_time=10)
pifacedigital.relays[RELAYS_UNLOCK].toggle() listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, close_button, settle_time=5)
time.sleep(2) listener.activate()
pifacedigital.relays[RELAYS_UNLOCK].toggle()
#print 'got doorbell'
def close_button(event): def signal_handler(signal, frame):
global STATUS listener.deactivate()
STATUS = False os.remove("/var/run/foodoord.pipe")
try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2)
urllib2.urlopen(old_api+'&status=closed', timeout=2)
except:
pass
set_led(RED) try:
update_api(True)
except:
pass
set_led(RED)
sys.exit(0)
listener = pifacedigitalio.InputEventListener() def set_led(color):
listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, doorbell, settle_time=10) if (color==RED):
listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, close_button, settle_time=5) pifacedigital.leds[LED_RED].turn_on()
listener.activate() pifacedigital.leds[LED_GREEN].turn_off()
def signal_handler(signal, frame): elif (color==GREEN):
listener.deactivate() pifacedigital.leds[LED_GREEN].turn_on()
os.remove("/var/run/foodoord.pipe") pifacedigital.leds[LED_RED].turn_off()
try: elif (color==ORANGE):
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2) pifacedigital.leds[LED_RED].turn_on()
urllib2.urlopen(old_api+'&status=closed', timeout=2) pifacedigital.leds[LED_GREEN].turn_on()
except:
pass
set_led(RED) pifacedigital = pifacedigitalio.PiFaceDigital()
sys.exit(0) signal.signal(signal.SIGTERM, signal_handler)
def set_led(color): #Startsettings
if (color==RED): STATUS = False
pifacedigital.leds[LED_RED].turn_on() pifacedigital.leds[LED_RED].turn_on()
pifacedigital.leds[LED_GREEN].turn_off()
elif (color==GREEN): #Setting up FiFo to get sshd-output
pifacedigital.leds[LED_GREEN].turn_on() try:
pifacedigital.leds[LED_RED].turn_off() 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
elif (color==ORANGE): with open("/var/run/foodoord.pipe", "r") as ssh_input:
pifacedigital.leds[LED_RED].turn_on() while 1:
pifacedigital.leds[LED_GREEN].turn_on() #Read sshd-output from pipe
Pipe = ssh_input.readline()[:-1]
pifacedigital = pifacedigitalio.PiFaceDigital() if (Pipe == "close" and STATUS):
signal.signal(signal.SIGTERM, signal_handler) pifacedigital.relays[RELAYS_LOCK].toggle()
time.sleep(1)
pifacedigital.relays[RELAYS_LOCK].toggle()
STATUS = False
#Startsettings try:
STATUS = False update_api(True)
pifacedigital.leds[LED_RED].turn_on() except:
pass
#Setting up FiFo to get sshd-output set_led(RED)
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: elif (Pipe == "open"):
while 1: pifacedigital.relays[RELAYS_UNLOCK].toggle()
#Read sshd-output from pipe time.sleep(2)
Pipe = ssh_input.readline()[:-1] pifacedigital.relays[RELAYS_UNLOCK].toggle()
if (Pipe == "close" and STATUS): if (STATUS==False):
pifacedigital.relays[RELAYS_LOCK].toggle()
time.sleep(1)
pifacedigital.relays[RELAYS_LOCK].toggle()
STATUS = False
try: try:
urllib2.urlopen(url+'&door=aerie&locked=1', timeout=2) update_api(False)
urllib2.urlopen(old_api+'&status=closed', timeout=2) except:
except: pass
pass
set_led(RED) STATUS = True
elif (Pipe == "open"): set_led(GREEN)
pifacedigital.relays[RELAYS_UNLOCK].toggle() time.sleep(0.1)
time.sleep(2)
pifacedigital.relays[RELAYS_UNLOCK].toggle()
if (STATUS==False):
try:
urllib2.urlopen(url+'&door=aerie&locked=0', timeout=2)
urllib2.urlopen(old_api+'&status=open', timeout=2)
except:
pass
STATUS = True
set_led(GREEN)
time.sleep(0.1)

View File

@ -1,4 +1,5 @@
#! /usr/bin/python #! /usr/bin/python
# vim: ts=2 sw=2 et
import os import os
import stat import stat
@ -37,61 +38,61 @@ ORANGE=3
def write_state(state): def write_state(state):
try: try:
handle = open("/tmp/door_state", "w") handle = open("/tmp/door_state", "w")
handle.write(state) handle.write(state)
handle.close() handle.close()
except: except:
pass pass
def update_api(locked): def update_api(locked):
try: try:
os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"cellar\": " + str(locked).lower() + " }' '" + doorapi + "' ") os.system("/usr/bin/curl -XPOST --header 'Content-Type: application/json' --data '{ \"consumer_key\": \"" + consumerkey + "\", \"consumer_secret\": \"" + consumersecret + "\", \"cellar\": " + str(locked).lower() + " }' '" + doorapi + "' ")
except: except:
pass pass
if __name__ == "__main__": if __name__ == "__main__":
#Startsettings #Startsettings
STATUS=False STATUS=False
gpio.setmode(gpio.BCM) gpio.setmode(gpio.BCM)
gpio.setup(PIN_OPEN, gpio.OUT) gpio.setup(PIN_OPEN, gpio.OUT)
gpio.setup(PIN_CLOSE, gpio.OUT) gpio.setup(PIN_CLOSE, gpio.OUT)
#Setting up FiFo to get sshd-output #Setting up FiFo to get sshd-output
try: try:
os.mkfifo("/var/run/foodoord.pipe") os.mkfifo("/var/run/foodoord.pipe")
os.chown("/var/run/foodoord.pipe", -1, grp.getgrnam('foodoor')[2]) 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) os.chmod("/var/run/foodoord.pipe", stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
except OSError: except OSError:
pass pass
with open("/var/run/foodoord.pipe", "r") as ssh_input: with open("/var/run/foodoord.pipe", "r") as ssh_input:
while 1: while 1:
#Read sshd-output from pipe #Read sshd-output from pipe
Pipe = ssh_input.readline()[:-1] Pipe = ssh_input.readline()[:-1]
if (Pipe == "close"): if (Pipe == "close"):
gpio.output(PIN_CLOSE,1) gpio.output(PIN_CLOSE,1)
time.sleep(1) time.sleep(1)
gpio.output(PIN_CLOSE,0) gpio.output(PIN_CLOSE,0)
write_state("closed") write_state("closed")
update_api(True) update_api(True)
elif (Pipe == "open"): elif (Pipe == "open"):
#Locking #Locking
gpio.output(PIN_OPEN,1) gpio.output(PIN_OPEN,1)
time.sleep(1) time.sleep(1)
gpio.output(PIN_OPEN,0) gpio.output(PIN_OPEN,0)
#Save State #Save State
write_state("open") write_state("open")
#Status Update #Status Update
update_api(False) update_api(False)
time.sleep(0.2) time.sleep(0.2)