X-Git-Url: https://git.ralfj.de/saartuer.git/blobdiff_plain/6e7d90d419547705cb125adc7fe3626d9dc0fd49..0889e9a1ec6bda2c4b16dabf0ab40793ac17756c:/tuerd?ds=inline diff --git a/tuerd b/tuerd index 7cc15d6..220b2bc 100755 --- a/tuerd +++ b/tuerd @@ -1,73 +1,55 @@ #!/usr/bin/python3 -import time, socket, os, stat -from datetime import datetime import RPi.GPIO as GPIO +import statemachine, actor, pins, tysock, waker, spaceapi +from libtuer import logger +import argparse + +# Parse arguments +parser = argparse.ArgumentParser(description='Run a door') +parser.add_argument("-d", "--debug", + action="store_true", dest="debug", + help="Don't send emails") +parser.add_argument("-f", "--fallback", + action="store_true", dest="fallback", + help="Fallback mode for unfunctional hardware: Depend on less sensor input") +args = parser.parse_args() +if args.debug: + import libtuer + libtuer.mailAddress = [] +if args.fallback: + logger.info("Starting in fallback mode") +else: + # to avoid exceptions or getting None + args.fallback = False + +# Not let's go! +logger.info("Starting up...") + +# initialize GPIO stuff GPIO.setmode(GPIO.BOARD) -def log (what): - print (datetime.now(),what) - -def waynesend (conn, what): - try: - conn.send(what) - except: - log("Couldn't send %s" % str(what)) - -class Pinoutput: - # actionsanddelays is a list of pairs: (bool to send, delay in seconds to wait afterwards) - def __init__ (self, name, pinnumber, actionsanddelays): - self.name = name - self.pin = pinnumber - self.todo = actionsanddelays - GPIO.setup(pinnumber, GPIO.OUT) - log ("Pin %d set to be an output pin for %s." % (pinnumber,name)) - - def __call__ (self, conn): - for (value,delay) in self.todo: - GPIO.output(self.pin, value) - log ("%s: Pin %d set to %s." % (self.name,self.pin,str(value))) - time.sleep(delay) - waynesend(conn,b"1") - -tuergroupid = 1005 - -pinlist = [Pinoutput("open", 12, [(True, 0.3), (False, 5.0)]), - Pinoutput("close", 16, [(True, 0.3), (False, 5.0)]), - Pinoutput("buzz", 22, [(True, 2.0), (False, 5.0)])] - -socketname = "/run/tuer.sock" - - -def doNothing (conn): - log ("doing nothing") - waynesend(conn,b"0") - pass - -sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM) +# bring 'em all up +the_actor = actor.Actor() +the_waker = waker.Waker() +the_api = spaceapi.SpaceApi(the_waker) +the_machine = statemachine.StateMachine(the_actor, the_waker, the_api, args.fallback) +the_socket = tysock.TySocket(the_machine) +the_pins = pins.PinsWatcher(the_machine) +# we do the socket accept thing in the main thread try: - os.unlink (socketname) -except OSError: + the_socket.accept() +except KeyboardInterrupt: + # this is what we waited for! + logger.info("Got SIGINT, terminating...") pass -sock.bind (socketname) - -os.chown (socketname, 0, tuergroupid) -os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP) - -sock.listen(1) - -pindict = {} -for pin in pinlist: - pindict[pin.name.encode()] = pin - -while True: - conn, addr = sock.accept() - data = conn.recv (128) - log("received command: %s" % str(data)) - pindict.get(data,doNothing)(conn) - conn.close() +# bring 'em all down +the_waker.stop() # this one first, it "randomly" calls other threads +the_pins.stop() # as does this +the_machine.stop() +the_api.stop() +the_actor.stop() +# shutdown GPIO stuff GPIO.cleanup() - -