Ejemplo n.º 1
0
    def publish(self, topic, payload, qos, retain=False):
        future = Future()
        packet_id = 0

        def puback(packet_id):
            future.set_result(dict(packet_id=packet_id))

        try:
            packet_id = _awscrt.mqtt_client_connection_publish(
                self._binding, topic, payload, qos.value, retain, puback)
        except Exception as e:
            future.set_exception(e)

        return future, packet_id
Ejemplo n.º 2
0
    def publish(self, topic, payload, qos, retain=False):
        """Publish message (async).

        If the device is offline, the PUBLISH packet will be sent once the connection resumes.

        Args:
            topic (str): Topic name.
            payload (buffer): Contents of message.
            qos (QoS): Quality of Service for delivering this message.
            retain (bool): If True, the server will store the message and its QoS
                so that it can be delivered to future subscribers whose subscriptions
                match its topic name.

        Returns:
            Tuple[concurrent.futures.Future, int]: Tuple containing a Future and
            the ID of the PUBLISH packet. The QoS determines when the Future completes:

            *   For QoS 0, completes as soon as the packet is sent.
            *   For QoS 1, completes when PUBACK is received.
            *   For QoS 2, completes when PUBCOMP is received.

            If successful, the Future will contain a dict with the following members:

            *   ['packet_id'] (int): ID of the PUBLISH packet that is complete.
        """
        future = Future()
        packet_id = 0

        def puback(packet_id, error_code):
            if error_code != 0:
                future.set_exception(awscrt.exceptions.from_code(error_code))
            else:
                future.set_result(dict(packet_id=packet_id))

        try:
            packet_id = _awscrt.mqtt_client_connection_publish(
                self._binding, topic, payload, qos.value, retain, puback)
        except Exception as e:
            future.set_exception(e)

        return future, packet_id