Add FSMAwareEventHandler which keeps track of FSM state

This commit is contained in:
Kristóf Tóth 2018-07-14 20:45:56 +02:00
parent 4236a17b69
commit 646a671ff3
2 changed files with 26 additions and 8 deletions

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from .event_handler_base import EventHandlerBase, TriggeredEventHandler, BroadcastingEventHandler
from .event_handler_base import EventHandlerBase, FSMAwareEventHandler, BroadcastingEventHandler
from .fsm_base import FSMBase
from .linear_fsm import LinearFSM
from .yaml_fsm import YamlFSM

View File

@ -107,20 +107,38 @@ class EventHandlerBase(ABC):
pass
class TriggeredEventHandler(EventHandlerBase, ABC):
class FSMAwareEventHandler(EventHandlerBase, ABC):
# pylint: disable=abstract-method
"""
Abstract base class for EventHandlers which are only triggered in case
TFWServer has successfully triggered an FSM step defined in __init__.
Abstract base class for EventHandlers which automatically
keep track of the state of the TFW FSM.
"""
def __init__(self, key, trigger):
def __init__(self, key):
super().__init__(key)
self.trigger = trigger
self.subscribe('fsm_update')
self.fsm_state = None
def dispatch_handling(self, message):
if message.get('trigger') == self.trigger:
if message['key'] == 'fsm_update':
self._handle_fsm_update(message)
else:
return super().dispatch_handling(message)
return None
def _handle_fsm_update(self, message):
try:
new_state = message['data']['current_state']
trigger = message['data']['last_trigger']
if self.fsm_state != new_state:
self.handle_fsm_step(self.fsm_state, new_state, trigger)
self.fsm_state = new_state
except KeyError:
LOG.error('Invalid fsm_update message received!')
def handle_fsm_step(self, from_state, to_state, trigger):
"""
Called in case the TFW FSM has stepped.
"""
pass
class BroadcastingEventHandler(EventHandlerBase, ABC):