2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Application.DTOs;
|
2025-09-16 14:42:23 +08:00
|
|
|
using DMS.Application.Events;
|
2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
using DMS.Application.Interfaces.Database;
|
|
|
|
|
using DMS.Application.Interfaces.Management;
|
|
|
|
|
using DMS.Core.Enums;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
namespace DMS.Application.Services.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备管理服务,负责设备相关的业务逻辑。
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public class DeviceManagementService : IDeviceManagementService
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
private readonly IDeviceAppService _deviceAppService;
|
2025-09-09 15:28:07 +08:00
|
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当设备数据发生变化时触发
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public event EventHandler<DeviceChangedEventArgs> OnDeviceChanged;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
public DeviceManagementService(IDeviceAppService deviceAppService,IAppDataStorageService appDataStorageService)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
_deviceAppService = deviceAppService;
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService = appDataStorageService;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步根据ID获取设备DTO。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<DeviceDto> GetDeviceByIdAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
return await _deviceAppService.GetDeviceByIdAsync(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步获取所有设备DTO列表。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<List<DeviceDto>> GetAllDevicesAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _deviceAppService.GetAllDevicesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
|
|
|
|
{
|
2025-10-01 18:09:30 +08:00
|
|
|
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
|
|
|
|
|
|
|
|
|
// 创建成功后,将设备添加到内存中
|
|
|
|
|
if (result?.Device != null)
|
|
|
|
|
{
|
|
|
|
|
AddDeviceToMemory(result.Device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步更新一个已存在的设备。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<int> UpdateDeviceAsync(DeviceDto deviceDto)
|
|
|
|
|
{
|
2025-10-01 18:09:30 +08:00
|
|
|
var result = await _deviceAppService.UpdateDeviceAsync(deviceDto);
|
|
|
|
|
|
|
|
|
|
// 更新成功后,更新内存中的设备
|
|
|
|
|
if (result > 0 && deviceDto != null)
|
|
|
|
|
{
|
|
|
|
|
UpdateDeviceInMemory(deviceDto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步删除一个设备。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
|
|
|
|
{
|
2025-10-01 18:09:30 +08:00
|
|
|
var device = await _deviceAppService.GetDeviceByIdAsync(deviceId); // 获取设备信息用于内存删除
|
|
|
|
|
var result = await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
|
|
|
|
|
|
|
|
|
|
// 删除成功后,从内存中移除设备
|
|
|
|
|
if (result && device != null)
|
|
|
|
|
{
|
|
|
|
|
RemoveDeviceFromMemory(deviceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步切换设备的激活状态。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task ToggleDeviceActiveStateAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
|
2025-10-01 18:09:30 +08:00
|
|
|
|
|
|
|
|
// 更新内存中的设备状态
|
|
|
|
|
var device = await _deviceAppService.GetDeviceByIdAsync(id);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
|
|
|
|
UpdateDeviceInMemory(device);
|
|
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在内存中添加设备
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public void AddDeviceToMemory(DeviceDto deviceDto)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
if (_appDataStorageService.Devices.TryAdd(deviceDto.Id, deviceDto))
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Added, deviceDto));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在内存中更新设备
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UpdateDeviceInMemory(DeviceDto deviceDto)
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.Devices.AddOrUpdate(deviceDto.Id, deviceDto, (key, oldValue) => deviceDto);
|
|
|
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Updated, deviceDto));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在内存中删除设备
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public void RemoveDeviceFromMemory(int deviceId)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
if (_appDataStorageService.Devices.TryGetValue(deviceId, out var deviceDto))
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
foreach (var variableTable in deviceDto.VariableTables)
|
|
|
|
|
{
|
|
|
|
|
foreach (var variable in variableTable.Variables)
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.Variables.TryRemove(variable.Id, out _);
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.VariableTables.TryRemove(variableTable.Id, out _);
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.Devices.TryRemove(deviceId, out _);
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Deleted, deviceDto));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|