question

matthofmarch avatar image
1 Vote"
matthofmarch asked SatishBoddu-MSFT commented

Create DigitalTwin containing Component

I am currently writing my final thesis for a technical high school and chose to use Azure Digital Twins as a way to represent machines (with nested sensors). I want to use components inside my machines for these sensors. Now my problem is, that I can not figure out how to create a machine (digital-twin) with a nested sensor(digital-twin as a component). It would be of great help if you could provide a .net core code sample or a command for the "digital-twins-samples" client that shows a way to create this nesting.

 //Machine
 {
     "@id": "dtmi:mysample:DigitalTwins:MoldMachine;1",
     "@type": "Interface",
     "displayName": "MoldMachine",
     "@context": "dtmi:dtdl:context;2",
     "extends": "dtmi:mysample:DigitalTwins:Machine;1",
     "contents": [
         {
             "@type": "Component",
             "name": "TemperatureSensor",
             "schema": "dtmi:virtualfactory:DigitalTwins:TemperatureSensor;1"
         }
     ]
 }
 //Sensor
 {
     "@context": "dtmi:dtdl:context;2",
     "@type": "Interface",
     "@id": "dtmi:mysample:DigitalTwins:TemperatureSensor;1",
     "contents": [
         {
             "@type": ["Telemetry", "Temperature"],
             "schema": "double",
             "unit": "degreeCelsius",
             "name":"TemperatureTelemetry"
         },
         {
             "@type": "Property",
             "schema": "double",
             "name":"TemperatureProperty"
         }
     ]
 }
azure-digital-twins
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @MatthiasHofmarcher-3488 , Thanks for reaching out to us, we are looking into this.

0 Votes 0 ·

1 Answer

matthofmarch avatar image
1 Vote"
matthofmarch answered SatishBoddu-MSFT commented

This worked for me:

 var sensor = new Dictionary<string, object>{
      {"$metadata", new Dictionary<string,object>{}},
      {"TemperatureProperty", 300}
 };
    
 BasicDigitalTwin machine = new BasicDigitalTwin();
 machine.Id = "Machine_-1";
 machine.Metadata.ModelId = "dtmi:mysample:DigitalTwins:MoldMachine;2";
 machine.CustomProperties.Add("TemperatureSensor", sensor);
    
 await dtClient.CreateDigitalTwinAsync(machine.Id, JsonSerializer.Serialize(machine));
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @MatthiasHofmarcher-3488 , Glad to hear that the issue is unblocked, thanks for your contribution !

0 Votes 0 ·