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()
Exemple #2
0
 async def test_calling_object_calls_input_function_and_returns_result(
         self, mocker, mock_function):
     callback = async_adapter.AwaitableCallback(mock_function)
     result = callback()
     assert mock_function.call_count == 1
     assert mock_function.call_args == mocker.call()
     assert result == mock_function.return_value
    async def disconnect(self):
        """Disconnect the client from the Azure IoT Hub or Azure IoT Edge Hub instance.
        """
        logger.info("Disconnecting from Hub...")
        disconnect_async = async_adapter.emulate_async(
            self._transport.disconnect)

        def sync_callback():
            logger.info("Successfully disconnected from Hub")

        callback = async_adapter.AwaitableCallback(sync_callback)

        await disconnect_async(callback=callback)
        await callback.completion()
    async def connect(self):
        """Connects the client to an Azure IoT Hub or Azure IoT Edge Hub instance.

        The destination is chosen based on the credentials passed via the auth_provider parameter
        that was provided when this object was initialized.
        """
        logger.info("Connecting to Hub...")
        connect_async = async_adapter.emulate_async(self._transport.connect)

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

        callback = async_adapter.AwaitableCallback(sync_callback)

        await connect_async(callback=callback)
        await callback.completion()
    async def _enable_feature(self, feature_name):
        """Enable an Azure IoT Hub feature in the transport

        :param feature_name: The name of the feature to enable.
        See azure.iot.hub.devicesdk.transport.constant for possible values.
        """
        logger.info("Enabling feature:" + feature_name + "...")
        enable_feature_async = async_adapter.emulate_async(
            self._transport.enable_feature)

        def sync_callback():
            logger.info("Successfully enabled feature:" + feature_name)

        callback = async_adapter.AwaitableCallback(sync_callback)

        await enable_feature_async(feature_name, callback=callback)
    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()
    async def send_method_response(self, method_request, payload, status):
        """Send a response to a method request via the Azure IoT Hub or Azure IoT Edge Hub.

        :param method_request: MethodRequest object representing the method request being
        responded to.
        :param payload: The desired payload for the method response.
        :param int status: The desired return status code for the method response.
        """
        logger.info("Sending method response to Hub...")
        send_method_response_async = async_adapter.emulate_async(
            self._transport.send_method_response)

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

        callback = async_adapter.AwaitableCallback(sync_callback)

        # TODO: maybe consolidate method_request, result and status into a new object
        await send_method_response_async(method_request,
                                         payload,
                                         status,
                                         callback=callback)
        await callback.completion()
Exemple #8
0
 async def test_awaiting_completion_of_callback_returns_result(
         self, mock_function):
     callback = async_adapter.AwaitableCallback(mock_function)
     callback()
     assert await callback.completion() == mock_function.return_value
     assert callback.future.done()
Exemple #9
0
 async def test_can_be_called_using_kwargs(self, mocker, mock_function):
     callback = async_adapter.AwaitableCallback(mock_function)
     result = callback(a=1, b=2, c=3)
     assert mock_function.call_count == 1
     assert mock_function.call_args == mocker.call(a=1, b=2, c=3)
     assert result == mock_function.return_value
Exemple #10
0
 async def test_calling_object_completes_future(self, mock_function):
     callback = async_adapter.AwaitableCallback(mock_function)
     assert not callback.future.done()
     callback()
     await asyncio.sleep(0.1)  # wait to give time to complete the callback
     assert callback.future.done()