async def send_to_output(self, message, output_name):
        """Sends an event/message to the given module output.

        These are outgoing events and are meant to be "output events"

        If the connection to the service has not previously been opened by a call to connect, this
        function will open the connection before sending the event.

        :param message: message to send to the given output. Anything passed that is not an instance of the
        Message class will be converted to Message object.
        :param output_name: Name of the output to send the event to.
        """
        if not isinstance(message, Message):
            message = Message(message)

        message.output_name = output_name

        logger.info("Sending message to output:" + output_name + "...")
        send_output_event_async = async_adapter.emulate_async(
            self._transport.send_output_event)

        def sync_callback():
            logger.info("Successfully sent message to output: " + output_name)

        callback = async_adapter.AwaitableCallback(sync_callback)

        await send_output_event_async(message, callback)
        await callback.completion()
    def _on_provider_message_received_callback(self, topic, payload):
        """
        Callback that is called by the provider when a message is received.  This message can be any MQTT message,
        including, but not limited to, a C2D message, an input message, a TWIN patch, a twin response (/res), and
        a method invocation.  This function needs to decide what kind of message it is based on the topic name and
        take the correct action.

        :param topic: MQTT topic name that the message arrived on
        :param payload: Payload of the message
        """
        logger.info("Message received on topic %s", topic)
        message_received = Message(payload)
        # TODO : Discuss everything in bytes , need to be changed, specially the topic
        topic_str = topic.decode("utf-8")
        topic_parts = topic_str.split("/")

        if _is_input_topic(topic_str):
            input_name = topic_parts[TOPIC_POS_INPUT_NAME]
            message_received.input_name = input_name
            _extract_properties(topic_parts[TOPIC_POS_MODULE],
                                message_received)
            self.on_transport_input_message_received(input_name,
                                                     message_received)
        elif _is_c2d_topic(topic_str):
            _extract_properties(topic_parts[TOPIC_POS_DEVICE],
                                message_received)
            self.on_transport_c2d_message_received(message_received)
        else:
            pass  # is there any other case
Exemplo n.º 3
0
 def test_instantiates_with_optional_contenttype_encoding(self):
     s = "After all this time? Always"
     type = "application/json"
     encoding = "utf-16"
     msg = Message(s, None, encoding, type)
     assert msg.content_encoding == encoding
     assert msg.content_type == type
Exemplo n.º 4
0
class TestMessage(object):

    data_str = "After all this time? Always"
    data_int = 987
    data_obj = Message(data_str)

    @pytest.mark.parametrize(
        "data", [data_str, data_int, data_obj], ids=["String", "Integer", "Message"]
    )
    def test_instantiates_from_data(self, data):
        msg = Message(data)
        assert msg.data == data

    def test_instantiates_with_optional_message_id(self):
        s = "After all this time? Always"
        message_id = "Postage12323"
        msg = Message(s, message_id)
        assert msg.message_id == message_id

    def test_instantiates_with_optional_contenttype_encoding(self):
        s = "After all this time? Always"
        type = "application/json"
        encoding = "utf-16"
        msg = Message(s, None, encoding, type)
        assert msg.content_encoding == encoding
        assert msg.content_type == type
    async def send_event(self, message):
        """Sends a message to the default events endpoint on the Azure IoT Hub or Azure IoT Edge Hub instance.

        If the connection to the service has not previously been opened by a call to connect, this
        function will open the connection before sending the event.

        :param message: The actual message to send. Anything passed that is not an instance of the
        Message class will be converted to Message object.
        """
        if not isinstance(message, Message):
            message = Message(message)

        logger.info("Sending message to Hub...")
        send_event_async = async_adapter.emulate_async(
            self._transport.send_event)

        def sync_callback():
            logger.info("Successfully sent message to Hub")

        callback = async_adapter.AwaitableCallback(sync_callback)

        await send_event_async(message, callback=callback)
        await callback.completion()
Exemplo n.º 6
0
 def test_instantiates_with_optional_message_id(self):
     s = "After all this time? Always"
     message_id = "Postage12323"
     msg = Message(s, message_id)
     assert msg.message_id == message_id
Exemplo n.º 7
0
 def test_instantiates_from_data(self, data):
     msg = Message(data)
     assert msg.data == data