Exemplo n.º 1
0
    def start(self):
        self._discover_hub()

        self._connect_to_mqtt_broker()

        time.sleep(5)  # To wait for the container to start

        count = 0
        while count < 10:
            data = {
                "id": DEVICE_INFO["id"],
                "n": DEVICE_INFO["name"],
                "v": random.randint(15, 23),
                "u": "C",
                "t": time.time(),
            }

            self.logger.debug("Publish data: {}".format(data))

            try:
                self.mqtt_client.publish(self.publish_topic,
                                         json.dumps(data),
                                         qos=0)
            except Exception as err:
                self.logger.error("Failed to publish the data. Reason: {}",
                                  get_traceback())
            finally:
                count += 1

            self.logger.debug("Sleeping.....")
            time.sleep(random.uniform(0.5, 1))
    def start(self):
        self._discover_hub()

        self._connect_to_mqtt_broker()

        count = 0
        while count < 10:
            data = {
                "id": self.device_info["id"],
                "n": self.device_info["n"],
                "v": 53,
                "u": "%",
                "t": time.time(),
            }

            self.logger.debug("Publish data: {}".format(data))

            try:
                self.mqtt_client.publish(self.publish_topic,
                                         json.dumps(data),
                                         qos=0)
            except Exception as err:
                self.logger.error("Failed to publish the data. Reason: {}",
                                  get_traceback())
            finally:
                count += 1

            self.logger.debug("Sleeping.....")
            time.sleep(random.uniform(0.5, 1))
            time.sleep(10000)
            break
Exemplo n.º 3
0
 def _connect_to_mqtt_broker(self, broker_address):
     while True:
         try:
             self.mqtt.connect(broker_address)
             return
         except Exception as err:
             self.logger.error(
                 "Failed to connect to the broker. Reason: {}".format(
                     get_traceback()))
             time.sleep(5)
Exemplo n.º 4
0
    def _process_message_received(self, client, user_data, message):
        parsed_message = json.loads(message.payload.decode("utf-8"))
        new_value = parsed_message["message"]
        self.logger.info("Received message : {}".format(parsed_message))
        self.logger.info("Topic: {}".format(message.topic))

        self.logger.info("Send to the gateway the newly received value")
        send_message = {
            "id": self.device_uuid,
            "v": new_value,
            "t": time.time()
        }

        try:
            self.mqtt.publish(self.publish_topic,
                              json.dumps(send_message),
                              qos=0)
        except Exception as err:
            self.logger.error("Failed to publish the data. Reason: {}",
                              get_traceback())