跳到主要内容

趋势与历史绑定

点表会保留最近成功采样历史。0.6 起可以把这段历史直接推到 WinForms / WPF 界面,用来接趋势图、报警时间线或最近采样列表。

回调式绑定

BindHistory 会在订阅时立即推送当前历史,之后每次成功采样进入历史时继续推送。采集失败只更新当前快照的 Error,不会写入历史。

points.BindHistory("temperature", this, history =>
{
HistoryText.Text = string.Join(
" -> ",
history.TakeLast(8).Select(sample => $"{sample.Value} {sample.AlarmState}"));
});

WinForms 传入任意 Control,WPF 传入任意 FrameworkElement。Zeus 会把回调封送到该控件所在的 UI 线程。

绑定源

如果页面已经使用数据绑定,可以创建历史绑定源:

var source = points.AsHistoryBindingSource("temperature", this);
TrendPanel.DataContext = source;

PointHistoryBindingSource 提供这些属性:

属性说明
History最近成功采样历史,顺序从旧到新
Latest最新一条成功采样
Count / HasSamples历史数量与是否已有采样
LatestValue / LatestUpdatedAt最新值与更新时间
LatestAlarmState / IsLatestAlarmed最新采样的报警状态

接图表控件

Zeus 不绑定某个图表库。你可以在 BindHistory 回调里把 PointSnapshot 转成图表控件需要的数据结构:

points.BindHistory("temperature", this, history =>
{
chart.Series[0].Points.Clear();
foreach (var sample in history)
{
if (sample.UpdatedAt is not null && sample.Value is not null)
{
chart.Series[0].Points.AddXY(sample.UpdatedAt.Value.LocalDateTime, sample.Value);
}
}
});

历史容量

默认每个点保留最近 128 次成功采样。如果要缩短或关闭历史,可以注册自定义 PointTable

builder.Services.AddSingleton(sp => new PointTable(sp.GetRequiredService<DeviceRegistry>(), historyCapacity: 32));

容量设为 0 时,BindHistory 仍会推送空历史,但不会保留采样。

下一步:需要显示当前值和报警色看 WinForms 适配器WPF 适配器