Create DigitalTwin containing Component

matthofmarch 111 Reputation points
2020-07-25T11:31:52.38+00:00

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
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
219 questions
{count} vote

Accepted answer
  1. matthofmarch 111 Reputation points
    2020-07-25T22:06:43.57+00:00

    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));
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful