Azure / opendigitaltwins-dtdl

Digital Twins Definition Language
Creative Commons Attribution 4.0 International
462 stars 160 forks source link

Relationship Properties on twins #135

Open sourabhkrishna opened 1 year ago

sourabhkrishna commented 1 year ago

I've below sample models one for the base device and its relationship, the relationships are provisioned as modules of the device. The relationship has a boolean property "activated" defined. The question is where would this property reside? on the device twin or module twin?

{
    "@id": "dtmi:com:testdevice;1",
    "@type": "Interface",
    "@context": "dtmi:dtdl:context;2",
    "displayName": "Base Device",
    "contents": [
      {
        "@type": "Property",
        "name": "id",
        "schema": "string"     
      },    
      {
        "@type": "Relationship",
        "@id": "dtmi:com:hasrelationship;1",
        "name": "hasrelationship",
        "displayName": "Test Device has Relationship",
        "target": "dtmi:com:testrelationship;1",
        "properties": [
            {
              "@type": "Property",
              "name": "activated",
              "schema": "boolean"
            }
          ]
      }
    ]
  }

{
    "@id": "dtmi:com:testrelationship;1",
    "@type": "Interface",
    "@context": "dtmi:dtdl:context;2",
    "displayName": "Relationship",
    "contents": [
      {
        "@type": "Property",
        "name": "id",
        "schema": "string"     
      }
    ]
  }
rido-min commented 1 year ago

Hi @sourabhkrishna

DTDL properties can be used to describe device twins or module twins. However, the language itself, neither the IoT Plug and Play conventions have any construct to express the connection between device twins and module twins.

From IoT Hub perspective, these are two complete unrelated entities, you can model a connection between the two with DTDL relationships, in that case is up to you to describe the module model id in the relationtship or in the module twin.

I'd love to better understand your use case related to modules to provide better guidance.

sourabhkrishna commented 1 year ago

@rido-min Our use case for using the module twins is for the Azure Device Update feature as this mandates the device to publish a particular pnp modelId whereas our devices have their own pnp modelId, so in order to separate the firmware update model from the base device model we use module identities for ADU and device identities for the actual device. This 1:1 relationship is described in the DTDL below. However our FW ran into issues while maintaining multiple MQTT connections to IoTHub (one for the device and another for the module), so I'm looking for options wherein if the module twin was updated then the relationship on the device model could have some sort of indication so that FW can switch connections to consume the module twin.

{
            "@id": "dtmi:com:FirmwareModule;1",
            "@type": "Relationship",
            "name": "FirmwareModule",
            "displayName": {
              "en": "Firmware module"
            },
            "description": {"en": "The module used to manage ADU."},
            "minMultiplicity": 0,
            "maxMultiplicity": 1,
            "target": "dtmi:azure:iot:deviceUpdateModel;1"
        },
sourabhkrishna commented 1 year ago

@rido-min did you get a chance to look at this?

rido-min commented 1 year ago

I'd suggest to reference the ADU interface as a component, as described in https://learn.microsoft.com/en-us/azure/iot-hub-device-update/device-update-plug-and-play