From d5b1f49de905742cf01b3fe1b211483c4a38a5c6 Mon Sep 17 00:00:00 2001 From: "tomasz.bartoszewski" Date: Sun, 27 Oct 2019 13:18:45 +0000 Subject: [PATCH 1/2] (GH-305) Upgrade library to version without dependency on boost --- resources/code-template/python/sendD2C.py | 62 +++++++++++------------ 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/resources/code-template/python/sendD2C.py b/resources/code-template/python/sendD2C.py index 75f87b2a..53392c20 100644 --- a/resources/code-template/python/sendD2C.py +++ b/resources/code-template/python/sendD2C.py @@ -7,66 +7,64 @@ # Using the Python Device SDK for IoT Hub: # https://github.com/Azure/azure-iot-sdk-python -# Run 'pip install azure-iothub-device-client' to install the required libraries for this application -# The sample connects to a device-specific MQTT endpoint on your IoT Hub. -import iothub_client -# pylint: disable=E0611 -from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult -from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue +# Run 'pip install azure-iot-device' to install the required libraries for this application + +import asyncio +from azure.iot.device.aio import IoTHubDeviceClient +from azure.iot.device import Message # The device connection string to authenticate the device with your IoT hub. CONNECTION_STRING = "{{deviceConnectionString}}" -# Using the MQTT protocol. -PROTOCOL = IoTHubTransportProvider.MQTT -MESSAGE_TIMEOUT = 10000 - # Define the JSON message to send to IoT Hub. TEMPERATURE = 20.0 HUMIDITY = 60 MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}" -def send_confirmation_callback(message, result, user_context): - print ( "IoT Hub responded to message with status: %s" % (result) ) - -def iothub_client_init(): - # Create an IoT Hub client - client = IoTHubClient(CONNECTION_STRING, PROTOCOL) - return client - -def iothub_client_telemetry_sample_run(): +async def iothub_client_telemetry_sample_run(): try: - client = iothub_client_init() - print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" ) + # Create instance of the device client using the authentication provider + device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) + + # Connect the device client. + await device_client.connect() while True: # Build the message with simulated telemetry values. temperature = TEMPERATURE + (random.random() * 15) humidity = HUMIDITY + (random.random() * 20) msg_txt_formatted = MSG_TXT % (temperature, humidity) - message = IoTHubMessage(msg_txt_formatted) + message = Message(msg_txt_formatted) # Add a custom application property to the message. # An IoT hub can filter on these properties without access to the message body. - prop_map = message.properties() if temperature > 30: - prop_map.add("temperatureAlert", "true") + message.custom_properties["temperatureAlert"] = "true" else: - prop_map.add("temperatureAlert", "false") + message.custom_properties["temperatureAlert"] = "false" + + # Send a single message + print( "Sending message: %s" % message ) + await device_client.send_message(message) + print("Message successfully sent!") - # Send the message. - print( "Sending message: %s" % message.get_string() ) - client.send_event_async(message, send_confirmation_callback, None) time.sleep(1) - except IoTHubError as iothub_error: - print ( "Unexpected error %s from IoTHub" % iothub_error ) - return except KeyboardInterrupt: print ( "IoTHubClient sample stopped" ) + except Exception as iothub_error: + print ( "Unexpected error %s from IoTHub" % iothub_error ) + finally: + print ( "Disconnecting client" ) + await device_client.disconnect() if __name__ == '__main__': print ( "IoT Hub simulated device" ) print ( "Press Ctrl-C to exit" ) - iothub_client_telemetry_sample_run() \ No newline at end of file + asyncio.run(iothub_client_telemetry_sample_run()) + + # If using Python 3.6 or below, use the following code instead of asyncio.run(main()): + # loop = asyncio.get_event_loop() + # loop.run_until_complete(main()) + # loop.close() \ No newline at end of file From f1a1f04f6743e1d11ae0ec4d837620835fadc689 Mon Sep 17 00:00:00 2001 From: "tomasz.bartoszewski" Date: Sun, 27 Oct 2019 14:37:30 +0000 Subject: [PATCH 2/2] Fix comment to match function name --- resources/code-template/python/sendD2C.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/code-template/python/sendD2C.py b/resources/code-template/python/sendD2C.py index 53392c20..9268cd2c 100644 --- a/resources/code-template/python/sendD2C.py +++ b/resources/code-template/python/sendD2C.py @@ -64,7 +64,7 @@ async def iothub_client_telemetry_sample_run(): print ( "Press Ctrl-C to exit" ) asyncio.run(iothub_client_telemetry_sample_run()) - # If using Python 3.6 or below, use the following code instead of asyncio.run(main()): + # If using Python 3.6 or below, use the following code instead of asyncio.run(iothub_client_telemetry_sample_run()): # loop = asyncio.get_event_loop() - # loop.run_until_complete(main()) + # loop.run_until_complete(iothub_client_telemetry_sample_run()) # loop.close() \ No newline at end of file