Skip to content

Commit 078a885

Browse files
authored
Merge pull request #179731 from cartertinney/ct-update-sample-iot
Fixed broken inline code for IoT Device SDK
2 parents adcbf14 + 0d33ee1 commit 078a885

File tree

2 files changed

+62
-65
lines changed

2 files changed

+62
-65
lines changed

articles/iot-edge/how-to-vs-code-develop-module.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,22 @@ When debugging modules using this method, your modules are running on top of the
292292
ptvsd.break_into_debugger()
293293
```
294294
295-
For example, if you want to debug the `receive_message_listener` function, you would insert that line of code as shown below:
295+
For example, if you want to debug the `receive_message_handler` function, you would insert that line of code as shown below:
296+
297+
```python
298+
def receive_message_handler(message):
299+
ptvsd.break_into_debugger()
300+
global RECEIVED_MESSAGES
301+
RECEIVED_MESSAGES += 1
302+
if message.input_name == "input1":
303+
print("Message received on input1")
304+
print( " Data: <<{}>>".format(message.data) )
305+
print( " Properties: {}".format(message.custom_properties))
306+
print( " Total calls received: {}".format(RECEIVED_MESSAGES))
307+
print("Forwarding message to output1")
308+
client.send_message_to_output(message, "output1")
309+
print("Message successfully forwarded")
296310
297-
```python
298-
def receive_message_listener(client):
299-
ptvsd.break_into_debugger()
300-
global RECEIVED_MESSAGES
301-
while True:
302-
message = client.receive_message_on_input("input1") # blocking call
303-
RECEIVED_MESSAGES += 1
304-
print("Message received on input1")
305-
print( " Data: <<{}>>".format(message.data) )
306-
print( " Properties: {}".format(message.custom_properties))
307-
print( " Total calls received: {}".format(RECEIVED_MESSAGES))
308-
print("Forwarding message to output1")
309-
client.send_message_to_output(message, "output1")
310-
print("Message successfully forwarded")
311311
```
312312
313313
1. In the Visual Studio Code command palette:

articles/iot-edge/tutorial-python-module.md

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Each template includes sample code, which takes simulated sensor data from the *
119119
import json
120120
```
121121

122-
3. Add the **TEMPERATURE_THRESHOLD** and **TWIN_CALLBACKS** variables under the global counters. The temperature threshold sets the value that the measured machine temperature must exceed for the data to be sent to the IoT hub.
122+
3. Add global definitions for **TEMPERATURE_THRESHOLD**, **RECEIVED_MESSAGES** and **TWIN_CALLBACKS** variables. The temperature threshold sets the value that the measured machine temperature must exceed for the data to be sent to the IoT hub.
123123

124124
```python
125125
# global counters
@@ -128,63 +128,60 @@ Each template includes sample code, which takes simulated sensor data from the *
128128
RECEIVED_MESSAGES = 0
129129
```
130130

131-
4. Replace the **input1_listener** function with the following code:
131+
4. Replace the **create_client** function with the following code:
132132

133133
```python
134-
# Define behavior for receiving an input message on input1
135-
# Because this is a filter module, we forward this message to the "output1" queue.
136-
async def input1_listener(module_client):
134+
def create_client():
135+
client = IoTHubModuleClient.create_from_edge_environment()
136+
137+
# Define function for handling received messages
138+
async def receive_message_handler(message):
137139
global RECEIVED_MESSAGES
140+
print("Message received")
141+
size = len(message.data)
142+
message_text = message.data.decode('utf-8')
143+
print(" Data: <<<{data}>>> & Size={size}".format(data=message.data, size=size))
144+
print(" Properties: {}".format(message.custom_properties))
145+
RECEIVED_MESSAGES += 1
146+
print("Total messages received: {}".format(RECEIVED_MESSAGES))
147+
148+
if message.input_name == "input1":
149+
message_json = json.loads(message_text)
150+
if "machine" in message_json and "temperature" in message_json["machine"] and message_json["machine"]["temperature"] > TEMPERATURE_THRESHOLD:
151+
message.custom_properties["MessageType"] = "Alert"
152+
print("ALERT: Machine temperature {temp} exceeds threshold {threshold}".format(
153+
temp=message_json["machine"]["temperature"], threshold=TEMPERATURE_THRESHOLD
154+
))
155+
await client.send_message_to_output(message, "output1")
156+
157+
# Define function for handling received twin patches
158+
async def receive_twin_patch_handler(twin_patch):
138159
global TEMPERATURE_THRESHOLD
139-
while True:
140-
try:
141-
input_message = await module_client.on_message_received("input1") # blocking call
142-
message = input_message.data
143-
size = len(message)
144-
message_text = message.decode('utf-8')
145-
print ( " Data: <<<%s>>> & Size=%d" % (message_text, size) )
146-
custom_properties = input_message.custom_properties
147-
print ( " Properties: %s" % custom_properties )
148-
RECEIVED_MESSAGES += 1
149-
print ( " Total messages received: %d" % RECEIVED_MESSAGES )
150-
data = json.loads(message_text)
151-
if "machine" in data and "temperature" in data["machine"] and data["machine"]["temperature"] > TEMPERATURE_THRESHOLD:
152-
custom_properties["MessageType"] = "Alert"
153-
print ( "Machine temperature %s exceeds threshold %s" % (data["machine"]["temperature"], TEMPERATURE_THRESHOLD))
154-
await module_client.send_message_to_output(input_message, "output1")
155-
except Exception as ex:
156-
print ( "Unexpected error in input1_listener: %s" % ex )
157-
158-
# twin_patch_listener is invoked when the module twin's desired properties are updated.
159-
async def twin_patch_listener(module_client):
160160
global TWIN_CALLBACKS
161-
global TEMPERATURE_THRESHOLD
162-
while True:
163-
try:
164-
data = await module_client.on_twin_desired_properties_patch_received() # blocking call
165-
print( "The data in the desired properties patch was: %s" % data)
166-
if "TemperatureThreshold" in data:
167-
TEMPERATURE_THRESHOLD = data["TemperatureThreshold"]
168-
TWIN_CALLBACKS += 1
169-
print ( "Total calls confirmed: %d\n" % TWIN_CALLBACKS )
170-
except Exception as ex:
171-
print ( "Unexpected error in twin_patch_listener: %s" % ex )
172-
```
173-
174-
5. Update the **listeners** to also listen twin updates.
175-
176-
```python
177-
# Schedule task for C2D Listener
178-
listeners = asyncio.gather(input1_listener(module_client), twin_patch_listener(module_client))
179-
180-
print ( "The sample is now waiting for messages. ")
161+
print("Twin Patch received")
162+
print(" {}".format(twin_patch))
163+
if "TemperatureThreshold" in twin_patch:
164+
TEMPERATURE_THRESHOLD = twin_patch["TemperatureThreshold"]
165+
TWIN_CALLBACKS += 1
166+
print("Total calls confirmed: {}".format(TWIN_CALLBACKS))
167+
168+
try:
169+
# Set handler on the client
170+
client.on_message_received = receive_message_handler
171+
client.on_twin_desired_properties_patch_received = receive_twin_patch_handler
172+
except:
173+
# Cleanup if failure occurs
174+
client.shutdown()
175+
raise
176+
177+
return client
181178
```
182179

183-
6. Save the main.py file.
180+
7. Save the main.py file.
184181

185-
7. In the VS Code explorer, open the **deployment.template.json** file in your IoT Edge solution workspace.
182+
8. In the VS Code explorer, open the **deployment.template.json** file in your IoT Edge solution workspace.
186183

187-
8. Add the **PythonModule** module twin to the deployment manifest. Insert the following JSON content at the bottom of the **moduleContent** section, after the **$edgeHub** module twin:
184+
9. Add the **PythonModule** module twin to the deployment manifest. Insert the following JSON content at the bottom of the **moduleContent** section, after the **$edgeHub** module twin:
188185

189186
```json
190187
"PythonModule": {
@@ -196,7 +193,7 @@ Each template includes sample code, which takes simulated sensor data from the *
196193

197194
![Add module twin to deployment template](./media/tutorial-python-module/module-twin.png)
198195

199-
9. Save the deployment.template.json file.
196+
10. Save the deployment.template.json file.
200197

201198
## Build and push your module
202199

0 commit comments

Comments
 (0)