次の方法で共有


BasicDigitalTwin クラス

定義

デジタル ツインを逆シリアル化するための省略可能なヘルパー クラス。

[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
継承
BasicDigitalTwin
属性

BasicDigitalTwin ヘルパー クラスを使用してデジタル ツインをシリアル化して作成する方法の例を次に示します。

// 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}'.");

BasicDigitalTwin ヘルパー クラスを使用してデジタル ツインを取得および逆シリアル化する方法の例を次に示します。

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}");

注釈

このヘルパー クラスは、 でのみ機能 System.Text.Jsonします。 を と共に使用すると、 ObjectSerializerパラメーター DigitalTwinsClientOptions は既定の (JsonObjectSerializer) でのみ機能します。

その他のサンプルについては、 リポジトリのサンプルを参照してください。

コンストラクター

BasicDigitalTwin()

デジタル ツインを逆シリアル化するための省略可能なヘルパー クラス。

プロパティ

Contents

このフィールドには、ツインの DTDL 定義の contents セクションで定義されているプロパティとコンポーネントが含まれます。

ETag

RFC7232 に従って、この要求が操作を実行するエンティティの弱い ETag を表す文字列。

Id

デジタル ツイン インスタンス内のデジタル ツインの一意の ID。 このフィールドは、すべてのデジタル ツインに存在します。

LastUpdatedOn

ツインが最後に更新された日時。

Metadata

デジタル ツインが準拠しているモデルに関する情報。 このフィールドは、すべてのデジタル ツインに存在します。

適用対象