Rework FSMManager predicate contract

This commit is contained in:
Kristóf Tóth 2018-02-26 17:47:35 +01:00
parent 250b9da6d6
commit 5d7223779a

View File

@ -4,6 +4,7 @@ from collections import defaultdict
from tfw.networking.server.controller_responder import ControllerResponder
from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
from tfw.message_sender import MessageSender
from tfw.config.logs import logging
log = logging.getLogger(__name__)
@ -42,13 +43,20 @@ class FSMManager:
def __init__(self, fsm):
self._fsm = fsm
self.trigger_predicates = defaultdict(list)
self.messenge_sender = MessageSender()
@property
def fsm(self):
return self._fsm
def trigger(self, trigger, message):
if all((predicate() for predicate in self.trigger_predicates[trigger])):
predicate_results = []
for predicate in self.trigger_predicates[trigger]:
success, message = predicate()
predicate_results.append(success)
self.messenge_sender.send('FSM', message)
if all(predicate_results):
try: self.fsm.trigger(trigger, message=message)
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))