Reintroduce FSM update messages with minor changes

This commit is contained in:
Kristóf Tóth 2018-07-12 15:20:35 +02:00
parent 17b24377dd
commit b06472ad19

View File

@ -1,13 +1,13 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft. # Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details. # All Rights Reserved. See LICENSE file for details.
from tfw import BroadcastingEventHandler from tfw import EventHandlerBase
from tfw.config.logs import logging from tfw.config.logs import logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class FSMManagingEventHandler(BroadcastingEventHandler): class FSMManagingEventHandler(EventHandlerBase):
def __init__(self, key, fsm_type): def __init__(self, key, fsm_type):
super().__init__(key) super().__init__(key)
self.fsm = fsm_type() self.fsm = fsm_type()
@ -22,28 +22,29 @@ class FSMManagingEventHandler(BroadcastingEventHandler):
try: try:
data = message['data'] data = message['data']
message['data'] = self.command_handlers[data['command']](data) message['data'] = self.command_handlers[data['command']](data)
self.server_connector.broadcast(self._fsm_updater.generate_fsm_update())
return message return message
except KeyError: except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message) LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
def handle_trigger(self, data): def handle_trigger(self, data):
self.fsm.step(data['value']) self.fsm.step(data['value'])
return self.with_fsm_update(data) return data
def with_fsm_update(self, data):
return {
**data,
**self._fsm_updater.get_fsm_state_and_transitions()
}
def handle_update(self, data): def handle_update(self, data):
return self.with_fsm_update(data) return data
class FSMUpdater: class FSMUpdater:
def __init__(self, fsm): def __init__(self, fsm):
self.fsm = fsm self.fsm = fsm
def generate_fsm_update(self):
return {
'key': 'fsm_update',
'data': self.get_fsm_state_and_transitions()
}
def get_fsm_state_and_transitions(self): def get_fsm_state_and_transitions(self):
state = self.fsm.state state = self.fsm.state
valid_transitions = [ valid_transitions = [