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 # StateClosing constants
22 CLOSE_REPEAT_TIMEOUT = 8
23 CLOSE_REPEAT_NUMBER = 3
25 # StateAboutToOpen constants
26 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")),\
27 (20, lambda:play_sound("flipswitch")), (30, lambda:play_sound("flipswitch")), (30, lambda:Logger.error("Space open but switch not flipped for 30 seconds")),\
28 (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"))]
31 # time that must pass between two bell_ringing events to buzz the door again (seconds)
32 AUF_BELLBUZZ_REPEAT_TIME = 2
34 # play_sound constants
35 SOUNDS_DIRECTORY = "/opt/tuer/sounds/"
36 SOUNDS_PLAYER = "/usr/bin/mplayer"
40 # commands you can send
48 def __init__(self, state_machine):
49 self.state_machine = state_machine
50 self.time_entered = time.time()
52 self.last_wakeup = self.time_entered
53 def handle_pins_event(self):
54 pass # one needn't implement this
55 def handle_buzz_event(self,arg): # this shouldn't be overwritten
56 self.actor.act(Actor.CMD_BUZZ)
57 arg("200 okay: buzz executed")
58 def handle_open_event(self,arg):
60 arg("412 Precondition Failed: The current state (%s) cannot handle the OPEN event" % self.__class__.__name__)
61 def handle_wakeup_event(self):
62 self.last_wakeup = time.time()
64 return self.state_machine.pins
66 return self.state_machine.actor
67 def handle_event(self,ev,arg): # don't override
69 self.handle_pins_event()
71 self.handle_buzz_event(arg)
73 self.handle_open_event(arg)
74 elif arg is CMD_WAKEUP:
75 self.handle_wakeup_event()
77 raise Exception("Unknown command number: %d" % ev)
79 class StateStart(State):
80 def handle_pins_event(self):
81 super().handle_pins_event()
86 if thepins.door_locked:
92 def handle_pins_event(self):
93 super().handle_pins_event()
95 if not pins.door_locked:
96 return StateAboutToOpen(self.state_machine)
97 def handle_open_event(self,callback):
98 # intentionally not calling super() implementation
99 return StateOpening(callback,self.state_machine)
101 class StateOpening(State):
102 def __init__(self,callback,sm):
103 super().__init__(self,sm)
104 self.callbacks=[callback]
105 # FIXME: can we send "202 processing: Trying to open the door" here? Are the callbacks multi-use?
107 self.actor().act(Actor.CMD_OPEN)
108 def notify(self, did_it_work):
109 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))
110 for cb in self.callbacks:
113 def handle_pins_event(self):
114 super().handle_pins_event()
116 if not pins.door_locked:
118 return StateAboutToOpen(self.state_machine)
119 def handle_open_event(self,callback):
120 # intentionally not calling super() implementation
121 # FIXME: 202 notification also here if possible
122 self.callbacks.append(callback)
123 def handle_wakeup_event(self):
124 super().handle_wakeup_event()
125 over = time.time() - self.time_entered
126 nexttry = (self.tries+1) * OPEN_REPEAT_TIMEOUT
128 if self.tries < OPEN_REPEAT_NUMBER:
129 self.actor().act(Actor.CMD_OPEN)
132 logger.critical("Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER)
134 return StateZu(self.state_machine)
136 class AbstractStateWhereOpeningIsRedundant(State):
137 def handle_open_event(self, callback):
138 # intentionally not calling super() implementation
139 # FIXME contradicting original plan where open would be ignored in StateAboutToOpen?
140 callback("299 redundant: Space seems to be already open. Still processing your request tough.")
141 logger.warning("Received OPEN command in StateAboutToOpen. This should not be necessary.")
142 self.actor().act(Actor.CMD_OPEN)
144 class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
145 def handle_pins_event(self):
146 super().handle_pins_event()
149 return StateZu(self.state_machine)
150 elif pins.space_active:
151 return StateAuf(self.state_machine)
152 def handle_wakeup_event(self):
153 super().handle_wakeup_event()
155 lasttime = self.last_wakeup - self.time_entered
156 thistime = now - self.time_entered
157 for (t,f) in filter(lambda (t,f): t<=thistime and t>lasttime, ABOUTOPEN_NERVLIST):
162 class StateAuf(AbstractStateWhereOpeningIsRedundant):
163 def __init__(self,sm):
165 self.last_buzzed = None
166 def handle_pins_event(self):
167 super().handle_pins_event()
169 if pins.bell_ringing:
171 if self.last_buzzed is None or now-self.last_buzzed < AUF_BELLBUZZ_REPEAT_TIME:
172 logger.info("buzzing because of bell ringing in state auf")
173 self.actor().act(Actor.CMD_BUZZ)
174 self.last_buzzed = now
175 if not pins.space_active:
176 logger.info("space switch off - starting leaving procedure")
177 return StateAboutToLeave(self.state_machine)
179 logger.error("door manually locked, but space switch on - going to StateZu")
180 play_sound("manual_lock")
181 return StateZu(self.state_machine)
182 # handle_wakeup_event intentionally not overwritten
184 class StateClosing(State):
185 # TODO: share code with StateOpening, and possibly also with the nerv-mechanism from StateAboutToOpen
186 def __init__(self,callback,sm):
188 self.callbacks=[callback]
189 # FIXME: can we send "202 processing: Trying to close the door" here? Are the callbacks multi-use?
191 self.actor().act(Actor.CMD_CLOSE)
192 def notify(self, did_it_work):
193 s = "200 okay: door closed" if did_it_work else ("500 internal server error: Couldn't close door with %d tries à %f seconds" % (CLOSE_REPEAT_NUMBER,CLOSE_REPEAT_TIMEOUT))
194 for cb in self.callbacks:
197 def handle_pins_event(self):
199 if not pins.door_locked:
201 return StateAboutToOpen(self.state_machine)
202 def handle_open_event(self,callback):
203 callback("409 conflict: The server is currently trying to close the door. Try again later.")
204 def handle_wakeup_event(self):
205 over = time.time() - self.time_entered
206 nexttry = (self.tries+1) * CLOSE_REPEAT_TIMEOUT
208 if self.tries < CLOSE_REPEAT_NUMBER:
209 self.actor().act(Actor.CMD_CLOSE)
212 logger.critical("Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
214 return StateAboutToOpen(self.state_machine)
216 class StateAboutToLeave(State):
220 class StateLeaving(State):
224 def __init__(self, actor):
226 self.callback = ThreadFunction(self._callback)
227 self.current_state = StateStart(self)
234 def _callback(self, cmd, arg=None):
236 if cmd == StateMachine.CMD_PINS:
239 newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state
240 self.old_pins = self.pins # FIXME not used?
241 while newstate is not None:
242 logger.info("StateMachine: new state = %s" % newstate.__class__.__name__)
243 self.current_state = newstate
244 newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)