#!/usr/bin/env python3 # vim: ts=2 sw=2 et import grp import json import os import stat import subprocess import time from configparser import ConfigParser import RPi.GPIO as gpio # Definitions for output LED_RED = 6 LED_GREEN = 7 RELAYS_LOCK = 0 RELAYS_UNLOCK = 1 PIN_OPEN = 24 PIN_CLOSE = 27 # 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') def write_state(state): try: with open("/tmp/door_state", "w") as handle: handle.write(state) except: pass def update_api(locked): try: subprocess.check_call([ "/usr/bin/curl", "-XPOST", "--header", "Content-Type: application/json", "--data", json.dumps({"consumer_key": CONSUMERKEY, "consumer_secret": CONSUMERSECRET, "cellar": locked}), DOORAPI ]) except: pass def main(): # Start settings gpio.setmode(gpio.BCM) gpio.setup(PIN_OPEN, gpio.OUT) gpio.setup(PIN_CLOSE, gpio.OUT) # 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 ssh_input = open("/var/run/foodoord.pipe", "r") while True: # Read sshd output from pipe pipe_cmd = ssh_input.readline().strip() if pipe_cmd == "close": gpio.output(PIN_CLOSE, 1) time.sleep(1) gpio.output(PIN_CLOSE, 0) write_state("closed") update_api(True) elif pipe_cmd == "open": # Locking gpio.output(PIN_OPEN, 1) time.sleep(1) gpio.output(PIN_OPEN, 0) write_state("open") # Save State update_api(False) # Status Update time.sleep(0.2) if __name__ == "__main__": main()