using System.Collections.ObjectModel;
using System.Windows.Threading;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Events;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
using DMS.WPF.ViewModels;
namespace DMS.WPF.Services;
///
/// 设备数据服务类,负责管理设备相关的数据和操作。
///
public class DeviceViewService : IDeviceDataService
{
private readonly IMapper _mapper;
private readonly IAppCenterService _appCenterService;
private readonly IAppStorageService _appStorageService;
private readonly IWpfDataService _dataStorageService;
private readonly IVariableTableDataService _variableTableDataService;
private readonly IEventService _eventService;
private readonly INotificationService _notificationService;
private readonly IMenuViewService _menuDataService;
private readonly IVariableDataService _variableDataService;
private readonly Dispatcher _uiDispatcher;
///
/// DeviceDataService类的构造函数。
///
/// AutoMapper 实例。
/// 数据服务中心实例。
public DeviceViewService(IMapper mapper, IAppCenterService appCenterService,
IAppStorageService appStorageService, IWpfDataService dataStorageService,
IVariableTableDataService variableTableDataService,
IEventService eventService, INotificationService notificationService,
IMenuViewService menuDataService, IVariableDataService variableDataService)
{
_mapper = mapper;
_appCenterService = appCenterService;
_appStorageService = appStorageService;
_dataStorageService = dataStorageService;
_variableTableDataService = variableTableDataService;
_eventService = eventService;
_notificationService = notificationService;
_menuDataService = menuDataService;
_variableDataService = variableDataService;
_uiDispatcher = Dispatcher.CurrentDispatcher;
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
}
private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e)
{
// 只处理连接状态变化
if (e.StateType == Core.Enums.DeviceStateType.Connection)
{
_uiDispatcher.Invoke(() =>
{
if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItem device))
{
device.IsRunning = e.StateValue;
if (device.IsRunning)
{
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
}
else
{
_notificationService.ShowSuccess($"设备:{device.Name},已断开连接。");
}
}
});
}
}
///
/// 加载所有设备数据。
///
public void LoadAllDevices()
{
foreach (var device in _appStorageService.Devices.Values)
{
_dataStorageService.Devices.Add(device.Id, _mapper.Map(device));
}
}
///
/// 添加设备。
///
public async Task AddDeviceAsync(CreateDeviceWithDetailsDto dto)
{
// 添加null检查
if (dto is null) return null;
if (dto.VariableTable is not null)
{
dto.VariableTable.Protocol = dto.Device.Protocol;
dto.VariableTableMenu.MenuType = MenuType.VariableTableMenu;
}
//查找到设备的根菜单
var parentMenu
= _appStorageService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == nameof(DevicesViewModel) &&
m.TargetId == 0);
if (parentMenu is not null)
{
dto.DeviceMenu.MenuType = MenuType.DeviceMenu;
dto.DeviceMenu.ParentId = parentMenu.Id;
}
var addDto = await _appCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
// 添加null检查
if (addDto is null) return null;
//给界面添加设备
_dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map(addDto.Device));
// 给界面添加设备菜单
if (addDto.DeviceMenu != null)
{
_menuDataService.AddMenuToView(_mapper.Map