File: dt_component_lifecycle.py

package info (click to toggle)
python-azure 20251104%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 770,224 kB
  • sloc: python: 6,357,217; ansic: 804; javascript: 287; makefile: 198; sh: 193; xml: 109
file content (131 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (2)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
from typing import Any, List, MutableMapping, cast
import uuid
from azure.identity import DefaultAzureCredential
from azure.core.exceptions import HttpResponseError
from azure.digitaltwins.core import DigitalTwinsClient

# Scenario example of how to:
# - create a DigitalTwins Service Client using the DigitalTwinsClient constructor
# - create model, component and twin
# - create digital twin based on the model
# - update component
# - get component
# - delete twin
# - decomission and delete model, component
#
# Preconditions:
# - Environment variables have to be set
# - DigitalTwins enabled device must exist on the ADT hub
#
# For the purpose of this example we will create temporary digital twin using random Ids.
# We have to make sure these model Ids are unique within the DT instance so we use generated UUIDs.
try:
    model_id = 'dtmi:samples:componentlifecyclemodel;1'
    component_id = 'dtmi:samples:componentlifecycle;1'
    digital_twin_id = 'digitalTwin-' + str(uuid.uuid4())

    temporary_component = {
        "@id": component_id,
        "@type": "Interface",
        "@context": "dtmi:dtdl:context;2",
        "displayName": "Component1",
        "contents": [
        {
            "@type": "Property",
            "name": "ComponentProp1",
            "schema": "string"
        }
        ]
    }

    temporary_model = {
        "@id": model_id,
        "@type": "Interface",
        "@context": "dtmi:dtdl:context;2",
        "displayName": "TempModel",
        "contents": [
        {
            "@type": "Property",
            "name": "Prop1",
            "schema": "double"
        },
        {
            "@type": "Component",
            "name": "Component1",
            "schema": component_id
        }
        ]
    }

    temporary_twin = {
        "$metadata": {
            "$model": model_id
        },
        "$dtId": digital_twin_id,
        "Prop1": 42,
        "Component1": {
            "$metadata": {},
        "ComponentProp1": "value1"
        }
    }

    # DefaultAzureCredential supports different authentication mechanisms and determines
    # the appropriate credential type based of the environment it is executing in.
    # It attempts to use multiple credential types in an order until it finds a working credential.

    # - AZURE_URL: The tenant ID in Azure Active Directory
    url = os.getenv("AZURE_URL")
    if url is None:
        raise ValueError("AZURE_URL environment variable is not set")

    # DefaultAzureCredential expects the following three environment variables:
    # - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
    # - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
    # - AZURE_CLIENT_SECRET: The client secret for the registered application
    credential = DefaultAzureCredential()
    service_client = DigitalTwinsClient(url, credential) # type: ignore

    # Create models
    models = service_client.create_models(cast(List[MutableMapping[str, Any]], [temporary_component, temporary_model]))
    print('Created Models:')
    print(models)

    # Create digital twin
    created_twin = service_client.upsert_digital_twin(digital_twin_id, temporary_twin)
    print('Created Digital Twin:')
    print(created_twin)

    # Update component
    component_name = "Component1"
    patch = [
        {
            "op": "replace",
            "path": "/ComponentProp1",
            "value": "value2"
        }
    ]
    service_client.update_component(digital_twin_id, component_name, patch)

    # Get component
    get_component = service_client.get_component(digital_twin_id, component_name)
    print('Get Component:')
    print(get_component)

    # Delete digital twin
    service_client.delete_digital_twin(digital_twin_id)

    # Decomission models
    service_client.decommission_model(model_id)
    service_client.decommission_model(component_id)

    # Delete models
    service_client.delete_model(model_id)
    service_client.delete_model(component_id)

except HttpResponseError as e:
    print("\nThis sample has caught an error. {0}".format(e.message))