2025-09-09 13:35:16 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2025-09-12 17:22:15 +08:00
|
|
|
using System.Windows.Threading;
|
2025-09-09 13:35:16 +08:00
|
|
|
using AutoMapper;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using DMS.Application.DTOs;
|
2025-10-01 18:02:33 +08:00
|
|
|
using DMS.Application.Events;
|
2025-09-09 13:35:16 +08:00
|
|
|
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;
|
2025-09-09 13:35:16 +08:00
|
|
|
using DMS.WPF.Interfaces;
|
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备数据服务类,负责管理设备相关的数据和操作。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DeviceDataService : IDeviceDataService
|
|
|
|
|
{
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
private readonly IAppDataCenterService _appDataCenterService;
|
2025-09-09 15:28:07 +08:00
|
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
2025-09-09 13:35:16 +08:00
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-09-19 07:27:56 +08:00
|
|
|
private readonly IVariableTableDataService _variableTableDataService;
|
2025-09-12 17:22:15 +08:00
|
|
|
private readonly IEventService _eventService;
|
|
|
|
|
private readonly INotificationService _notificationService;
|
2025-09-09 13:35:16 +08:00
|
|
|
private readonly IMenuDataService _menuDataService;
|
|
|
|
|
private readonly IVariableDataService _variableDataService;
|
2025-09-12 17:22:15 +08:00
|
|
|
private readonly Dispatcher _uiDispatcher;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// DeviceDataService类的构造函数。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
|
|
|
|
/// <param name="appDataCenterService">数据服务中心实例。</param>
|
2025-09-12 17:22:15 +08:00
|
|
|
public DeviceDataService(IMapper mapper, IAppDataCenterService appDataCenterService,
|
2025-09-19 07:27:56 +08:00
|
|
|
IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService,IVariableTableDataService variableTableDataService,
|
2025-09-18 11:24:56 +08:00
|
|
|
IEventService eventService, INotificationService notificationService,
|
2025-09-12 17:22:15 +08:00
|
|
|
IMenuDataService menuDataService, IVariableDataService variableDataService)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
_appDataCenterService = appDataCenterService;
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService = appDataStorageService;
|
2025-09-09 13:35:16 +08:00
|
|
|
_dataStorageService = dataStorageService;
|
2025-09-19 07:27:56 +08:00
|
|
|
_variableTableDataService = variableTableDataService;
|
2025-09-12 17:22:15 +08:00
|
|
|
_eventService = eventService;
|
|
|
|
|
_notificationService = notificationService;
|
2025-09-09 13:35:16 +08:00
|
|
|
_menuDataService = menuDataService;
|
|
|
|
|
_variableDataService = variableDataService;
|
2025-09-12 17:22:15 +08:00
|
|
|
_uiDispatcher = Dispatcher.CurrentDispatcher;
|
|
|
|
|
|
2025-10-01 18:41:05 +08:00
|
|
|
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
|
2025-09-12 17:22:15 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:41:05 +08:00
|
|
|
private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e)
|
2025-09-12 17:22:15 +08:00
|
|
|
{
|
2025-10-01 18:41:05 +08:00
|
|
|
// 只处理连接状态变化
|
|
|
|
|
if (e.StateType == Core.Enums.DeviceStateType.Connection)
|
2025-09-12 17:22:15 +08:00
|
|
|
{
|
2025-10-01 18:41:05 +08:00
|
|
|
_uiDispatcher.Invoke(() =>
|
2025-09-12 17:22:15 +08:00
|
|
|
{
|
2025-09-18 11:24:56 +08:00
|
|
|
|
2025-10-01 18:41:05 +08:00
|
|
|
if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItemViewModel device))
|
2025-09-12 17:22:15 +08:00
|
|
|
{
|
2025-10-01 18:41:05 +08:00
|
|
|
|
|
|
|
|
device.IsRunning = e.StateValue;
|
|
|
|
|
if (device.IsRunning)
|
|
|
|
|
{
|
|
|
|
|
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_notificationService.ShowSuccess($"设备:{device.Name},已断开连接。");
|
|
|
|
|
}
|
2025-09-12 17:22:15 +08:00
|
|
|
}
|
2025-10-01 18:41:05 +08:00
|
|
|
});
|
|
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载所有设备数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LoadAllDevices()
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
foreach (var deviceDto in _appDataStorageService.Devices.Values)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
2025-09-18 11:24:56 +08:00
|
|
|
_dataStorageService.Devices.Add(deviceDto.Id, _mapper.Map<DeviceItemViewModel>(deviceDto));
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加设备。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<CreateDeviceWithDetailsDto> AddDevice(CreateDeviceWithDetailsDto dto)
|
|
|
|
|
{
|
2025-09-14 19:58:18 +08:00
|
|
|
// 添加null检查
|
2025-09-18 11:24:56 +08:00
|
|
|
if (dto == null)
|
2025-09-14 19:58:18 +08:00
|
|
|
return null;
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
var addDto = await _appDataCenterService.DeviceManagementService.CreateDeviceWithDetailsAsync(dto);
|
2025-09-18 11:24:56 +08:00
|
|
|
|
2025-09-14 09:03:07 +08:00
|
|
|
// 添加null检查
|
2025-09-18 11:24:56 +08:00
|
|
|
if (addDto == null && addDto.Device == null)
|
2025-09-14 19:58:18 +08:00
|
|
|
{
|
2025-09-18 11:24:56 +08:00
|
|
|
return null;
|
2025-09-14 19:58:18 +08:00
|
|
|
}
|
2025-09-18 11:24:56 +08:00
|
|
|
|
|
|
|
|
//给界面添加设备
|
|
|
|
|
_dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map<DeviceItemViewModel>(addDto.Device));
|
|
|
|
|
|
|
|
|
|
// 给界面添加设备菜单
|
|
|
|
|
if (addDto.DeviceMenu != null)
|
2025-09-14 19:58:18 +08:00
|
|
|
{
|
|
|
|
|
_menuDataService.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.DeviceMenu));
|
2025-09-18 11:24:56 +08:00
|
|
|
|
2025-09-14 19:58:18 +08:00
|
|
|
}
|
2025-09-18 11:24:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加变量表和变量表菜单
|
|
|
|
|
if (addDto.VariableTable != null)
|
2025-09-14 09:03:07 +08:00
|
|
|
{
|
2025-09-18 11:24:56 +08:00
|
|
|
await _variableDataService.AddVariableTableToView(addDto.VariableTable);
|
2025-10-01 19:16:47 +08:00
|
|
|
// 添加变量表到内存的操作现在在服务内部完成,无需额外调用
|
2025-09-18 11:24:56 +08:00
|
|
|
|
|
|
|
|
if (addDto.VariableTable != null && addDto.VariableTableMenu != null)
|
|
|
|
|
{
|
|
|
|
|
_menuDataService.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.VariableTableMenu));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-09-14 09:03:07 +08:00
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-10-01 18:02:33 +08:00
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
return addDto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除设备。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<bool> DeleteDevice(DeviceItemViewModel device)
|
|
|
|
|
{
|
2025-09-19 07:27:56 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
//从数据库和内存中删除设备相关数据
|
2025-09-09 15:28:07 +08:00
|
|
|
if (!await _appDataCenterService.DeviceManagementService.DeleteDeviceByIdAsync(device.Id))
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-09-19 07:27:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从界面删除设备相关数据集
|
2025-10-01 18:02:33 +08:00
|
|
|
var variableTablesCopy = device.VariableTables.ToList();
|
|
|
|
|
foreach (var variableTable in variableTablesCopy)
|
2025-09-19 07:27:56 +08:00
|
|
|
{
|
|
|
|
|
await _variableTableDataService.DeleteVariableTable(variableTable);
|
|
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-19 07:27:56 +08:00
|
|
|
var deviceMenu= _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
|
|
|
|
|
if (deviceMenu != null)
|
|
|
|
|
{
|
|
|
|
|
_menuDataService.DeleteMenuItem(deviceMenu);
|
|
|
|
|
}
|
|
|
|
|
_dataStorageService.Devices.Remove(device.Id);
|
2025-10-01 18:02:33 +08:00
|
|
|
|
2025-09-18 11:24:56 +08:00
|
|
|
|
2025-09-19 07:27:56 +08:00
|
|
|
return true;
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新设备。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<bool> UpdateDevice(DeviceItemViewModel device)
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
if (!_appDataStorageService.Devices.TryGetValue(device.Id, out var deviceDto))
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mapper.Map(device, deviceDto);
|
2025-09-09 15:28:07 +08:00
|
|
|
if (await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(deviceDto) > 0)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
2025-10-01 18:09:30 +08:00
|
|
|
// 更新数据库后会自动更新内存,无需额外操作
|
2025-09-09 13:35:16 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|