From 6e7d90d419547705cb125adc7fe3626d9dc0fd49 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 14 Sep 2013 18:33:48 +0200 Subject: [PATCH] Initial commit tyshell: working and basically complete tuerd: working but missing the log ringd: just an experiment --- ringd | 36 ++++++++++++++++++++++++++++ tuerd | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tyshell | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100755 ringd create mode 100755 tuerd create mode 100755 tyshell diff --git a/ringd b/ringd new file mode 100755 index 0000000..812043a --- /dev/null +++ b/ringd @@ -0,0 +1,36 @@ +#!/usr/bin/python3 +import time, socket +import RPi.GPIO as GPIO + +tuerSock = "/run/tuer.sock" + +ringPin = 18 + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(ringPin, GPIO.IN) + +lastEvent = 0 + + +while True: + GPIO.wait_for_edge(ringPin, GPIO.BOTH) + # measure time since event + now = time.time() + timePassed = now-lastEvent + print("Time between events %f" % timePassed) + # remember, remember + lastEvent = now + # action to be taken? + if timePassed >= 1.5 and timePassed <= 3: + print("Opening door") + # talk with tuerd + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(tuerSock) + s.send(b'buzz') + data = s.recv(4) + s.close() + print("...done") + if data != b'1': + print("Received unexpected answer %s" % str(data)) + +GPIO.cleanup() diff --git a/tuerd b/tuerd new file mode 100755 index 0000000..7cc15d6 --- /dev/null +++ b/tuerd @@ -0,0 +1,73 @@ +#!/usr/bin/python3 +import time, socket, os, stat +from datetime import datetime +import RPi.GPIO as GPIO +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) + +try: + os.unlink (socketname) +except OSError: + 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() + +GPIO.cleanup() + + diff --git a/tyshell b/tyshell new file mode 100755 index 0000000..34ef515 --- /dev/null +++ b/tyshell @@ -0,0 +1,72 @@ +#!/usr/bin/python3 +import os +import readline +import shlex +import sys +import subprocess +import socket + +tuerSock = "/run/tuer.sock" + +# use a histfile +histfile = os.path.join(os.path.expanduser("~"), ".pyshellhist") +try: + readline.read_history_file(histfile) +except IOError: + pass +import atexit +atexit.register(readline.write_history_file, histfile) + +# available commands +def help(c): + print("Available commands: %s" % ", ".join(sorted(commands.keys()))) + +def extcmd(cmd): + def run(c): + ret = subprocess.call(cmd) + if ret != 0: + print("Command returned non-zero exit statis %d" % ret) + return run + +def sendcmd(addr, cmd): + def run(c): + print("Running %s..." % (cmd)) + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(addr) + s.send(cmd.encode()) + data = s.recv(4) + s.close() + print("...done") + if data != b'1': + print("Received unexpected answer %s" % str(data)) + return run + +commands = { + 'exit': None, # catched below, quits the loop + 'help': help, + 'open': sendcmd(tuerSock, 'open'), + 'close': sendcmd(tuerSock, 'close'), + 'buzz': sendcmd(tuerSock, 'buzz'), +} + +# input loop +print("Welcome to tyshell. Use help to see what you can do.") +while True: + try: + command = input("$ ") + except EOFError: + print() + break + command = shlex.split(command) + if not len(command): continue + # execute command + if command[0] == "exit": + break + elif command[0] in commands: + try: + commands[command[0]](command) + except Exception as e: + print("Error while executing %s: %s" % (command[0], str(e))) + else: + print("Command %s not found. Use help." % command[0]) +print("Bye") -- 2.30.2