1 from libtuer import ThreadFunction, logger, fire_and_forget
2 from actor import Actor
5 # logger.{debug,info,warning,error,critical}
9 soundfiles = os.listdir(SOUNDS_DIRECTORY+what)
10 except FileNotFoundError:
11 logger.error("StateMachine: Unable to list sound files in %s" % (SOUNDS_DIRECTORY+what))
13 soundfile = SOUNDS_DIRECTORY + what + '/' + random.choice(soundfiles)
14 fire_and_forget ([SOUNDS_PLAYER,soundfile], logger.error, "StateMachine: ")
17 # StateOpening constants
18 OPEN_REPEAT_TIMEOUT = 8
19 OPEN_REPEAT_NUMBER = 3
21 # StateAboutToOpen constants
22 ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (10, lambda:Logger.warning("Space open but switch not flipped for 10 seconds")),\
23 (20, lambda:play_sound("flipswitch")), (30, lambda:play_sound("flipswitch")), (30, lambda:Logger.error("Space open but switch not flipped for 30 seconds")),\
24 (40, lambda:play_sound("flipswitch")), (50, lambda:play_sound("flipswitch")), (56, lambda:play_sound("flipswitch")), (60, lambda:Logger.critical("Space open but switch not flipped for 60 seconds"))]
27 # time that must pass between two bell_ringing events to buzz the door again (seconds)
28 AUF_BELLBUZZ_REPEAT_TIME = 2
30 # play_sound constants
31 SOUNDS_DIRECTORY = "/opt/tuer/sounds/"
32 SOUNDS_PLAYER = "/usr/bin/mplayer"
36 # commands you can send
44 def __init__(self, state_machine):
45 self.state_machine = state_machine
46 self.time_entered = time.time()
48 self.last_wakeup = self.time_entered
49 def handle_pins_event(self):
50 pass # one needn't implement this
51 def handle_buzz_event(self,arg): # this shouldn't be overwritten
52 self.actor.act(Actor.CMD_BUZZ)
53 arg("200 okay: buzz executed")
54 def handle_open_event(self,arg):
56 arg("412 Precondition Failed: The current state (%s) cannot handle the OPEN event" % self.__class__.__name__)
57 def handle_wakeup_event(self):
58 self.last_wakeup = time.time()
60 return self.state_machine.pins
62 return self.state_machine.actor
63 def handle_event(self,ev,arg): # don't override
65 self.handle_pins_event()
67 self.handle_buzz_event(arg)
69 self.handle_open_event(arg)
70 elif arg is CMD_WAKEUP:
71 self.handle_wakeup_event()
73 raise Exception("Unknown command number: %d" % ev)
75 class StateStart(State):
76 def handle_pins_event(self):
81 if thepins.door_locked:
87 def handle_pins_event(self):
89 if not pins.door_locked:
90 return StateAboutToOpen(self.state_machine)
91 def handle_open_event(self,callback):
92 return StateOpening(callback,self.state_machine)
94 class StateOpening(State):
95 def __init__(self,callback,sm):
96 super().__init__(self,sm)
97 self.callbacks=[callback]
98 # FIXME: can we send "202 processing: Trying to open the door" here? Are the callbacks multi-use?
100 self.actor().act(Actor.CMD_OPEN)
101 def notify(self, did_it_work):
102 s = "200 okay: door open" if did_it_work else ("500 internal server error: Couldn't open door with %d tries à %f seconds" % (OPEN_REPEAT_NUMBER,OPEN_REPEAT_TIMEOUT))
103 for cb in self.callbacks:
106 def handle_pins_event(self):
108 if not pins.door_locked:
110 return StateAboutToOpen(self.state_machine)
111 def handle_open_event(self,callback):
112 # FIXME: 202 notification also here if possible
113 self.callbacks.append(callback)
114 def handle_wakeup_event(self):
115 over = time.time() - self.time_entered
116 nexttry = (self.tries+1) * OPEN_REPEAT_TIMEOUT
118 if self.tries < OPEN_REPEAT_NUMBER:
119 self.actor().act(Actor.CMD_OPEN)
122 logger.critical("Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER)
124 return StateZu(self.state_machine)
126 class AbstractStateWhereOpeningIsRedundant(State):
127 def handle_open_event(self, callback):
128 # FIXME contradicting original plan where open would be ignored in StateAboutToOpen?
129 callback("299 redundant: Space seems to be already open. Still processing your request tough.")
130 logger.warning("Received OPEN command in StateAboutToOpen. This should not be necessary.")
131 self.actor().act(Actor.CMD_OPEN)
133 class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
134 def handle_pins_event(self):
137 return StateZu(self.state_machine)
138 elif pins.space_active:
139 return StateAuf(self.state_machine)
140 def handle_wakeup_event(self):
142 lasttime = self.last_wakeup - self.time_entered
143 thistime = now - self.time_entered
144 for (t,f) in filter(lambda (t,f): t<=thistime and t>lasttime, ABOUTOPEN_NERVLIST):
149 class StateAuf(AbstractStateWhereOpeningIsRedundant):
150 def __init__(self,sm):
152 self.last_buzzed = None
153 def handle_pins_event(self):
155 if pins.bell_ringing:
157 if self.last_buzzed is None or now-self.last_buzzed < AUF_BELLBUZZ_REPEAT_TIME:
158 logger.info("buzzing because of bell ringing in state auf")
159 self.actor().act(Actor.CMD_BUZZ)
160 self.last_buzzed = now
161 if not pins.space_active:
162 logger.info("space switch off - starting leaving procedure")
163 return StateAboutToLeave(self.state_machine)
165 logger.error("door manually locked, but space switch on - going to StateZu")
166 play_sound("manual_lock")
167 return StateZu(self.state_machine)
168 # handle_wakeup_event intentionally not overwritten
170 class StateClosing(State):
174 class StateAboutToLeave(State):
178 class StateLeaving(State):
182 def __init__(self, actor):
184 self.callback = ThreadFunction(self._callback)
185 self.current_state = StateStart(self)
192 def _callback(self, cmd, arg=None):
194 if cmd == StateMachine.CMD_PINS:
197 newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state
198 self.old_pins = self.pins # FIXME not used?
199 while newstate is not None:
200 logger.info("StateMachine: new state = %s" % newstate.__class__.__name__)
201 self.current_state = newstate
202 newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)