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.
18
18
19
-
Use a direct method to initiate device management actions (such as reboot, factory reset, and firmware update) from a back-end application.
19
+
Use a direct method from a back-end application to initiate device management actions, such as reboot, factory reset, and firmware update.
20
20
21
21
The device is responsible for:
22
22
23
-
* Handling the method request sent from IoT Hub.
24
-
* Initiating the corresponding device-specific action on the device.
25
-
* Providing status updates through reported properties to IoT Hub.
23
+
* Handling the direct method request sent from IoT Hub
24
+
* Initiating the corresponding device-specific action on the device
25
+
* Providing status updates through reported properties to IoT Hub
26
26
27
27
This article shows you how a back-end app and a device app can work together to initiate and monitor a remote device action using a direct method.
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-device-management-dotnet.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,9 @@ catch (Exception ex)
86
86
}
87
87
```
88
88
89
-
In this example, the `onReboot` callback method implements the direct method on the device. This code updates reported properties related to a device reboot. The reported properties can be read and verified by a IoT Hub or backend application, as demonstrated in the [Create a backend application](#create-a-backend-application) section of this article.
89
+
Continuing the example, the `onReboot` callback method implements the direct method on the device.
90
+
91
+
The handler function calls [MethodResponse](/dotnet/api/microsoft.azure.devices.client.methodresponse) to send a response acknowledgement to the calling application.
@@ -178,13 +180,11 @@ To invoke a method on a device:
178
180
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the [Create a direct method callback](#create-a-direct-method-callback) section of this article.
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class exposes all the methods you require to interact with direct methods from the device.
36
+
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class exposes all the methods you require to interact with direct methods on the device.
37
37
38
38
To connect to a device:
39
39
@@ -106,7 +106,7 @@ As a parameter to the `DeviceMethod` constructor, supply the **service** shared
DeviceMethod methodClient = new DeviceMethod(iotHubConnectionString);
111
111
```
112
112
@@ -116,21 +116,32 @@ DeviceMethod methodClient = new DeviceMethod(iotHubConnectionString);
116
116
117
117
### Invoke a method on a device
118
118
119
-
Call [invoke](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod?#method-details) to invoke a method on a device and return its result.
119
+
Call [invoke](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod?#method-details) to invoke a method on a device and return the result status.
120
120
121
-
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the **Create a direct method callback** section of this article.
121
+
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the **Create a direct method callback listener** section of this article.
122
+
123
+
The payload parameter is optional. Use `null` if there is no payload supplied. The payload parameter can take different data forms including string, byte array, and HashMap. For examples, see [Direct Method Tests](https://github.com/Azure/azure-iot-sdk-java/blob/main/iot-e2e-tests/common/src/test/java/tests/integration/com/microsoft/azure/sdk/iot/iothub/methods/DirectMethodsTests.java).
* [Thermostat service sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/aeea7806be7e894d8a977c16b7e6618728267a94/service/iot-service-samples/pnp-service-sample/thermostat-service-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/service/Thermostat.java#L69)
* [Thermostat service sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/aeea7806be7e894d8a977c16b7e6618728267a94/service/iot-service-samples/pnp-service-sample/thermostat-service-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/service/Thermostat.java)
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-device-management-node.md
+7-55Lines changed: 7 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,15 +101,17 @@ client.open(function(err) {
101
101
102
102
### Create a direct method callback
103
103
104
-
Call [onDeviceMethod](/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.
104
+
Call [onDeviceMethod](/javascript/api/azure-iot-device/client?view=azure-node-latest#azure-iot-device-client-ondevicemethod) 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.
105
+
106
+
The callback handler function should call `send` to send a response acknowledgement message to the calling application.
105
107
106
108
This example sets up a direct method handler named `onReboot`.
107
109
108
110
```javascript
109
111
client.onDeviceMethod('reboot', onReboot);
110
112
```
111
113
112
-
In this example, the `onReboot` callback method implements the direct method on the device. The code is executed when the "rebootDevice" direct method is called from a service application. This code updates reported properties related to a simulated device reboot. The reported properties can be read and verified by an IoT Hub or backend application, as demonstrated in the **Create a backend application** section of this article.
114
+
In this example, the `onReboot` callback method implements the direct method on the device. The code is executed when the "reboot" direct method is called from a service application.
113
115
114
116
```javascript
115
117
varonReboot=function(request, response) {
@@ -123,37 +125,14 @@ var onReboot = function(request, response) {
// Add your device's reboot API for physical restart.
150
129
console.log('Rebooting!');
151
130
};
152
131
```
153
132
154
133
### SDK device samples
155
134
156
-
The Azure IoT SDK for Node.js provides working samples of device apps that handle device management tasks. For more information, see [The device management pattern samples](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/doc/dmpatterns.md).
135
+
The Azure IoT SDK for Node.js provides working samples of device apps that handle device management tasks. For more information, see [Device management pattern samples](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/doc/dmpatterns.md).
157
136
158
137
## Create a backend application
159
138
@@ -167,10 +146,6 @@ Run this command to install **azure-iothub** on your development machine:
167
146
npm install azure-iothub --save
168
147
```
169
148
170
-
### Create a Registry object
171
-
172
-
The [Registry](/javascript/api/azure-iothub/registry) class exposes all methods required to interact with direct methods from a backend application.
173
-
174
149
### Connect to IoT hub
175
150
176
151
You can connect a backend service to IoT Hub using the following methods:
@@ -182,18 +157,16 @@ You can connect a backend service to IoT Hub using the following methods:
182
157
183
158
#### Connect using a shared access policy
184
159
185
-
Use [fromConnectionString](/javascript/api/azure-iothub/registry?#azure-iothub-registry-fromconnectionstring) to connect to IoT hub.
160
+
Use [fromConnectionString](/javascript/api/azure-iothub/client?#azure-iothub-client-fromconnectionstring) to connect to IoT hub.
186
161
187
162
To invoke a direct method on a device through IoT Hub, your service needs the **service connect** permission. By default, every IoT Hub is created with a shared access policy named **service** that grants this permission.
188
163
189
164
As a parameter to `CreateFromConnectionString`, supply the **service** shared access policy connection string. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
190
165
191
166
```javascript
192
-
var Registry =require('azure-iothub').Registry;
193
167
var Client =require('azure-iothub').Client;
194
168
var connectionString ='{IoT hub shared access policy connection string}';
195
169
var client =Client.fromConnectionString(connectionString);
196
-
var registry =Registry.fromConnectionString(serviceConnectionString);
197
170
```
198
171
199
172
#### Connect using Microsoft Entra
@@ -205,7 +178,7 @@ var registry = Registry.fromConnectionString(serviceConnectionString);
205
178
Use [invokeDeviceMethod](/javascript/api/azure-iothub/client?#azure-iothub-client-invokedevicemethod) to invoke a direct method by name on a device. The method name parameter identifies the direct method. The method name is "reboot" in the examples within this article.
206
179
207
180
```javascript
208
-
varstartRebootDevice=function(twin) {
181
+
varstartRebootDevice=function(deviceToReboot) {
209
182
210
183
var methodName ="reboot";
211
184
@@ -225,27 +198,6 @@ var startRebootDevice = function(twin) {
225
198
};
226
199
```
227
200
228
-
This example function uses device twin queries to discover the last reboot time for the device that was updated as described in the **Create a direct method callback** section of this article.
console.log('Waiting for device to report last reboot time.');
245
-
});
246
-
};
247
-
```
248
-
249
201
### SDK service samples
250
202
251
203
The Azure IoT SDK for Node.js provides working samples of service apps that handle device management tasks. For more information, see [The device management pattern samples](https://github.com/Azure/azure-iot-sdk-node/blob/a85e280350a12954f46672761b0b516d08d374b5/doc/dmpatterns.md).
0 commit comments