projects
/
saartuer.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
my senf to tyshell (code beauty farm)
[saartuer.git]
/
tuerd
diff --git
a/tuerd
b/tuerd
index 7cc15d6cf0ce5c98f44ca356ff0ca00398005624..1d49f652b5e7b892a7ec0789c96a308f39d9cf6a 100755
(executable)
--- a/
tuerd
+++ b/
tuerd
@@
-1,73
+1,93
@@
#!/usr/bin/python3
#!/usr/bin/python3
-import time, socket, os, stat
+import time, socket, os, stat
, atexit
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
+# ******** definitions *********
+# logging function
+# TODO: loglevel, log like a real daemon
def log (what):
print (datetime.now(),what)
def log (what):
print (datetime.now(),what)
+# send to client for information but don't care if it arrives
def waynesend (conn, what):
try:
conn.send(what)
except:
log("Couldn't send %s" % str(what))
def waynesend (conn, what):
try:
conn.send(what)
except:
log("Couldn't send %s" % str(what))
+# for command not found: do nothing with the pins and send a "0" to the client
+def doNothing (conn):
+ log ("doing nothing")
+ waynesend(conn,b"0")
+ pass
+
+# commands: on a pin do a series of timed on/off switches
class Pinoutput:
class Pinoutput:
- # actionsanddelays is a list of pairs: (bool to send, delay in seconds to wait afterwards)
+ # name is for logging and also used for mapping command names to instances of this class
+ # actionsanddelays is a list of pairs: (bool to set on pin, 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 __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))
-
+ # actually send the signal to the pins
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)
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)
+ # notify success
waynesend(conn,b"1")
waynesend(conn,b"1")
-tuergroupid = 1005
+# ******** configuration *********
+tuergroupid = 1005
+socketname = "/run/tuer.sock"
pinlist = [Pinoutput("open", 12, [(True, 0.3), (False, 5.0)]),
Pinoutput("close", 16, [(True, 0.3), (False, 5.0)]),
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
)])]
+ Pinoutput("buzz", 22, [(True, 2.0), (False,
0.1
)])]
-socketname = "/run/tuer.sock"
+# ******** main *********
+# at the end do a cleanup
+atexit.register(GPIO.cleanup);
-def doNothing (conn):
- log ("doing nothing")
- waynesend(conn,b"0")
- p
ass
+# convert list of pin objects to dictionary for command lookup
+pindict = {}
+for pin in pinlist:
+ p
indict[pin.name.encode()] = pin
+# create socket
sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
sock = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM)
-
+# delete old socket file and don't bitch around if it's not there
try:
os.unlink (socketname)
except OSError:
pass
try:
os.unlink (socketname)
except OSError:
pass
-
+# bind socket to file name
sock.bind (socketname)
sock.bind (socketname)
-
+# allow only users in the tuergroup to write to the socket
os.chown (socketname, 0, tuergroupid)
os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
os.chown (socketname, 0, tuergroupid)
os.chmod (socketname, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP)
-
+# listen to the people, but not too many at once
sock.listen(1)
sock.listen(1)
-pindict = {}
-for pin in pinlist:
- pindict[pin.name.encode()] = pin
-
+# main loop
+# FIXME: DoS by opening socket but not sending data, because this loop is single threaded; maybe settimeout helps a bit.
while True:
while True:
+ # accept connections
conn, addr = sock.accept()
conn, addr = sock.accept()
- data = conn.recv (128)
+ # TODO: use addr to determine the client for logging
+ # get some data from the client (enough to hold any valid command)
+ data = conn.recv (32)
+ # log the command
log("received command: %s" % str(data))
log("received command: %s" % str(data))
+ # lookup the command, if it's not in the dict, use the doNothing function instead
+ # and execute the looked up command or doNothing with the connection, so it can respond to the client
pindict.get(data,doNothing)(conn)
pindict.get(data,doNothing)(conn)
+ # close connection cleanly
conn.close()
conn.close()
-GPIO.cleanup()
-
-