| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Text.Json.Serialization; |
| | 6 | |
|
| | 7 | | namespace Azure.DigitalTwins.Core.Serialization |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// An optional, helper class for deserializing a digital twin. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azure.D |
| | 14 | | /// </remarks> |
| | 15 | | /// <example> |
| | 16 | | /// Here's an example of how to use the BasicDigitalTwin helper class to serialize and create a digital twin. |
| | 17 | | /// |
| | 18 | | /// <code snippet="Snippet:DigitalTwinsSampleCreateBasicTwin"> |
| | 19 | | /// // Create digital twin with component payload using the BasicDigitalTwin serialization helper |
| | 20 | | /// |
| | 21 | | /// var basicTwin = new BasicDigitalTwin |
| | 22 | | /// { |
| | 23 | | /// Id = basicDtId, |
| | 24 | | /// // model Id of digital twin |
| | 25 | | /// Metadata = { ModelId = modelId }, |
| | 26 | | /// CustomProperties = |
| | 27 | | /// { |
| | 28 | | /// // digital twin properties |
| | 29 | | /// { "Prop1", "Value1" }, |
| | 30 | | /// { "Prop2", 987 }, |
| | 31 | | /// // component |
| | 32 | | /// { |
| | 33 | | /// "Component1", |
| | 34 | | /// new ModelProperties |
| | 35 | | /// { |
| | 36 | | /// // component properties |
| | 37 | | /// CustomProperties = |
| | 38 | | /// { |
| | 39 | | /// { "ComponentProp1", "Component value 1" }, |
| | 40 | | /// { "ComponentProp2", 123 }, |
| | 41 | | /// }, |
| | 42 | | /// } |
| | 43 | | /// }, |
| | 44 | | /// }, |
| | 45 | | /// }; |
| | 46 | | /// |
| | 47 | | /// string basicDtPayload = JsonSerializer.Serialize(basicTwin); |
| | 48 | | /// |
| | 49 | | /// await client.CreateDigitalTwinAsync(basicDtId, basicDtPayload); |
| | 50 | | /// Console.WriteLine($"Created digital twin '{basicDtId}'."); |
| | 51 | | /// </code> |
| | 52 | | /// |
| | 53 | | /// Here's an example of how to use the BasicDigitalTwin helper class to get and deserialize a digital twin. |
| | 54 | | /// |
| | 55 | | /// <code snippet="Snippet:DigitalTwinsSampleGetBasicDigitalTwin"> |
| | 56 | | /// Response<string> getBasicDtResponse = await client.GetDigitalTwinAsync(basicDtId); |
| | 57 | | /// if (getBasicDtResponse.GetRawResponse().Status == (int)HttpStatusCode.OK) |
| | 58 | | /// { |
| | 59 | | /// BasicDigitalTwin basicDt = JsonSerializer.Deserialize<BasicDigitalTwin>(getBasicDtResponse.Value); |
| | 60 | | /// |
| | 61 | | /// // Must cast Component1 as a JsonElement and get its raw text in order to deserialize it as a dictionary |
| | 62 | | /// string component1RawText = ((JsonElement)basicDt.CustomProperties["Component1"]).GetRawText(); |
| | 63 | | /// IDictionary<string, object> component1 = JsonSerializer.Deserialize<IDictionary<string, object&g |
| | 64 | | /// |
| | 65 | | /// Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id}:\n\t" + |
| | 66 | | /// $"ETag: {basicDt.ETag}\n\t" + |
| | 67 | | /// $"Prop1: {basicDt.CustomProperties["Prop1"]}\n\t" + |
| | 68 | | /// $"Prop2: {basicDt.CustomProperties["Prop2"]}\n\t" + |
| | 69 | | /// $"ComponentProp1: {component1["ComponentProp1"]}\n\t" + |
| | 70 | | /// $"ComponentProp2: {component1["ComponentProp2"]}"); |
| | 71 | | /// } |
| | 72 | | /// </code> |
| | 73 | | /// </example> |
| | 74 | | public class BasicDigitalTwin |
| | 75 | | { |
| | 76 | | /// <summary> |
| | 77 | | /// The unique Id of the digital twin in a digital twins instance. This field is present on every digital twin. |
| | 78 | | /// </summary> |
| | 79 | | [JsonPropertyName("$dtId")] |
| 0 | 80 | | public string Id { get; set; } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// A string representing a weak ETag for the entity that this request performs an operation against, as per RFC |
| | 84 | | /// </summary> |
| | 85 | | [JsonPropertyName("$etag")] |
| 0 | 86 | | public string ETag { get; set; } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Information about the model a digital twin conforms to. This field is present on every digital twin. |
| | 90 | | /// </summary> |
| | 91 | | [JsonPropertyName("$metadata")] |
| 0 | 92 | | public DigitalTwinMetadata Metadata { get; set; } = new DigitalTwinMetadata(); |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Additional properties of the digital twin. This field will contain any properties of the digital twin that a |
| | 96 | | /// </summary> |
| | 97 | | [JsonExtensionData] |
| 0 | 98 | | public IDictionary<string, object> CustomProperties { get; set; } = new Dictionary<string, object>(); |
| | 99 | | } |
| | 100 | | } |