Beispiel #1
0
    def __init__(self, broker,
                hostname=None,
                hostport=1883,
                username=None,
                password=None,
                keepalive=60,
                **kwargs):
        super(MqttApplication, self).__init__(broker=broker, **kwargs)
        self._client = MqttClient()
        self._client.on_connect = \
                lambda mqtt_client, obj, rc: self._on_connect(mqtt_client, obj, rc)
        self._client.on_disconnect = \
                lambda mqtt_client, obj, rc: self._on_disconnect(mqtt_client, obj, rc)
        self._client.on_publish = \
                lambda mqtt_client, obj, mid: self._on_publish(mqtt_client, obj, mid)
        self._client.on_subscribe = \
                lambda mqtt_client, userdata, mid, qos: self._on_subscribe(mqtt_client, mid, qos)
        self._client.on_message = \
                lambda mqtt_client, userdata, msg: self._on_message(mqtt_client, msg.payload, msg.topic, msg.qos, msg.retain)

        self._hostname = hostname
        self._hostport = hostport
        self._username = username
        self._password = password
        self._keepalive = keepalive

        self._is_connected = False
    def __init__(self,
                 host: str,
                 on_message_clbk: Callable[[Optional[Cep2Zigbee2mqttMessage]], None],
                 port: int = 1883,
                 topics: List[str] = [ROOT_TOPIC]):
        """ Class initializer where the MQTT broker's host and port can be set, the list of topics
        to subscribe and a callback to handle events from zigbee2mqtt.

        Args:
            host (str): string with the hostname, or IP address, of the MQTT broker.
            on_message_clbk (Callable[[Zigbee2mqttMessage], None]): a function that is called when
                a message is received from zigbee2mqtt. This returns None if the 
            port (int): network port of the MQTT broker. Defaults to 1883.
            topics (List[str], optional): a list of topics that the client will subscribe to.
                Defaults to ["zigbee2mqtt/#"].
        """
        self.__client = MqttClient()
        self.__client.on_connect = self.__on_connect
        self.__client.on_disconnect = self.__on_disconnect
        self.__client.on_message = self.__on_message
        self.__connected = False
        self.__events_queue = Queue()
        self.__host = host
        self.__on_message_clbk = on_message_clbk
        self.__port = port
        self.__stop_worker = Event()
        self.__subscriber_thread = Thread(target=self.__worker,
                                          daemon=True)
        self.__topics = topics
Beispiel #3
0
    def create_client(self,
                      host,
                      port,
                      username,
                      password,
                      clientid,
                      cafile=None):
        """Creating an MQTT Client Object"""
        client = MqttClient(clientid)

        if username and password:
            client.username_pw_set(username=username, password=password)
        else:
            self.logger.warn("Proceeding without username and password")

        if cafile:
            client.tls_set(ca_certs=cafile)
        else:
            self.logger.warn("Proceeding without certificate file")

        try:
            client.on_connect = self.on_connect
            client.on_message = self.on_message
            client.connect(host=host, port=port)
        except OSError as error:
            self.logger.error(error)

        return client
def create_client(host, port, username, password):
    """Creating an MQTT Client Object"""
    client = MqttClient()

    if username and password:
        client.username_pw_set(username=username, password=password)

    client.connect(host=host, port=port)
    return client
Beispiel #5
0
def create_client(host, port, username, password, cafile=None):
    """Creating an MQTT Client Object"""
    client = MqttClient()

    if username and password:
        client.username_pw_set(username=username, password=password)

    if cafile:
        client.tls_set(ca_certs=cafile)

    client.connect(host=host, port=port)
    return client
def create_client(host, port, username, password, clientid, cafile):
    """Creating an MQTT Client Object"""
    client = MqttClient(clientid)

    if username and password:
        client.username_pw_set(username=username, password=password)

    try:
        client.tls_set(ca_certs=cafile)
    except:
        print("Proceeding without certificate file")

    client.connect(host=host, port=port)
    return client
Beispiel #7
0
 def __init__(self, peer):
     """
     Initialize client
     :param peer: The peer behind the MQTT client.
     :return:
     """
     self.__peer = peer
     self.__mqtt = MqttClient()
     self.__mqtt.on_connect = self._on_connect
     self.__mqtt.on_disconnect = self._on_disconnect
     self.__mqtt.on_message = self._on_message
     self.__callback_handler = None
     self.__WILL_TOPIC = "/".join(
         (TOPIC_PREFIX, peer.app_id, RIP_TOPIC))
    def __init__(self, on_receive_cube_fn=None):
        self.on_receive_cube_fn = on_receive_cube_fn

        # https://www.infoq.com/articles/practical-mqtt-with-paho
        self.mqtt_client = MqttClient()

        if self.on_receive_cube_fn is not None:

            def on_connect(client, userdata, flags, rc):
                self.mqtt_client.subscribe(
                    CubeSerializingSocket.CUBE_TOPIC_NAME)

            self.mqtt_client.on_connect = on_connect
            self.mqtt_client.message_callback_add(
                CubeSerializingSocket.CUBE_TOPIC_NAME,
                self.receive_zipped_cube)

        self.mqtt_client.connect('127.0.0.1')
        self.mqtt_client.loop_start()
Beispiel #9
0
 def make_client(self) -> MqttClient:
     mqtt_client = MqttClient()
     mqtt_client.on_connect = self.mqtt_on_connect
     mqtt_client.on_message = self.mqtt_on_message
     self.connection.config(mqtt_client)
     return mqtt_client
Beispiel #10
0
 def __init__(self):
     super(MqttBridge, self).__init__()
     self.client = MqttClient()
     self.client.on_message = self.onMqttMessage
     self.client.on_connect = self.onConnect