title | description | author | ms.service | services | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Schedule jobs with Azure IoT Hub (Python) | Microsoft Docs |
How to schedule an Azure IoT Hub job to invoke a direct method on multiple devices. You use the Azure IoT SDKs for Python to implement the simulated device apps and a service app to run the job. |
kgremban |
iot-hub |
iot-hub |
python |
conceptual |
03/17/2020 |
kgremban |
devx-track-python |
[!INCLUDE iot-hub-selector-schedule-jobs]
Azure IoT Hub is a fully managed service that enables a back-end app to create and track jobs that schedule and update millions of devices. Jobs can be used for the following actions:
- Update desired properties
- Update tags
- Invoke direct methods
Conceptually, a job wraps one of these actions and tracks the progress of execution against a set of devices, which is defined by a device twin query. For example, a back-end app can use a job to invoke a reboot method on 10,000 devices, specified by a device twin query and scheduled at a future time. That application can then track progress as each of those devices receive and execute the reboot method.
Learn more about each of these capabilities in these articles:
-
Device twin and properties: Get started with device twins and Tutorial: How to use device twin properties
-
Direct methods: IoT Hub developer guide - direct methods and Quickstart: direct methods
[!INCLUDE iot-hub-basic]
This tutorial shows you how to:
-
Create a Python simulated device app that has a direct method, which enables lockDoor, which can be called by the solution back end.
-
Create a Python console app that calls the lockDoor direct method in the simulated device app using a job and updates the desired properties using a device job.
At the end of this tutorial, you have two Python apps:
simDevice.py, which connects to your IoT hub with the device identity and receives a lockDoor direct method.
scheduleJobService.py, which calls a direct method in the simulated device app and updates the device twin's desired properties using a job.
[!INCLUDE iot-hub-include-python-sdk-note]
[!INCLUDE iot-hub-include-python-v2-installation-notes]
[!INCLUDE iot-hub-include-create-hub]
[!INCLUDE iot-hub-include-create-device]
In this section, you create a Python console app that responds to a direct method called by the cloud, which triggers a simulated lockDoor method.
-
At your command prompt, run the following command to install the azure-iot-device package:
pip install azure-iot-device
-
Using a text editor, create a new simDevice.py file in your working directory.
-
Add the following
import
statements and variables at the start of the simDevice.py file. ReplacedeviceConnectionString
with the connection string of the device you created above:import time from azure.iot.device import IoTHubDeviceClient, MethodResponse CONNECTION_STRING = "{deviceConnectionString}"
-
Define the following function, which will instantiate a client and configure it to respond to the lockDoor method, as well as receive device twin updates:
def create_client(): # Instantiate the client client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) # Define behavior for responding to the lockDoor direct method def method_request_handler(method_request): if method_request.name == "lockDoor": print("Locking Door!") resp_status = 200 resp_payload = {"Response": "lockDoor called successfully"} method_response = MethodResponse.create_from_method_request( method_request=method_request, status=resp_status, payload=resp_payload ) client.send_method_response(method_response) # Define behavior for receiving a twin patch def twin_patch_handler(twin_patch): print("") print("Twin desired properties patch received:") print(twin_patch) # Set the handlers on the client try: print("Beginning to listen for 'lockDoor' direct method invocations...") client.on_method_request_received = method_request_handler print("Beginning to listen for updates to the Twin desired properties...") client.on_twin_desired_properties_patch_received = twin_patch_handler except: # If something goes wrong while setting the handlers, clean up the client client.shutdown() raise
-
Add the following code to run the sample:
def main(): print ("Starting the IoT Hub Python jobs sample...") client = create_client() print ("IoTHubDeviceClient waiting for commands, press Ctrl-C to exit") try: while True: time.sleep(100) except KeyboardInterrupt: print("IoTHubDeviceClient sample stopped!") finally: # Graceful exit print("Shutting down IoT Hub Client") client.shutdown() if __name__ == '__main__': main()
-
Save and close the simDevice.py file.
Note
To keep things simple, this tutorial does not implement any retry policy. In production code, you should implement retry policies (such as an exponential backoff), as suggested in the article, Transient Fault Handling.
In this article, you create a backend service that invokes a direct method on a device and updates the device twin. The service needs the service connect permission to call a direct method on a device. The service also needs the registry read and registry write permissions to read and write the identity registry. There is no default shared access policy that contains only these permissions, so you need to create one.
To create a shared access policy that grants service connect, registry read, and registry write permissions and to get a connection string for this policy, follow these steps:
-
Open your IoT hub in the Azure portal. The easiest way to get to your IoT hub is to select Resource groups, select the resource group where your IoT hub is located, and then select your IoT hub from the list of resources.
-
On the left-side pane of your IoT hub, select Shared access policies.
-
From the top menu above the list of policies, select Add.
-
On the Add a shared access policy pane, enter a descriptive name for your policy; for example: serviceAndRegistryReadWrite. Under Permissions, select Service connect and Registry write (Registry read is automatically selected when you select Registry write). Then select Create.
-
Back on the Shared access policies pane, select your new policy from the list of policies.
-
Under Shared access keys, select the copy icon for the Connection string -- primary key and save the value.
For more information about IoT Hub shared access policies and permissions, see Access control and permissions.
In this section, you create a Python console app that initiates a remote lockDoor on a device using a direct method and also updates the device twin's desired properties.
-
At your command prompt, run the following command to install the azure-iot-hub package:
pip install azure-iot-hub
-
Using a text editor, create a new scheduleJobService.py file in your working directory.
-
Add the following
import
statements and variables at the start of the scheduleJobService.py file. Replace the{IoTHubConnectionString}
placeholder with the IoT hub connection string you copied previously in Get the IoT hub connection string. Replace the{deviceId}
placeholder with the device ID you registered in Register a new device in the IoT hub:import os import sys import datetime import time import threading import uuid import msrest from azure.iot.hub import IoTHubJobManager, IoTHubRegistryManager from azure.iot.hub.models import JobProperties, JobRequest, Twin, TwinProperties, CloudToDeviceMethod CONNECTION_STRING = "{IoTHubConnectionString}" DEVICE_ID = "{deviceId}" METHOD_NAME = "lockDoor" METHOD_PAYLOAD = "{\"lockTime\":\"10m\"}" UPDATE_PATCH = {"building":43,"floor":3} TIMEOUT = 60 WAIT_COUNT = 5 # Create IoTHubJobManager iothub_job_manager = IoTHubJobManager.from_connection_string(CONNECTION_STRING)
-
Add the following methods to run the jobs that call the direct method and device twin:
def device_method_job(job_id, device_id, execution_time): print ( "" ) print ( "Scheduling job: " + str(job_id) ) job_request = JobRequest() job_request.job_id = job_id job_request.type = "scheduleDeviceMethod" job_request.start_time = datetime.datetime.utcnow().isoformat() job_request.cloud_to_device_method = CloudToDeviceMethod(method_name=METHOD_NAME, payload=METHOD_PAYLOAD) job_request.max_execution_time_in_seconds = execution_time job_request.query_condition = "DeviceId in ['{}']".format(device_id) new_job_response = iothub_job_manager.create_scheduled_job(job_id, job_request) def device_twin_job(job_id, device_id, execution_time): print ( "" ) print ( "Scheduling job " + str(job_id) ) job_request = JobRequest() job_request.job_id = job_id job_request.type = "scheduleUpdateTwin" job_request.start_time = datetime.datetime.utcnow().isoformat() job_request.update_twin = Twin(etag="*", properties=TwinProperties(desired=UPDATE_PATCH)) job_request.max_execution_time_in_seconds = execution_time job_request.query_condition = "DeviceId in ['{}']".format(device_id) new_job_response = iothub_job_manager.create_scheduled_job(job_id, job_request)
-
Add the following code to schedule the jobs and update job status. Also include the
main
routine:def iothub_jobs_sample_run(): try: method_job_id = uuid.uuid4() device_method_job(method_job_id, DEVICE_ID, TIMEOUT) print ( "" ) print ( "Direct method called with Job Id: " + str(method_job_id) ) twin_job_id = uuid.uuid4() device_twin_job(twin_job_id, DEVICE_ID, TIMEOUT) print ( "" ) print ( "Device twin called with Job Id: " + str(twin_job_id) ) while True: print ( "" ) method_job_status = iothub_job_manager.get_scheduled_job(method_job_id) print ( "...job " + str(method_job_id) + " " + method_job_status.status ) twin_job_status = iothub_job_manager.get_scheduled_job(twin_job_id) print ( "...job " + str(twin_job_id) + " " + twin_job_status.status ) print ( "Job status posted, press Ctrl-C to exit" ) time.sleep(WAIT_COUNT) except msrest.exceptions.HttpOperationError as ex: print ( "" ) print ( "Http error {}".format(ex.response.text) ) return except Exception as ex: print ( "" ) print ( "Unexpected error {}".format(ex) ) return except KeyboardInterrupt: print ( "" ) print ( "IoTHubService sample stopped" ) if __name__ == '__main__': print ( "Starting the IoT Hub jobs Python sample..." ) print ( " Connection string = {0}".format(CONNECTION_STRING) ) print ( " Device ID = {0}".format(DEVICE_ID) ) iothub_jobs_sample_run()
-
Save and close the scheduleJobService.py file.
You are now ready to run the applications.
-
At the command prompt in your working directory, run the following command to begin listening for the reboot direct method:
python simDevice.py
-
At another command prompt in your working directory, run the following command to trigger the jobs to lock the door and update the twin:
python scheduleJobService.py
-
You see the device responses to the direct method and device twins update in the console.
In this tutorial, you used a job to schedule a direct method to a device and the update of the device twin's properties.
To continue getting started with IoT Hub and device management patterns such as end-to-end image-based update in Device Update for Azure IoT Hub tutorial using the Raspberry Pi 3 B+ Reference Image.