You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Back-end apps can use Azure IoT Hub primitives, such as [device twins](iot-hub-devguide-device-twins.md) and [direct methods](iot-hub-devguide-direct-methods.md), to remotely start and monitor device management actions on devices. This article shows you how a back-end app and a device app can work together to initiate and monitor a remote device reboot using IoT Hub.
17
+
Back-end apps can use Azure IoT Hub primitives, such as [device twins](iot-hub-devguide-device-twins.md) and [direct methods](iot-hub-devguide-direct-methods.md), to remotely start and monitor device management actions on devices.
18
18
19
-
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update) from a back-end app in the cloud. The device is responsible for:
19
+
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update) from a back-end app in the cloud.
20
+
21
+
The device is responsible for:
20
22
21
23
* Handling the method request sent from IoT Hub.
22
24
23
25
* Initiating the corresponding device-specific action on the device.
24
26
25
27
* Providing status updates through *reported properties* to IoT Hub.
26
28
27
-
You can use a back-end app in the cloud to run device twin queries to report on the progress of your device management actions.
29
+
The back-end app can run device twin queries to report on the progress of the device management actions.
28
30
29
-
This article shows you how to develop two types of applications:
31
+
This article shows you how a back-end app and a device app can work together to initiate and monitor a remote device reboot using IoT Hub:
30
32
31
-
*Device apps can call a direct method to reboot a device and report the last reboot time. Direct methods are invoked from the cloud.
32
-
*Service apps can call a direct method in a device app through an IoT hub. The service app can display the response and updated reported properties.
33
+
*A service app calls a direct method to reboot in a device app through an IoT hub. The service app then displays the response and updated reported properties.
34
+
*A device app handles a direct method to reboot a device and updates the last reboot time.
These import statements are required by`IoTHubDeviceClient`.
30
+
Add import statements for`IoTHubDeviceClient`.
31
31
32
32
```python
33
33
# import the device client library
34
-
import asyncio
35
-
from azure.iot.device.aio import IoTHubDeviceClient
34
+
import time
35
+
import datetime
36
+
from azure.iot.device import IoTHubDeviceClient, MethodResponse
36
37
```
37
38
38
39
### Connect to a device
39
40
40
41
The [IoTHubDeviceClient](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient) class contains methods that can be used to work with direct methods.
41
42
42
-
To connect an application to a device:
43
-
1. Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to add the device connection string
44
-
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client to an Azure IoT hub
43
+
Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to connect an application to a device using a device connection string.
45
44
46
45
```python
47
46
# substitute the device connection string in conn_str
Call [on_method_request_received](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?view=azure-python&branch=main#azure-iot-device-iothubdeviceclient-on-method-request-received) to create a handler function or coroutine that is called when a direct method is received. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
54
+
Call [on_method_request_received](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-on-method-request-received) to create a handler function or coroutine that is called when a direct method is received. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
59
55
60
56
This example sets up a desired properties patch handler named `method_request_handler`.
61
57
@@ -114,7 +110,7 @@ This section describes how to initiate a remote reboot on a device using a direc
114
110
115
111
The [IoTHubRegistryManager](/python/api/azure-iot-hub/azure.iot.hub.iothubregistrymanager) class exposes all methods required to create a backend application to send messages to a device.
116
112
117
-
### Import statements
113
+
### Service import statements
118
114
119
115
Add these import statements to connect to Iot Hub, receive cloud-to-device methods, and call device twin methods.
0 commit comments