如何在IoT Central图表X轴使用自定义时间戳属性
在IoT Central中自定义仪表板图表X轴时间戳的实现方法
问题场景
在IoT Central应用中,仪表板图表默认使用系统字段_timestamp(消息到达IoT Central的时间)作为X轴时间戳,但希望将自定义属性Controller.Timestamp(设备端生成的源时间戳)替换为X轴的取值。以下是示例原始数据:
{ "_eventcreationtime": "2022-09-05T18:05:18.316Z", "Controller": { "Temperature4K_1": 71.468, "Temperature4K_2": 61.428, "Temperature40K_1": 39.135, "Temperature40K_2": 32.69, "TelemetryMagnetID": "Ex accusamus non commodi id ab excepturi facere et.", "Timestamp": "2023-01-07T19:50:11.189Z" // 希望用作X轴的自定义时间戳 }, "_eventtype": "Telemetry", "_timestamp": "2022-09-05T18:05:18.369Z" // 图表默认使用的时间戳 }
解决方案
1. 直接设置的局限性
IoT Central目前不支持直接在仪表板图表的配置中选择自定义属性作为X轴的时间戳,图表默认绑定系统生成的时间字段(_timestamp或_eventcreationtime)。
2. 通过C# IoT设备SDK实现
可以通过将自定义时间戳设置为消息的系统属性iothub-creation-time,让IoT Central识别并使用该时间作为消息的创建时间,从而在图表X轴中生效。
具体代码示例如下:
using Microsoft.Azure.Devices.Client; using Newtonsoft.Json; using System.Text; // 初始化设备客户端(替换为你的设备连接字符串) var deviceClient = DeviceClient.CreateFromConnectionString("<你的设备连接字符串>", TransportType.Mqtt); // 构造遥测数据 var telemetryData = new { Controller = new { Temperature4K_1 = 71.468, Temperature4K_2 = 61.428, Temperature40K_1 = 39.135, Temperature40K_2 = 32.69, TelemetryMagnetID = "Ex accusamus non commodi id ab excepturi facere et.", Timestamp = "2023-01-07T19:50:11.189Z" } }; // 序列化遥测数据为JSON string payload = JsonConvert.SerializeObject(telemetryData); Message message = new Message(Encoding.UTF8.GetBytes(payload)); // 将自定义时间戳赋值给系统属性iothub-creation-time message.Properties.Add("iothub-creation-time", telemetryData.Controller.Timestamp); // 发送遥测消息 await deviceClient.SendEventAsync(message);
注意事项
- 自定义时间戳必须符合ISO 8601格式(如
yyyy-MM-ddTHH:mm:ss.fffZ),否则系统会忽略该属性,继续使用默认的_timestamp。 - 设置
iothub-creation-time后,IoT Central的_eventcreationtime字段会被替换为该自定义时间,图表会自动使用这个时间作为X轴的取值。
内容的提问来源于stack exchange,提问作者Nelson Morais
相关产品推荐
相关产品推荐

