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"))]
26 # play_sound constants
27 SOUNDS_DIRECTORY = "/opt/tuer/sounds/"
28 SOUNDS_PLAYER = "/usr/bin/mplayer"
32 # commands you can send
40 def __init__(self, state_machine):
41 self.state_machine = state_machine
42 self.time_entered = time.time()
44 def handle_pins_event(self):
45 pass # one needn't implement this
46 def handle_buzz_event(self,arg): # this shouldn't be overwritten
47 self.actor.act(Actor.CMD_BUZZ)
48 arg("200 okay: buzz executed")
49 def handle_open_event(self,arg):
51 arg("412 Precondition Failed: The current state (%s) cannot handle the OPEN event" % self.__class__.__name__)
52 def handle_wakeup_event(self):
53 pass # one needn't implement this
55 return self.state_machine.pins
57 return self.state_machine.actor
58 def handle_event(self,ev,arg):
60 self.handle_pins_event()
62 self.handle_buzz_event(arg)
64 self.handle_open_event(arg)
65 elif arg is CMD_WAKEUP:
66 self.handle_wakeup_event()
68 raise Exception("Unknown command number: %d" % ev)
70 class StateStart(State):
71 def __init__(self, sm):
72 State.__init__(self,sm)
73 def handle_pins_event(self):
78 if thepins.door_locked:
84 def __init__(self,sm):
85 State.__init__(self,sm)
86 def handle_pins_event(self):
88 if not pins.door_locked:
89 return StateAboutToOpen(self.state_machine)
90 def handle_open_event(self,callback):
91 return StateOpening(callback,self.state_machine)
93 class StateOpening(State):
94 def __init__(self,callback,sm):
95 State.__init__(self,sm)
96 self.callbacks=[callback]
98 self.actor().act(Actor.CMD_OPEN)
99 def notify(self, did_it_work):
100 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))
101 for cb in self.callbacks:
104 def handle_pins_event(self):
106 if not pins.door_locked:
108 return StateAboutToOpen(self.state_machine)
109 def handle_open_event(self,callback):
110 self.callbacks.append(callback)
111 def handle_wakeup_event(self):
112 over = time.time() - self.time_entered
113 nexttry = (self.tries+1) * OPEN_REPEAT_TIMEOUT
115 if self.tries < OPEN_REPEAT_NUMBER:
116 self.actor().act(Actor.CMD_OPEN)
119 #TODO: LOG ERROR und EMAIL an Admins
121 return StateZu(self.state_machine)
123 class StateAboutToOpen(State):
124 def __init__(self, sm):
126 def handle_pins_event(self):
129 return StateZu(self.state_machine)
130 elif pins.space_active:
131 return StateAuf(self.state_machine)
133 over = time.time() - self.time_entered
135 logger.debug("AboutToOpen since %f seconds. TODO: nerv the user" % over)
138 class StateAuf(State):
142 class StateClosing(State):
146 class StateAboutToLeave(State):
150 class StateLeaving(State):
154 def __init__(self, actor):
156 self.callback = ThreadFunction(self._callback)
157 self.current_state = None
163 def _callback(self, cmd, arg=None):
165 if cmd == StateMachine.CMD_PINS:
168 newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state
169 while newstate is not None:
170 logger.info("StateMachine: new state = %s" % newstate.__class__.__name__)
171 self.current_state = newstate
172 newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)