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
@@ -162,7 +162,7 @@ To install the **Microsoft.Azure.Devices** NuGet package:
162
162
163
163
### Connect to IoT hub
164
164
165
-
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring?#microsoft-azure-devices-client-deviceclient-createfromconnectionstring(system-string-microsoft-azure-devices-client-transporttype)). As a parameter, supply the **IoT Hub service connection string** that you created in the Prerequisites section. The backend application connects to the device through IoT Hub.
165
+
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring?#microsoft-azure-devices-client-deviceclient-createfromconnectionstring(system-string-microsoft-azure-devices-client-transporttype)). As a parameter, supply the **IoT Hub service connection string** that you created in the Prerequisites section.
166
166
167
167
```csharp
168
168
usingMicrosoft.Azure.Devices;
@@ -177,7 +177,7 @@ You can retrieve current device twin fields into a [Twin](/dotnet/api/microsoft.
177
177
178
178
The `Twin` class includes [properties](/dotnet/api/microsoft.azure.devices.shared.twin?&#properties) that correspond to each section of a device twin. Use the `Twin` class properties to view and update device twin fields. You can use the `Twin` object properties to update multiple twin fields before writing the updates to the device using `UpdateTwinAsync`.
179
179
180
-
After making twin field updates, call [UpdateTwinAsync](/dotnet/api/microsoft.azure.devices.registrymanager.updatetwinasync?#microsoft-azure-devices-registrymanager-updatetwinasync(system-string-microsoft-azure-devices-shared-twin-system-string)) to write `Twin` object field updates back to a device. Use a `try` and `catch` logic coupled with an error handler to catch incorrectly formatted patch errors from `UpdateTwinAsync`.
180
+
After making twin field updates, call [UpdateTwinAsync](/dotnet/api/microsoft.azure.devices.registrymanager.updatetwinasync?#microsoft-azure-devices-registrymanager-updatetwinasync(system-string-microsoft-azure-devices-shared-twin-system-string)) to write `Twin` object field updates back to a device. Use `try` and `catch` logic coupled with an error handler to catch incorrectly formatted patch errors from `UpdateTwinAsync`.
181
181
182
182
#### Read and update device twin tags
183
183
@@ -205,7 +205,7 @@ var tagspatch =
205
205
// Assign the patch to the Twin object
206
206
twin.Tags["location"] =tagspatch;
207
207
208
-
// Apply the patch to update the device twin tags
208
+
// Apply the patch to update the device twin tags section
You can also create and apply a device twin information update patch that contains a block of field updates, including different field types such as tags mixed with desired properties. IoT Hub parses and applies the patch if it is correctly formatted and the fields are updatable. For example, device twin reported properties cannot be updated by a backend application and the patch for these fields are not be applied.
221
+
You can create and apply a JSON-formatted device twin information update patch. IoT Hub parses and applies the patch if it is correctly formatted.
222
222
223
223
This example calls `GetTwinAsync` to retrieve the current device twin fields into a `Twin` object, creates a JSON-formatted `tag` patch with region and plant location information, then calls `UpdateTwinAsync` to apply the patch to update the device twin. An error message is displayed if `UpdateTwinAsync` failed.
224
224
@@ -249,9 +249,9 @@ catch (Exception e)
249
249
250
250
#### View and update twin desired properties
251
251
252
-
Use device twin [Properties.Desired](/dotnet/api/microsoft.azure.devices.shared.twinproperties.desired?#microsoft-azure-devices-shared-twinproperties-desired) to read and write device property information. Update twin `Desired` properties using a JSON-formatted patch.
252
+
Use device twin [TwinProperties.Desired](/dotnet/api/microsoft.azure.devices.shared.twinproperties.desired?#microsoft-azure-devices-shared-twinproperties-desired) property to read and write device property information. Update twin `Desired` properties using a JSON-formatted patch.
253
253
254
-
This example calls `GetTwinAsync` to retrieve the current device twin fields into a `Twin` object, updates the twin `speed` desired property, and then calls `UpdateTwinAsync` to apply the patch to update the device twin.
254
+
This example calls `GetTwinAsync` to retrieve the current device twin fields into a `Twin` object, updates the twin `speed` desired property, and then calls `UpdateTwinAsync` to apply the `Twin` object to update the device twin.
You can also apply twin updates using these SDK methods:
267
267
268
-
* Call [ReplaceTwinAsync](/dotnet/api/microsoft.azure.devices.registrymanager.replacetwinasync) to replace the entire device twin schema.
268
+
* Call [ReplaceTwinAsync](/dotnet/api/microsoft.azure.devices.registrymanager.replacetwinasync) to replace the entire device twin.
269
269
* Call [UpdateTwins2Async](/dotnet/api/microsoft.azure.devices.registrymanager.updatetwins2async) to update a list of twins previously created within the system.
270
270
271
271
### Create a device twin query
272
272
273
273
This section demonstrates two device twin queries. Device twin queries are SQL-like queries that return a result set of device twins.
274
274
275
-
To create a device twin query, call [CreateQuery](/dotnet/api/microsoft.azure.devices.registrymanager.createquery) to submit a digital twins SQL query and obtain an [IQuery](/dotnet/api/microsoft.azure.devices.iquery) Interface. You can optionally `CreateQuery` with a second parameter to specify a maximum number of items per page.
275
+
To create a device twin query, call [CreateQuery](/dotnet/api/microsoft.azure.devices.registrymanager.createquery) to submit a twins SQL query and obtain an [IQuery](/dotnet/api/microsoft.azure.devices.iquery) Interface. You can optionally call`CreateQuery` with a second parameter to specify a maximum number of items per page.
276
276
277
277
Next call `GetNextAsTwinAsync` or `GetNextAsJsonAsync` method as many times as needed to retrieve all twin results.
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-device-twins-java.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ Device applications can read and write twin reported properties, and be notified
23
23
24
24
This section describes how to create device application code to:
25
25
26
-
* Retrieve a device twin and examine reported properties
26
+
* Retrieve and view a device twin
27
27
* Update reported device twin properties
28
28
* Subscribe to desired property changes
29
29
@@ -61,13 +61,14 @@ To connect to a device:
61
61
client.open(true);
62
62
```
63
63
64
-
### Retrieve a device twin and examine reported properties
64
+
### Retrieveand view a device twin
65
65
66
-
After opening the client connection, call [getTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-gettwin(com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice)) to retrieve the current twin properties.
66
+
After opening the client connection, call [getTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-gettwin(com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice)) to retrieve the current twin properties into a `Twin` object.
67
67
68
68
For example:
69
69
70
70
```java
71
+
privatestaticTwin twin;
71
72
System.out.println("Getting current twin");
72
73
twin = client.getTwin();
73
74
System.out.println("Received current twin:");
@@ -82,9 +83,9 @@ To update reported properties:
82
83
83
84
1. Call [getReportedProperties](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwindevice?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice-getreportedproperties()) to fetch the twin reported properties into a [TwinCollection](/java/api/com.microsoft.azure.sdk.iot.deps.twin.twincollection) object.
84
85
85
-
1. Use [put](/java/api/com.microsoft.azure.sdk.iot.deps.twin.twincollection?#com-microsoft-azure-sdk-iot-deps-twin-twincollection-put(java-lang-string-java-lang-object)) to update a reported property. Call `put` for each reported property update.
86
+
1. Use [put](/java/api/com.microsoft.azure.sdk.iot.deps.twin.twincollection?#com-microsoft-azure-sdk-iot-deps-twin-twincollection-put(java-lang-string-java-lang-object)) to update a reported property within the `TwinCollection` object. Call `put` for each reported property update.
86
87
87
-
1. Use [updateReportedProperties](/java/api/com.microsoft.azure.sdk.iot.device.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-device-devicetwin-devicetwin-updatereportedproperties(java-util-set(com-microsoft-azure-sdk-iot-device-devicetwin-property))) to update the group of reported properties that were updated using the `put` method.
88
+
1. Use [updateReportedProperties](/java/api/com.microsoft.azure.sdk.iot.device.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-device-devicetwin-devicetwin-updatereportedproperties(java-util-set(com-microsoft-azure-sdk-iot-device-devicetwin-property))) to apply the group of reported properties that were updated using the `put` method.
88
89
89
90
For example:
90
91
@@ -159,6 +160,8 @@ To connect to IoT Hub to view and update device twin information:
159
160
1. Use a [DeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-devicetwin(java-lang-string)) constructor to create the connection to IoT hub. The `DeviceTwin` object handles the communication with your IoT hub. As parameters, supply the **IoT Hub service connection string** that you created in the Prerequisites section and the `DeviceTwinClientOptions` object.
160
161
1. The [DeviceTwinDevice](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwindevice) object represents the device twin with its properties and tags.
The [azure-iot-device](/javascript/api/azure-iot-device) package contains objects that interface with IoT devices. The [Twin](/javascript/api/azure-iot-device/twin) class includes twin-specific objects. This section describes `Client` class code that is used to read and write device twin data.
40
39
41
-
### Create modules
42
-
43
-
Create Client and Protocol modules using the installed packages.
For more information about the differences between MQTT, AMQP, and HTTPS support, see [Cloud-to-device communications guidance](../articles/iot-hub/iot-hub-devguide-c2d-guidance.md) and [Choose a communication protocol](../articles/iot-hub/iot-hub-devguide-protocols.md).
61
59
62
-
This example assigns the AMQP protocol to a `Protocol` variable.
60
+
### Create Client and Protocol modules
61
+
62
+
Create a Client module using the installed package.
Call [fromConnectionString](/javascript/api/azure-iot-device/client?#azure-iot-device-client-fromconnectionstring) to establish a device-to-IoT hub connection using these parameters:
80
+
Call [fromConnectionString](/javascript/api/azure-iot-device/client?#azure-iot-device-client-fromconnectionstring) to supply deviceconnection parameters:
71
81
72
82
***connStr** - A connection string which encapsulates "device connect" permissions for an IoT hub. The connection string contains Hostname, Device ID & Shared Access Key in this format:
73
-
"HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>". See the prerequisites section for how to look up the device primary connection string.
let client =Client.fromConnectionString(deviceConnectionString, Protocol);
79
90
```
80
91
81
92
### Open the connection to IoT Hub
82
93
83
-
Use the [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open a connection between an IoT device and IoT Hub.
84
-
Use `.catch(err)` to catch an error and call handler code.
94
+
Use [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open a connection between an IoT device and IoT Hub.
95
+
Use `.catch(err)` to catch an error and execute handler code.
85
96
86
97
For example:
87
98
@@ -94,19 +105,19 @@ client.open() //open the connection
94
105
95
106
### Retrieve a device twin and examine reported properties
96
107
97
-
Call [getTwin](/javascript/api/azure-iot-device/client?#azure-iot-device-client-gettwin-1) to assign the twin object. This object can be used to read devicetwin information.
108
+
Call [getTwin](/javascript/api/azure-iot-device/client?#azure-iot-device-client-gettwin-1) to retrieve current device twin information into a [Twin](/javascript/api/azure-iot-device/twin) object.
98
109
99
110
For example:
100
111
101
112
```javascript
102
-
client.getTwin(function(err, twin) {
113
+
client.getTwin(function(err, twin))
103
114
if (err)
104
115
console.error('could not get twin');
105
116
```
106
117
107
118
### Update reported device twin properties
108
119
109
-
Use [update](/javascript/api/azure-iothub/twin?#azure-iothub-twin-update) to update device reported properties. Include a JSON-formatted patch as the first parameter and callback method as the second parameter to the method.
120
+
Use [update](/javascript/api/azure-iothub/twin?#azure-iothub-twin-update) to update device reported properties. Include a JSON-formatted patch as the first parameter and function execution status callback method as the second parameter to the method.
110
121
111
122
In this example, a JSON-formatted device twin patch is stored in the `patch` variable. The patch contains a device twin `connectivity` update value of `cellular`. The patch and error handler are passed to the `update` method. If there is an error, a console error message is displayed.
112
123
@@ -134,9 +145,9 @@ The desired property event listener can take one of the following forms:
134
145
135
146
#### Receive all patches with a single event handler
136
147
137
-
You can create code to receive any desired property change.
148
+
You can create a listener to receive any desired property change.
138
149
139
-
This code will output any properties that are received from the service.
150
+
This example code outputs any properties that are received from the service.
140
151
141
152
```javascript
142
153
twin.on('properties.desired', function (delta) {
@@ -147,7 +158,7 @@ twin.on('properties.desired', function (delta) {
147
158
148
159
#### Receive an event if anything changes under a properties grouping
149
160
150
-
You can create code to receive an event if anything under a property grouping changes.
161
+
You can create a listener to receive an event if anything under a property grouping changes.
151
162
152
163
For example:
153
164
@@ -181,7 +192,7 @@ For example:
181
192
182
193
You can set up a listener for a single property change. In this example, the code for this event is executed only if the `fanOn` boolean value is part of the patch. The code outputs the new desired `fanOn` state whenever the service updates it.
183
194
184
-
In this example, a backend application applies this desired property patch:
195
+
1. A backend application applies this desired property patch:
185
196
186
197
```javascript
187
198
consttwinPatch2= {
@@ -197,7 +208,7 @@ In this example, a backend application applies this desired property patch:
197
208
};
198
209
```
199
210
200
-
The listener triggers only when the `fanOn` property changes:
211
+
1. The listener triggers only when the `fanOn` property changes:
201
212
202
213
```javascript
203
214
twin.on('properties.desired.climate.hvac.systemControl', function (fanOn) {
@@ -215,15 +226,12 @@ The SDK contains two device twin samples:
215
226
216
227
## Create a backend application
217
228
218
-
A backend application:
219
-
220
-
* Connects to a device through IoT Hub
221
-
* Can read device reported and desired properties, write device desired properties, and run device queries
229
+
A backend application connects to a device through IoT Hub and can read device reported and desired properties, write device desired properties, and run device queries.
222
230
223
231
This section describes how to create a backend application that:
224
232
225
-
* Retrieve and update a device twin
226
-
* Create a device twin query
233
+
* Retrieves and updates a device twin
234
+
* Creates a device twin query
227
235
228
236
### Install service SDK packages
229
237
@@ -297,7 +305,7 @@ Use [createQuery](/javascript/api/azure-iothub/registry?#azure-iothub-registry-c
297
305
* **sqlQuery** - The query written as an SQL string.
298
306
* **pageSize** - The desired number of results per page (optional. default: 1000, max: 10000).
299
307
300
-
When the code creates the query object, it specifies the maximum number of returned documents in the second parameter. The query object contains a `hasMoreResults` boolean property that you can use to invoke the `nextAsTwin`methods multiple times to retrieve all results. A method called `next` is available for results that are not device twins, for example, the results of aggregation queries.
308
+
If the **pageSize** parameter is specified, the query object contains a `hasMoreResults` boolean property that you can check and use the `nextAsTwin`method to get the next twin results page as many times as needed to retrieve all results. A method called `next` is available for results that are not device twins, for example, the results of aggregation queries.
301
309
302
310
This example query selects only the device twins of devices located in the `Redmond43` plant.
0 commit comments