Condividi tramite


BasicDigitalTwin Class

Definition

An optional, helper class for deserializing a digital twin.

[System.Text.Json.Serialization.JsonConverter(typeof(Azure.DigitalTwins.Core.BasicDigitalTwinJsonConverter))]
public class BasicDigitalTwin
[<System.Text.Json.Serialization.JsonConverter(typeof(Azure.DigitalTwins.Core.BasicDigitalTwinJsonConverter))>]
type BasicDigitalTwin = class
Public Class BasicDigitalTwin
Inheritance
BasicDigitalTwin
Attributes

Examples

Here's an example of how to use the BasicDigitalTwin helper class to serialize and create a digital twin.

// Create digital twin with component payload using the BasicDigitalTwin serialization helper

var basicTwin = new BasicDigitalTwin
{
    Id = basicDtId,
    // model Id of digital twin
    Metadata =
    {
        ModelId = modelId,
        PropertyMetadata = new Dictionary<string, DigitalTwinPropertyMetadata>
        {
            {
                "Prop2",
                new DigitalTwinPropertyMetadata
                {
                    // must always be serialized as ISO 8601
                    SourceTime = DateTimeOffset.UtcNow,
                }
            }
        },
    },
    Contents =
    {
        // digital twin properties
        { "Prop1", "Value1" },
        { "Prop2", 987 },
        // component
        {
            "Component1",
            new BasicDigitalTwinComponent
            {
                // writeable component metadata
                Metadata =  new Dictionary<string, DigitalTwinPropertyMetadata>
                {
                    {
                        "ComponentProp2",
                        new DigitalTwinPropertyMetadata
                        {
                            // must always be serialized as ISO 8601
                            SourceTime = DateTimeOffset.UtcNow,
                        }
                    }
                },
                // component properties
                Contents =
                {
                    { "ComponentProp1", "Component value 1" },
                    { "ComponentProp2", 123 },
                },
            }
        },
    },
};

Response<BasicDigitalTwin> createDigitalTwinResponse = await client.CreateOrReplaceDigitalTwinAsync(basicDtId, basicTwin);
Console.WriteLine($"Created digital twin '{createDigitalTwinResponse.Value.Id}'.");

Here's an example of how to use the BasicDigitalTwin helper class to get and deserialize a digital twin.

Response<BasicDigitalTwin> getBasicDtResponse = await client.GetDigitalTwinAsync<BasicDigitalTwin>(basicDtId);
BasicDigitalTwin basicDt = getBasicDtResponse.Value;

// Must cast Component1 as a JsonElement and get its raw text in order to deserialize it as a dictionary
string component1RawText = ((JsonElement)basicDt.Contents["Component1"]).GetRawText();
var component1 = JsonSerializer.Deserialize<BasicDigitalTwinComponent>(component1RawText);

Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id}:\n\t" +
    $"ETag: {basicDt.ETag}\n\t" +
    $"ModelId: {basicDt.Metadata.ModelId}\n\t" +
    $"LastUpdatedOn: {basicDt.LastUpdatedOn}\n\t" +
    $"Prop1: {basicDt.Contents["Prop1"]}, last updated on {basicDt.Metadata.PropertyMetadata["Prop1"].LastUpdatedOn}\n\t" +
    $"Prop2: {basicDt.Contents["Prop2"]}, last updated on {basicDt.Metadata.PropertyMetadata["Prop2"].LastUpdatedOn} and sourced at {basicDt.Metadata.PropertyMetadata["Prop2"].SourceTime}\n\t" +
    $"Component1.LastUpdatedOn: {component1.LastUpdatedOn}\n\t" +
    $"Component1.Prop1: {component1.Contents["ComponentProp1"]}, last updated on: {component1.Metadata["ComponentProp1"].LastUpdatedOn}\n\t" +
    $"Component1.Prop2: {component1.Contents["ComponentProp2"]}, last updated on: {component1.Metadata["ComponentProp2"].LastUpdatedOn} and sourced at: {component1.Metadata["ComponentProp2"].SourceTime}");

Remarks

This helper class will only work with System.Text.Json. When used with the ObjectSerializer, parameter to DigitalTwinsClientOptions it will only work with the default (JsonObjectSerializer).

For more samples, see our repo samples.

Constructors

BasicDigitalTwin()

Properties

Contents

This field will contain properties and components as defined in the contents section of the DTDL definition of the twin.

ETag

A string representing a weak ETag for the entity that this request performs an operation against, as per RFC7232.

Id

The unique Id of the digital twin in a digital twins instance. This field is present on every digital twin.

LastUpdatedOn

The date and time the twin was last updated.

Metadata

Information about the model a digital twin conforms to. This field is present on every digital twin.

Applies to