コード例 #1
0
 def run(self):
     while (not self.stopped.wait(
             prefs['email']['notification_timeout'])) and (not self.sent):
         email = MailSender()
         email.send_message(prefs['email']['subject'],
                            fromaddr=prefs['email']['from'],
                            to=prefs['email']['to'],
                            body="Desconexão ocorrida com o broker MQTT.")
         self.sent = True
コード例 #2
0
 def run(self):
     get_logger().info(
         "Starting REST client connecting to base URL {}".format(
             prefs['web-service']['base_url']))
     if self.retrieve_token():
         while True:
             for message in queue.get_all_not_sent():
                 self.send_message(message)
             time.sleep(1)
     else:
         get_logger().error("Abandoning thread...")
         email = MailSender()
         email.send_message(
             prefs['email']['subject'],
             fromaddr=prefs['email']['from'],
             to=prefs['email']['to'],
             body="Thread REST client encerrada. Verificar logs.")
コード例 #3
0
 def run(self):
     get_logger().info("Starting MQTT client...")
     connected = False
     email_sent = False
     while True:
         if not connected:
             try:
                 self.mqttc_cli.on_connect = self.on_connect
                 self.mqttc_cli.on_subscribe = self.on_subscribe
                 self.mqttc_cli.on_publish = self.on_publish
                 self.mqttc_cli.on_message = self.on_message
                 self.mqttc_cli.on_disconnect = self.on_disconnect
                 self.mqttc_cli.on_log = self.on_log
                 self.mqttc_cli.reconnect_delay_set(min_delay=1,
                                                    max_delay=30)
                 get_logger().debug(
                     "Connecting to MQTT broker on {}:{}, with timeout = {}."
                     .format(prefs['mqtt']['connection']['host'],
                             prefs['mqtt']['connection']['port'],
                             prefs['mqtt']['connection']['timeout']))
                 self.mqttc_cli.connect(
                     prefs['mqtt']['connection']['host'],
                     prefs['mqtt']['connection']['port'],
                     prefs['mqtt']['connection']['timeout'])
                 self.mqttc_cli.subscribe(prefs['mqtt']['topics']['status'],
                                          0)
                 self.is_running = True
                 connected = True
                 email_sent = False
                 self.mqttc_cli.loop_start()
             except (ConnectionRefusedError, TimeoutError) as ex:
                 if not email_sent:
                     email = MailSender()
                     email.send_message(
                         prefs['email']['subject'],
                         fromaddr=prefs['email']['from'],
                         to=prefs['email']['to'],
                         body=
                         "Não foi possível conectar servidor ao broker MQTT."
                     )
                     email_sent = True
                 get_logger().error(
                     "Cannot connect to MQTT broker! Exception: {}".format(
                         ex))
         time.sleep(5)