Implement triggering of FSM from ServerUplinkConnector

This commit is contained in:
Kristóf Tóth 2018-04-11 15:22:54 +02:00
parent 4842574cf3
commit 73fa65c6e8
2 changed files with 18 additions and 4 deletions

View File

@ -25,6 +25,7 @@ class TFWServer:
self.application = Application(
[(r'/ws', ZMQWebSocketProxy, {'make_response': self.make_response,
'proxy_filter': self.proxy_filter,
'handle_trigger': self.handle_trigger,
'event_handler_connector': self._event_handler_connector})]
)
#self.controller_responder = ControllerResponder(self.fsm) TODO: add this once controller stuff is resolved
@ -42,6 +43,10 @@ class TFWServer:
message['FSMUpdate'] = self._fsm_updater.get_fsm_state_and_transitions()
return message
def handle_trigger(self, message):
LOG.debug('Executing handler for trigger "%s"', message.get('trigger', ''))
self.trigger_fsm(message)
def trigger_fsm(self, message):
trigger = message.get('trigger', '')
try:

View File

@ -31,14 +31,15 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
def zmq_callback(self, msg_parts):
keyhandlers = {'mirror': self.mirror}
key, data = deserialize_all(*msg_parts)
LOG.debug('Received on pull socket: %s', data)
key, message = deserialize_all(*msg_parts)
LOG.debug('Received on pull socket: %s', message)
self.handle_trigger(message)
if key not in keyhandlers:
for instance in ZMQWebSocketHandler.instances:
instance.write_message(data)
instance.write_message(message)
else:
try:
keyhandlers[key](data['data'])
keyhandlers[key](message['data'])
except KeyError:
LOG.error('Invalid mirror message format! Ignoring.')
@ -57,6 +58,10 @@ class ZMQWebSocketHandler(WebSocketHandler, ABC):
def send_message(self, message: dict, key: str = None):
self._event_handler_connector.send_message(message, key)
@abstractmethod
def handle_trigger(self, message):
raise NotImplementedError
# much secure, very cors, wow
def check_origin(self, origin):
return True
@ -68,6 +73,7 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler):
super(ZMQWebSocketProxy, self).initialize(**kwargs)
self._make_response = kwargs['make_response']
self._proxy_filter = kwargs['proxy_filter']
self._handle_trigger = kwargs['handle_trigger']
def on_message(self, message):
message = json.loads(message)
@ -76,3 +82,6 @@ class ZMQWebSocketProxy(ZMQWebSocketHandler):
def make_response(self, message):
return self._make_response(message)
def handle_trigger(self, message):
self._handle_trigger(message)