using System.Collections.ObjectModel; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using DMS.Application.DTOs; using DMS.Application.DTOs.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.Core.Models; using DMS.Message; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Items; namespace DMS.WPF.Services; /// /// 数据事件服务类,负责处理数据变更事件和消息传递。 /// public class DataEventService : IDataEventService { private readonly IMapper _mapper; private readonly IDataStorageService _dataStorageService; private readonly IAppDataCenterService _appDataCenterService; private readonly IWPFDataService _wpfDataService; /// /// DataEventService类的构造函数。 /// public DataEventService(IMapper mapper,IDataStorageService dataStorageService, IAppDataCenterService appDataCenterService,IWPFDataService wpfDataService ) { _mapper = mapper; _dataStorageService = dataStorageService; _appDataCenterService = appDataCenterService; _wpfDataService = wpfDataService; // 监听变量值变更事件 _appDataCenterService.VariableValueChanged += OnVariableValueChanged; _appDataCenterService.OnLoadDataCompleted += OnLoadDataCompleted; // 监听日志变更事件 // _appDataCenterService.NlogChanged += _logDataService.OnNlogChanged; } private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e) { if (e.IsSuccess) { _wpfDataService.DeviceDataService.LoadAllDevices(); _wpfDataService.VariableTableDataService.LoadAllVariableTables(); _wpfDataService.VariableDataService.LoadAllVariables(); _wpfDataService.MenuDataService.LoadAllMenus(); _wpfDataService.MqttDataService.LoadMqttServers(); _wpfDataService.LogDataService.LoadAllLog(); } } /// /// 处理变量值变更事件。 /// private void OnVariableValueChanged(object sender, VariableValueChangedEventArgs e) { // 在UI线程上更新变量值 App.Current.Dispatcher.BeginInvoke(new Action(() => { // 查找并更新对应的变量 var variableToUpdate = _dataStorageService.Variables.FirstOrDefault(v => v.Id == e.VariableId); if (variableToUpdate != null) { variableToUpdate.DataValue = e.NewValue; variableToUpdate.DisplayValue = e.NewValue; variableToUpdate.UpdatedAt = e.UpdateTime; } })); } /// /// 处理LoadMessage消息。 /// public async Task Receive(LoadMessage message) { } }