added support for C2D messsages
This commit is contained in:
Родитель
82ca83dc3e
Коммит
d9c236cad4
|
@ -34,7 +34,7 @@ Features enabled in this sample:
|
|||
|
||||
This sample application uses the device template model in the device-capability-model.json file. This template should be imported into the IoT Central application, and appropriate views generated or created from the device template.
|
||||
|
||||
To use first open the iot_central_config.py file and enter in the necessary configuration information based on you IoT Central application
|
||||
To use first open the iot_central_config.py file and enter in the necessary configuration information based on you IoT Central application. If X509 device authentication is desired please see these articles on generating the necessary certificates for IoT Central https://docs.microsoft.com/en-us/azure/iot-central/core/concepts-get-connected#connect-devices-using-x509-certificates, https://github.com/Azure/azure-iot-sdk-c/tree/master/tools/CACertificates
|
||||
|
||||
```python
|
||||
# IoT Central connection settings
|
||||
|
@ -92,4 +92,6 @@ completed sending message
|
|||
q
|
||||
Quitting...
|
||||
Disconnecting from IoT Hub
|
||||
```
|
||||
```
|
||||
|
||||
Note: Currently the Cloud to Device (C2D) feature is in preview and behind a feature flag. To use C2D please add the following to the url ?flights=c2d
|
|
@ -86,9 +86,55 @@
|
|||
"schema": "string"
|
||||
},
|
||||
"displayName": {
|
||||
"en": "echo"
|
||||
"en": "Echo"
|
||||
},
|
||||
"name": "echo"
|
||||
},
|
||||
{
|
||||
"@type": "Command",
|
||||
"durable": true,
|
||||
"request": {
|
||||
"@type": "SchemaField",
|
||||
"displayName": {
|
||||
"en": "Color"
|
||||
},
|
||||
"name": "color",
|
||||
"schema": {
|
||||
"@type": "Enum",
|
||||
"valueSchema": "string",
|
||||
"enumValues": [
|
||||
{
|
||||
"name": "red",
|
||||
"displayName": "Red",
|
||||
"enumValue": "RED"
|
||||
},
|
||||
{
|
||||
"name": "green",
|
||||
"displayName": "Green",
|
||||
"enumValue": "GREEN"
|
||||
},
|
||||
{
|
||||
"name": "blue",
|
||||
"displayName": "Blue",
|
||||
"enumValue": "BLUE"
|
||||
},
|
||||
{
|
||||
"name": "yellow",
|
||||
"displayName": "Yellow",
|
||||
"enumValue": "YELLOW"
|
||||
},
|
||||
{
|
||||
"name": "white",
|
||||
"displayName": "White",
|
||||
"enumValue": "WHITE"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"displayName": {
|
||||
"en": "fireworks"
|
||||
},
|
||||
"name": "fireworks"
|
||||
}
|
||||
],
|
||||
"@context": [
|
||||
|
|
|
@ -115,6 +115,7 @@ async def twin_patch_handler(device_client):
|
|||
await desired_ack(patch, 200, "completed")
|
||||
|
||||
|
||||
# coroutine that handles direct methods from IoT Central (or hub) until terminated
|
||||
async def direct_method_handler(device_client):
|
||||
while not terminate:
|
||||
method_request = (
|
||||
|
@ -133,15 +134,43 @@ async def direct_method_handler(device_client):
|
|||
await device_client.send_method_response(method_response)
|
||||
|
||||
|
||||
# ASCII art fireworks in the color passed in :-)
|
||||
def fireworks(color):
|
||||
print(color.format(" .''."))
|
||||
print(color.format(" .''. *''* :_\/_: ."))
|
||||
print(color.format(" :_\/_: . .:.*_\/_* : /\ : .'.:.'."))
|
||||
print(color.format(" .''.: /\ : _\(/_ ':'* /\ * : '..'. -=:o:=-"))
|
||||
print(color.format(" :_\/_:'.:::. /)\*''* * '.\\'/.'_\(/_'.':'.'"))
|
||||
print(color.format(" : /\ : ::::: '*_\/_* -= o =- /)\ ' *"))
|
||||
print(color.format(" '..' ':::' * /\ * .'/.\\'. '"))
|
||||
print(color.format(" * * *"))
|
||||
print(color.format(" *"))
|
||||
|
||||
|
||||
# handles the Cloud to Device (C2D) messages
|
||||
async def message_listener(device_client):
|
||||
while not terminate:
|
||||
message = await device_client.receive_message() # blocking call
|
||||
print("the data in the message received was ")
|
||||
print(message.data)
|
||||
print("custom properties are")
|
||||
print(message.custom_properties)
|
||||
print("content Type: {0}".format(message.content_type))
|
||||
print("")
|
||||
if message.custom_properties['method-name'] == 'pythonsample:fireworks':
|
||||
data_str = message.data.decode('utf-8')
|
||||
output_text = 'Fireworks! Fireworks!, Fireworks!'
|
||||
if data_str == '"RED"':
|
||||
fireworks("\033[91m {}\033[00m")
|
||||
elif data_str == '"GREEN"':
|
||||
fireworks("\033[92m {}\033[00m")
|
||||
elif data_str == '"BLUE"':
|
||||
fireworks("\033[34m {}\033[00m")
|
||||
elif data_str == '"YELLOW"':
|
||||
fireworks("\033[93m {}\033[00m")
|
||||
elif data_str == '"WHITE"':
|
||||
fireworks("\033[00m {}\033[00m")
|
||||
else:
|
||||
print("the data in the message received was ")
|
||||
print(message.data)
|
||||
print("custom properties are")
|
||||
print(message.custom_properties)
|
||||
print("content Type: {0}".format(message.content_type))
|
||||
print("")
|
||||
|
||||
|
||||
# main function: looks for cached DPS information in the file dpsCache.json and uses it to do a direct connection to the IoT hub.
|
||||
|
@ -230,7 +259,7 @@ async def main():
|
|||
websockets=use_websockets
|
||||
)
|
||||
|
||||
# connect
|
||||
# connect to IoT Hub
|
||||
try:
|
||||
await device_client.connect()
|
||||
connected = True
|
||||
|
|
Загрузка…
Ссылка в новой задаче