Esempio n. 1
0
    def start(self):
        """
        Starts the connection to the MQTT broker
        :return:
        """
        l.debug("Initializing the MQTT connection...")
        self._mqtt_client.connect(self.domain, self.port, keepalive=30)

        # Starts a new thread that handles mqtt protocol and calls us back via callbacks
        l.debug("(Re)Starting the MQTT loop.")
        self._mqtt_client.loop_stop(True)
        self._mqtt_client.loop_start()
        self.connect_event.wait()

        # Subscribe to the corresponding topics ...
        self.device_topic = build_device_request_topic(self.target_device_uuid)
        self.client_response_topic = build_client_response_topic(
            self.user_id, self._app_id)
        self.user_topic = build_client_user_topic(self.user_id)

        l.info(f"Subscribing to topic: {self.device_topic}")
        self._mqtt_client.subscribe(self.device_topic)
        self.subscribe_event.wait()
        self.subscribe_event.clear()

        l.info(f"Subscribing to topic: {self.client_response_topic}")
        self._mqtt_client.subscribe(self.client_response_topic)
        self.subscribe_event.wait()
        self.subscribe_event.clear()

        l.info("Subscribing to topic: {self.user_topic}")
        self._mqtt_client.subscribe(self.user_topic)
        self.subscribe_event.wait()
        self.subscribe_event.clear()
Esempio n. 2
0
 async def _async_send_and_wait_ack(self, future: Future, target_device_uuid: str, message: dict, timeout: float):
     md = self._mqtt_client.publish(topic=build_device_request_topic(target_device_uuid), payload=message, qos=1)
     try:
         return await asyncio.wait_for(future, timeout, loop=self._loop)
     except TimeoutError as e:
         _LOGGER.error(f"Timeout occurred while waiting a response for message {message} sent to device uuid "
                       f"{target_device_uuid}. Timeout was: {timeout} seconds.")
         raise CommandTimeoutError()