Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import asyncio
from six.moves import input
from azure.iot.device.aio import IoTHubModuleClient


async def main():
# The client object is used to interact with your Azure IoT hub.
module_client = IoTHubModuleClient.create_from_edge_environment()

# connect the client.
await module_client.connect()

# define behavior for receiving a twin patch
async def twin_patch_listener(device_client):
while True:
patch = await device_client.receive_twin_desired_properties_patch() # blocking call
print("the data in the desired properties patch was: {}".format(patch))

# define behavior for halting the application
def stdin_listener():
while True:
selection = input("Press Q to quit\n")
if selection == "Q" or selection == "q":
print("Quitting...")
break

# Schedule task for twin patch
asyncio.create_task(twin_patch_listener(module_client))

# Run the stdin listener in the event loop
loop = asyncio.get_running_loop()
user_finished = loop.run_in_executor(None, stdin_listener)

# Wait for user to indicate they are done listening for messages
await user_finished

# Finally, disconnect
await module_client.disconnect()


if __name__ == "__main__":
asyncio.run(main())

# 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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import asyncio
import random
from azure.iot.device.aio import IoTHubModuleClient


async def main():
# The client object is used to interact with your Azure IoT hub.
module_client = IoTHubModuleClient.create_from_edge_environment()

# connect the client.
await module_client.connect()

# update the reported properties
reported_properties = {"temperature": random.randint(320, 800) / 10}
print("Setting reported temperature to {}".format(reported_properties["temperature"]))
await module_client.patch_twin_reported_properties(reported_properties)

# Finally, disconnect
await module_client.disconnect()


if __name__ == "__main__":
asyncio.run(main())

# 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()