Files
DMS/DMS.WPF/Services/DeviceViewService.cs

205 lines
6.9 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using System.Windows.Threading;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
2025-10-01 18:02:33 +08:00
using DMS.Application.Events;
using DMS.Application.Interfaces;
2025-09-19 07:27:56 +08:00
using DMS.Core.Enums;
2025-09-16 14:42:23 +08:00
using DMS.Core.Events;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
2025-10-20 22:47:22 +08:00
using DMS.WPF.ViewModels;
namespace DMS.WPF.Services;
/// <summary>
/// 设备数据服务类,负责管理设备相关的数据和操作。
/// </summary>
2025-10-20 22:47:22 +08:00
public class DeviceViewService : IDeviceDataService
{
private readonly IMapper _mapper;
private readonly IAppCenterService _appCenterService;
private readonly IAppStorageService _appStorageService;
2025-10-20 19:39:17 +08:00
private readonly IWpfDataService _dataStorageService;
2025-09-19 07:27:56 +08:00
private readonly IVariableTableDataService _variableTableDataService;
private readonly IEventService _eventService;
private readonly INotificationService _notificationService;
2025-10-20 22:47:22 +08:00
private readonly IMenuViewService _menuDataService;
private readonly IVariableDataService _variableDataService;
private readonly Dispatcher _uiDispatcher;
/// <summary>
/// DeviceDataService类的构造函数。
/// </summary>
/// <param name="mapper">AutoMapper 实例。</param>
/// <param name="appCenterService">数据服务中心实例。</param>
2025-10-20 22:47:22 +08:00
public DeviceViewService(IMapper mapper, IAppCenterService appCenterService,
IAppStorageService appStorageService, IWpfDataService dataStorageService,
IVariableTableDataService variableTableDataService,
IEventService eventService, INotificationService notificationService,
2025-10-20 22:47:22 +08:00
IMenuViewService menuDataService, IVariableDataService variableDataService)
{
_mapper = mapper;
_appCenterService = appCenterService;
_appStorageService = appStorageService;
_dataStorageService = dataStorageService;
2025-09-19 07:27:56 +08:00
_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},已断开连接。");
}
}
});
}
}
/// <summary>
/// 加载所有设备数据。
/// </summary>
public void LoadAllDevices()
{
foreach (var device in _appStorageService.Devices.Values)
{
_dataStorageService.Devices.Add(device.Id, _mapper.Map<DeviceItem>(device));
}
}
/// <summary>
/// 添加设备。
/// </summary>
2025-10-20 22:47:22 +08:00
public async Task<CreateDeviceWithDetailsDto?> AddDeviceAsync(CreateDeviceWithDetailsDto dto)
{
2025-09-14 19:58:18 +08:00
// 添加null检查
2025-10-20 19:39:17 +08:00
if (dto is null) return null;
2025-10-20 22:47:22 +08:00
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;
}
2025-09-14 19:58:18 +08:00
var addDto = await _appCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
2025-09-14 09:03:07 +08:00
// 添加null检查
2025-10-20 19:39:17 +08:00
if (addDto is null) return null;
//给界面添加设备
_dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map<DeviceItem>(addDto.Device));
// 给界面添加设备菜单
if (addDto.DeviceMenu != null)
2025-09-14 19:58:18 +08:00
{
2025-10-20 19:39:17 +08:00
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(addDto.DeviceMenu));
2025-09-14 19:58:18 +08:00
}
// 添加变量表和变量表菜单
if (addDto.VariableTable != null)
2025-09-14 09:03:07 +08:00
{
await _variableDataService.AddVariableTableToView(addDto.VariableTable);
// 添加变量表到内存的操作现在在服务内部完成,无需额外调用
if (addDto.VariableTable != null && addDto.VariableTableMenu != null)
{
2025-10-20 19:39:17 +08:00
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(addDto.VariableTableMenu));
}
2025-09-14 09:03:07 +08:00
}
2025-10-20 19:39:17 +08:00
return addDto;
}
/// <summary>
/// 删除设备。
/// </summary>
2025-10-20 22:47:22 +08:00
public async Task<bool> DeleteDevice(DeviceItem deviceItem)
{
2025-10-20 19:39:17 +08:00
2025-10-20 22:47:22 +08:00
if (!_appStorageService.Devices.TryGetValue(deviceItem.Id,out var device))
{
return false;
}
//从数据库和内存中删除设备相关数据
2025-10-20 22:47:22 +08:00
bool res = await _appCenterService.DeviceManagementService.DeleteAsync(device);
if (!res)
{
return false;
}
2025-10-20 19:39:17 +08:00
2025-09-19 07:27:56 +08:00
// 从界面删除设备相关数据集
2025-10-20 22:47:22 +08:00
var variableTablesCopy = deviceItem.VariableTables.ToList();
2025-10-01 18:02:33 +08:00
foreach (var variableTable in variableTablesCopy)
2025-09-19 07:27:56 +08:00
{
2025-10-20 19:39:17 +08:00
await _variableTableDataService.DeleteVariableTable(variableTable);
2025-09-19 07:27:56 +08:00
}
2025-10-20 22:47:22 +08:00
var deviceMenu
= _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu &&
m.TargetId == deviceItem.Id);
2025-09-19 07:27:56 +08:00
if (deviceMenu != null)
{
2025-10-20 19:39:17 +08:00
await _menuDataService.DeleteMenuItem(deviceMenu);
2025-09-19 07:27:56 +08:00
}
2025-10-20 22:47:22 +08:00
_dataStorageService.Devices.Remove(deviceItem.Id);
2025-10-20 19:39:17 +08:00
2025-09-19 07:27:56 +08:00
return true;
}
/// <summary>
/// 更新设备。
/// </summary>
public async Task<bool> UpdateDevice(DeviceItem device)
{
if (!_appStorageService.Devices.TryGetValue(device.Id, out var existingDevice))
{
return false;
}
_mapper.Map(device, existingDevice);
if (await _appCenterService.DeviceManagementService.UpdateDeviceAsync(existingDevice) > 0)
{
// 更新数据库后会自动更新内存,无需额外操作
return true;
}
return false;
}
}