Code style and shellcheck
This commit is contained in:
217
debian/usr/sbin/foodoord_oben
vendored
217
debian/usr/sbin/foodoord_oben
vendored
@ -1,143 +1,136 @@
|
||||
#!/usr/bin/env python3
|
||||
# vim: ts=2 sw=2 et
|
||||
|
||||
import os
|
||||
import stat
|
||||
import time
|
||||
import pifacedigitalio
|
||||
import signal
|
||||
import sys
|
||||
import grp
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from configparser import ConfigParser
|
||||
|
||||
#Read config
|
||||
import pifacedigitalio
|
||||
|
||||
# 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 implemented yet
|
||||
|
||||
# Definitions for LED color
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
ORANGE = 3
|
||||
|
||||
# Read config
|
||||
parser = ConfigParser()
|
||||
parser.read('/etc/foodoord.conf')
|
||||
|
||||
doorapi = parser.get('doorstatus', 'status_url')
|
||||
consumerkey = parser.get('doorstatus', 'key')
|
||||
consumersecret = parser.get('doorstatus', 'secret')
|
||||
|
||||
|
||||
#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
|
||||
DOORAPI = parser.get('doorstatus', 'status_url')
|
||||
CONSUMERKEY = parser.get('doorstatus', 'key')
|
||||
CONSUMERSECRET = parser.get('doorstatus', 'secret')
|
||||
|
||||
|
||||
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 + "' ")
|
||||
subprocess.check_call([
|
||||
"/usr/bin/curl", "-XPOST",
|
||||
"--header", "Content-Type: application/json",
|
||||
"--data",
|
||||
json.dumps({"consumer_key": CONSUMERKEY, "consumer_secret": CONSUMERSECRET, "aerie": locked})
|
||||
])
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
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 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()
|
||||
|
||||
|
||||
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()
|
||||
class Foodoord:
|
||||
def __init__(self):
|
||||
self.status_open = False
|
||||
|
||||
def signal_handler(signal, frame):
|
||||
listener.deactivate()
|
||||
os.remove("/var/run/foodoord.pipe")
|
||||
self.listener = pifacedigitalio.InputEventListener()
|
||||
self.listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, self.doorbell, settle_time=10)
|
||||
self.listener.register(1, pifacedigitalio.IODIR_RISING_EDGE, self.close_button, settle_time=5)
|
||||
|
||||
try:
|
||||
update_api(True)
|
||||
except:
|
||||
pass
|
||||
def signal_handler(self, _signal, _frame):
|
||||
self.listener.deactivate()
|
||||
os.remove("/var/run/foodoord.pipe")
|
||||
|
||||
set_led(RED)
|
||||
sys.exit(0)
|
||||
update_api(True)
|
||||
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:
|
||||
update_api(True)
|
||||
except:
|
||||
pass
|
||||
def doorbell(self, event):
|
||||
if self.status_open:
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
time.sleep(2)
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
|
||||
def close_button(self, _event):
|
||||
self.status_open = False
|
||||
update_api(True)
|
||||
set_led(RED)
|
||||
|
||||
elif (Pipe == "open"):
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
time.sleep(2)
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
def main(self):
|
||||
self.listener.activate()
|
||||
|
||||
if (STATUS==False):
|
||||
pifacedigital = pifacedigitalio.PiFaceDigital()
|
||||
signal.signal(signal.SIGTERM, self.signal_handler)
|
||||
|
||||
try:
|
||||
update_api(False)
|
||||
except:
|
||||
# Start settings
|
||||
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
|
||||
|
||||
STATUS = True
|
||||
ssh_input = open("/var/run/foodoord.pipe", "r")
|
||||
while True:
|
||||
# Read sshd-output from pipe
|
||||
pipe_cmd = ssh_input.readline().strip()
|
||||
|
||||
set_led(GREEN)
|
||||
time.sleep(0.1)
|
||||
if pipe_cmd == "close" and self.status_open:
|
||||
pifacedigital.relays[RELAYS_LOCK].toggle()
|
||||
time.sleep(1)
|
||||
pifacedigital.relays[RELAYS_LOCK].toggle()
|
||||
|
||||
self.status_open = False
|
||||
update_api(True)
|
||||
set_led(RED)
|
||||
|
||||
elif pipe_cmd == "open":
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
time.sleep(2)
|
||||
pifacedigital.relays[RELAYS_UNLOCK].toggle()
|
||||
|
||||
if not self.status_open:
|
||||
update_api(False)
|
||||
self.status_open = True
|
||||
set_led(GREEN)
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Foodoord().main()
|
||||
|
Reference in New Issue
Block a user