Skip to content

Commit a5104e9

Browse files
committed
Updates
1 parent 25ae73a commit a5104e9

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ zone_pivot_groups: iot-hub-howto-c2d-1
1313
ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
1414
---
1515

16-
# Get started with device twins
16+
## Get started with device twins
1717

1818
Device twins are JSON documents that store device state information, including metadata, configurations, and conditions. IoT Hub persists a device twin for each device that connects to it.
1919

@@ -72,7 +72,7 @@ This article is meant to complement runnable SDK samples that are referenced fro
7272

7373
* Make sure that port 8883 is open in your firewall. The device sample in this article uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
7474

75-
## IoT Hub service connection string
75+
## Get the IoT Hub service connection string
7676

7777
In this article, you create a back-end service that adds desired properties to a device twin and then queries the identity registry to find all devices with reported properties that have been updated accordingly. Your service needs the **service connect** permission to modify desired properties of a device twin, and it needs the **registry read** permission to query the identity registry. There is no default shared access policy that contains only these two permissions, so you need to create one.
7878

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@ ReportedPropertiesUpdateResponse response = client.updateReportedProperties(repo
9090
System.out.println("Successfully set property \"HomeTemp(F)\" to value " + newTemperature);
9191
```
9292

93+
### Subscribe to desired property changes
94+
95+
Call [subscribeToDesiredProperties](/java/api/com.microsoft.azure.sdk.iot.device.internalclient?#com-microsoft-azure-sdk-iot-device-internalclient-subscribetodesiredproperties(java-util-map(com-microsoft-azure-sdk-iot-device-devicetwin-property-com-microsoft-azure-sdk-iot-device-devicetwin-pair(com-microsoft-azure-sdk-iot-device-devicetwin-propertycallback(java-lang-string-java-lang-object)-java-lang-object)))) to subscribe to desired properties. This client will receive a callback each time a desired property is updated. That callback will either contain the full desired properties set, or only the updated desired property depending on how the desired property was changed.
96+
97+
This example subscribes to desired propery changes. Any desired property changes will be passed to a handler named `DesiredPropertiesUpdatedHandler`.
98+
99+
```java
100+
client.subscribeToDesiredProperties(new DesiredPropertiesUpdatedHandler(), null);
101+
```
102+
103+
In this example, the `DesiredPropertiesUpdatedHandler` desired property change callback handler calls [getDesiredProperties](https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwindevice?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice-getdesiredproperties()) to retrieve the property changes, then prints out the updated twin properties.
104+
105+
```java
106+
private static class DesiredPropertiesUpdatedHandler implements DesiredPropertiesCallback
107+
{
108+
@Override
109+
public void onDesiredPropertiesUpdated(Twin desiredPropertyUpdateTwin, Object context)
110+
{
111+
if (twin == null)
112+
{
113+
// No need to care about this update because these properties will be present in the twin retrieved by getTwin.
114+
System.out.println("Received desired properties update before getting current twin. Ignoring this update.");
115+
return;
116+
}
117+
118+
// desiredPropertyUpdateTwin.getDesiredProperties() contains all the newly updated desired properties as well as the new version of the desired properties
119+
twin.getDesiredProperties().putAll(desiredPropertyUpdateTwin.getDesiredProperties());
120+
twin.getDesiredProperties().setVersion(desiredPropertyUpdateTwin.getDesiredProperties().getVersion());
121+
System.out.println("Received desired property update. Current twin:");
122+
System.out.println(twin);
123+
}
124+
}
125+
```
126+
93127
### SDK sample
94128

95129
The SDK includes this [Device Twin Sample](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples/device-twin-sample).
@@ -190,13 +224,13 @@ The [Query](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.query) clas
190224

191225
To create a device query:
192226

193-
* Use [createSqlQuery](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.sqlquery?#com-microsoft-azure-sdk-iot-service-devicetwin-sqlquery-createsqlquery(java-lang-string-com-microsoft-azure-sdk-iot-service-devicetwin-sqlquery-fromtype-java-lang-string-java-lang-string)) to build the twins SQL query.
227+
1. Use [createSqlQuery](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.sqlquery?#com-microsoft-azure-sdk-iot-service-devicetwin-sqlquery-createsqlquery(java-lang-string-com-microsoft-azure-sdk-iot-service-devicetwin-sqlquery-fromtype-java-lang-string-java-lang-string)) to build the twins SQL query.
194228

195-
* Use [queryTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-querytwin(java-lang-string-java-lang-integer)) to execute the query.
229+
1. Use [queryTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-querytwin(java-lang-string-java-lang-integer)) to execute the query.
196230

197-
* Use [hasNextDeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-hasnextdevicetwin(com-microsoft-azure-sdk-iot-service-devicetwin-query)) to check if there's another device twin in the result set.
231+
1. Use [hasNextDeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-hasnextdevicetwin(com-microsoft-azure-sdk-iot-service-devicetwin-query)) to check if there's another device twin in the result set.
198232

199-
* Use [getNextDeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-getnextdevicetwin(com-microsoft-azure-sdk-iot-service-devicetwin-query)) to retrieve the next device twin from the result set.
233+
1. Use [getNextDeviceTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-getnextdevicetwin(com-microsoft-azure-sdk-iot-service-devicetwin-query)) to retrieve the next device twin from the result set.
200234

201235
This example queries two IoT hub queries. Each query returns a maximum of 100 devices.
202236

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,88 @@ twin.properties.reported.update(patch, function(err) {
117117
});
118118
```
119119
120+
### Receive notice of desired property changes
121+
122+
Use [twin.on](/javascript/api/azure-iot-device/twin?view=azure-node-latest#azure-iot-device-twin-on) to set up a desired property change event listener.
123+
124+
The desired property event listener can take one of the following forms:
125+
126+
#### Receive all patches with a single event handler
127+
128+
This code will output any properties that are received from the service.
129+
130+
```javascript
131+
twin.on('properties.desired', function (delta) {
132+
console.log('new desired properties received:');
133+
console.log(JSON.stringify(delta));
134+
});
135+
```
136+
137+
#### Receive an event if anything changes under a properties grouping
138+
139+
You can create code to receive an event if anything under a property grouping changes.
140+
141+
For example:
142+
143+
1. The `minTemperature` and `maxTemperature` properties are located under a property grouping named `properties.desired.climate changes`.
144+
145+
1. A backend application applies this patch to update `minTemperature` and `maxTemperature` desired properties:
146+
147+
```javascript
148+
Example patch document for service API code:
149+
const twinPatch1 = {
150+
properties: {
151+
desired: {
152+
climate: { minTemperature: 68, maxTemperature: 76, },
153+
},
154+
},
155+
};
156+
```
157+
158+
1. This code sets up a desired properties change listener that triggers for any changes within the `properties.desired.climate` property grouping. If there is a desired property change within this group, min and max temp change messages will be displayed to the console:
159+
160+
```javascript
161+
twin.on('properties.desired.climate', function (delta) {
162+
if (delta.minTemperature || delta.maxTemperature) {
163+
console.log('updating desired temp:');
164+
console.log('min temp = ' + twin.properties.desired.climate.minTemperature);
165+
console.log('max temp = ' + twin.properties.desired.climate.maxTemperature);
166+
}
167+
});
168+
```
169+
170+
#### Receive an event for a single property change
171+
172+
You can set up a listener for a single property change. In this example, the code for this event is only fired if the `fanOn` boolean value is part of the patch. The code outputs the new desired fan state whenever the service updates it.
173+
174+
In this example, a backend application applies this desired property patch:
175+
176+
```javascript
177+
const twinPatch2 = {
178+
properties: {
179+
desired: {
180+
climate: {
181+
hvac: {
182+
systemControl: { fanOn: true, },
183+
},
184+
},
185+
},
186+
},
187+
};
188+
```
189+
190+
The listener triggers only when the `fanOn` property changes:
191+
192+
```javascript
193+
twin.on('properties.desired.climate.hvac.systemControl', function (fanOn) {
194+
console.log('setting fan state to ' + fanOn);
195+
});
196+
```
197+
120198
### SDK samples
121199
200+
The SDK contains two device twin samples:
201+
122202
* [Simple sample device twin](https://github.com/Azure/azure-iot-sdk-node/blob/main/device/samples/javascript/simple_sample_device_twin.js)
123203
124204
* [Simple sample device twin with SAS](https://github.com/Azure/azure-iot-sdk-node/blob/main/device/samples/javascript/simple_sample_device_with_sas.js)

0 commit comments

Comments
 (0)