Skip to content

Commit f114cfb

Browse files
committed
Updated Java, dotnet, TOC, and redirect files
1 parent b20fcd7 commit f114cfb

File tree

4 files changed

+35
-101
lines changed

4 files changed

+35
-101
lines changed

articles/iot-hub/.openpublishing.redirection.iot-hub.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,26 @@
15391539
"source_path_from_root": "/articles/iot-hub/device-twins-node.md",
15401540
"redirect_url": "/azure/iot-hub/how-to-device-twins",
15411541
"redirect_document_id": false
1542+
},
1543+
{
1544+
"source_path_from_root": "/articles/iot-hub/device-management-dotnet.md",
1545+
"redirect_url": "/azure/iot-hub/how-to-device-management",
1546+
"redirect_document_id": false
1547+
},
1548+
{
1549+
"source_path_from_root": "/articles/iot-hub/device-management-java.md",
1550+
"redirect_url": "/azure/iot-hub/how-to-device-management",
1551+
"redirect_document_id": false
1552+
},
1553+
{
1554+
"source_path_from_root": "/articles/iot-hub/device-management-python.md",
1555+
"redirect_url": "/azure/iot-hub/how-to-device-management",
1556+
"redirect_document_id": false
1557+
},
1558+
{
1559+
"source_path_from_root": "/articles/iot-hub/device-management-node.md",
1560+
"redirect_url": "/azure/iot-hub/how-to-device-management",
1561+
"redirect_document_id": false
15421562
}
15431563
]
15441564
}

articles/iot-hub/TOC.yml

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,8 @@
301301
displayName: module twins, module identity
302302
href: module-twins-node.md
303303
- name: Get started with device management
304-
items:
305-
- name: New version
306-
displayName: device management, status updates, device maintenance windows
307-
href: how-to-device-management.md
308-
- name: .NET
309-
displayName: device management, status updates, device maintenance windows
310-
href: device-management-dotnet.md
311-
- name: Python
312-
displayName: device management, status updates, device maintenance windows
313-
href: device-management-python.md
314-
- name: Node.js
315-
displayName: device management, status updates, device maintenance windows
316-
href: device-management-node.md
317-
- name: Java
318-
displayName: device management, status updates, device maintenance windows
319-
href: device-management-java.md
304+
displayName: device management, status updates, device maintenance windows
305+
href: how-to-device-management.md
320306
- name: Schedule and broadcast jobs
321307
items:
322308
- name: CLI

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

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ This article describes how to use the [Azure IoT SDK for .NET](https://github.co
1919

2020
## Create a device application
2121

22-
This section describes how to use device application code to:
23-
24-
* Respond to a direct method called by the cloud
25-
* Trigger a simulated device reboot
26-
* Use the reported properties to enable device twin queries to identify devices and when they were last rebooted
22+
This section describes how to use device application code to create a direct method callback listener.
2723

2824
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
2925

@@ -50,17 +46,13 @@ The `CreateFromConnectionString` [TransportType](/dotnet/api/microsoft.azure.dev
5046
This example connects to a device using the `Mqtt` transport protocol.
5147

5248
```csharp
53-
using Microsoft.Azure.Devices.Client;
54-
using Microsoft.Azure.Devices.Shared;
55-
using Newtonsoft.Json;
56-
5749
static string DeviceConnectionString = "{IoT hub device connection string}";
5850
static deviceClient = null;
5951
deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString,
6052
TransportType.Mqtt);
6153
```
6254

63-
### Create a direct method callback
55+
### Create a direct method callback listener
6456

6557
Use [SetMethodHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setmethodhandlerasync) to initialize a direct method callback listener. 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.
6658

@@ -99,16 +91,6 @@ static Task<MethodResponse> onReboot(MethodRequest methodRequest, object userCon
9991
try
10092
{
10193
Console.WriteLine("Rebooting!");
102-
103-
// Update device twin with reboot time.
104-
TwinCollection reportedProperties, reboot, lastReboot;
105-
lastReboot = new TwinCollection();
106-
reboot = new TwinCollection();
107-
reportedProperties = new TwinCollection();
108-
lastReboot["lastReboot"] = DateTime.Now;
109-
reboot["reboot"] = lastReboot;
110-
reportedProperties["iothubDM"] = reboot;
111-
Client.UpdateReportedPropertiesAsync(reportedProperties).Wait();
11294
}
11395
catch (Exception ex)
11496
{
@@ -135,23 +117,14 @@ The Azure IoT SDK for .NET provides working samples of device apps that handle d
135117

136118
## Create a backend application
137119

138-
This section describes how to trigger a direct method on a device and then use device twin queries to monitor the status of that device.
120+
This section describes how to trigger a direct method on a device.
139121

140122
The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class exposes all methods required to create a backend application to send direct method calls to devices.
141123

142124
### Required service NuGet package
143125

144126
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
145127

146-
### Using statements
147-
148-
Add the following `using` statements.
149-
150-
```csharp
151-
using Microsoft.Azure.Devices;
152-
using Microsoft.Azure.Devices.Shared;
153-
```
154-
155128
### Connect to IoT hub
156129

157130
Connect a backend application using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring?#microsoft-azure-devices-serviceclient-createfromconnectionstring(system-string-microsoft-azure-devices-serviceclientoptions)).
@@ -163,7 +136,6 @@ As a parameter to `CreateFromConnectionString`, supply the **service** shared ac
163136
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
164137

165138
```csharp
166-
using Microsoft.Azure.Devices;
167139
static ServiceClient client;
168140
static string connectionString = "{IoT hub service shared access policy connection string}";
169141
client = ServiceClient.CreateFromConnectionString(connectionString);
@@ -190,16 +162,6 @@ client.InvokeDeviceMethodAsync(targetDevice, method);
190162
Console.WriteLine("Invoked firmware update on device.");
191163
```
192164

193-
This example gets the device twin for the rebooting device and outputs the reported properties. This output shows that the `onReboot` callback method updated the `lastReboot`, `Reboot`, and `iothubDM` reported properties.
194-
195-
```csharp
196-
public static async Task QueryTwinRebootReported()
197-
{
198-
Twin twin = await registryManager.GetTwinAsync(targetDevice);
199-
Console.WriteLine(twin.Properties.Reported.ToJson());
200-
}
201-
```
202-
203165
### SDK service samples
204166

205167
The Azure IoT SDK for .NET provides working samples of service apps that handle message tasks. For more information, see:

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

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,19 @@ To connect to a device:
6161

6262
### Create a direct method callback
6363

64-
Call [subscribeToDeviceMethod](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-subscribetodevicemethod(com-microsoft-azure-sdk-iot-device-devicetwin-devicemethodcallback-java-lang-object-com-microsoft-azure-sdk-iot-device-iothubeventcallback-java-lang-object)) to initialize a direct method callback listener. 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.
64+
Call [subscribeToMethods](https://azure.github.io/azure-iot-sdk-java/master/device/com/microsoft/azure/sdk/iot/device/InternalClient.html#subscribeToMethods-com.microsoft.azure.sdk.iot.device.twin.MethodCallback-java.lang.Object-int-) to initialize a direct method callback listener. `subscribeToMethods` listens for incoming direct methods until the connection is terminated. The method name and payload is received for each direct method call.
6565

66-
This example sets up a callback listener named `reboot` that will trigger when the "reboot" direct method name is called.
67-
68-
```java
69-
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(), null);
70-
```
71-
72-
In this example, the `DirectMethodCallback` callback method implements the direct method on the device.
66+
For example:
7367

7468
```java
75-
protected static class DirectMethodCallback implements com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback
76-
{
77-
@Override
78-
public DeviceMethodData call(String methodName, Object methodData, Object context)
79-
{
80-
DeviceMethodData deviceMethodData;
81-
switch (methodName)
82-
{
83-
case "reboot" :
84-
{
85-
int status = METHOD_SUCCESS;
86-
System.out.println("Received reboot request");
87-
deviceMethodData = new DeviceMethodData(status, "Started reboot");
88-
RebootDeviceThread rebootThread = new RebootDeviceThread();
89-
Thread t = new Thread(rebootThread);
90-
t.start();
91-
break;
92-
}
93-
default:
94-
{
95-
int status = METHOD_NOT_DEFINED;
96-
deviceMethodData = new DeviceMethodData(status, "Not defined direct method " + methodName);
97-
}
98-
}
99-
return deviceMethodData;
100-
}
101-
}
69+
client.subscribeToMethods(
70+
(methodName, methodData, context) ->
71+
{
72+
System.out.println("Received a direct method invocation with name " + methodName + " and payload " + methodData.getPayloadAsJsonString());
73+
return new DirectMethodResponse(200, methodData);
74+
},
75+
null);
76+
System.out.println("Successfully subscribed to direct methods");
10277
```
10378

10479
> [!NOTE]
@@ -114,15 +89,6 @@ This section describes how to initiate a remote reboot on a device using a direc
11489

11590
The `ServiceClient` [DeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod) class contains methods that services can use to access device twins.
11691

117-
### Service import statements
118-
119-
Use the following service import statements to access the Azure IoT SDK for Java.
120-
121-
```java
122-
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
123-
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
124-
```
125-
12692
### Connect to IoT hub
12793

12894
Use the [DeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod?#com-microsoft-azure-sdk-iot-service-devicetwin-devicemethod-devicemethod(java-lang-string)) constructor to add the service primary connection string and connect to IoT Hub.

0 commit comments

Comments
 (0)