如何在设备与 DPS 之间传输有效负载How to transfer a payload between device and DPS

有时,DPS 需要设备中的更多数据才能正常地将设备预配到适当的 IoT 中心,而这些数据需由设备提供。Sometimes DPS needs more data from devices to properly provision them to the right IoT Hub, and that data needs to be provided by the device. 反之,DPS 可将数据返回给设备,以便为客户端逻辑提供辅助。Vice versa, DPS can return data to the device to facilitate client side logics.

何时使用此功能When to use it

此功能可用作自定义分配的增强功能。This feature can be used as an enhancement for custom allocation. 例如,你希望在无需人工干预的情况下,根据设备型号分配设备。For instance, you want to allocate your devices based on the device model without human intervention. 在这种情况下,需使用自定义分配In this case, you will use custom allocation. 可以在注册设备调用过程中将设备配置为报告型号信息。You can configure the device to report the model information as part of the register device call. DPS 会将设备的有效负载传入自定义分配 Webhook。DPS will pass the device’s payload into to the custom allocation webhook. 当函数收到设备型号信息时,可以确定此设备要转到哪个 IoT 中心。And your function can decide which IoT Hub this device will go to when it receives device model information. 同样,如果 Webhook 希望将某些数据返回给设备,它会在 Webhook 响应中以字符串的形式传回数据。Similarly, if the webhook wishes to return some data to the device, it will pass the data back as a string in the webhook response.

设备将数据有效负载发送到 DPSDevice sends data payload to DPS

当设备向 DPS 发送注册设备调用,可以增强注册调用以获取正文中的其他字段。When your device is sending a register device call to DPS, The register call can be enhanced to take other fields in the body. 正文如下所示:The body looks like the following:

{ 
    “registrationId”: “mydevice”, 
    “tpm”:                
    { 
        “endorsementKey”: “stuff”, 
        “storageRootKey”: “things” 
    }, 
    “payload”: “your additional data goes here. It can be nested JSON.” 
 } 

DPS 将数据返回给设备DPS returns data to the device

如果自定义分配策略 Webhook 希望将某些数据返回给设备,它会在 Webhook 响应中以字符串的形式传回数据。If the custom allocation policy webhook wishes to return some data to the device, it will pass the data back as a string in the webhook response. 下面的 payload 节中提供了相关更改。The change is in the payload section below.

{ 
    "iotHubHostName": "sample-iot-hub-1.azure-devices.cn", 
    "initialTwin": { 
        "tags": { 
            "tag1": true 
            }, 
            "properties": { 
                "desired": { 
                    "tag2": true 
                 } 
             } 
         }, 
     "payload": "whatever is returned by the webhook" 
 } 

SDK 支持SDK support

此功能在 C、C#、JAVA 和 Node.js 客户端 SDK 中可用。This feature is available in C, C#, JAVA and Node.js client SDKs.

后续步骤Next steps

  • 使用适用于 Azure IoT 中心和 Azure IoT 中心设备预配服务的 Azure IoT SDK进行开发Develop using the Azure IoT SDK for Azure IoT Hub and Azure IoT Hub Device Provisioning Service