Fix statemachine

This commit is contained in:
sqozz 2021-02-25 17:19:59 +01:00
parent a74f7f19cf
commit 947639b62e
5 changed files with 49 additions and 81 deletions

View file

@ -75,7 +75,9 @@ if __name__ == '__main__':
feap = FeApUserInterface(cfg.pinconfig) feap = FeApUserInterface(cfg.pinconfig)
controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig) controller = statemachine.StateMachineController(phone, feap, cfg.dialconfig)
webinterface = FeTapWeb({}, controller) 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 # TODO: Use real events from daemon
controller.queue_event('registration_in_progress') controller.queue_event('registration_in_progress')

View file

@ -149,6 +149,7 @@ class SchelltState(BaseState):
class AcceptingState(BaseState): class AcceptingState(BaseState):
def __init__(self, controller): def __init__(self, controller):
super(AcceptingState, self).__init__(controller) super(AcceptingState, self).__init__(controller)
print('Accepting call in AcceptingState')
self._controller.phone.accept_call() self._controller.phone.accept_call()
def on_call_accepted(self): def on_call_accepted(self):
@ -277,6 +278,9 @@ class StateMachineController(object):
self.feap = feap self.feap = feap
self.dialconfig = dialconfig self.dialconfig = dialconfig
self.publish_event = None
self.publish_state = None
self.publish_transition = None
self.state = InitState(self) self.state = InitState(self)
self.__timeout = None self.__timeout = None
@ -292,6 +296,9 @@ class StateMachineController(object):
if not evname: if not evname:
return return
if self.publish_event is not None:
self.publish_event(self.state, evname, evargs, evkwargs)
print('!!! event: %s' % (evname)) print('!!! event: %s' % (evname))
handler = getattr(self.state, 'on_%s' % (evname)) handler = getattr(self.state, 'on_%s' % (evname))
try: try:
@ -306,11 +313,11 @@ class StateMachineController(object):
oldstate = self.state.__class__ oldstate = self.state.__class__
print('%s -> %s' % (oldstate.__name__, newstate.__name__)) print('%s -> %s' % (oldstate.__name__, newstate.__name__))
self.state = None
self.state = newstate(self) self.state = newstate(self)
self.publish_state()
def publish_state(self): if self.publish_transition is not None:
pass self.publish_transition(oldstate, self.state, evname, evargs, evkwargs)
def queue_event(self, evname, *evargs, **evkwargs): def queue_event(self, evname, *evargs, **evkwargs):
if not hasattr(AbstractState, 'on_%s' % (evname)): if not hasattr(AbstractState, 'on_%s' % (evname)):

18
test.py Normal file
View file

@ -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)

View file

@ -6,76 +6,6 @@ from statemachine import InitState, RegisteringState, IdleState, SchelltState, A
import threading import threading
import json 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(): class FeTapWeb():
def __init__(self, config, statemachine_controller): def __init__(self, config, statemachine_controller):
self.config = config self.config = config
@ -90,14 +20,22 @@ class FeTapWeb():
def root(self): def root(self):
return "<h1>Hello world!</h1>" return "<h1>Hello world!</h1>"
def publish_status(self): def on_event(self, state, event, evargs, evkwargs):
state_name = self.controller.state.__class__.__name__ if event == "incoming_call":
new_state = eval("Web" + state_name)(self) remote_number = self.controller.phone.get_remote_number()
self.controller.state = new_state 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 = {"state": state_name}
data = json.dumps(data) self.send_event(data)
for ws in self.websockets: print(ostate, nstate, event, evargs, evkwargs)
ws.send(data)
def send_event(self, js_event): def send_event(self, js_event):
data = json.dumps(js_event) data = json.dumps(js_event)

View file

@ -7,6 +7,9 @@
<link rel="stylesheet" href="css/styles.css"> <link rel="stylesheet" href="css/styles.css">
</head> </head>
<body> <body>
<input type="tel" id="phonenumber" />
<label for="state_history">State history:</label>
<textarea name="state_history" cols="40" rows="5"></textarea>
<script src="js/scripts.js"></script> <script src="js/scripts.js"></script>
</body> </body>
</html> </html>