Refactor FSMUpdate message sending logic to class FSMUpdater

This commit is contained in:
Kristóf Tóth 2018-02-23 14:55:58 +01:00
parent 940e74d6da
commit cf4a5bc09d

View File

@ -10,8 +10,9 @@ log = logging.getLogger(__name__)
class TFWServer:
def __init__(self, fsm_type):
self._fsm = fsm_type()
self.uplink = ServerUplinkConnector()
self._fsm.subscribe(self.fsm_callback)
self.fsm_updater = FSMUpdater(self._fsm)
self._fsm.subscribe(self.fsm_updater.update)
self.application = Application(
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
'proxy_filter': self.proxy_filter})]
@ -29,7 +30,16 @@ class TFWServer:
try: return self.fsm.trigger(message['key'], message=message)
except AttributeError: return False
def fsm_callback(self, kwargs_dict):
def listen(self, port):
self.application.listen(port)
class FSMUpdater:
def __init__(self, fsm):
self.fsm = fsm
self.uplink = ServerUplinkConnector()
def update(self, kwargs_dict):
self.uplink.send(*self.generate_fsm_update())
def generate_fsm_update(self):
@ -39,6 +49,3 @@ class TFWServer:
'valid_transitions':
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
return key, response
def listen(self, port):
self.application.listen(port)