Skip to content

Commit ff02067

Browse files
committed
Edits
1 parent e9f8932 commit ff02067

5 files changed

+54
-126
lines changed

articles/iot-hub/how-to-device-management.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ zone_pivot_groups: iot-hub-howto-c2d-1
1616

1717
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.
1818

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.
2020

2121
The device is responsible for:
2222

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
2626

2727
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.
2828

includes/iot-hub-howto-device-management-dotnet.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ catch (Exception ex)
8686
}
8787
```
8888

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.
9092

9193
```csharp
9294
static Task<MethodResponse> onReboot(MethodRequest methodRequest, object userContext)
@@ -159,9 +161,9 @@ As a parameter to `CreateFromConnectionString`, supply the **service** shared ac
159161
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
160162

161163
```csharp
162-
static ServiceClient client;
163-
static string connectionString = "{IoT hub service shared access policy connection string}";
164-
client = ServiceClient.CreateFromConnectionString(connectionString);
164+
ServiceClient serviceClient;
165+
string connectionString = "{IoT hub service shared access policy connection string}";
166+
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
165167
```
166168

167169
#### Connect using Microsoft Entra
@@ -178,13 +180,11 @@ To invoke a method on a device:
178180
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.
179181

180182
```csharp
183+
static string targetDevice = "myDeviceId";
181184
CloudToDeviceMethod method = new CloudToDeviceMethod("reboot");
182185
method.ResponseTimeout = TimeSpan.FromSeconds(30);
183186

184-
CloudToDeviceMethodResult result = await
185-
186-
static string targetDevice = "myDeviceId";
187-
client.InvokeDeviceMethodAsync(targetDevice, method);
187+
CloudToDeviceMethodResult response = await serviceClient.InvokeDeviceMethodAsync(targetDevice, method);
188188

189189
Console.WriteLine("Invoked firmware update on device.");
190190
```

includes/iot-hub-howto-device-management-java.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import com.microsoft.azure.sdk.iot.device.*;
3333

3434
### Connect to a device
3535

36-
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.
3737

3838
To connect to a device:
3939

@@ -106,7 +106,7 @@ As a parameter to the `DeviceMethod` constructor, supply the **service** shared
106106
For example:
107107

108108
```java
109-
public static final String iotHubConnectionString = "HostName=xxxxx.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxx";
109+
String iotHubConnectionString = "HostName=xxxxx.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxx";
110110
DeviceMethod methodClient = new DeviceMethod(iotHubConnectionString);
111111
```
112112

@@ -116,21 +116,32 @@ DeviceMethod methodClient = new DeviceMethod(iotHubConnectionString);
116116

117117
### Invoke a method on a device
118118

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.
120120

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).
124+
125+
For example:
122126

123127
```java
124-
public static final String deviceId = "myFirstDevice";
125-
private static final String methodName = "reboot";
126-
private static final Long responseTimeout = TimeUnit.SECONDS.toSeconds(30);
127-
private static final Long connectTimeout = TimeUnit.SECONDS.toSeconds(5);
128-
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, null);
128+
String deviceId = "myFirstDevice";
129+
String methodName = "reboot";
130+
String payload = "Test payload";
131+
Long responseTimeout = TimeUnit.SECONDS.toSeconds(30);
132+
Long connectTimeout = TimeUnit.SECONDS.toSeconds(5);
133+
134+
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, payload);
135+
if (result == null)
136+
{
137+
throw new IOException("Method invoke returns null");
138+
}
139+
System.out.println("Status=" + result.getStatus());
129140
```
130141

131142
### SDK service samples
132143

133144
The Azure IoT SDK for Java provides a working sample of service apps that handle direct method tasks. For more information, see:
134145

135-
* [Direct Method Sample](https://github.com/Azure/azure-iot-service-sdk-java/tree/main/service/iot-service-samples/direct-method-sample).
136-
* [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)
146+
* [Direct method sample](https://github.com/Azure/azure-iot-service-sdk-java/tree/main/service/iot-service-samples/direct-method-sample)
147+
* [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)

includes/iot-hub-howto-device-management-node.md

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,17 @@ client.open(function(err) {
101101

102102
### Create a direct method callback
103103

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.
105107

106108
This example sets up a direct method handler named `onReboot`.
107109

108110
```javascript
109111
client.onDeviceMethod('reboot', onReboot);
110112
```
111113

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.
113115

114116
```javascript
115117
var onReboot = function(request, response) {
@@ -123,37 +125,14 @@ var onReboot = function(request, response) {
123125
}
124126
});
125127

126-
// Report the reboot before the physical restart
127-
var date = new Date();
128-
var patch = {
129-
iothubDM : {
130-
reboot : {
131-
lastReboot : date.toISOString(),
132-
}
133-
}
134-
};
135-
136-
// Get device Twin
137-
client.getTwin(function(err, twin) {
138-
if (err) {
139-
console.error('could not get twin');
140-
} else {
141-
console.log('twin acquired');
142-
twin.properties.reported.update(patch, function(err) {
143-
if (err) throw err;
144-
console.log('Device reboot twin state reported')
145-
});
146-
}
147-
});
148-
149128
// Add your device's reboot API for physical restart.
150129
console.log('Rebooting!');
151130
};
152131
```
153132

154133
### SDK device samples
155134

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).
157136

158137
## Create a backend application
159138

@@ -167,10 +146,6 @@ Run this command to install **azure-iothub** on your development machine:
167146
npm install azure-iothub --save
168147
```
169148

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-
174149
### Connect to IoT hub
175150

176151
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:
182157

183158
#### Connect using a shared access policy
184159

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.
186161

187162
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.
188163

189164
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).
190165

191166
```javascript
192-
var Registry = require('azure-iothub').Registry;
193167
var Client = require('azure-iothub').Client;
194168
var connectionString = '{IoT hub shared access policy connection string}';
195169
var client = Client.fromConnectionString(connectionString);
196-
var registry = Registry.fromConnectionString(serviceConnectionString);
197170
```
198171

199172
#### Connect using Microsoft Entra
@@ -205,7 +178,7 @@ var registry = Registry.fromConnectionString(serviceConnectionString);
205178
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.
206179

207180
```javascript
208-
var startRebootDevice = function(twin) {
181+
var startRebootDevice = function(deviceToReboot) {
209182

210183
var methodName = "reboot";
211184

@@ -225,27 +198,6 @@ var startRebootDevice = function(twin) {
225198
};
226199
```
227200

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.
229-
230-
```javascript
231-
var queryTwinLastReboot = function() {
232-
233-
registry.getTwin(deviceToReboot, function(err, twin){
234-
235-
if (twin.properties.reported.iothubDM != null)
236-
{
237-
if (err) {
238-
console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
239-
} else {
240-
var lastRebootTime = twin.properties.reported.iothubDM.reboot.lastReboot;
241-
console.log('Last reboot time: ' + JSON.stringify(lastRebootTime, null, 2));
242-
}
243-
} else
244-
console.log('Waiting for device to report last reboot time.');
245-
});
246-
};
247-
```
248-
249201
### SDK service samples
250202

251203
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

Comments
 (0)