better logging; properly detect whether the bell button was newly pressed
authorRalf Jung <post@ralfj.de>
Mon, 14 Oct 2013 19:47:26 +0000 (21:47 +0200)
committerRalf Jung <post@ralfj.de>
Mon, 14 Oct 2013 19:47:26 +0000 (21:47 +0200)
actor.py
libtuer.py
statemachine.py
tyshell

index 8f57d9f163508ab17b173b6517063544bbd89f64..8dd73231e18b53f9001ded2b8f816f48160cf6b6 100644 (file)
--- a/actor.py
+++ b/actor.py
@@ -24,11 +24,11 @@ class Actor:
                        logger.info("Actor: Running command %s" % name)
                        for (value, delay) in todo:
                                if value is not None:
                        logger.info("Actor: Running command %s" % name)
                        for (value, delay) in todo:
                                if value is not None:
-                                       logger.debug("Setting pin %d to %d" % (pin, value))
+                                       logger.debug("Actor: Setting pin %d to %d" % (pin, value))
                                        GPIO.output(pin, value)
                                time.sleep(delay)
                else:
                                        GPIO.output(pin, value)
                                time.sleep(delay)
                else:
-                       logger.error("Actor: Gut unknown command %d" % cmd)
+                       logger.critical("Actor: Got unknown command %d" % cmd)
        
        def stop(self):
                self.act.stop()
        
        def stop(self):
                self.act.stop()
index 613ce4eb480d5ad47bfeac87d6c19d499ed5db91..8f9c48f7a0b0ee6d828f1de220569a1c24abe96e 100644 (file)
@@ -17,6 +17,7 @@ def sendeMail(subject, text, receivers, sender='sphinx@hacksaar.de', replyTo=Non
        msg['To'] = ', '.join(receivers)
        if replyTo is not None:
                msg['Reply-To'] = replyTo
        msg['To'] = ', '.join(receivers)
        if replyTo is not None:
                msg['Reply-To'] = replyTo
+       # FIXME set time
        # put into envelope and send
        s = smtplib.SMTP('ralfj.de')
        s.sendmail(sender, receivers, msg.as_string())
        # put into envelope and send
        s = smtplib.SMTP('ralfj.de')
        s.sendmail(sender, receivers, msg.as_string())
index 893287a53237351f7c290d8b3c07b1adb36f6e7c..01cf83f33b2d600bb383024a276c89c2d1e3f904 100644 (file)
@@ -27,10 +27,6 @@ ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("flipswitch")), (5, lambda:play_so
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (0, lambda:logger.error("Space open but switch not flipped for 30 seconds")),\
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (6, lambda:play_sound("flipswitch")), (4, lambda:logger.critical("Space open but switch not flipped for 60 seconds"))]
 
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (0, lambda:logger.error("Space open but switch not flipped for 30 seconds")),\
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (6, lambda:play_sound("flipswitch")), (4, lambda:logger.critical("Space open but switch not flipped for 60 seconds"))]
 
-# StateAuf constants
-#  time that must pass between two bell_ringing events to buzz the door again (seconds)
-AUF_BELLBUZZ_REPEAT_TIME = 2
-
 # Timeout we wait after the switch was switched to "Closed", until we assume nobody will open the door and we just lock it
 # ALso the time we wait after the door was opend, till we assume something went wrong and start nerving
 LEAVE_TIMEOUT = 20
 # Timeout we wait after the switch was switched to "Closed", until we assume nobody will open the door and we just lock it
 # ALso the time we wait after the door was opend, till we assume something went wrong and start nerving
 LEAVE_TIMEOUT = 20
@@ -84,6 +80,8 @@ class StateMachine():
                                return self._nerver.nerv()
                def pins(self):
                        return self.state_machine.pins
                                return self._nerver.nerv()
                def pins(self):
                        return self.state_machine.pins
+               def old_pins(self):
+                       return self.state_machine.old_pins
                def actor(self):
                        return self.state_machine.actor
                def handle_event(self,ev,arg): # don't override
                def actor(self):
                        return self.state_machine.actor
                def handle_event(self,ev,arg): # don't override
@@ -144,7 +142,7 @@ class StateMachine():
                        # TODO: 202 notification also here if possible
                        self.callbacks.append(callback)
                def could_not_open(self):
                        # TODO: 202 notification also here if possible
                        self.callbacks.append(callback)
                def could_not_open(self):
-                       logger.critical("Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER)
+                       logger.critical("StateMachine: Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER)
                        self.notify(False)
                        return StateMachine.StateZu(self.state_machine)
        
                        self.notify(False)
                        return StateMachine.StateZu(self.state_machine)
        
@@ -152,7 +150,7 @@ class StateMachine():
                def handle_cmd_unlock_event(self, callback):
                        # intentionally not calling super() implementation
                        callback("299 redundant: Space seems to be already open. Still processing your request tough.")
                def handle_cmd_unlock_event(self, callback):
                        # intentionally not calling super() implementation
                        callback("299 redundant: Space seems to be already open. Still processing your request tough.")
-                       logger.info("Received OPEN command in StateAboutToOpen. This should not be necessary.")
+                       logger.info("StateMachine: Received UNLOCK command in StateAboutToOpen. This should not be necessary.")
                        self.actor().act(Actor.CMD_UNLOCK)
        
        class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
                        self.actor().act(Actor.CMD_UNLOCK)
        
        class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
@@ -173,18 +171,15 @@ class StateMachine():
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
-                       if pins.bell_ringing:
-                               # TODO: use old_pins instead of a timer
-                               now = time.time()
-                               if self.last_buzzed is None or now-self.last_buzzed < AUF_BELLBUZZ_REPEAT_TIME:
-                                       logger.info("buzzing because of bell ringing in state auf")
-                                       self.actor().act(Actor.CMD_BUZZ)
-                                       self.last_buzzed = now
+                       if pins.bell_ringing and not self.old_pins().bell_ringing:
+                               # someone just pressed the bell
+                               logger.info("StateMachine: buzzing because of bell ringing in state auf")
+                               self.actor().act(Actor.CMD_BUZZ)
                        if not pins.space_active:
                        if not pins.space_active:
-                               logger.info("space switch off - starting leaving procedure")
+                               logger.info("StateMachine: space switch off - starting leaving procedure")
                                return StateMachine.StateAboutToLeave(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateAboutToLeave(self.state_machine)
                        if pins.door_locked:
-                               logger.info("door manually locked, but space switch on - going to StateZu")
+                               logger.info("StateMachine: door manually locked, but space switch on - going to StateZu")
                                play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)
        
                                play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)
        
@@ -201,14 +196,14 @@ class StateMachine():
                        pins = self.pins()
                        if not pins.door_closed:
                                # TODO play a sound? This shouldn't happen, door was opened while we are locking
                        pins = self.pins()
                        if not pins.door_closed:
                                # TODO play a sound? This shouldn't happen, door was opened while we are locking
-                               logger.warning("door manually opened during locking")
+                               logger.warning("StateMachine: door manually opened during locking")
                                return StateMachine.StateAboutToOpen(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
                def handle_cmd_unlock_event(self,callback):
                        callback("409 conflict: The server is currently trying to lock the door. Try again later.")
                def could_not_close(self):
                                return StateMachine.StateAboutToOpen(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
                def handle_cmd_unlock_event(self,callback):
                        callback("409 conflict: The server is currently trying to lock the door. Try again later.")
                def could_not_close(self):
-                       logger.critical("Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
+                       logger.critical("StateMachine: Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
                        return StateMachine.StateAboutToOpen(self.state_machine)
        
        class StateAboutToLeave(State):
                        return StateMachine.StateAboutToOpen(self.state_machine)
        
        class StateAboutToLeave(State):
@@ -253,6 +248,6 @@ class StateMachine():
                newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state
                self.old_pins = self.pins
                while newstate is not None:
                newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state
                self.old_pins = self.pins
                while newstate is not None:
-                       logger.debug("StateMachine: new state = %s" % newstate.__class__.__name__)
+                       logger.debug("StateMachine: Doing state transition %s -> %s" % (self.current_state.__class__.__name__, newstate.__class__.__name__))
                        self.current_state = newstate
                        newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)
                        self.current_state = newstate
                        newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)
diff --git a/tyshell b/tyshell
index 9f327258fd21c1f2b8c05e4d09971e2d04165313..f8627d214cf05c29f6d5cad27ee92a1d0d5f76fd 100755 (executable)
--- a/tyshell
+++ b/tyshell
@@ -32,7 +32,7 @@ def extcmd(cmd):
 
 def sendcmd(addr, cmd):
        def run(c):
 
 def sendcmd(addr, cmd):
        def run(c):
-               print("Running %s..." % (cmd))
+               print("206 Sending command %s..." % (cmd))
                s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                s.connect(addr)
                s.settimeout(60.0)
                s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                s.connect(addr)
                s.settimeout(60.0)