From 947639b62e93f47535480748a554c2e58800fdee Mon Sep 17 00:00:00 2001 From: sqozz Date: Thu, 25 Feb 2021 17:19:59 +0100 Subject: [PATCH] Fix statemachine --- fetapd.py | 4 +- statemachine.py | 13 ++++-- test.py | 18 ++++++++ webinterface.py | 92 +++++++---------------------------------- webinterface/index.html | 3 ++ 5 files changed, 49 insertions(+), 81 deletions(-) create mode 100644 test.py diff --git a/fetapd.py b/fetapd.py index 2cc54d4..254c9fd 100644 --- a/fetapd.py +++ b/fetapd.py @@ -75,7 +75,9 @@ if __name__ == '__main__': feap = FeApUserInterface(cfg.pinconfig) controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig) webinterface = FeTapWeb({}, controller) - controller.publish_state = webinterface.publish_status + #controller.publish_state = webinterface.publish_status + controller.publish_event = webinterface.on_event + controller.publish_transition = webinterface.on_transition # TODO: Use real events from daemon controller.queue_event('registration_in_progress') diff --git a/statemachine.py b/statemachine.py index dd0fab5..c294492 100644 --- a/statemachine.py +++ b/statemachine.py @@ -149,6 +149,7 @@ class SchelltState(BaseState): class AcceptingState(BaseState): def __init__(self, controller): super(AcceptingState, self).__init__(controller) + print('Accepting call in AcceptingState') self._controller.phone.accept_call() def on_call_accepted(self): @@ -277,6 +278,9 @@ class StateMachineController(object): self.feap = feap self.dialconfig = dialconfig + self.publish_event = None + self.publish_state = None + self.publish_transition = None self.state = InitState(self) self.__timeout = None @@ -292,6 +296,9 @@ class StateMachineController(object): if not evname: return + if self.publish_event is not None: + self.publish_event(self.state, evname, evargs, evkwargs) + print('!!! event: %s' % (evname)) handler = getattr(self.state, 'on_%s' % (evname)) try: @@ -306,11 +313,11 @@ class StateMachineController(object): oldstate = self.state.__class__ print('%s -> %s' % (oldstate.__name__, newstate.__name__)) + self.state = None self.state = newstate(self) - self.publish_state() - def publish_state(self): - pass + if self.publish_transition is not None: + self.publish_transition(oldstate, self.state, evname, evargs, evkwargs) def queue_event(self, evname, *evargs, **evkwargs): if not hasattr(AbstractState, 'on_%s' % (evname)): diff --git a/test.py b/test.py new file mode 100644 index 0000000..5d26adf --- /dev/null +++ b/test.py @@ -0,0 +1,18 @@ +from webinterface import FeTapWeb +import time + +class StubController(): + def __init__(self): + self.state = IdleState() + pass + +class IdleState(): + def __init__(self): + pass + +controller = StubController() +webinterface = FeTapWeb({"host": "0.0.0.0", "port": 8080}, controller) +webinterface.start() +while True: + webinterface.publish_status() + time.sleep(1) diff --git a/webinterface.py b/webinterface.py index 4d1a92d..5067427 100644 --- a/webinterface.py +++ b/webinterface.py @@ -6,76 +6,6 @@ from statemachine import InitState, RegisteringState, IdleState, SchelltState, A import threading import json -class WebInitState(InitState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebRegisteringState(RegisteringState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebIdleState(IdleState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - - def on_incoming_call(self): - super().on_incoming_call() - remote_number = self.web.controller.phone.get_remote_number() - self.web.send_event({"event": "incoming_call", "data": {"from": remote_number}}) - -class WebSchelltState(SchelltState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebAcceptingState(AcceptingState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebCallTerminatingState(CallTerminatingState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebForgottenState(ForgottenState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebBusyBeepingState(BusyBeepingState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebCallRunningState(CallRunningState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebWecktState(WecktState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebConnectingState(ConnectingState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - -class WebDialingState(DialingState): - def __init__(self, web): - self.web = web - super().__init__(web.controller) - - def on_nummernschalter_input(self, num): - super().on_nummernschalter_input(num) - self.web.send_event({"event": "num_entered", "data": {"number": int(num)}}) - - class FeTapWeb(): def __init__(self, config, statemachine_controller): self.config = config @@ -90,14 +20,22 @@ class FeTapWeb(): def root(self): return "

Hello world!

" - def publish_status(self): - state_name = self.controller.state.__class__.__name__ - new_state = eval("Web" + state_name)(self) - self.controller.state = new_state + def on_event(self, state, event, evargs, evkwargs): + if event == "incoming_call": + remote_number = self.controller.phone.get_remote_number() + self.send_event({"event": "incoming_call", "data": {"from": remote_number}}) + elif event == "nummernschalter_input": + self.send_event({"event": "num_entered", "data": {"number": int(evargs[0])}}) + else: + pass + + print(state, event, evargs, evkwargs) + + def on_transition(self, ostate, nstate, event, evargs, evkwargs): + state_name = nstate.__class__.__name__ data = {"state": state_name} - data = json.dumps(data) - for ws in self.websockets: - ws.send(data) + self.send_event(data) + print(ostate, nstate, event, evargs, evkwargs) def send_event(self, js_event): data = json.dumps(js_event) diff --git a/webinterface/index.html b/webinterface/index.html index 32384e6..95410c2 100644 --- a/webinterface/index.html +++ b/webinterface/index.html @@ -7,6 +7,9 @@ + + +