0

We have a case where we'd like to provide the user with a UI where they can create an Azure Digital Twin from a list of acceptable Models. The user should get presented a UI similar to the Twin Properties presented in the Azure Digital Twin Explorer.

But to do this, I need to dynamicly generate an "empty" twin from a model. Is there a helper function or similar to generate a Twin from a DtdlModel which in turn recursivly fetches referenced and inherited properties?

Thanks

wmmhihaa
  • 744
  • 8
  • 21
  • Did you try using the Azure Digital Twins REST API to create a digital twin from a model? You may want to try generating an "empty" twin from a model, you can use the 'Get Digital Twin Models' method to retrieve the DTDL model's definition. You can then parse the model definition to determine the twin's properties and generate a JSON object that represents the initial state of the twin. [Ref](https://learn.microsoft.com/en-us/azure/digital-twins/concepts-apis-sdks) – SatishBoddu Mar 01 '23 at 01:40
  • Thanks for the reply. As far as I can see, the Azure Digital Twins REST API only have an "Add" method, and it doen't take a model parameter. I've been trying to generate an empty model using the DTDL from the 'Get Digital Twin Models', but it's fairly complex as some models inherits many properties from Components and extensions. – wmmhihaa Mar 01 '23 at 07:52
  • 1) This will return a list of all the DTDL models in your instance. GET https://yourinstance.digitaltwins.net/api/v2/models Authorization: Bearer {your-access-token} 2) Next, you can create an empty twin instance using the /twins endpoint: POST https://yourinstance.digitaltwins.net/api/v2/twins Authorization: Bearer {your-access-token} Content-Type: application/json { "$metadata": {}, "ModelId": "{your-model-id}" } 3) Get twin's properties GET https://yourinstance.digitaltwins.net/api/v2/twins/{your-twin-id}/properties Authorization: Bearer {your-access-token} – SatishBoddu Mar 01 '23 at 18:15
  • Please try above and let me know if that helps? – SatishBoddu Mar 01 '23 at 18:16
  • One approach you can take to generate an "empty" twin based on a DTDL model is to recursively traverse the model definition to build up a JSON object that represents the twin's properties. – SatishBoddu Mar 01 '23 at 18:18
  • @SatishBoddu I like how you think, but the problem is that some of the properties are manatory so it won't let me create the empty twin. For instance I'm trying to create a "building" using { "$metadata": {}, "ModelId": "{dtmi:org:w3id:rec:Building;1}" }. But as "area" and capacity are required I get this exception:{"code":"ValidationFailed","message":"Invalid twin specified","details":[{"code":"ValidationFailed","message":"Twin is missing mandatory component area."},{"code":"ValidationFailed","message":"Twin is missing mandatory component capacity."}]} – wmmhihaa Mar 01 '23 at 18:58
  • 1
    @SatishBoddu regarding the "ecursively traverse the model definition" approch... that is what I was trying, it's is quite complex traversing through all the components and inheritance... – wmmhihaa Mar 01 '23 at 19:01
  • I agree, let me check with the product team meanwhile on this query...stay tuned. – SatishBoddu Mar 01 '23 at 21:00
  • Ok i got the below response from the product team! **First:** _In the current implementation its required that all components be specified even if they are empty._ **Second:** _You don't need to specify the properties, just an empty component._ Let me know if this helps! – SatishBoddu Mar 01 '23 at 21:28
  • 1
    Another comment with suggestion i received is as follows: _imagine a way to declaratively specify such a mapping and then have a tool that would read this declaration and a DTDL model to produce an empty twin structure_ – SatishBoddu Mar 01 '23 at 22:42
  • Regarding: "You don't need to specify the properties". If I make a PUT call call to /digitaltwins/room_mikaels_office?api-version=2023-02-27-preview whith the payload: {"$dtId":"my_office","$etag":null,"$metadata":{"$model":"dtmi:org:w3id:rec:Room;1"}} I will get a validation error saying "Twin is missing mandatory component area.". Same if I make the call using the SDK. Am I missing somehting? – wmmhihaa Mar 02 '23 at 13:18

0 Answers0