向 IoT Central 提供消息中的时间戳

mki*_*ner 4 azure azure-iot-sdk azure-iot-central

我想将“真实设备”与 Azure IoT Central 连接,并使用 MQTT 将本地源应用程序连接到它。我使用代码进行连接和替换。

但是,我找不到有关如何提供时间戳的任何信息。线程建议将“iothub-creation-time-utc”设置为“属性”——但是我不知道该怎么做。有没有这方面的文件?

Rom*_*iss 6

将属性添加到消息中:

message.properties.add('iothub-creation-time-utc', utcDT);
Run Code Online (Sandbox Code Playgroud)


Ste*_*SFT 5

根据您问题中的链接,我假设您正在使用 Node.js 来开发设备代码。这里有一个示例代码片段,展示了如何设置创建时间属性: https: //learn.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');
  }
}
Run Code Online (Sandbox Code Playgroud)