From 3ba75f501e1fc683dee50e371292af89934bc569 Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Wed, 17 Nov 2021 18:28:18 +1000 Subject: [PATCH 1/2] Basic example to serialize model & twin --- DigitalTwins-CodeFirst-dotnet.sln | 17 +++++++++ Examples/SimpleExample/Models/Factory.cs | 20 ++++++++++ Examples/SimpleExample/Models/FactoryFloor.cs | 12 ++++++ Examples/SimpleExample/Program.cs | 37 +++++++++++++++++++ Examples/SimpleExample/SimpleExample.csproj | 14 +++++++ 5 files changed, 100 insertions(+) create mode 100644 Examples/SimpleExample/Models/Factory.cs create mode 100644 Examples/SimpleExample/Models/FactoryFloor.cs create mode 100644 Examples/SimpleExample/Program.cs create mode 100644 Examples/SimpleExample/SimpleExample.csproj diff --git a/DigitalTwins-CodeFirst-dotnet.sln b/DigitalTwins-CodeFirst-dotnet.sln index 6f4f13c..25759f4 100644 --- a/DigitalTwins-CodeFirst-dotnet.sln +++ b/DigitalTwins-CodeFirst-dotnet.sln @@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{D8068994 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telstra.Twins.Test", "Tests\Telstra.Twins.Test\Telstra.Twins.Test.csproj", "{4E5BF74D-C0EF-4D13-872C-F40FC347AD67}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{5BA14B5E-B883-4B92-802D-0BA49C93842B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleExample", "Examples\SimpleExample\SimpleExample.csproj", "{BAB10325-0CE2-4528-A2FE-C65F74982D28}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -60,8 +64,21 @@ Global {4E5BF74D-C0EF-4D13-872C-F40FC347AD67}.Release|x64.Build.0 = Release|Any CPU {4E5BF74D-C0EF-4D13-872C-F40FC347AD67}.Release|x86.ActiveCfg = Release|Any CPU {4E5BF74D-C0EF-4D13-872C-F40FC347AD67}.Release|x86.Build.0 = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|x64.ActiveCfg = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|x64.Build.0 = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|x86.ActiveCfg = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Debug|x86.Build.0 = Debug|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|Any CPU.Build.0 = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|x64.ActiveCfg = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|x64.Build.0 = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|x86.ActiveCfg = Release|Any CPU + {BAB10325-0CE2-4528-A2FE-C65F74982D28}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {4E5BF74D-C0EF-4D13-872C-F40FC347AD67} = {D8068994-FDA6-41F1-9196-59AADA892F11} + {BAB10325-0CE2-4528-A2FE-C65F74982D28} = {5BA14B5E-B883-4B92-802D-0BA49C93842B} EndGlobalSection EndGlobal diff --git a/Examples/SimpleExample/Models/Factory.cs b/Examples/SimpleExample/Models/Factory.cs new file mode 100644 index 0000000..c872c2c --- /dev/null +++ b/Examples/SimpleExample/Models/Factory.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using Telstra.Twins.Attributes; + +namespace SimpleExample.Models +{ + [DigitalTwin(Version = 1, DisplayName = "Digital Factory - Interface Model")] + public class Factory + { + [TwinProperty] public string? Country { get; set; } + + [TwinProperty] public string? FactoryId { get; set; } + + [TwinProperty] public string? FactoryName { get; set; } + + [TwinRelationship(DisplayName = "Has Floors")] + public List Floors { get; set; } = new(); + + [TwinProperty] public string? ZipCode { get; set; } + } +} diff --git a/Examples/SimpleExample/Models/FactoryFloor.cs b/Examples/SimpleExample/Models/FactoryFloor.cs new file mode 100644 index 0000000..956dcc6 --- /dev/null +++ b/Examples/SimpleExample/Models/FactoryFloor.cs @@ -0,0 +1,12 @@ +using Telstra.Twins.Attributes; + +namespace SimpleExample.Models +{ + [DigitalTwin(Version = 1, DisplayName = "Digital Factory - Interface Model")] + public class FactoryFloor + { + [TwinProperty] public string? FloorId { get; set; } + + [TwinProperty] public string? FloorName { get; set; } + } +} diff --git a/Examples/SimpleExample/Program.cs b/Examples/SimpleExample/Program.cs new file mode 100644 index 0000000..9af5bc0 --- /dev/null +++ b/Examples/SimpleExample/Program.cs @@ -0,0 +1,37 @@ +using System; +using SimpleExample.Models; +using Telstra.Twins.Core; +using Telstra.Twins.Services; + +Console.WriteLine("Simple Digital Twin Serializer Example"); + +var modelLibrary = new ModelLibrary(); +var serializer = new DigitalTwinSerializer(modelLibrary); + +Console.WriteLine("Models:"); +Console.WriteLine(); + +var model1Dtdl = serializer.SerializeModel(); +Console.WriteLine(model1Dtdl); +Console.WriteLine(); + +var model2Dtdl = serializer.SerializeModel(); +Console.WriteLine(model2Dtdl); +Console.WriteLine(); + +Console.WriteLine("Twins:"); +Console.WriteLine(); + +var factory = new Factory +{ + FactoryId = "factory1", Country = "AU", ZipCode = "4000", FactoryName = "Chocolate Factory" +}; +factory.Floors.Add(new FactoryFloor { FloorId = "floor1", FloorName = "Floor 1" }); + +var twin1Dtdl = serializer.SerializeTwin(factory); +Console.WriteLine(twin1Dtdl); +Console.WriteLine(); + +var twin2Dtdl = serializer.SerializeTwin(factory.Floors[0]); +Console.WriteLine(twin2Dtdl); +Console.WriteLine(); diff --git a/Examples/SimpleExample/SimpleExample.csproj b/Examples/SimpleExample/SimpleExample.csproj new file mode 100644 index 0000000..80d533b --- /dev/null +++ b/Examples/SimpleExample/SimpleExample.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + From 700fc34da21194040902895423de7012f22a744f Mon Sep 17 00:00:00 2001 From: Sly Gryphon Date: Wed, 24 Nov 2021 18:28:49 +1000 Subject: [PATCH 2/2] Convert example back to dotnet 3.1 LTS --- Examples/SimpleExample/Models/Factory.cs | 2 +- Examples/SimpleExample/Program.cs | 74 ++++++++++++--------- Examples/SimpleExample/SimpleExample.csproj | 3 +- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/Examples/SimpleExample/Models/Factory.cs b/Examples/SimpleExample/Models/Factory.cs index c872c2c..e0a6d04 100644 --- a/Examples/SimpleExample/Models/Factory.cs +++ b/Examples/SimpleExample/Models/Factory.cs @@ -13,7 +13,7 @@ public class Factory [TwinProperty] public string? FactoryName { get; set; } [TwinRelationship(DisplayName = "Has Floors")] - public List Floors { get; set; } = new(); + public List Floors { get; set; } = new List(); [TwinProperty] public string? ZipCode { get; set; } } diff --git a/Examples/SimpleExample/Program.cs b/Examples/SimpleExample/Program.cs index 9af5bc0..a2c793a 100644 --- a/Examples/SimpleExample/Program.cs +++ b/Examples/SimpleExample/Program.cs @@ -3,35 +3,47 @@ using Telstra.Twins.Core; using Telstra.Twins.Services; -Console.WriteLine("Simple Digital Twin Serializer Example"); - -var modelLibrary = new ModelLibrary(); -var serializer = new DigitalTwinSerializer(modelLibrary); - -Console.WriteLine("Models:"); -Console.WriteLine(); - -var model1Dtdl = serializer.SerializeModel(); -Console.WriteLine(model1Dtdl); -Console.WriteLine(); - -var model2Dtdl = serializer.SerializeModel(); -Console.WriteLine(model2Dtdl); -Console.WriteLine(); - -Console.WriteLine("Twins:"); -Console.WriteLine(); - -var factory = new Factory +namespace SimpleExample { - FactoryId = "factory1", Country = "AU", ZipCode = "4000", FactoryName = "Chocolate Factory" -}; -factory.Floors.Add(new FactoryFloor { FloorId = "floor1", FloorName = "Floor 1" }); - -var twin1Dtdl = serializer.SerializeTwin(factory); -Console.WriteLine(twin1Dtdl); -Console.WriteLine(); - -var twin2Dtdl = serializer.SerializeTwin(factory.Floors[0]); -Console.WriteLine(twin2Dtdl); -Console.WriteLine(); + internal static class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Simple Digital Twin Serializer Example"); + + var modelLibrary = new ModelLibrary(); + var serializer = new DigitalTwinSerializer(modelLibrary); + + Console.WriteLine("Models:"); + Console.WriteLine(); + + var model1Dtdl = serializer.SerializeModel(); + Console.WriteLine(model1Dtdl); + Console.WriteLine(); + + var model2Dtdl = serializer.SerializeModel(); + Console.WriteLine(model2Dtdl); + Console.WriteLine(); + + Console.WriteLine("Twins:"); + Console.WriteLine(); + + var factory = new Factory + { + FactoryId = "factory1", + Country = "AU", + ZipCode = "4000", + FactoryName = "Chocolate Factory" + }; + factory.Floors.Add(new FactoryFloor { FloorId = "floor1", FloorName = "Floor 1" }); + + var twin1Dtdl = serializer.SerializeTwin(factory); + Console.WriteLine(twin1Dtdl); + Console.WriteLine(); + + var twin2Dtdl = serializer.SerializeTwin(factory.Floors[0]); + Console.WriteLine(twin2Dtdl); + Console.WriteLine(); + } + } +} diff --git a/Examples/SimpleExample/SimpleExample.csproj b/Examples/SimpleExample/SimpleExample.csproj index 80d533b..fd895e1 100644 --- a/Examples/SimpleExample/SimpleExample.csproj +++ b/Examples/SimpleExample/SimpleExample.csproj @@ -2,8 +2,7 @@ Exe - net6.0 - enable + netcoreapp3.1 enable