-
Notifications
You must be signed in to change notification settings - Fork 13
/
get_digital_twin_sample.py
35 lines (28 loc) · 1.18 KB
/
get_digital_twin_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import os
import msrest
from azure.iot.hub import DigitalTwinClient
iothub_connection_str = os.getenv("IOTHUB_CONNECTION_STRING")
device_id = os.getenv("IOTHUB_DEVICE_ID")
try:
# Create DigitalTwinClient
digital_twin_client = DigitalTwinClient.from_connection_string(iothub_connection_str)
# Get digital twin and retrieve the modelId from it
digital_twin = digital_twin_client.get_digital_twin(device_id)
if digital_twin:
print(digital_twin)
print("Model Id: " + digital_twin["$metadata"]["$model"])
else:
print("No digital_twin found")
except msrest.exceptions.HttpOperationError as ex:
print("HttpOperationError error {0}".format(ex.response.text))
except Exception as exc:
print("Unexpected error {0}".format(exc))
except KeyboardInterrupt:
print("{} stopped".format(__file__))
finally:
print("{} finished".format(__file__))