Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix digital twins samples #16118

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$metadata": {
"$model": "dtmi:samples:Building;1"
"$model": "dtmi:com:samples:Building;1"
},
"AverageTemperature": 68,
"TemperatureUnit": "Celsius"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$metadata": {
"$model": "dtmi:samples:Floor;1"
"$model": "dtmi:com:samples:Floor;1"
},
"AverageTemperature": 75
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$metadata": {
"$model": "dtmi:samples:HVAC;1"
"$model": "dtmi:com:samples:HVAC;1"
},
"Efficiency": 94,
"TargetTemperature": 72,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$metadata": {
"$model": "dtmi:samples:Room;1"
"$model": "dtmi:com:samples:Room;1"
},
"Temperature": 80,
"Humidity": 25,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"@id": "dtmi:samples:Building;1",
"@id": "dtmi:com:samples:Building;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "Building",
"contents": [
{
"@type": "Relationship",
"name": "has",
"target": "dtmi:samples:Floor;1",
"target": "dtmi:com:samples:Floor;1",
"properties": [
{
"@type": "Property",
Expand All @@ -19,7 +19,7 @@
{
"@type": "Relationship",
"name": "isEquippedWith",
"target": "dtmi:samples:HVAC;1"
"target": "dtmi:com:samples:HVAC;1"
},
{
"@type": "Property",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"@id": "dtmi:samples:Floor;1",
"@id": "dtmi:com:samples:Floor;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "Floor",
"contents": [
{
"@type": "Relationship",
"name": "contains",
"target": "dtmi:samples:Room;1"
"target": "dtmi:com:samples:Room;1"
},
{
"@type": "Property",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@id": "dtmi:samples:HVAC;1",
"@id": "dtmi:com:samples:HVAC;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "HVAC",
Expand All @@ -22,7 +22,7 @@
{
"@type": "Relationship",
"name": "controlsTemperature",
"target": "dtmi:samples:Floor;1"
"target": "dtmi:com:samples:Floor;1"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@id": "dtmi:samples:Room;1",
"@id": "dtmi:com:samples:Room;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "Room",
Expand Down Expand Up @@ -27,7 +27,7 @@
{
"@type": "Component",
"name": "wifiAccessPoint",
"schema": "dtmi:samples:Wifi;1"
"schema": "dtmi:com:samples:Wifi;1"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@id": "dtmi:samples:Wifi;1",
"@id": "dtmi:com:samples:Wifi;1",
"@type": "Interface",
"@context": "dtmi:dtdl:context;2",
"displayName": "Wifi",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,25 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)

#endregion Snippet:DigitalTwinsSampleDeleteModel
}

/// <summary>
/// Try to delete a model, but don't fail if the model does not exist. Useful in setting up or tearing down after running a sample if the
/// sample re-uses the same model Id each time
/// </summary>
/// <param name="client">The client to use when deleting the model</param>
/// <param name="modelId">The id of the model to delete</param>
/// <returns>An empty task once the model has been deleted.</returns>
public static async Task TryDeleteModelAsync(DigitalTwinsClient client, string modelId)
{
try
{
Console.WriteLine($"Deleting model Id '{modelId}' if it exists.");
await client.DeleteModelAsync(modelId);
}
catch (RequestFailedException ex) when (ex.Status == 404)
{
// Model did not exist yet, and that's fine
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using Azure.DigitalTwins.Core.Serialization;
using Azure.DigitalTwins.Samples;
using static Azure.DigitalTwins.Core.Samples.SampleLogger;

namespace Azure.DigitalTwins.Core.Samples
Expand All @@ -21,36 +22,40 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)
// Despite not being a good code practice, this prevents code snippets from being out of context for the user when making API calls that accept Ids as parameters.

PrintHeader("RELATIONSHIP SAMPLE");
string sampleBuildingModelId = "dtmi:com:samples:SampleBuilding;1";
string sampleFloorModelId = "dtmi:com:samples:SampleFloor;1";
await ModelLifecycleSamples.TryDeleteModelAsync(client, sampleBuildingModelId);
await ModelLifecycleSamples.TryDeleteModelAsync(client, sampleFloorModelId);

// Create a building digital twin model.
string buildingModelPayload = SamplesConstants.TemporaryModelWithRelationshipPayload
.Replace(SamplesConstants.ModelId, "dtmi:samples:SampleBuilding;1")
.Replace(SamplesConstants.RelationshipTargetModelId, sampleFloorModelId)
.Replace(SamplesConstants.ModelId, sampleBuildingModelId)
.Replace(SamplesConstants.ModelDisplayName, "Building")
.Replace(SamplesConstants.RelationshipName, "contains")
.Replace(SamplesConstants.RelationshipTargetModelId, "dtmi:samples:SampleFloor;1");
.Replace(SamplesConstants.RelationshipName, "contains");

await client.CreateModelsAsync(
new[]
{
buildingModelPayload
});
Console.WriteLine($"Created model 'dtmi:samples:SampleBuilding;1'.");
Console.WriteLine($"Created model '{sampleBuildingModelId}'.");

// Create a floor digital twin model.
string floorModelPayload = SamplesConstants.TemporaryModelWithRelationshipPayload
.Replace(SamplesConstants.ModelId, "dtmi:samples:SampleFloor;1")
.Replace(SamplesConstants.RelationshipTargetModelId, sampleBuildingModelId)
.Replace(SamplesConstants.ModelId, sampleFloorModelId)
.Replace(SamplesConstants.ModelDisplayName, "Floor")
.Replace(SamplesConstants.RelationshipName, "containedIn")
.Replace(SamplesConstants.RelationshipTargetModelId, "dtmi:samples:SampleBuilding;1");
.Replace(SamplesConstants.RelationshipName, "containedIn");

await client.CreateModelsAsync(new[] { floorModelPayload });
Console.WriteLine($"Created model 'dtmi:samples:SampleFloor;1.'");
Console.WriteLine($"Created model '{sampleFloorModelId}'");

// Create a building digital twin.
var buildingDigitalTwin = new BasicDigitalTwin
{
Id = "buildingTwinId",
Metadata = { ModelId = "dtmi:samples:SampleBuilding;1" }
Metadata = { ModelId = sampleBuildingModelId }
};

string buildingDigitalTwinPayload = JsonSerializer.Serialize(buildingDigitalTwin);
Expand All @@ -61,7 +66,7 @@ await client.CreateModelsAsync(
var floorDigitalTwin = new BasicDigitalTwin
{
Id = "floorTwinId",
Metadata = { ModelId = "dtmi:samples:SampleFloor;1" }
Metadata = { ModelId = sampleFloorModelId }
};

string floorDigitalTwinPayload = JsonSerializer.Serialize(floorDigitalTwin);
Expand Down Expand Up @@ -197,8 +202,8 @@ await client.CreateModelsAsync(

try
{
await client.DeleteModelAsync("dtmi:samples:SampleBuilding;1");
await client.DeleteModelAsync("dtmi:samples:SampleFloor;1");
await client.DeleteModelAsync(sampleBuildingModelId);
await client.DeleteModelAsync(sampleFloorModelId);
}
catch (RequestFailedException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ public static class SamplesConstants
/// <summary>
/// Room model Id
/// </summary>
public const string RoomModelId = "dtmi:samples:Room;1";
public const string RoomModelId = "dtmi:com:samples:Room;1";

/// <summary>
/// WiFi model Id
/// </summary>
public const string WifiModelId = "dtmi:samples:Wifi;1";
public const string WifiModelId = "dtmi:com:samples:Wifi;1";

/// <summary>
/// Building model Id
/// </summary>
public const string BuildingModelId = "dtmi:samples:Building;1";
public const string BuildingModelId = "dtmi:com:samples:Building;1";

/// <summary>
/// Floor model Id
/// </summary>
public const string FloorModelId = "dtmi:samples:Floor;1";
public const string FloorModelId = "dtmi:com:samples:Floor;1";

/// <summary>
/// HVAC model Id
/// </summary>
public const string HvacModelId = "dtmi:samples:HVAC;1";
public const string HvacModelId = "dtmi:com:samples:HVAC;1";

/// <summary>
/// Placeholder for model Id in the temporary payload.
Expand All @@ -43,12 +43,12 @@ public static class SamplesConstants
/// <summary>
/// Temporary model Id prefix
/// </summary>
public const string TemporaryModelPrefix = "dtmi:samples:TempModel;";
public const string TemporaryModelPrefix = "dtmi:com:samples:TempModel;";

/// <summary>
/// Temporary component model Id prefix
/// </summary>
public const string TemporaryComponentModelPrefix = "dtmi:samples:ComponentModel;";
public const string TemporaryComponentModelPrefix = "dtmi:com:samples:ComponentModel;";

/// <summary>
/// Placeholder for model display name in the temporary payload.
Expand Down