Example #1
0
    def connect(self):
        MQTT_Base.connect(self)

        self.mqtt_client = mqtt_client.Client(
            client_id=self.mqtt_settings['MQTT_CLIENT_ID'])
        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        #self.mqtt_client.on_publish = self._on_publish
        self.mqtt_client.on_disconnect = self._on_disconnect
        self.mqtt_client.enable_logger(mqtt_logger)
        self.mqtt_client.enable_logger()

        if self.mqtt_settings['MQTT_USERNAME']:
            self.mqtt_client.username_pw_set(
                self.mqtt_settings['MQTT_USERNAME'],
                password=self.mqtt_settings['MQTT_PASSWORD'])

        try:
            self.mqtt_client.connect(
                self.mqtt_settings['MQTT_BROKER'],
                port=self.mqtt_settings['MQTT_PORT'],
                keepalive=self.mqtt_settings['MQTT_KEEPALIVE'],
            )

            self.mqtt_client.loop_start()

        except Exception as e:
            logger.warning('MQTT Unable to connect to Broker {}'.format(e))
Example #2
0
    def connect(self):
        MQTT_Base.connect(self)

        self.mqtt_client = mqtt_client.Client(
            client_id=self.mqtt_settings["MQTT_CLIENT_ID"])
        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        # self.mqtt_client.on_publish = self._on_publish
        self.mqtt_client.on_disconnect = self._on_disconnect
        #self.mqtt_client.enable_logger(mqtt_logger)
        #self.mqtt_client.enable_logger()

        self.set_will(self.last_will, "lost", True, 1)

        if self.mqtt_settings["MQTT_USERNAME"]:
            self.mqtt_client.username_pw_set(
                self.mqtt_settings["MQTT_USERNAME"],
                password=self.mqtt_settings["MQTT_PASSWORD"],
            )

        try:
            self.mqtt_client.connect(
                self.mqtt_settings["MQTT_BROKER"],
                port=self.mqtt_settings["MQTT_PORT"],
                keepalive=self.mqtt_settings["MQTT_KEEPALIVE"],
            )

            self.mqtt_client.loop_start()

        except Exception as e:
            logger.warning("MQTT Unable to connect to Broker {}".format(e))

        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        self.mqtt_client.on_disconnect = self._on_disconnect

        if self.mqtt_settings["MQTT_USERNAME"]:
            self.mqtt_client.username_pw_set(
                self.mqtt_settings["MQTT_USERNAME"],
                password=self.mqtt_settings["MQTT_PASSWORD"],
            )

        def start():
            try:
                asyncio.set_event_loop(self.event_loop)
                logger.info('Starting Asyincio looping forever')
                self.event_loop.run_forever()
                logger.warning('Event loop stopped')

            except Exception as e:
                logger.error('Error in event loop {}'.format(e))

        self.event_loop = asyncio.new_event_loop()

        logger.info("Starting MQTT publish thread")
        self._ws_thread = threading.Thread(target=start, args=())

        self._ws_thread.daemon = True
        self._ws_thread.start()
Example #3
0
    def connect(self):
        MQTT_Base.connect(self)

        self.mqtt_client = MQTTClient(
            'gmqtt'  #self.mqtt_settings["MQTT_CLIENT_ID"]
        )

        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        self.mqtt_client.on_disconnect = self._on_disconnect

        if self.mqtt_settings["MQTT_USERNAME"]:
            self.mqtt_client.set_auth_credentials(
                self.mqtt_settings["MQTT_USERNAME"],
                self.mqtt_settings["MQTT_PASSWORD"],
            )

        def start():
            try:
                logger.warning('Connecting to MQTT')
                asyncio.set_event_loop(self.event_loop)
                #                self.event_loop.run_until_complete(
                #                   self.mqtt_client.connect(self.mqtt_settings["MQTT_BROKER"], self.mqtt_settings["MQTT_PORT"],keepalive=self.mqtt_settings["MQTT_KEEPALIVE"], version=MQTTv311)
                #              )
                logger.warning('Looping forever')
                self.event_loop.run_forever()
                logger.warning('Event loop stopped')
                #self.session.close()
            except Exception as e:
                logger.error('Error in event loop {}'.format(e))

        self.event_loop = asyncio.new_event_loop()

        logger.warning("Starting MQTT thread")
        self._ws_thread = threading.Thread(target=start, args=())

        self._ws_thread.daemon = True
        self._ws_thread.start()

        future = asyncio.run_coroutine_threadsafe(
            self.mqtt_client.connect(
                self.mqtt_settings["MQTT_BROKER"],
                self.mqtt_settings["MQTT_PORT"],
                keepalive=self.mqtt_settings["MQTT_KEEPALIVE"],
                version=MQTTv311), self.event_loop)
Example #4
0
    def connect(self):
        MQTT_Base.connect(self)

        self.mqtt_transport = "tcp"

        if "MQTT_TRANSPORT" in self.mqtt_settings:
            if (self.mqtt_settings["MQTT_TRANSPORT"] in ["tcp", "websockets"]):
                self.mqtt_transport = self.mqtt_settings["MQTT_TRANSPORT"]
            else:
                logger.warning(
                    "MQTT transport {} not supported, falling back to TCP".
                    format(self.mqtt_settings["MQTT_TRANSPORT"]))

        # If Websocket path is set, assume websockets transport
        if "MQTT_WS_PATH" in self.mqtt_settings:
            self.mqtt_transport = "websockets"

        self.mqtt_client = mqtt_client.Client(
            client_id=self.mqtt_settings["MQTT_CLIENT_ID"],
            transport=self.mqtt_transport
            #clean_session=0
        )
        self.mqtt_client.on_connect = self._on_connect
        self.mqtt_client.on_message = self._on_message
        # self.mqtt_client.on_publish = self._on_publish
        self.mqtt_client.on_disconnect = self._on_disconnect
        #self.mqtt_client.enable_logger(mqtt_logger)
        #self.mqtt_client.enable_logger()

        self.set_will(self.last_will, "lost", True, 1)

        if "MQTT_WS_PATH" in self.mqtt_settings:
            self.mqtt_client.ws_set_options(
                path=self.mqtt_settings["MQTT_WS_PATH"])

        if self.mqtt_settings["MQTT_USERNAME"]:
            self.mqtt_client.username_pw_set(
                self.mqtt_settings["MQTT_USERNAME"],
                password=self.mqtt_settings["MQTT_PASSWORD"],
            )

        if self.mqtt_settings["MQTT_USE_TLS"]:
            self.mqtt_client.tls_set()

        try:
            self.mqtt_client.connect(
                self.mqtt_settings["MQTT_BROKER"],
                port=self.mqtt_settings["MQTT_PORT"],
                keepalive=self.mqtt_settings["MQTT_KEEPALIVE"],
            )
            self.mqtt_client.loop_start()

        except Exception as e:
            logger.warning("MQTT Unable to connect to Broker {}".format(e))

        def start():
            try:
                asyncio.set_event_loop(self.event_loop)
                logger.info('Starting Asyincio looping forever')
                self.event_loop.run_forever()
                logger.warning('Event loop stopped')

            except Exception as e:
                logger.error('Error in event loop {}'.format(e))

        self.event_loop = asyncio.new_event_loop()

        logger.info("Starting MQTT publish thread")
        self._ws_thread = threading.Thread(target=start, args=())

        self._ws_thread.daemon = True
        self._ws_thread.start()