| | 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 | | /// Although relationships have a user-defined schema, these properties should exist on every instance. This is |
| | 11 | | /// useful to use as a base class to ensure your custom relationships have the necessary properties. |
| | 12 | | /// </summary> |
| | 13 | | /// <remarks> |
| | 14 | | /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azure.D |
| | 15 | | /// </remarks> |
| | 16 | | /// <example> |
| | 17 | | /// Here's an example of how to use the BasicRelationship helper class to serialize and create a relationship from a |
| | 18 | | /// |
| | 19 | | /// <code snippet="Snippet:DigitalTwinsSampleCreateBasicRelationship"> |
| | 20 | | /// var buildingFloorRelationshipPayload = new BasicRelationship |
| | 21 | | /// { |
| | 22 | | /// Id = "buildingFloorRelationshipId", |
| | 23 | | /// SourceId = "buildingTwinId", |
| | 24 | | /// TargetId = "floorTwinId", |
| | 25 | | /// Name = "contains", |
| | 26 | | /// CustomProperties = |
| | 27 | | /// { |
| | 28 | | /// { "Prop1", "Prop1 value" }, |
| | 29 | | /// { "Prop2", 6 } |
| | 30 | | /// } |
| | 31 | | /// }; |
| | 32 | | /// |
| | 33 | | /// string serializedRelationship = JsonSerializer.Serialize(buildingFloorRelationshipPayload); |
| | 34 | | /// await client.CreateRelationshipAsync("buildingTwinId", "buildingFloorRelationshipId", serial |
| | 35 | | /// Console.WriteLine($"Created a digital twin relationship 'buildingFloorRelationshipId' from twin & |
| | 36 | | /// </code> |
| | 37 | | /// |
| | 38 | | /// Here's an example of how to use the BasicRelationship helper class to get and deserialize a relationship. |
| | 39 | | /// |
| | 40 | | /// <code snippet="Snippet:DigitalTwinsSampleGetBasicRelationship"> |
| | 41 | | /// Response<string> getBasicRelationshipResponse = await client.GetRelationshipAsync("buildingTwinId&quo |
| | 42 | | /// if (getBasicRelationshipResponse.GetRawResponse().Status == (int)HttpStatusCode.OK) |
| | 43 | | /// { |
| | 44 | | /// BasicRelationship basicRelationship = JsonSerializer.Deserialize<BasicRelationship>(getBasicRelationsh |
| | 45 | | /// Console.WriteLine($"Retrieved relationship '{basicRelationship.Id}' from twin {basicRelations |
| | 46 | | /// $"Prop1: {basicRelationship.CustomProperties["Prop1"]}\n\t" + |
| | 47 | | /// $"Prop2: {basicRelationship.CustomProperties["Prop2"]}"); |
| | 48 | | /// } |
| | 49 | | /// </code> |
| | 50 | | /// </example> |
| | 51 | | public class BasicRelationship |
| | 52 | | { |
| | 53 | | /// <summary> |
| | 54 | | /// The unique Id of the relationship. This field is present on every relationship. |
| | 55 | | /// </summary> |
| | 56 | | [JsonPropertyName("$relationshipId")] |
| 4 | 57 | | public string Id { get; set; } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// The unique Id of the target digital twin. This field is present on every relationship. |
| | 61 | | /// </summary> |
| | 62 | | [JsonPropertyName("$targetId")] |
| 4 | 63 | | public string TargetId { get; set; } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// The unique Id of the source digital twin. This field is present on every relationship. |
| | 67 | | /// </summary> |
| | 68 | | [JsonPropertyName("$sourceId")] |
| 4 | 69 | | public string SourceId { get; set; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// The name of the relationship, which defines the type of link (e.g. Contains). This field is present on every |
| | 73 | | /// </summary> |
| | 74 | | [JsonPropertyName("$relationshipName")] |
| 4 | 75 | | public string Name { get; set; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Additional properties defined in the model. This field will contain any properties of the relationship that |
| | 79 | | /// </summary> |
| | 80 | | [JsonExtensionData] |
| 0 | 81 | | public IDictionary<string, object> CustomProperties { get; set; } = new Dictionary<string, object>(); |
| | 82 | | } |
| | 83 | | } |