1. 重构 DeviceManagementService 类:
- 修改 CreateDeviceWithDetailsAsync 方法,在数据库创建成功后自动添加到内存
- 修改 UpdateDeviceAsync 方法,在数据库更新成功后自动更新内存
- 修改 DeleteDeviceByIdAsync 方法,在数据库删除成功后自动从内存移除
- 修改 ToggleDeviceActiveStateAsync 方法,在数据库切换激活状态后更新内存
2. 更新 DeviceDataService 类:
- 移除了在 AddDevice 方法中的独立 AddDeviceToMemory 调用
- 移除了在 DeleteDevice 方法中的独立 RemoveDeviceFromMemory 调用
- 为 UpdateDevice 方法添加了注释,说明内存自动更新
3. 更新 DeviceMonitoringService 类:
- 在 OnDeviceActiveChanged 方法中使用 Task 运行异步的 UpdateDeviceAsync 调用
4. 更新接口文档:
- 在 IDeviceManagementService 接口中更新了内存操作方法的注释,说明通常由其他操作自动调用
This commit is contained in:
@@ -42,16 +42,19 @@ public interface IDeviceManagementService
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 在内存中添加设备
|
/// 在内存中添加设备
|
||||||
|
/// 注意:此方法通常由其他操作(如CreateDeviceWithDetailsAsync)自动调用,一般不需要直接调用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void AddDeviceToMemory(DeviceDto deviceDto);
|
void AddDeviceToMemory(DeviceDto deviceDto);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 在内存中更新设备
|
/// 在内存中更新设备
|
||||||
|
/// 注意:此方法通常由其他操作(如UpdateDeviceAsync)自动调用,一般不需要直接调用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void UpdateDeviceInMemory(DeviceDto deviceDto);
|
void UpdateDeviceInMemory(DeviceDto deviceDto);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 在内存中删除设备
|
/// 在内存中删除设备
|
||||||
|
/// 注意:此方法通常由其他操作(如DeleteDeviceByIdAsync)自动调用,一般不需要直接调用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void RemoveDeviceFromMemory(int deviceId);
|
void RemoveDeviceFromMemory(int deviceId);
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,15 @@ public class DeviceManagementService : IDeviceManagementService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
||||||
{
|
{
|
||||||
return await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
||||||
|
|
||||||
|
// 创建成功后,将设备添加到内存中
|
||||||
|
if (result?.Device != null)
|
||||||
|
{
|
||||||
|
AddDeviceToMemory(result.Device);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -55,7 +63,15 @@ public class DeviceManagementService : IDeviceManagementService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<int> UpdateDeviceAsync(DeviceDto deviceDto)
|
public async Task<int> UpdateDeviceAsync(DeviceDto deviceDto)
|
||||||
{
|
{
|
||||||
return await _deviceAppService.UpdateDeviceAsync(deviceDto);
|
var result = await _deviceAppService.UpdateDeviceAsync(deviceDto);
|
||||||
|
|
||||||
|
// 更新成功后,更新内存中的设备
|
||||||
|
if (result > 0 && deviceDto != null)
|
||||||
|
{
|
||||||
|
UpdateDeviceInMemory(deviceDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -63,7 +79,16 @@ public class DeviceManagementService : IDeviceManagementService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
||||||
{
|
{
|
||||||
return await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
|
var device = await _deviceAppService.GetDeviceByIdAsync(deviceId); // 获取设备信息用于内存删除
|
||||||
|
var result = await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
|
||||||
|
|
||||||
|
// 删除成功后,从内存中移除设备
|
||||||
|
if (result && device != null)
|
||||||
|
{
|
||||||
|
RemoveDeviceFromMemory(deviceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -72,6 +97,13 @@ public class DeviceManagementService : IDeviceManagementService
|
|||||||
public async Task ToggleDeviceActiveStateAsync(int id)
|
public async Task ToggleDeviceActiveStateAsync(int id)
|
||||||
{
|
{
|
||||||
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
|
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
|
||||||
|
|
||||||
|
// 更新内存中的设备状态
|
||||||
|
var device = await _deviceAppService.GetDeviceByIdAsync(id);
|
||||||
|
if (device != null)
|
||||||
|
{
|
||||||
|
UpdateDeviceInMemory(device);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -40,7 +40,11 @@ public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable
|
|||||||
{
|
{
|
||||||
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
|
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
|
||||||
{
|
{
|
||||||
_appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device);
|
// 更新设备激活状态 - 同时更新数据库和内存
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,8 +103,6 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
|
|
||||||
//给界面添加设备
|
//给界面添加设备
|
||||||
_dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map<DeviceItemViewModel>(addDto.Device));
|
_dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map<DeviceItemViewModel>(addDto.Device));
|
||||||
//更新数据中心
|
|
||||||
_appDataCenterService.DeviceManagementService.AddDeviceToMemory(addDto.Device);
|
|
||||||
|
|
||||||
// 给界面添加设备菜单
|
// 给界面添加设备菜单
|
||||||
if (addDto.DeviceMenu != null)
|
if (addDto.DeviceMenu != null)
|
||||||
@@ -144,13 +142,11 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
public async Task<bool> DeleteDevice(DeviceItemViewModel device)
|
public async Task<bool> DeleteDevice(DeviceItemViewModel device)
|
||||||
{
|
{
|
||||||
|
|
||||||
//从数据库删除设备相关数据
|
//从数据库和内存中删除设备相关数据
|
||||||
if (!await _appDataCenterService.DeviceManagementService.DeleteDeviceByIdAsync(device.Id))
|
if (!await _appDataCenterService.DeviceManagementService.DeleteDeviceByIdAsync(device.Id))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//从Application项目删除设备相关数据
|
|
||||||
_appDataCenterService.DeviceManagementService.RemoveDeviceFromMemory(device.Id);
|
|
||||||
|
|
||||||
|
|
||||||
// 从界面删除设备相关数据集
|
// 从界面删除设备相关数据集
|
||||||
@@ -184,6 +180,7 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
_mapper.Map(device, deviceDto);
|
_mapper.Map(device, deviceDto);
|
||||||
if (await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(deviceDto) > 0)
|
if (await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(deviceDto) > 0)
|
||||||
{
|
{
|
||||||
|
// 更新数据库后会自动更新内存,无需额外操作
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user