英文:
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):
{
"@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": "The time interval of this data",
"schema": "double",
"writable": true
},
{
"@type": "Property",
"name": "PartId",
"displayName": "PartId",
"description": "The unique id of the machine part",
"writable": true,
"schema": "string"
},
{
"@type": "Property",
"name": "Alert",
"displayName": "Alert",
"description": "whether or not this machine part needs maintenance",
"writable": true,
"schema": "boolean"
},
{
"@type": "Property",
"name": "ledState",
"displayName": "LED state",
"description": "Returns the current state of the onboard LED.",
"writable": true,
"schema": "boolean"
},
{
"@type": "Telemetry",
"name": "Description",
"schema": "string"
},
{
"@type": "Telemetry",
"name": "Code",
"schema": "integer"
},
{
"@type": "Telemetry",
"name": "Temperature",
"displayName": "Temperature",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "Humidity",
"displayName": "Humidity",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "pressure",
"displayName": "Pressure",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "magnetometerX",
"displayName": "Magnetometer X / mgauss",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "magnetometerY",
"displayName": "Magnetometer Y / mgauss",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "magnetometerZ",
"displayName": "Magnetometer Z / mgauss",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "accelerometerX",
"displayName": "Accelerometer X",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "accelerometerY",
"displayName": "Accelerometer Y",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "accelerometerZ",
"displayName": "Accelerometer Z",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "gyroscopeX",
"displayName": "Gyroscope X",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "gyroscopeY",
"displayName": "Gyroscope Y",
"schema": "double"
},
{
"@type": "Telemetry",
"name": "gyroscopeZ",
"displayName": "Gyroscope Z",
"schema": "double"
}
]
}
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't know why. How do I set the properties in the DTDL model I upload to IoT P&P to be reported or desired?
{
"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.4599426Z"
},
"manufacturer": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"model": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"swVersion": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"osName": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"processorArchitecture": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"processorManufacturer": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"totalStorage": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
},
"totalMemory": {
"$lastUpdated": "2023-01-09T07:28:25.4599426Z"
}
},
"ledState": {
"$lastUpdated": "2023-01-09T07:28:25.7411971Z"
},
"telemetryInterval": {
"$lastUpdated": "2023-01-09T07:28:26.0068306Z",
"ac": {
"$lastUpdated": "2023-01-09T07:28:26.0068306Z"
},
"av": {
"$lastUpdated": "2023-01-09T07:28:26.0068306Z"
},
"value": {
"$lastUpdated": "2023-01-09T07:28:26.0068306Z"
}
}
},
"$version": 155
}
},
"capabilities": {
"iotEdge": 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'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'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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论