def message(self, msg: wp_queueing.QueueMessage) -> None: """ Handle an incoming message containing a hardware probe. Parameters: msg : wp_queueing.QueueMessage Message received from the message broker containing the digital input probe. """ mth_name = "{}.{}()".format(self.__class__.__name__, inspect.currentframe().f_code.co_name) self.logger.debug(f'{mth_name}: "{str(msg)}"') if self.mqtt_data is None or self.mqtt_data[1] is None: return if msg.msg_topic != self.mqtt_input[1]: self.logger.debug(f'{mth_name}: unexpected topic "{msg.msg_topic}"; expected "{self.mqtt_input[1]}"') return probe = iot_msg_input.InputProbe() try: probe.from_dict(msg.msg_payload) except TypeError as except_: self.logger.error(f'{mth_name}: {str(except_)}') return except ValueError as except_: self.logger.error(f'{mth_name}: {str(except_)}') return # If the probe is too old, we discard it. probe_age = (datetime.now() - probe.probe_time).total_seconds() if probe_age > 10: self.logger.warning('{}: topic="{}", msg_id="{}"'.format(mth_name, msg.msg_topic, msg.msg_id)) self.logger.warning('{}: probe age = {:.0f} seconds > 10, message discarded'.format(mth_name, probe_age)) return msmt = self._sensor.measure(probe) out_msg = wp_queueing.QueueMessage(self._output_topic) out_msg.msg_payload = msmt self.mqtt_data[0].publish_single(msmt)
def _health_timer_event(self) -> None: """ Indicates the the health check timer has expired and health check information must be published. """ if self._device is None: return health_result = self._device.health() msg = wp_queueing.QueueMessage(self._health_topic()) msg.msg_payload = health_result.to_dict() self._mqtt_publish.publish_single(msg)
def _polling_timer_event(self) -> None: """ Indicates that the polling timer has expired and the underlying device must be probed. """ if self._device is None: return poll_result = self._device.probe() for probe in poll_result: msg = wp_queueing.QueueMessage(self._data_topic(probe)) msg.msg_payload = probe.to_dict() self._mqtt_publish.publish_single(msg)
def polling_timer_event(self): """ Indicates that the polling timer has expired and the MQTT broker must be queried for new messages. """ super().polling_timer_event() self.mqtt_input[0].receive() out_data_list = self._actor.timer_tick() for out_data in out_data_list: out_msg = wp_queueing.QueueMessage(self.mqtt_data[1]) out_msg.msg_payload = out_data self.mqtt_data[0].publish_single(out_msg)
def health_timer_event(self) -> None: """ Indicates the the health check timer has expired and health check information must be published. """ super().health_timer_event() mth_name = "{}.{}()".format(self.__class__.__name__, inspect.currentframe().f_code.co_name) self.logger.debug(mth_name) if self._device is None or self.mqtt_health is None: return health_result = self._device.check_health() msg = wp_queueing.QueueMessage(self._health_topic()) msg.msg_payload = health_result self.mqtt_health[0].publish_single(msg) self.logger.debug('{}: publish "{}"'.format(mth_name, json.dumps(msg.msg_payload)))
def polling_timer_event(self) -> None: """ Indicates that the polling timer has expired and the underlying device must be probed. """ super().polling_timer_event() mth_name = "{}.{}()".format(self.__class__.__name__, inspect.currentframe().f_code.co_name) self.logger.debug(mth_name) if self._device is None or self.mqtt_data is None: return poll_result = self._device.probe() for probe in poll_result: msg = wp_queueing.QueueMessage(self._data_topic(probe)) msg.msg_payload = probe self.mqtt_data[0].publish_single(msg)
def message(self, msg: wp_queueing.QueueMessage) -> None: """ Handle an incoming message containing an actor command. Parameters: msg : wp_queueing.QueueMessage Message received from the message broker containing the actor command. """ mth_name = "{}.{}()".format(self.__class__.__name__, inspect.currentframe().f_code.co_name) self.logger.debug(f'{mth_name}: "{str(msg)}"') if self.mqtt_data is None or self.mqtt_data[1] is None: return if msg.msg_topic != self.mqtt_input[1]: self.logger.debug( f'{mth_name}: unexpected topic "{msg.msg_topic}"; expected "{self.mqtt_input[1]}"' ) return actor_cmd = iot_msg_actor.ActorCommand() try: actor_cmd.from_dict(msg.msg_payload) except TypeError as except_: self.logger.error(f'{mth_name}: {str(except_)}') return except ValueError as except_: self.logger.error(f'{mth_name}: {str(except_)}') return # If the command is too old, we discard it. cmd_age = (datetime.now() - actor_cmd.cmd_time).total_seconds() if cmd_age > 10: self.logger.warning('{}: topic="{}", msg_id="{}"'.format( mth_name, msg.msg_topic, msg.msg_id)) self.logger.warning( '{}: command age = {:.0f} seconds > 10, message discarded'. format(mth_name, cmd_age)) return out_data = self._actor.process_command(actor_cmd) if out_data is not None and self.mqtt_data is not None: out_msg = wp_queueing.QueueMessage(self.mqtt_data[1]) out_msg.msg_payload = out_data self.mqtt_data[0].publish_single(out_msg)