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
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.
19
19
@@ -72,7 +72,7 @@ This article is meant to complement runnable SDK samples that are referenced fro
72
72
73
73
* 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).
74
74
75
-
## IoT Hub service connection string
75
+
## Get the IoT Hub service connection string
76
76
77
77
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.
System.out.println("Successfully set property \"HomeTemp(F)\" to value "+ newTemperature);
91
91
```
92
92
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`.
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.
// 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
System.out.println("Received desired property update. Current twin:");
122
+
System.out.println(twin);
123
+
}
124
+
}
125
+
```
126
+
93
127
### SDK sample
94
128
95
129
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
190
224
191
225
To create a device query:
192
226
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.
194
228
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.
196
230
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.
198
232
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.
200
234
201
235
This example queries two IoT hub queries. Each query returns a maximum of 100 devices.
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:
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) {
#### 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
+
consttwinPatch2= {
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) {
0 commit comments