You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.1 KiB

10 years ago
import threading
import Queue as queue
10 years ago
class IllegalEventError(Exception):
pass
10 years ago
class AbstractState(object):
def __init__(self, controller):
pass
def on_registration_in_progress(self):
raise IllegalEventError()
def on_registration_successful(self):
raise IllegalEventError()
def on_gabelschalter_up(self):
raise IllegalEventError()
def on_gabelschalter_down(self):
raise IllegalEventError()
def on_incoming_call(self):
raise IllegalEventError()
def on_call_ended(self):
raise IllegalEventError()
def on_call_accepted(self):
raise IllegalEventError()
def on_call_ringing(self):
raise IllegalEventError()
def on_nummernschalter_input(self, num):
raise IllegalEventError()
def on_timeout(self):
raise IllegalEventError()
class InitState(AbstractState):
def on_registration_in_progress(self):
print('registration in progress')
return RegisteringState
10 years ago
class RegisteringState(AbstractState):
def on_registration_successful(self):
print('registration successfull')
return IdleState
10 years ago
class IdleState(AbstractState):
def on_incoming_call(self):
print('incomfing call')
def on_gabelschalter_up(self):
print('gabel up')
return DialingState
10 years ago
def on_gabelschalter_down(self):
pass
def on_nummernschalter_input(self, x):
pass
10 years ago
class SchelltState(AbstractState):
def on_gabelschalter_up(self):
return AcceptingState
def on_call_ended(self):
return IdleState
10 years ago
class AcceptingState(AbstractState):
def on_call_accepted(self):
return CallRunningState
10 years ago
class CallTerminatingState(AbstractState):
def on_call_ended(self):
return IdleState
def on_call_accepted(self):
return None
10 years ago
class ForgottenState(AbstractState):
def on_gabelschalter_down(self):
return IdleState
10 years ago
class BusyBeepingState(AbstractState):
def on_timeout(self):
return ForgottenState
def on_gabelschalter_down(self):
return IdleState
10 years ago
class CallRunningState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
10 years ago
class WecktState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ended(self):
return BusyBeepingState
def on_call_accepted(self):
return CallRunningState
10 years ago
class ConnectingState(AbstractState):
def on_gabelschalter_down(self):
return CallTerminatingState
def on_call_ringing(self):
return WecktState
10 years ago
class DialingState(AbstractState):
def on_gabelschalter_down(self):
return IdleState
10 years ago
def on_nummernschalter_input(self, num):
print('nummernschalter: %d' % (num))
10 years ago
def on_timeout(self):
return ConnectingState
10 years ago
class TelefonapparatUserInterface(object):
def __init__(self):
pass
10 years ago
def add_gabelschalter_callback(self, cb):
pass
10 years ago
def add_nummernschalter_callback(self, cb):
pass
10 years ago
def set_wecker(self, enabled):
pass
10 years ago
def set_schauzeichen(self, enabled):
pass
10 years ago
class StateMachineController(object):
def __init__(self):
self.__state = InitState(self)
self.__running = True
self.__evqueue = queue.Queue()
self.__evthread = threading.Thread(target=self.__event_dispatcher)
self.__evthread.start()
def __event_dispatcher(self):
while self.__running:
(evname, evargs, evkwargs) = self.__evqueue.get()
if not evname:
return
print('!!! event: %s' % (evname))
handler = getattr(self.__state, 'on_%s' % (evname))
try:
newstate = handler(*evargs, **evkwargs)
except IllegalEventError:
print('illegal event occured!!!!!!!!!!!!!!!!!!!!')
if not newstate:
continue
oldstate = self.__state.__class__
print('%s -> %s' % (oldstate.__name__, newstate.__name__))
self.__state = newstate(self)
def queue_event(self, evname, *evargs, **evkwargs):
if not hasattr(AbstractState, 'on_%s' % (evname)):
raise ValueError('Illegal event name: %s' % (evname))
self.__evqueue.put((evname, evargs, evkwargs))
def stop(self, hard=False):
if hard:
self.__running = False
self.__evqueue.put((None, None, None))
10 years ago
if __name__ == '__main__':
c = StateMachineController()
c.queue_event('registration_in_progress')
c.queue_event('registration_successful')
c.queue_event('gabelschalter_up')
c.queue_event('nummernschalter_input', 4)
c.queue_event('nummernschalter_input', 2)
#c.queue_event('gabelschalter_down')
#c.queue_event('call_accepted')
c.queue_event('timeout')
c.queue_event('call_ringing')
#c.queue_event('gabelschalter_down')
c.queue_event('call_accepted')
c.queue_event('call_ended')
c.queue_event('timeout')
c.queue_event('gabelschalter_down')
c.stop()
10 years ago