Skip to content

Commit a228d9d

Browse files
authored
Merge pull request #108086 from JimacoMS3/python-jobs-howto-v2
Updated python job how-to service code to v2
2 parents a6181a5 + a46cb80 commit a228d9d

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

articles/iot-hub/iot-hub-python-python-schedule-jobs.md

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.service: iot-hub
66
services: iot-hub
77
ms.devlang: python
88
ms.topic: conceptual
9-
ms.date: 08/16/2019
9+
ms.date: 03/17/2020
1010
ms.author: robinsh
1111
---
1212

@@ -50,7 +50,7 @@ At the end of this tutorial, you have two Python apps:
5050

5151
## Prerequisites
5252

53-
[!INCLUDE [iot-hub-include-python-installation-notes](../../includes/iot-hub-include-python-installation-notes.md)]
53+
[!INCLUDE [iot-hub-include-python-v2-installation-notes](../../includes/iot-hub-include-python-v2-installation-notes.md)]
5454

5555
## Create an IoT hub
5656

@@ -171,18 +171,14 @@ For more information about IoT Hub shared access policies and permissions, see [
171171
172172
## Schedule jobs for calling a direct method and updating a device twin's properties
173173
174-
In this section, you create a Python console app that initiates a remote **lockDoor** on a device using a direct method and update the device twin's properties.
174+
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.
175175
176-
1. At your command prompt, run the following command to install the **azure-iot-service-client** package:
176+
1. At your command prompt, run the following command to install the **azure-iot-hub** package:
177177
178178
```cmd/sh
179-
pip install azure-iothub-service-client
179+
pip install azure-iot-hub
180180
```
181181
182-
> [!NOTE]
183-
> The pip package for azure-iothub-service-client is currently available only for Windows OS. For Linux/Mac OS, please refer to the Linux and Mac OS-specific sections on the [Prepare your development environment for Python](https://github.com/Azure/azure-iot-sdk-python/blob/v1-deprecated/doc/python-devbox-setup.md) post.
184-
>
185-
186182
2. Using a text editor, create a new **scheduleJobService.py** file in your working directory.
187183
188184
3. 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](#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](#register-a-new-device-in-the-iot-hub):
@@ -193,35 +189,28 @@ In this section, you create a Python console app that initiates a remote **lockD
193189
import threading
194190
import uuid
195191
196-
import iothub_service_client
197-
from iothub_service_client import IoTHubRegistryManager, IoTHubRegistryManagerAuthMethod
198-
from iothub_service_client import IoTHubDeviceTwin, IoTHubDeviceMethod, IoTHubError
192+
from azure.iot.hub import IoTHubRegistryManager
193+
from azure.iot.hub.models import Twin, TwinProperties, CloudToDeviceMethod, CloudToDeviceMethodResult, QuerySpecification, QueryResult
199194
200195
CONNECTION_STRING = "{IoTHubConnectionString}"
201196
DEVICE_ID = "{deviceId}"
202197
203198
METHOD_NAME = "lockDoor"
204199
METHOD_PAYLOAD = "{\"lockTime\":\"10m\"}"
205-
UPDATE_JSON = "{\"properties\":{\"desired\":{\"building\":43,\"floor\":3}}}"
200+
UPDATE_PATCH = {"building":43,"floor":3}
206201
TIMEOUT = 60
207202
WAIT_COUNT = 5
208203
```
209204
210205
4. Add the following function that is used to query for devices:
211206
212207
```python
213-
def query_condition(device_id):
214-
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
208+
def query_condition(iothub_registry_manager, device_id):
215209
216-
number_of_devices = 10
217-
dev_list = iothub_registry_manager.get_device_list(number_of_devices)
210+
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE deviceId = '{}'".format(device_id))
211+
query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 1)
218212
219-
for device in range(0, number_of_devices):
220-
if dev_list[device].deviceId == device_id:
221-
return 1
222-
223-
print ( "Device not found" )
224-
return 0
213+
return len(query_result.items)
225214
```
226215
227216
5. Add the following methods to run the jobs that call the direct method and device twin:
@@ -232,10 +221,13 @@ In this section, you create a Python console app that initiates a remote **lockD
232221
print ( "Scheduling job: " + str(job_id) )
233222
time.sleep(wait_time)
234223
235-
if query_condition(device_id):
236-
iothub_device_method = IoTHubDeviceMethod(CONNECTION_STRING)
224+
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
225+
226+
227+
if query_condition(iothub_registry_manager, device_id):
228+
deviceMethod = CloudToDeviceMethod(method_name=METHOD_NAME, payload=METHOD_PAYLOAD)
237229
238-
response = iothub_device_method.invoke(device_id, METHOD_NAME, METHOD_PAYLOAD, TIMEOUT)
230+
response = iothub_registry_manager.invoke_device_method(DEVICE_ID, deviceMethod)
239231
240232
print ( "" )
241233
print ( "Direct method " + METHOD_NAME + " called." )
@@ -245,10 +237,13 @@ In this section, you create a Python console app that initiates a remote **lockD
245237
print ( "Scheduling job " + str(job_id) )
246238
time.sleep(wait_time)
247239
248-
if query_condition(device_id):
249-
iothub_twin_method = IoTHubDeviceTwin(CONNECTION_STRING)
240+
iothub_registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
241+
242+
if query_condition(iothub_registry_manager, device_id):
250243
251-
twin_info = iothub_twin_method.update_twin(DEVICE_ID, UPDATE_JSON)
244+
twin = iothub_registry_manager.get_twin(DEVICE_ID)
245+
twin_patch = Twin(properties= TwinProperties(desired=UPDATE_PATCH))
246+
twin = iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag)
252247
253248
print ( "" )
254249
print ( "Device twin updated." )
@@ -293,9 +288,9 @@ In this section, you create a Python console app that initiates a remote **lockD
293288
time.sleep(1)
294289
status_counter += 1
295290
296-
except IoTHubError as iothub_error:
291+
except Exception as ex:
297292
print ( "" )
298-
print ( "Unexpected error {0}" % iothub_error )
293+
print ( "Unexpected error {0}" % ex )
299294
return
300295
except KeyboardInterrupt:
301296
print ( "" )

0 commit comments

Comments
 (0)