- 修改 CreateDeviceWithDetailsAsync 方法,在数据库创建成功后自动添加到内存
- 修改 UpdateDeviceAsync 方法,在数据库更新成功后自动更新内存
- 修改 DeleteDeviceByIdAsync 方法,在数据库删除成功后自动从内存移除
- 修改 ToggleDeviceActiveStateAsync 方法,在数据库切换激活状态后更新内存
2. 更新 DeviceDataService 类:
- 移除了在 AddDevice 方法中的独立 AddDeviceToMemory 调用
- 移除了在 DeleteDevice 方法中的独立 RemoveDeviceFromMemory 调用
- 为 UpdateDevice 方法添加了注释,说明内存自动更新
3. 更新 DeviceMonitoringService 类:
- 在 OnDeviceActiveChanged 方法中使用 Task 运行异步的 UpdateDeviceAsync 调用
4. 更新接口文档:
- 在 IDeviceManagementService 接口中更新了内存操作方法的注释,说明通常由其他操作自动调用
153 lines
4.7 KiB
C#
153 lines
4.7 KiB
C#
using DMS.Application.DTOs;
|
|
using DMS.Application.Events;
|
|
using DMS.Application.Interfaces;
|
|
using DMS.Application.Interfaces.Database;
|
|
using DMS.Application.Interfaces.Management;
|
|
using DMS.Core.Enums;
|
|
|
|
namespace DMS.Application.Services.Management;
|
|
|
|
/// <summary>
|
|
/// 设备管理服务,负责设备相关的业务逻辑。
|
|
/// </summary>
|
|
public class DeviceManagementService : IDeviceManagementService
|
|
{
|
|
private readonly IDeviceAppService _deviceAppService;
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
|
|
|
/// <summary>
|
|
/// 当设备数据发生变化时触发
|
|
/// </summary>
|
|
public event EventHandler<DeviceChangedEventArgs> OnDeviceChanged;
|
|
|
|
public DeviceManagementService(IDeviceAppService deviceAppService,IAppDataStorageService appDataStorageService)
|
|
{
|
|
_deviceAppService = deviceAppService;
|
|
_appDataStorageService = appDataStorageService;
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
|
|
|
// 创建成功后,将设备添加到内存中
|
|
if (result?.Device != null)
|
|
{
|
|
AddDeviceToMemory(result.Device);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步更新一个已存在的设备。
|
|
/// </summary>
|
|
public async Task<int> UpdateDeviceAsync(DeviceDto deviceDto)
|
|
{
|
|
var result = await _deviceAppService.UpdateDeviceAsync(deviceDto);
|
|
|
|
// 更新成功后,更新内存中的设备
|
|
if (result > 0 && deviceDto != null)
|
|
{
|
|
UpdateDeviceInMemory(deviceDto);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步删除一个设备。
|
|
/// </summary>
|
|
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
|
{
|
|
var device = await _deviceAppService.GetDeviceByIdAsync(deviceId); // 获取设备信息用于内存删除
|
|
var result = await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
|
|
|
|
// 删除成功后,从内存中移除设备
|
|
if (result && device != null)
|
|
{
|
|
RemoveDeviceFromMemory(deviceId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步切换设备的激活状态。
|
|
/// </summary>
|
|
public async Task ToggleDeviceActiveStateAsync(int id)
|
|
{
|
|
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
|
|
|
|
// 更新内存中的设备状态
|
|
var device = await _deviceAppService.GetDeviceByIdAsync(id);
|
|
if (device != null)
|
|
{
|
|
UpdateDeviceInMemory(device);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在内存中添加设备
|
|
/// </summary>
|
|
public void AddDeviceToMemory(DeviceDto deviceDto)
|
|
{
|
|
if (_appDataStorageService.Devices.TryAdd(deviceDto.Id, deviceDto))
|
|
{
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Added, deviceDto));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在内存中更新设备
|
|
/// </summary>
|
|
public void UpdateDeviceInMemory(DeviceDto deviceDto)
|
|
{
|
|
_appDataStorageService.Devices.AddOrUpdate(deviceDto.Id, deviceDto, (key, oldValue) => deviceDto);
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Updated, deviceDto));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 在内存中删除设备
|
|
/// </summary>
|
|
public void RemoveDeviceFromMemory(int deviceId)
|
|
{
|
|
if (_appDataStorageService.Devices.TryGetValue(deviceId, out var deviceDto))
|
|
{
|
|
foreach (var variableTable in deviceDto.VariableTables)
|
|
{
|
|
foreach (var variable in variableTable.Variables)
|
|
{
|
|
_appDataStorageService.Variables.TryRemove(variable.Id, out _);
|
|
}
|
|
|
|
_appDataStorageService.VariableTables.TryRemove(variableTable.Id, out _);
|
|
}
|
|
|
|
_appDataStorageService.Devices.TryRemove(deviceId, out _);
|
|
|
|
OnDeviceChanged?.Invoke(this,new DeviceChangedEventArgs(DataChangeType.Deleted, deviceDto));
|
|
}
|
|
}
|
|
|
|
|
|
} |