Provide timestamp in message to IoT central

时间:2019-03-19 14:59:11

标签: azure azure-iot-sdk azure-iot-central

I want to connect a 'real device' with Azure IoT Central and connect a local source application to it using MQTT. I use this code for the connection and replace.

However, I cannot find any information on how to provide the timestamp. This thread suggests to set "iothub-creation-time-utc" as a "property" - I am not sure how to do that however. Is there any documentation on this?

2 个答案:

答案 0 :(得分:3)

将属性添加到消息中:

message.properties.add('iothub-creation-time-utc', utcDT);

答案 1 :(得分:1)

根据您问题中的链接,我假设您正在使用Node.js来开发设备代码。有一个示例代码片段显示了如何在此处设置创建时间属性: https://docs.microsoft.com/en-us/azure/iot-accelerators/iot-accelerators-connecting-pi-node

function sendTelemetry(data, schema) {
  if (deviceOnline) {
    var d = new Date();
    var payload = JSON.stringify(data);
    var message = new Message(payload);
    message.properties.add('iothub-creation-time-utc', d.toISOString());
    message.properties.add('iothub-message-schema', schema);

    console.log('Sending device message data:\n' + payload);
    client.sendEvent(message, printErrorFor('send event'));
  } else {
    console.log('Offline, not sending telemetry');
  }
}