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