Azure / opendigitaltwins-dtdl

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

Validating Digital Twin updates against the Digital Twin model #129

Closed EGitDeveloper closed 4 months ago

EGitDeveloper commented 2 years ago

My apologies in advance if this is not correct place to ask the question.

In Azure IoT Central and Azure Digital Twin service, if I create and upload model with the following property:

{
    "@type": [ "Property", "Temperature" ],
    "name": "targetTemperature",
    "displayName": "Target Temperature",
    "unit": "degreeCelsius",
    "writable": true,
    ---> "schema": "double"
}

And I try to update writable property with string/boolean - update fails.

[
  {
    "op": "replace",
    "path": "/targetTemperature",
    ---> "value":  "some value"
  }
]

Are there any specific libraries, SDKs or examples that I can use or reference to implement same kind of functionality in my application? That is, validate that updates conform with DTDL model.

rido-min commented 2 years ago

Well, the value should be a double, instead of string/boolean

Can you clarify which APIs/SDKs are you using for Central and ADT?

Central: https://docs.microsoft.com/en-us/rest/api/iotcentral/1.1-previewdataplane/devices/update-properties ADT: https://docs.microsoft.com/en-us/rest/api/digital-twins/dataplane/twins/digitaltwins_update https://github.com/Azure/azure-iot-sdk-node/blob/main/service/samples/javascript/update_digital_twin.js

EGitDeveloper commented 2 years ago

Well, the value should be a double, instead of string/boolean

Can you clarify which APIs/SDKs are you using for Central and ADT?

Central: https://docs.microsoft.com/en-us/rest/api/iotcentral/1.1-previewdataplane/devices/update-properties ADT: https://docs.microsoft.com/en-us/rest/api/digital-twins/dataplane/twins/digitaltwins_update https://github.com/Azure/azure-iot-sdk-node/blob/main/service/samples/javascript/update_digital_twin.js

I'm working on my own API and solution that is neither based on IOT Central or Digital Twins service, however I'm using both of them as a reference/learning tool. That's why I'm wondering how I could design my solution to mimic IOT Central/Digital Twins service and refuse updates that break the DTDL model.

To update DT or call commands I've used the following examples. My solution is based on IoT Hub and Plug and Play Digital Twins.

Say my personal API has PATCH endpoint to update DT that accepts PATCH body - how do I validate PATCH request against DTDL model? "To update property on device with XYZ DTDL model you need to provide X type of a value, but you have provided Y, therefore you get BAD REQUEST response."

rido-min commented 2 years ago

Got it,

Unfortunately, we don't have any API to validate payloads against a given DTDL interface. This is a feature that we are actively investigating.

Would you like to provide feedback in a 30 mins chat? you can reach out to me: rmpablos at microsoft dot com

-Rido

EGitDeveloper commented 2 years ago

That was basically it. I was researching Plug & Play Digital Twins and wondering if there's any public API/SDK that I can use to validate update payload against DTDL model. Azure IOT Central and Digital Twins service both do it so I thought I might have missed documentation somewhere.

Thank you for clarification.

rido-min commented 2 years ago

Thanks for the feedback,

I will keep this issue open until we publish some validation APIs.

Thanks, Rido

rido-min commented 2 years ago

Hi @EGitDeveloper

Let me correct myself:

We have published a preview parser that includes a validation api, the following code snippet shows how to validate a payload.

var objectModel = new ModelParser().Parse(new string[] { ReadFile("geotypes-1.json") });

var rootInterface = (DTInterfaceInfo)objectModel[new Dtmi("dtmi:samplesv2:geoTypes;1")];

var myPoint = (DTTelemetryInfo)rootInterface.Contents["myPoint"];
var validPayload = myPoint.Schema.ValidateInstance(ToJs(new { type = "Point", coordinates = new double[] { 12.35, 23.56} }));
Console.WriteLine(validPayload);

var myGeopoint = (DTTelemetryInfo)rootInterface.Contents["myGeopoint"];
var validPayload2 = myGeopoint.Schema.ValidateInstance(ToJs(new { lat =12.35, lon = 23.56 }));
Console.WriteLine(validPayload2);
{
    "@context": [
        "dtmi:dtdl:context;2",
        "dtmi:iotcentral:context;2"
    ],
    "@id": "dtmi:samplesv2:geoTypes;1",
    "@type": "Interface",
    "contents": [
      {
        "@type": "Telemetry",
        "name": "myPoint",
        "schema": "point"
      },
      {
          "@type": ["Telemetry", "Location"],
          "name": "myGeopoint",
          "schema": "geopoint"
      }
    ]
  }

full gist is available here

EGitDeveloper commented 2 years ago

Thank you for update! Looking forward to testing it out,

rido-min commented 1 year ago

Hi @EGitDeveloper

Does this API solve your problem? If so, can you close this issue?

rido-min commented 4 months ago

closing due to lack of response