2025-09-22 06:24:08 +08:00
|
|
|
|
using System.Windows.Media;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
2025-10-02 18:47:45 +08:00
|
|
|
|
using DMS.Application.Events;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-17 17:18:10 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
|
|
|
|
|
using DMS.Core.Events;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
|
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
2025-09-22 06:24:08 +08:00
|
|
|
|
using LiveChartsCore;
|
|
|
|
|
|
using LiveChartsCore.Defaults;
|
|
|
|
|
|
using LiveChartsCore.SkiaSharpView;
|
|
|
|
|
|
using LiveChartsCore.SkiaSharpView.Painting;
|
|
|
|
|
|
using LiveChartsCore.SkiaSharpView.WPF;
|
2025-09-17 17:18:10 +08:00
|
|
|
|
using ObservableCollections;
|
2025-09-22 06:24:08 +08:00
|
|
|
|
using SkiaSharp;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.ViewModels;
|
|
|
|
|
|
|
2025-09-11 18:09:35 +08:00
|
|
|
|
partial class VariableHistoryViewModel : ViewModelBase, INavigatable
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-09-11 18:09:35 +08:00
|
|
|
|
private readonly IHistoryAppService _historyAppService;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
private readonly IWPFDataService _wpfDataService;
|
|
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-09-17 17:18:10 +08:00
|
|
|
|
private readonly IEventService _eventService;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// 历史记录条数限制
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-11 18:09:35 +08:00
|
|
|
|
private int? _historyLimit;
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// <summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// 历史记录开始时间
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// </summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private DateTime? _startTime;
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// <summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// 历史记录结束时间
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// </summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private DateTime? _endTime;
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-12 08:35:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 选中的变量历史记录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-17 13:32:29 +08:00
|
|
|
|
private VariableItemViewModel _currentVariable;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 变量历史记录列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public NotifyCollectionChangedSynchronizedViewList<VariableHistoryDto> VariableHistories { get; }
|
|
|
|
|
|
|
2025-09-17 14:13:48 +08:00
|
|
|
|
// 折线图相关属性
|
2025-09-22 06:24:08 +08:00
|
|
|
|
public ISeries[] LineSeriesCollection { get; set; }
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-22 06:24:08 +08:00
|
|
|
|
private Axis[] _lineAxisX;
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-09-22 06:24:08 +08:00
|
|
|
|
private Axis[] _lineAxisY;
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
2025-09-11 11:04:07 +08:00
|
|
|
|
private readonly ObservableList<VariableHistoryDto> _variableHistoryList;
|
|
|
|
|
|
private readonly ISynchronizedView<VariableHistoryDto, VariableHistoryDto> _variableHistorySynchronizedView;
|
|
|
|
|
|
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 所有变量的缓存列表,用于搜索
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private List<VariableHistoryDto> _allVariableHistories;
|
|
|
|
|
|
|
|
|
|
|
|
public VariableHistoryViewModel(IMapper mapper, IDialogService dialogService, IHistoryAppService historyAppService,
|
2025-09-15 13:12:14 +08:00
|
|
|
|
IWPFDataService wpfDataService, IDataStorageService dataStorageService,
|
2025-09-17 17:18:10 +08:00
|
|
|
|
IEventService eventService,
|
2025-09-15 13:12:14 +08:00
|
|
|
|
INotificationService notificationService)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
_dialogService = dialogService;
|
2025-09-11 18:09:35 +08:00
|
|
|
|
_historyAppService = historyAppService;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
_wpfDataService = wpfDataService;
|
|
|
|
|
|
_dataStorageService = dataStorageService;
|
2025-09-17 17:18:10 +08:00
|
|
|
|
_eventService = eventService;
|
2025-09-11 11:04:07 +08:00
|
|
|
|
_notificationService = notificationService;
|
|
|
|
|
|
|
|
|
|
|
|
_variableHistoryList = new ObservableList<VariableHistoryDto>();
|
|
|
|
|
|
_variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v);
|
|
|
|
|
|
VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged();
|
2025-09-11 18:09:35 +08:00
|
|
|
|
_allVariableHistories = new List<VariableHistoryDto>();
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-11 18:09:35 +08:00
|
|
|
|
// 初始化默认值
|
2025-09-22 06:24:08 +08:00
|
|
|
|
_historyLimit = 50; // 默认限制1000条记录
|
2025-09-11 18:09:35 +08:00
|
|
|
|
_startTime = null;
|
|
|
|
|
|
_endTime = null;
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化图表属性
|
2025-09-22 06:24:08 +08:00
|
|
|
|
LineAxisX = new Axis[] { new Axis() };
|
|
|
|
|
|
LineAxisY = new Axis[] { new Axis() };
|
|
|
|
|
|
LineSeriesCollection = new ISeries[0];
|
2025-09-17 17:18:10 +08:00
|
|
|
|
|
|
|
|
|
|
_eventService.OnVariableValueChanged += OnVariableValueChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
|
|
|
|
|
{
|
2025-10-03 12:10:37 +08:00
|
|
|
|
if (e.Variable.Id != CurrentVariable.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var variableHistory = new VariableHistoryDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
VariableId = CurrentVariable.Id,
|
|
|
|
|
|
Timestamp = DateTime.Now,
|
2025-10-03 12:39:37 +08:00
|
|
|
|
Value = e.Variable.DisplayValue,
|
|
|
|
|
|
NumericValue = e.Variable.NumericValue
|
2025-10-03 12:10:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
_variableHistoryList.Add(variableHistory);
|
|
|
|
|
|
|
|
|
|
|
|
// 限制历史记录数量以防止内存溢出
|
|
|
|
|
|
if (HistoryLimit.HasValue && _variableHistoryList.Count > HistoryLimit.Value)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 移除最旧的记录
|
|
|
|
|
|
_variableHistoryList.RemoveAt(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新图表数据
|
|
|
|
|
|
UpdateChartData();
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// 加载所有变量的历史记录
|
2025-09-11 11:04:07 +08:00
|
|
|
|
/// </summary>
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// <param name="limit">返回记录的最大数量,null表示无限制</param>
|
|
|
|
|
|
/// <param name="startTime">开始时间,null表示无限制</param>
|
|
|
|
|
|
/// <param name="endTime">结束时间,null表示无限制</param>
|
2025-09-17 14:13:48 +08:00
|
|
|
|
private async void LoadAllVariableHistories(int variableId, int? limit = null, DateTime? startTime = null,
|
|
|
|
|
|
DateTime? endTime = null)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-09-11 18:09:35 +08:00
|
|
|
|
try
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
_variableHistoryList.Clear();
|
2025-09-17 14:13:48 +08:00
|
|
|
|
var allHistories
|
|
|
|
|
|
= await _historyAppService.GetVariableHistoriesAsync(variableId, limit, startTime, endTime);
|
2025-09-11 18:09:35 +08:00
|
|
|
|
_allVariableHistories = allHistories.ToList();
|
|
|
|
|
|
_variableHistoryList.AddRange(_allVariableHistories);
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新图表数据
|
|
|
|
|
|
UpdateChartData();
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
2025-09-11 18:09:35 +08:00
|
|
|
|
catch (Exception ex)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-09-11 18:09:35 +08:00
|
|
|
|
// 记录更详细的错误信息
|
|
|
|
|
|
_notificationService.ShowError($"加载变量历史记录失败: {ex.Message}", ex);
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-12 08:35:35 +08:00
|
|
|
|
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
|
|
|
|
|
|
{
|
2025-09-17 13:32:29 +08:00
|
|
|
|
if (_dataStorageService.Variables.TryGetValue(menu.TargetId, out VariableItemViewModel variableItem))
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentVariable = variableItem;
|
|
|
|
|
|
// 加载所有变量的历史记录
|
2025-09-17 14:13:48 +08:00
|
|
|
|
LoadAllVariableHistories(variableItem.Id, HistoryLimit, StartTime, EndTime);
|
2025-09-17 13:32:29 +08:00
|
|
|
|
}
|
2025-09-12 08:35:35 +08:00
|
|
|
|
}
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-12 08:35:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新加载历史记录命令
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private void Reload()
|
|
|
|
|
|
{
|
2025-09-17 14:13:48 +08:00
|
|
|
|
if (CurrentVariable != null)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-09-17 14:13:48 +08:00
|
|
|
|
LoadAllVariableHistories(CurrentVariable.Id, HistoryLimit, StartTime, EndTime);
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-17 13:32:29 +08:00
|
|
|
|
|
2025-09-12 08:35:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据搜索文本过滤历史记录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="searchText"></param>
|
|
|
|
|
|
private void FilterHistoriesBySearchText(string searchText)
|
2025-09-11 18:09:35 +08:00
|
|
|
|
{
|
2025-09-12 08:35:35 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(searchText))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果搜索文本为空,显示所有历史记录
|
|
|
|
|
|
_variableHistoryList.Clear();
|
|
|
|
|
|
_variableHistoryList.AddRange(_allVariableHistories);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 根据搜索文本过滤历史记录
|
|
|
|
|
|
var filteredHistories = _allVariableHistories
|
|
|
|
|
|
.Where(h =>
|
|
|
|
|
|
h.VariableName?.Contains(
|
|
|
|
|
|
searchText, StringComparison.OrdinalIgnoreCase) == true)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
_variableHistoryList.Clear();
|
|
|
|
|
|
_variableHistoryList.AddRange(filteredHistories);
|
|
|
|
|
|
}
|
2025-09-11 18:09:35 +08:00
|
|
|
|
}
|
2025-09-15 13:12:14 +08:00
|
|
|
|
|
2025-09-11 18:09:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据变量ID加载历史记录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableId">变量ID</param>
|
|
|
|
|
|
/// <param name="limit">返回记录的最大数量,null表示无限制</param>
|
|
|
|
|
|
/// <param name="startTime">开始时间,null表示无限制</param>
|
|
|
|
|
|
/// <param name="endTime">结束时间,null表示无限制</param>
|
2025-09-15 13:12:14 +08:00
|
|
|
|
public async Task LoadVariableHistoriesAsync(int variableId, int? limit = null, DateTime? startTime = null,
|
|
|
|
|
|
DateTime? endTime = null)
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-09-11 18:09:35 +08:00
|
|
|
|
try
|
2025-09-11 11:04:07 +08:00
|
|
|
|
{
|
2025-09-11 18:09:35 +08:00
|
|
|
|
_variableHistoryList.Clear();
|
|
|
|
|
|
var histories = await _historyAppService.GetVariableHistoriesAsync(variableId, limit, startTime, endTime);
|
|
|
|
|
|
_variableHistoryList.AddRange(histories);
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新图表数据
|
|
|
|
|
|
UpdateChartData();
|
2025-09-11 18:09:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 记录更详细的错误信息
|
|
|
|
|
|
_notificationService.ShowError($"加载变量历史记录失败: {ex.Message}", ex);
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
2025-10-03 12:39:37 +08:00
|
|
|
|
// 添加字段来保存轴实例,以保持缩放状态
|
|
|
|
|
|
private Axis[] _lineAxisXInstance;
|
|
|
|
|
|
private Axis[] _lineAxisYInstance;
|
|
|
|
|
|
|
2025-09-17 14:13:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新图表数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void UpdateChartData()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_variableHistoryList == null || _variableHistoryList.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 清空图表数据
|
2025-09-22 06:24:08 +08:00
|
|
|
|
LineSeriesCollection = new ISeries[0];
|
2025-09-17 14:13:48 +08:00
|
|
|
|
OnPropertyChanged(nameof(LineSeriesCollection));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 12:10:37 +08:00
|
|
|
|
// 如果系列集合为空或没有有效的系列,则重新创建
|
|
|
|
|
|
if (LineSeriesCollection == null || LineSeriesCollection.Length == 0)
|
2025-09-17 14:13:48 +08:00
|
|
|
|
{
|
2025-10-03 12:10:37 +08:00
|
|
|
|
// 创建数值点集合
|
|
|
|
|
|
var values = new List<DateTimePoint>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var history in _variableHistoryList)
|
2025-09-17 14:13:48 +08:00
|
|
|
|
{
|
2025-10-03 12:39:37 +08:00
|
|
|
|
values.Add(new DateTimePoint(history.Timestamp, history.NumericValue));
|
2025-09-17 14:13:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 12:10:37 +08:00
|
|
|
|
// 创建线性序列
|
|
|
|
|
|
var series = new LineSeries<DateTimePoint>
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = CurrentVariable?.Name ?? "变量值",
|
|
|
|
|
|
Values = values,
|
|
|
|
|
|
Fill = null,
|
|
|
|
|
|
Stroke = new SolidColorPaint(new SKColor(41, 128, 185)) { StrokeThickness = 2 },
|
|
|
|
|
|
GeometrySize = 6, // 显示数据点,圆点大小为6
|
|
|
|
|
|
LineSmoothness = 0 // 使用直线连接点,也可以设为其他值实现曲线
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 更新序列集合
|
|
|
|
|
|
LineSeriesCollection = new ISeries[] { series };
|
2025-10-03 12:39:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化坐标轴并保存实例引用
|
|
|
|
|
|
_lineAxisXInstance = new Axis[]
|
|
|
|
|
|
{
|
|
|
|
|
|
new Axis
|
|
|
|
|
|
{
|
|
|
|
|
|
Labeler = value => new DateTime((long)value).ToString("MM-dd HH:mm:ss")
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
_lineAxisYInstance = new Axis[]
|
|
|
|
|
|
{
|
|
|
|
|
|
new Axis
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = CurrentVariable?.Name ?? "值",
|
|
|
|
|
|
// MinLimit = 0 // 设置Y轴从0开始
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 设置属性值
|
|
|
|
|
|
LineAxisX = _lineAxisXInstance;
|
|
|
|
|
|
LineAxisY = _lineAxisYInstance;
|
|
|
|
|
|
|
|
|
|
|
|
// 通知属性更改
|
|
|
|
|
|
OnPropertyChanged(nameof(LineSeriesCollection));
|
|
|
|
|
|
OnPropertyChanged(nameof(LineAxisX));
|
|
|
|
|
|
OnPropertyChanged(nameof(LineAxisY));
|
2025-10-03 12:10:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2025-09-17 14:13:48 +08:00
|
|
|
|
{
|
2025-10-03 12:10:37 +08:00
|
|
|
|
// 对于实时更新,保持原有完整的更新逻辑以确保数据一致性
|
|
|
|
|
|
// 创建数值点集合
|
|
|
|
|
|
var values = new List<DateTimePoint>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var history in _variableHistoryList)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 尝试将值转换为double
|
2025-10-03 12:39:37 +08:00
|
|
|
|
values.Add(new DateTimePoint(history.Timestamp, history.NumericValue));
|
2025-10-03 12:10:37 +08:00
|
|
|
|
}
|
2025-09-17 14:13:48 +08:00
|
|
|
|
|
2025-10-03 12:10:37 +08:00
|
|
|
|
// 更新当前系列
|
|
|
|
|
|
var currentSeries = (LineSeries<DateTimePoint>)LineSeriesCollection[0];
|
|
|
|
|
|
currentSeries.Values = values;
|
|
|
|
|
|
currentSeries.Name = CurrentVariable?.Name ?? "变量值";
|
2025-10-03 12:39:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 通知系列更改,但保留当前轴的缩放状态(不需要更新轴)
|
|
|
|
|
|
OnPropertyChanged(nameof(LineSeriesCollection));
|
2025-10-03 12:10:37 +08:00
|
|
|
|
}
|
2025-09-17 14:13:48 +08:00
|
|
|
|
}
|
2025-09-11 11:04:07 +08:00
|
|
|
|
}
|