diff --git a/src/app/event_handler_connector.py b/src/app/event_handler_connector.py index 95400bf..5e9c466 100644 --- a/src/app/event_handler_connector.py +++ b/src/app/event_handler_connector.py @@ -41,6 +41,3 @@ class EventHandlerConnector(EventHandlerDownlinkConnector, EventHandlerUplinkCon anchor = parse_anchor_from_message(message) encoded_message = [part.encode('utf-8') for part in (anchor, message)] self._zmq_pub_socket.send_multipart(encoded_message) - - -ehc = EventHandlerConnector() diff --git a/src/app/fsm_base.py b/src/app/fsm_base.py index e1ac5f0..d9ce109 100644 --- a/src/app/fsm_base.py +++ b/src/app/fsm_base.py @@ -1,10 +1,8 @@ from transitions import Machine -from event_handler_connector import ehc - class FSMBase: - states, transitions = [], [] + states, transitions, message_handlers = [], [], [] def __init__(self, initial: str = None): self.machine = Machine(model=self, @@ -17,4 +15,9 @@ class FSMBase: def forward_message(self, event_data): message = event_data.kwargs.get('message') - ehc.send_message(message) + #ehc.send_message(message) + for msghandler in self.message_handlers: + msghandler(message) + + def subscribe_message_handler(self, msghandler): + self.message_handlers.append(msghandler) diff --git a/src/app/handlers/zmq_websocket_handler.py b/src/app/handlers/zmq_websocket_handler.py index 30edcc5..1c0fb4a 100644 --- a/src/app/handlers/zmq_websocket_handler.py +++ b/src/app/handlers/zmq_websocket_handler.py @@ -3,15 +3,17 @@ import json from tornado.websocket import WebSocketHandler from util import parse_anchor_from_message -from event_handler_connector import ehc +from event_handler_connector import EventHandlerConnector + class ZMQWebSocketHandler(WebSocketHandler): def __init__(self, application, request, **kwargs): super().__init__(application, request, **kwargs) + self._event_handler_connector = EventHandlerConnector() def open(self, *args, **kwargs): logging.debug('WebSocket connection initiated') - ehc.register_callback(self.zmq_callback) + self._event_handler_connector.register_callback(self.zmq_callback) def zmq_callback(self, msg_parts): anchor, data = msg_parts @@ -26,7 +28,7 @@ class ZMQWebSocketHandler(WebSocketHandler): raise NotImplementedError def send_message(self, message: str, anchor: str = None): - ehc.send_message(message, anchor) + self._event_handler_connector.send_message(message, anchor) def on_close(self): pass @@ -40,6 +42,10 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler): def __init__(self, FSM, application, request, **kwargs): super().__init__(application, request, **kwargs) self.fsm = FSM() + self.fsm.subscribe_message_handler(self.handle_fsm_message) + + def handle_fsm_message(self, message): + self._event_handler_connector.send_message(message) def make_response(self, message): self.fsm.trigger(parse_anchor_from_message(message), message=message)