|
|
@@ -54,9 +54,59 @@ public partial class ViewCViewModel : ObservableObject
|
|
|
UpdateTexts();
|
|
|
_lang.LanguageChanged += UpdateTexts;
|
|
|
InitializeChart();
|
|
|
+
|
|
|
+ // 订阅 PLC 轮询数据
|
|
|
+ PlcPollingService.Instance.DataUpdated += OnDataUpdated;
|
|
|
+
|
|
|
_ = LoadFlowRecordsAsync();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// PLC 数据更新回调 - 记录流量数据
|
|
|
+ /// </summary>
|
|
|
+ private void OnDataUpdated(PlcDataModel data)
|
|
|
+ {
|
|
|
+ // 将当前流量数据添加到图表
|
|
|
+ AddDataPoint(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void AddDataPoint(PlcDataModel data)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var now = DateTime.Now;
|
|
|
+
|
|
|
+ // 更新进水流量图表
|
|
|
+ if (InflowSeries?.Length > 0 && InflowSeries[0] is ColumnSeries<DateTimePoint> inflowSeries)
|
|
|
+ {
|
|
|
+ var values = inflowSeries.Values?.ToList() ?? new List<DateTimePoint>();
|
|
|
+ values.Add(new DateTimePoint(now, data.InflowRate));
|
|
|
+
|
|
|
+ // 保留最近 60 个数据点
|
|
|
+ if (values.Count > 60)
|
|
|
+ values.RemoveAt(0);
|
|
|
+
|
|
|
+ inflowSeries.Values = values.ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新出水流量图表
|
|
|
+ if (OutflowSeries?.Length > 0 && OutflowSeries[0] is ColumnSeries<DateTimePoint> outflowSeries)
|
|
|
+ {
|
|
|
+ var values = outflowSeries.Values?.ToList() ?? new List<DateTimePoint>();
|
|
|
+ values.Add(new DateTimePoint(now, data.OutflowRate));
|
|
|
+
|
|
|
+ if (values.Count > 60)
|
|
|
+ values.RemoveAt(0);
|
|
|
+
|
|
|
+ outflowSeries.Values = values.ToArray();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Log.Error(ex, "更新图表数据失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void UpdateTexts()
|
|
|
{
|
|
|
TitleText = _lang.Get("FlowRecords");
|