make the actor more multi-threaded and do more logging
[saartuer.git] / actor.py
index 0a53263d3cdd9485da0b26ddaa2039f18c8ebea4..439a7bb0cd2bc0d783d1d01831f420e9b47a0a18 100644 (file)
--- a/actor.py
+++ b/actor.py
@@ -7,28 +7,43 @@ class Actor:
        CMD_UNLOCK = 1
        CMD_LOCK = 2
        
+       class CMD():
+               def __init__(self, name, pin, tid, todo):
+                       self.name = name
+                       self.pin = pin
+                       self.tid = tid
+                       self.todo = todo
+                       # don't do the GPIO setup here, the main init did not yet run
+               
+               def execute(self):
+                       logger.info("Actor: Running command %s" % self.name)
+                       for (value, delay) in self.todo:
+                               if value is not None:
+                                       logger.debug("Actor: Setting pin %d to %d" % (self.pin, value))
+                                       GPIO.output(self.pin, value)
+                               time.sleep(delay)
+       
        CMDs = {
-               CMD_UNLOCK:  ("unlock", 12, [(True, 0.3), (False, 0.1)]),
-               CMD_LOCK:  ("lock", 16, [(True, 0.3), (False, 0.1)]),
-               CMD_BUZZ: ("buzz", 22, [(True, 2.0), (False, 0.1)]),
+               CMD_UNLOCK:  CMD("unlock", pin=12, tid=0, todo=[(True, 0.3), (False, 0.1)]),
+               CMD_LOCK:  CMD("lock", pin=16, tid=0, todo=[(True, 0.3), (False, 0.1)]),
+               CMD_BUZZ: CMD("buzz", pin=22, tid=1, todo=[(True, 2.0), (False, 0.1)]),
        }
        
        def __init__(self):
-               self.act = ThreadFunction(self._act, name="Actor")
-               for (name, pin, todo) in self.CMDs.values():
-                       GPIO.setup(pin, GPIO.OUT)
+               # launch threads, all running the "_execute" method
+               self.threads = {}
+               for cmd in Actor.CMDs.values():
+                       GPIO.setup(cmd.pin, GPIO.OUT)
+                       if not cmd.tid in self.threads:
+                               self.threads[cmd.tid] = ThreadFunction(self._execute, "Actor TID %d" % cmd.tid)
        
-       def _act(self, cmd):
-               if cmd in self.CMDs:
-                       (name, pin, todo) = self.CMDs[cmd]
-                       logger.info("Actor: Running command %s" % name)
-                       for (value, delay) in todo:
-                               if value is not None:
-                                       logger.debug("Actor: Setting pin %d to %d" % (pin, value))
-                                       GPIO.output(pin, value)
-                               time.sleep(delay)
-               else:
-                       logger.critical("Actor: Got unknown command %d" % cmd)
+       def _execute(self, cmd):
+               Actor.CMDs[cmd].execute()
+       
+       def act(self, cmd):
+               # dispatch command to correct thread
+               self.threads[Actor.CMDs[cmd].tid](cmd)
        
        def stop(self):
-               self.act.stop()
+               for thread in self.threads.values():
+                       thread.stop()