Ejemplo n.º 1
0
    def on_message(self, callback):
        """
        callback: Callback invoked when message received, or None to disable.
                Function should take the following arguments and return nothing:
                    topic (str): Topic receiving message.
                    payload (bytes): Payload of message.
                    **kwargs (dict): Forward-compatibility kwargs.
        """
        assert callable(callback) or callback is None

        if callback:

            def callback_wrapper(topic, payload):
                callback(topic=topic, payload=payload)
        else:
            callback_wrapper = None

        _awscrt.mqtt_client_connection_on_message(self._binding,
                                                  callback_wrapper)
Ejemplo n.º 2
0
    def on_message(self, callback):
        """Set callback to be invoked when ANY message is received.

        callback: Callback to invoke when message received, or None to disable.
            Function should take the following arguments and return nothing:

                *   `topic` (str): Topic receiving message.

                *   `payload` (bytes): Payload of message.

                *   `dup` (bool): DUP flag. If True, this might be re-delivery
                    of an earlier attempt to send the message.

                *   `qos` (:class:`QoS`): Quality of Service used to deliver the message.

                *   `retain` (bool): Retain flag. If True, the message was sent
                    as a result of a new subscription being made by the client.

                *   `**kwargs` (dict): Forward-compatibility kwargs.
        """
        assert callable(callback) or callback is None

        if callback:

            def callback_wrapper(topic, payload, dup, qos, retain):
                try:
                    callback(topic=topic,
                             payload=payload,
                             dup=dup,
                             qos=QoS(qos),
                             retain=retain)
                except TypeError:
                    # This callback used to have fewer args.
                    # Try again, passing only those those args, to cover case where
                    # user function failed to take forward-compatibility **kwargs.
                    callback(topic=topic, payload=payload)
        else:
            callback_wrapper = None

        _awscrt.mqtt_client_connection_on_message(self._binding,
                                                  callback_wrapper)
Ejemplo n.º 3
0
    def on_message(self, callback):
        """Set callback to be invoked when ANY message is received.

        callback: Callback to invoke when message received, or None to disable.
            Function should take the following arguments and return nothing:

                *   `topic` (str): Topic receiving message.

                *   `payload` (bytes): Payload of message.

                *   `dup` (bool): DUP flag. If True, this might be re-delivery
                    of an earlier attempt to send the message.

                *   `qos` (:class:`QoS`): Quality of Service used to deliver the message.

                *   `retain` (bool): Retain flag. If True, the message was sent
                    as a result of a new subscription being made by the client.

                *   `**kwargs` (dict): Forward-compatibility kwargs.
        """
        assert callable(callback) or callback is None

        if callback:

            uses_old_signature = self._check_uses_old_message_callback_signature(callback)

            def callback_wrapper(topic, payload, dup, qos, retain):
                if uses_old_signature:
                    callback(topic=topic, payload=payload)
                else:
                    callback(topic=topic, payload=payload, dup=dup, qos=QoS(qos), retain=retain)

        else:
            callback_wrapper = None

        _awscrt.mqtt_client_connection_on_message(self._binding, callback_wrapper)