如何在物联网插件和播放DTDL模型中设置设备双属性的期望和报告属性。

huangapple go评论82阅读模式
英文:

How set desired and reported properties for device twin in IoT Plug and Play DTDL model

问题

以下是您提供的内容的翻译:

我有一个DTDL模型,我上传到IoT插件和播放v0.15.4.0的本地文件夹(先前版本的行为相同):

{
    "@id": "dtmi:com:sergio:devkit;1",
    "@type": "Interface",
    "@context": [
        "dtmi:dtdl:context;2"
    ],
    "displayName": "Machine Part -v1",
    "description": "Machine Part data",
    "contents": [
        {
            "@type": "Property",
            "name": "telemetryInterval",
            "displayName": "telemetryInterval",
            "description": "This data的时间间隔",
            "schema": "double",
            "writable": true
        },
        {
            "@type": "Property",
            "name": "PartId",
            "displayName": "PartId",
            "description": "机器部件的唯一ID",
            "writable": true,
            "schema": "string"
        },

        {
            "@type": "Property",
            "name": "Alert",
            "displayName": "Alert",
            "description": "这个机器部件是否需要维护",
            "writable": true,
            "schema": "boolean"
        },
        {
            "@type": "Property",
            "name": "ledState",
            "displayName": "LED状态",
            "description": "返回板载LED的当前状态。",
            "writable": true,
            "schema": "boolean"
        },
        {
            "@type": "Telemetry",
            "name": "Description",
            "schema": "string"
        },
        {
            "@type": "Telemetry",
            "name": "Code",
            "schema": "integer"
        },
        {
            "@type": "Telemetry",
            "name": "Temperature",
            "displayName": "温度",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "Humidity",
            "displayName": "湿度",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "pressure",
            "displayName": "压力",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "magnetometerX",
            "displayName": "磁力计X / mgauss",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "magnetometerY",
            "displayName": "磁力计Y / mgauss",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "magnetometerZ",
            "displayName": "磁力计Z / mgauss",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "accelerometerX",
            "displayName": "加速度计X",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "accelerometerY",
            "displayName": "加速度计Y",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "accelerometerZ",
            "displayName": "加速度计Z",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "gyroscopeX",
            "displayName": "陀螺仪X",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "gyroscopeY",
            "displayName": "陀螺仪Y",
            "schema": "double"
        },
        {
            "@type": "Telemetry",
            "name": "gyroscopeZ",
            "displayName": "陀螺仪Z",
            "schema": "double"
        }
    ]
}
我的期望是,因为ledState和Alert属性是可写的,它们应该同时作为报告和期望的属性。然而,Azure门户中的IoT Hub设备(如下所示)显示它们不同,我不知道为什么。我如何设置我上传到IoT P&P的DTDL模型中的属性为报告或期望?

{
    "deviceId": "mymxchip",
    "etag": "AAAAAAAAAAk=",
    "deviceEtag": "NDU1NDk0NDM4",
    "status": "enabled",
    "statusUpdateTime": "0001-01-01T00:00:00Z",
    "connectionState": "Connected",
    "lastActivityTime": "2023-01-09T07:26:48.611708Z",
    "cloudToDeviceMessageCount": 0,
    "authenticationType": "sas",
    "x509Thumbprint": {
        "primaryThumbprint": null,
        "secondaryThumbprint": null
    },
    "modelId": "dtmi:com:sergio:devkit;1",
    "version": 164,
    "properties": {
        "desired": {
            "telemetryInterval": 5,
            "Alert": true,
            "$metadata": {
                "$lastUpdated": "2023-01-08T07:27:01.9659284Z",
                "$lastUpdatedVersion": 9,
                "telemetryInterval": {
                    "$lastUpdated": "2023-01-08T06:54:11.9502921Z",
                    "$lastUpdatedVersion": 8
                },
                "Alert": {
                    "$lastUpdated": "2023-01-08T07:27:01.9659284Z",
                    "$lastUpdatedVersion": 9
                }
            },
            "$version": 9
        },
        "reported": {
            "deviceInformation": {
                "__t": "c",
                "manufacturer": "MXCHIP",
                "model": "AZ3166",
                "swVersion": "1.0.0",
                "osName": "Azure RTOS",
                "processorArchitecture": "Arm Cortex M4",
                "processorManufacturer": "STMicroelectronics",
                "totalStorage": 1024,
                "totalMemory": 128
            },
            "ledState": false,
            "telemetryInterval": {
                "ac": 200,
                "av": 1,
                "value": 5
            },
            "$metadata": {
                "$lastUpdated": "2023-01-09T07:28:26.0068306Z",
                "deviceInformation": {
                    "$lastUpdated": "2023-01-09T07:28:25.4599426Z",
                    "__t": {
                        "$lastUpdated": "2023-01-09T07:28:25.

<details>
<summary>英文:</summary>

I have this dtdl model I upload to local folder in IoT Plug and Play v0.15.4.0 (prior version behaviour is the same):

    {
    &quot;@id&quot;: &quot;dtmi:com:sergio:devkit;1&quot;,
    &quot;@type&quot;: &quot;Interface&quot;,
    &quot;@context&quot;: [
        &quot;dtmi:dtdl:context;2&quot;
    ],
    &quot;displayName&quot;: &quot;Machine Part -v1&quot;,
    &quot;description&quot;: &quot;Machine Part data&quot;,
    &quot;contents&quot;: [
        {
            &quot;@type&quot;: &quot;Property&quot;,
            &quot;name&quot;: &quot;telemetryInterval&quot;,
            &quot;displayName&quot;: &quot;telemetryInterval&quot;,
            &quot;description&quot;:  &quot;The time interval of this data&quot;,
            &quot;schema&quot;: &quot;double&quot;,
            &quot;writable&quot;: true
        },
        {
            &quot;@type&quot;: &quot;Property&quot;,
            &quot;name&quot;: &quot;PartId&quot;,
            &quot;displayName&quot;: &quot;PartId&quot;,
            &quot;description&quot;: &quot;The unique id of the machine part&quot;,
            &quot;writable&quot;: true,
            &quot;schema&quot;: &quot;string&quot;
        },

        {
            &quot;@type&quot;: &quot;Property&quot;,
            &quot;name&quot;: &quot;Alert&quot;,
            &quot;displayName&quot;: &quot;Alert&quot;,
            &quot;description&quot;: &quot;whether or not this machine part needs maintenance&quot;,
            &quot;writable&quot;: true,
            &quot;schema&quot;: &quot;boolean&quot;
        },
        {
            &quot;@type&quot;: &quot;Property&quot;,
            &quot;name&quot;: &quot;ledState&quot;,
            &quot;displayName&quot;: &quot;LED state&quot;,
            &quot;description&quot;: &quot;Returns the current state of the onboard LED.&quot;,
            &quot;writable&quot;: true,
            &quot;schema&quot;: &quot;boolean&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;Description&quot;,
            &quot;schema&quot;: &quot;string&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;Code&quot;,
            &quot;schema&quot;: &quot;integer&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;Temperature&quot;,
            &quot;displayName&quot;: &quot;Temperature&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;Humidity&quot;,
            &quot;displayName&quot;: &quot;Humidity&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;pressure&quot;,
            &quot;displayName&quot;: &quot;Pressure&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;magnetometerX&quot;,
            &quot;displayName&quot;: &quot;Magnetometer X / mgauss&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;magnetometerY&quot;,
            &quot;displayName&quot;: &quot;Magnetometer Y / mgauss&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;magnetometerZ&quot;,
            &quot;displayName&quot;: &quot;Magnetometer Z / mgauss&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;accelerometerX&quot;,
            &quot;displayName&quot;: &quot;Accelerometer X&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;accelerometerY&quot;,
            &quot;displayName&quot;: &quot;Accelerometer Y&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;accelerometerZ&quot;,
            &quot;displayName&quot;: &quot;Accelerometer Z&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;gyroscopeX&quot;,
            &quot;displayName&quot;: &quot;Gyroscope X&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;gyroscopeY&quot;,
            &quot;displayName&quot;: &quot;Gyroscope Y&quot;,
            &quot;schema&quot;: &quot;double&quot;
        },
        {
            &quot;@type&quot;: &quot;Telemetry&quot;,
            &quot;name&quot;: &quot;gyroscopeZ&quot;,
            &quot;displayName&quot;: &quot;Gyroscope Z&quot;,
            &quot;schema&quot;: &quot;double&quot;
        }
    ]
}

My expectation is that because properties ledState and Alert properties are writeable, these should be both reported and desired properties. However the IoT Hub Device in Azure portal (see below) shows them differently and I don&#39;t know why. How do I set the properties in the DTDL model I upload to IoT P&amp;P to be reported or desired?

    {
    &quot;deviceId&quot;: &quot;mymxchip&quot;,
    &quot;etag&quot;: &quot;AAAAAAAAAAk=&quot;,
    &quot;deviceEtag&quot;: &quot;NDU1NDk0NDM4&quot;,
    &quot;status&quot;: &quot;enabled&quot;,
    &quot;statusUpdateTime&quot;: &quot;0001-01-01T00:00:00Z&quot;,
    &quot;connectionState&quot;: &quot;Connected&quot;,
    &quot;lastActivityTime&quot;: &quot;2023-01-09T07:26:48.611708Z&quot;,
    &quot;cloudToDeviceMessageCount&quot;: 0,
    &quot;authenticationType&quot;: &quot;sas&quot;,
    &quot;x509Thumbprint&quot;: {
        &quot;primaryThumbprint&quot;: null,
        &quot;secondaryThumbprint&quot;: null
    },
    &quot;modelId&quot;: &quot;dtmi:com:sergio:devkit;1&quot;,
    &quot;version&quot;: 164,
    &quot;properties&quot;: {
        &quot;desired&quot;: {
            &quot;telemetryInterval&quot;: 5,
            &quot;Alert&quot;: true,
            &quot;$metadata&quot;: {
                &quot;$lastUpdated&quot;: &quot;2023-01-08T07:27:01.9659284Z&quot;,
                &quot;$lastUpdatedVersion&quot;: 9,
                &quot;telemetryInterval&quot;: {
                    &quot;$lastUpdated&quot;: &quot;2023-01-08T06:54:11.9502921Z&quot;,
                    &quot;$lastUpdatedVersion&quot;: 8
                },
                &quot;Alert&quot;: {
                    &quot;$lastUpdated&quot;: &quot;2023-01-08T07:27:01.9659284Z&quot;,
                    &quot;$lastUpdatedVersion&quot;: 9
                }
            },
            &quot;$version&quot;: 9
        },
        &quot;reported&quot;: {
            &quot;deviceInformation&quot;: {
                &quot;__t&quot;: &quot;c&quot;,
                &quot;manufacturer&quot;: &quot;MXCHIP&quot;,
                &quot;model&quot;: &quot;AZ3166&quot;,
                &quot;swVersion&quot;: &quot;1.0.0&quot;,
                &quot;osName&quot;: &quot;Azure RTOS&quot;,
                &quot;processorArchitecture&quot;: &quot;Arm Cortex M4&quot;,
                &quot;processorManufacturer&quot;: &quot;STMicroelectronics&quot;,
                &quot;totalStorage&quot;: 1024,
                &quot;totalMemory&quot;: 128
            },
            &quot;ledState&quot;: false,
            &quot;telemetryInterval&quot;: {
                &quot;ac&quot;: 200,
                &quot;av&quot;: 1,
                &quot;value&quot;: 5
            },
            &quot;$metadata&quot;: {
                &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:26.0068306Z&quot;,
                &quot;deviceInformation&quot;: {
                    &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;,
                    &quot;__t&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;manufacturer&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;model&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;swVersion&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;osName&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;processorArchitecture&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;processorManufacturer&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;totalStorage&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    },
                    &quot;totalMemory&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.4599426Z&quot;
                    }
                },
                &quot;ledState&quot;: {
                    &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:25.7411971Z&quot;
                },
                &quot;telemetryInterval&quot;: {
                    &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:26.0068306Z&quot;,
                    &quot;ac&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:26.0068306Z&quot;
                    },
                    &quot;av&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:26.0068306Z&quot;
                    },
                    &quot;value&quot;: {
                        &quot;$lastUpdated&quot;: &quot;2023-01-09T07:28:26.0068306Z&quot;
                    }
                }
            },
            &quot;$version&quot;: 155
        }
    },
    &quot;capabilities&quot;: {
        &quot;iotEdge&quot;: false
    }
}

</details>


# 答案1
**得分**: 1

你说得对,DTDL可写属性将会出现在设备孪生中,既作为报告属性,也作为期望属性。然而,这取决于设备的行为。

如果你使用服务API设置了一个期望属性,那么它会更新设备孪生中的期望属性值。设备应该在接收并处理期望属性后发送一个确认(Ack) - 在你上面的示例中,看起来设备已经为`telemetryInterval`属性执行了这个操作。但是设备尚未确认`Alert`期望属性的更新。

当设备发送报告属性时,设备孪生会使用设备发送的值进行更新。在上面的示例中,设备已发送了`deviceInformation`、`ledState`和`telemetryInterval`的报告属性。`telemetryInterval`是响应期望属性而发送的。

你可以在以下链接找到更多详细信息:

- https://learn.microsoft.com/en-us/azure/iot-develop/concepts-convention#writable-properties
- https://learn.microsoft.com/en-us/azure/iot-develop/concepts-developer-guide-device#writable-properties(多种语言的设备代码示例)

以下示例是针对温控器设备如何遵循PnP约定的:

- https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/solutions/PnpDeviceSamples/Thermostat
- https://github.com/Azure/azure-iot-sdk-java/tree/main/device/iot-device-samples/pnp-device-sample
- https://github.com/Azure/azure-iot-sdk-node/tree/main/device/samples
- https://github.com/Azure/azure-iot-sdk-python/tree/main/samples/pnp

<details>
<summary>英文:</summary>

You&#39;re correct that a DTDL writable property will appear as both a reported and desired property in the device twin. However, this is dependant and the behavior of the device.

If you set a desired property using a service API, that updates the desired property value in the device twin. The device should then send an Ack when it&#39;s received and processed the desired property - in your example above, it looks as if the device has done this for the `telemetryInterval` property. The device hasn&#39;t acknowledged the `Alert` desired property update.

When the device sends a reported property, the device twin is updated with the value sent by the device. In the example above, the device has sent `deviceInformation`, `ledState`, and `telemetryInterval` reported properties. The `telemetryInterval` was sent in response to a desired property.

You can find more details here:

 - https://learn.microsoft.com/en-us/azure/iot-develop/concepts-convention#writable-properties
 - https://learn.microsoft.com/en-us/azure/iot-develop/concepts-developer-guide-device#writable-properties (device code samples in multiple languages)

To following samples for a thermostat device all illustrate how to follow the PnP conventions for writable properties:

- https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/solutions/PnpDeviceSamples/Thermostat
- https://github.com/Azure/azure-iot-sdk-java/tree/main/device/iot-device-samples/pnp-device-sample
- https://github.com/Azure/azure-iot-sdk-node/tree/main/device/samples
- https://github.com/Azure/azure-iot-sdk-python/tree/main/samples/pnp

</details>



huangapple
  • 本文由 发表于 2023年1月9日 15:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定