Fix statemachine
This commit is contained in:
parent
a74f7f19cf
commit
947639b62e
|
@ -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')
|
||||
|
|
|
@ -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)):
|
||||
|
|
18
test.py
Normal file
18
test.py
Normal 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)
|
|
@ -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 "<h1>Hello world!</h1>"
|
||||
|
||||
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)
|
||||
|
|
|
@ -7,6 +7,9 @@
|
|||
<link rel="stylesheet" href="css/styles.css">
|
||||
</head>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue