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
## Create a service app that updates desired properties and queries twins
46
+
47
+
In this section, you create a Python console app that adds location metadata to the device twin associated with your **{Device ID}**. The app queries IoT hub for devices located in the US and then queries devices that report a cellular network connection.
48
+
49
+
1. In your working directory, open a command prompt and install the **Azure IoT Hub Service SDK for Python**.
50
+
51
+
```cmd/sh
52
+
pip install azure-iot-hub
53
+
```
54
+
55
+
2. Using a text editor, create a new **AddTagsAndQuery.py** file.
56
+
57
+
3. Add the following code to import the required modules from the service SDK:
58
+
59
+
```python
60
+
import sys
61
+
from time import sleep
62
+
from azure.iot.hub import IoTHubRegistryManager
63
+
from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult
64
+
```
65
+
66
+
4. Add the following code. Replace `[IoTHub Connection String]` with the IoT hub connection string you copied in [Get the IoT hub connection string](#get-the-iot-hub-connection-string). Replace `[Device Id]` with the device ID (the name) from your registered device in the IoT Hub.
print("Devices in Redmond43 plant: {}".format(', '.join([twin.device_id for twin in query_result.items])))
97
+
98
+
print()
99
+
100
+
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity = 'cellular'")
print("Devices in Redmond43 plant using cellular network: {}".format(', '.join([twin.device_id for twin in query_result.items])))
103
+
104
+
exceptExceptionas ex:
105
+
print("Unexpected error {0}".format(ex))
106
+
return
107
+
exceptKeyboardInterrupt:
108
+
print("IoT Hub Device Twin service sample stopped")
109
+
```
110
+
111
+
The **IoTHubRegistryManager**object exposes all the methods required to interact with device twins from the service. The code first initializes the **IoTHubRegistryManager**object, then updates the device twin for**DEVICE_ID**, andfinally runs two queries. The first selects only the device twins of devices located in the **Redmond43** plant, and the second refines the query to select only the devices that are also connected through a cellular network.
112
+
113
+
6. Add the following code at the end of **AddTagsAndQuery.py** to implement the **iothub_service_sample_run** function:
114
+
115
+
```python
116
+
if__name__=='__main__':
117
+
print("Starting the Python IoT Hub Device Twin service sample...")
118
+
print()
119
+
120
+
iothub_service_sample_run()
121
+
```
122
+
123
+
7. Run the application with:
124
+
125
+
```cmd/sh
126
+
python AddTagsAndQuery.py
127
+
```
128
+
129
+
You should see one device in the results for the query asking forall devices located in**Redmond43**and none for the query that restricts the results to devices that use a cellular network. In the next section, you'll create a device app that will use a cellular network and you'll rerun this query to see how it changes.
130
+
131
+

132
+
45
133
## Create a device app that updates reported properties
46
134
47
135
In this section, you create a Python console app that connects to your hub as your **{Device ID}**and then updates its device twin's reported properties to confirm that it's connected using a cellular network.
@@ -142,94 +230,6 @@ In this section, you create a Python console app that connects to your hub as yo
142
230
143
231

144
232
145
-
## Create a service app that updates desired properties and queries twins
146
-
147
-
In this section, you create a Python console app that adds location metadata to the device twin associated with your **{Device ID}**. The app queries IoT hub for devices located in the US and then queries devices that report a cellular network connection.
148
-
149
-
1. In your working directory, open a command prompt and install the **Azure IoT Hub Service SDK for Python**.
150
-
151
-
```cmd/sh
152
-
pip install azure-iot-hub
153
-
```
154
-
155
-
2. Using a text editor, create a new **AddTagsAndQuery.py** file.
156
-
157
-
3. Add the following code to import the required modules from the service SDK:
158
-
159
-
```python
160
-
import sys
161
-
from time import sleep
162
-
from azure.iot.hub import IoTHubRegistryManager
163
-
from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult
164
-
```
165
-
166
-
4. Add the following code. Replace `[IoTHub Connection String]` with the IoT hub connection string you copied in [Get the IoT hub connection string](#get-the-iot-hub-connection-string). Replace `[Device Id]` with the device ID (the name) from your registered device in the IoT Hub.
print("Devices in Redmond43 plant: {}".format(', '.join([twin.device_id for twin in query_result.items])))
197
-
198
-
print()
199
-
200
-
query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity = 'cellular'")
print("Devices in Redmond43 plant using cellular network: {}".format(', '.join([twin.device_id for twin in query_result.items])))
203
-
204
-
exceptExceptionas ex:
205
-
print("Unexpected error {0}".format(ex))
206
-
return
207
-
exceptKeyboardInterrupt:
208
-
print("IoT Hub Device Twin service sample stopped")
209
-
```
210
-
211
-
The **IoTHubRegistryManager**object exposes all the methods required to interact with device twins from the service. The code first initializes the **IoTHubRegistryManager**object, then updates the device twin for**DEVICE_ID**, andfinally runs two queries. The first selects only the device twins of devices located in the **Redmond43** plant, and the second refines the query to select only the devices that are also connected through a cellular network.
212
-
213
-
6. Add the following code at the end of **AddTagsAndQuery.py** to implement the **iothub_service_sample_run** function:
214
-
215
-
```python
216
-
if__name__=='__main__':
217
-
print("Starting the Python IoT Hub Device Twin service sample...")
218
-
print()
219
-
220
-
iothub_service_sample_run()
221
-
```
222
-
223
-
7. Run the application with:
224
-
225
-
```cmd/sh
226
-
python AddTagsAndQuery.py
227
-
```
228
-
229
-
You should see one device in the results for the query asking forall devices located in**Redmond43**and none for the query that restricts the results to devices that use a cellular network.
230
-
231
-

232
-
233
233
In this article, you:
234
234
235
235
* Added device metadata as tags from a back-end app
0 commit comments