Skip to content

Commit

Permalink
samples(adt): more misc changes per Azure SDK review (#12532)
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson authored Jun 5, 2020
1 parent da79d63 commit 6a8d65f
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ public DigitalTwinMetadata() { }
[System.Text.Json.Serialization.JsonPropertyNameAttribute("$model")]
public string ModelId { get { throw null; } set { } }
[System.Text.Json.Serialization.JsonExtensionDataAttribute]
public System.Collections.Generic.IDictionary<string, object> WriteableProperties { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> WriteableProperties { get { throw null; } }
}
public partial class ModelProperties
{
public ModelProperties() { }
[System.Text.Json.Serialization.JsonExtensionDataAttribute]
public System.Collections.Generic.IDictionary<string, object> CustomProperties { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, object> CustomProperties { get { throw null; } }
[System.Text.Json.Serialization.JsonPropertyNameAttribute("$metadata")]
public Azure.DigitalTwins.Core.Serialization.DigitalTwinMetadata Metadata { get { throw null; } set { } }
public Azure.DigitalTwins.Core.Serialization.DigitalTwinMetadata Metadata { get { throw null; } }
}
public partial class UpdateOperationsUtility
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,47 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
.Replace(SamplesConstants.ComponentId, componentModelId);

// Then we create models
Response<IReadOnlyList<Models.ModelData>> createModelsResponse = await client
.CreateModelsAsync(new[] { newComponentModelPayload, newModelPayload });
Console.WriteLine($"Successfully created models Ids {componentModelId} and {modelId} with response {createModelsResponse.GetRawResponse().Status}.");
Response<IReadOnlyList<Models.ModelData>> createModelsResponse = await client.CreateModelsAsync(
new[]
{
newComponentModelPayload,
newModelPayload
});
Console.WriteLine($"Created models Ids {componentModelId} and {modelId} with response {createModelsResponse.GetRawResponse().Status}.");

#region Snippet:DigitalTwinsSampleCreateBasicTwin

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

var basicDigitalTwin = new BasicDigitalTwin
var basicTwin = new BasicDigitalTwin
{
Id = basicDtId
Id = basicDtId,
// model Id of digital twin
Metadata = { ModelId = modelId },
CustomProperties =
{
// digital twin properties
{ "Prop1", "Value1" },
{ "Prop2", 987 },
// component
{
"Component1",
new ModelProperties
{
// model Id of component
Metadata = { ModelId = componentModelId },
// component properties
CustomProperties =
{
{ "ComponentProp1", "Component value 1" },
{ "ComponentProp2", 123 },
},
}
},
},
};
basicDigitalTwin.Metadata.ModelId = modelId;
basicDigitalTwin.CustomProperties.Add("Prop1", "Value1");
basicDigitalTwin.CustomProperties.Add("Prop2", 987);

var componentMetadata = new ModelProperties();
componentMetadata.Metadata.ModelId = componentModelId;
componentMetadata.CustomProperties.Add("ComponentProp1", "ComponentValue1");
componentMetadata.CustomProperties.Add("ComponentProp2", 123);

basicDigitalTwin.CustomProperties.Add("Component1", componentMetadata);

string basicDtPayload = JsonSerializer.Serialize(basicDigitalTwin);
string basicDtPayload = JsonSerializer.Serialize(basicTwin);

Response<string> createBasicDtResponse = await client.CreateDigitalTwinAsync(basicDtId, basicDtPayload);
Console.WriteLine($"Created digital twin {basicDtId} with response {createBasicDtResponse.GetRawResponse().Status}.");
Expand All @@ -81,21 +98,25 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
string component1RawText = ((JsonElement)basicDt.CustomProperties["Component1"]).GetRawText();
var component1 = JsonSerializer.Deserialize<IDictionary<string, object>>(component1RawText);

Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id} with ETag {basicDt.ETag} " +
$"and Prop1: '{basicDt.CustomProperties["Prop1"]}', Prop2: {basicDt.CustomProperties["Prop2"]}," +
$"ComponentProp1: '{component1["ComponentProp1"]}', ComponentProp2: {component1["ComponentProp2"]}");
Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id}:\n\t" +
$"ETag: {basicDt.ETag}\n\t" +
$"Prop1: {basicDt.CustomProperties["Prop1"]}\n\t" +
$"Prop2: {basicDt.CustomProperties["Prop2"]}\n\t" +
$"ComponentProp1: {component1["ComponentProp1"]}\n\t" +
$"ComponentProp2: {component1["ComponentProp2"]}");
}

#endregion Snippet:DigitalTwinsSampleGetBasicDigitalTwin

string customDtId = await GetUniqueTwinIdAsync(SamplesConstants.TemporaryTwinPrefix, client);

// Alternatively, you can create your own custom data types to serialize and deserialize your digital twins.
// By specifying your properties and types directly, it requires less code or knowledge of the type for
// interaction.

#region Snippet:DigitalTwinsSampleCreateCustomTwin

string customDtId = await GetUniqueTwinIdAsync(SamplesConstants.TemporaryTwinPrefix, client);
var customDigitalTwin = new CustomDigitalTwin
var customTwin = new CustomDigitalTwin
{
Id = customDtId,
Metadata = new CustomDigitalTwinMetadata { ModelId = modelId },
Expand All @@ -108,7 +129,7 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
ComponentProp2 = 123,
}
};
string dt2Payload = JsonSerializer.Serialize(customDigitalTwin);
string dt2Payload = JsonSerializer.Serialize(customTwin);

Response<string> createCustomDtResponse = await client.CreateDigitalTwinAsync(customDtId, dt2Payload);
Console.WriteLine($"Created digital twin {customDtId} with response {createCustomDtResponse.GetRawResponse().Status}.");
Expand All @@ -124,9 +145,12 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
if (getCustomDtResponse.GetRawResponse().Status == (int)HttpStatusCode.OK)
{
CustomDigitalTwin customDt = JsonSerializer.Deserialize<CustomDigitalTwin>(getCustomDtResponse.Value);
Console.WriteLine($"Retrieved and deserialized digital twin {customDt.Id} with ETag {customDt.ETag} " +
$"and Prop1: '{customDt.Prop1}', Prop2: {customDt.Prop2}," +
$"ComponentProp1: '{customDt.Component1.ComponentProp1}', ComponentProp2: {customDt.Component1.ComponentProp2}");
Console.WriteLine($"Retrieved and deserialized digital twin {customDt.Id}:\n\t" +
$"ETag: {customDt.ETag}\n\t" +
$"Prop1: {customDt.Prop1}\n\t" +
$"Prop2: {customDt.Prop2}\n\t" +
$"ComponentProp1: {customDt.Component1.ComponentProp1}\n\t" +
$"ComponentProp2: {customDt.Component1.ComponentProp2}");
}

#endregion Snippet:DigitalTwinsSampleGetCustomDigitalTwin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,28 +354,30 @@ public async Task ConnectTwinsTogetherAsync()
// We deserialize as BasicRelationship to get the entire custom relationship (custom relationship properties).
IEnumerable<BasicRelationship> relationships = JsonSerializer.Deserialize<IEnumerable<BasicRelationship>>(relationshipSet.Value);

#region Snippet:DigitalTwinsSampleCreateRelationship

// From loaded relationships, get the source Id and Id from each one,
// and create it with full relationship payload
foreach (BasicRelationship relationship in relationships)
{
try
{
#region Snippet:DigitalTwinsSampleCreateRelationship

string serializedRelationship = JsonSerializer.Serialize(relationship);

await client.CreateRelationshipAsync(
relationship.SourceId,
relationship.Id,
serializedRelationship);

#endregion Snippet:DigitalTwinsSampleCreateRelationship

Console.WriteLine($"Linked {serializedRelationship}");
Console.WriteLine($"Linked twin {relationship.SourceId} to twin {relationship.TargetId} as '{relationship.Name}'");
}
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.Conflict)
{
Console.WriteLine($"Relationship {relationship.Id} already exists: {ex.Message}");
}
}

#endregion Snippet:DigitalTwinsSampleCreateRelationship
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
try
{
await client.DecommissionModelAsync(sampleModelId);

Console.WriteLine($"Successfully decommissioned model {sampleModelId}");
}
catch (Exception ex)
catch (RequestFailedException ex)
{
FatalError($"Failed to decommision model {sampleModelId} due to:\n{ex}");
}
Expand Down
Loading

0 comments on commit 6a8d65f

Please sign in to comment.