2025-09-09 13:35:16 +08:00
|
|
|
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;
|
2025-09-15 20:54:32 +08:00
|
|
|
using DMS.Core.Events;
|
2025-09-09 13:35:16 +08:00
|
|
|
using DMS.Core.Models;
|
|
|
|
|
using DMS.Message;
|
|
|
|
|
using DMS.WPF.Interfaces;
|
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据事件服务类,负责处理数据变更事件和消息传递。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DataEventService : IDataEventService
|
|
|
|
|
{
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-09-15 20:54:32 +08:00
|
|
|
private readonly IEventService _eventService;
|
2025-09-09 13:35:16 +08:00
|
|
|
private readonly IAppDataCenterService _appDataCenterService;
|
|
|
|
|
private readonly IWPFDataService _wpfDataService;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// DataEventService类的构造函数。
|
|
|
|
|
/// </summary>
|
2025-09-14 16:16:10 +08:00
|
|
|
public DataEventService(IMapper mapper,
|
|
|
|
|
IDataStorageService dataStorageService,
|
2025-09-15 20:54:32 +08:00
|
|
|
IEventService eventService,
|
2025-09-14 16:16:10 +08:00
|
|
|
IAppDataCenterService appDataCenterService,
|
2025-09-15 13:12:14 +08:00
|
|
|
IWPFDataService wpfDataService)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
_dataStorageService = dataStorageService;
|
2025-09-15 20:54:32 +08:00
|
|
|
_eventService = eventService;
|
2025-09-09 13:35:16 +08:00
|
|
|
_appDataCenterService = appDataCenterService;
|
|
|
|
|
_wpfDataService = wpfDataService;
|
|
|
|
|
|
|
|
|
|
// 监听变量值变更事件
|
2025-09-15 20:54:32 +08:00
|
|
|
_eventService.OnVariableValueChanged += OnVariableValueChanged;
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted;
|
2025-09-09 13:35:16 +08:00
|
|
|
// 监听日志变更事件
|
2025-09-09 15:28:07 +08:00
|
|
|
// _appDataCenterService.OnLogChanged += _logDataService.OnNlogChanged;
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理变量值变更事件。
|
|
|
|
|
/// </summary>
|
2025-09-15 13:12:14 +08:00
|
|
|
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
// 在UI线程上更新变量值
|
|
|
|
|
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
// 查找并更新对应的变量
|
2025-09-09 15:57:04 +08:00
|
|
|
var variableToUpdate = _dataStorageService.Variables.FirstOrDefault(v => v.Id == e.VariableId);
|
2025-09-09 13:35:16 +08:00
|
|
|
if (variableToUpdate != null)
|
|
|
|
|
{
|
2025-09-09 15:57:04 +08:00
|
|
|
variableToUpdate.DataValue = e.NewValue;
|
|
|
|
|
variableToUpdate.DisplayValue = e.NewValue;
|
|
|
|
|
variableToUpdate.UpdatedAt = e.UpdateTime;
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理LoadMessage消息。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task Receive(LoadMessage message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|