From 2dda2029bdd578f4a484cf9681be234c23afb412 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 1 Oct 2025 18:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=201.=20=E9=87=8D=E6=9E=84=20DeviceManagementSe?= =?UTF-8?q?rvice=20=E7=B1=BB=EF=BC=9A=20=20=20=20=20=20=20-=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20CreateDeviceWithDetailsAsync=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8=E6=95=B0=E6=8D=AE=E5=BA=93=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=90=8E=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0=E5=86=85=E5=AD=98=20=20=20=20=20=20=20-=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20UpdateDeviceAsync=20=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=9C=A8=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9B=B4=E6=96=B0=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=90=8E=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E5=86=85?= =?UTF-8?q?=E5=AD=98=20=20=20=20=20=20=20-=20=E4=BF=AE=E6=94=B9=20DeleteDe?= =?UTF-8?q?viceByIdAsync=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=9C=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=88=A0=E9=99=A4=E6=88=90=E5=8A=9F=E5=90=8E?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BB=8E=E5=86=85=E5=AD=98=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=20=20=20=20=20=20=20-=20=E4=BF=AE=E6=94=B9=20ToggleDeviceActiv?= =?UTF-8?q?eStateAsync=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=9C=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=88=87=E6=8D=A2=E6=BF=80=E6=B4=BB=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E5=90=8E=E6=9B=B4=E6=96=B0=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2. 更新 DeviceDataService 类: - 移除了在 AddDevice 方法中的独立 AddDeviceToMemory 调用 - 移除了在 DeleteDevice 方法中的独立 RemoveDeviceFromMemory 调用 - 为 UpdateDevice 方法添加了注释,说明内存自动更新 3. 更新 DeviceMonitoringService 类: - 在 OnDeviceActiveChanged 方法中使用 Task 运行异步的 UpdateDeviceAsync 调用 4. 更新接口文档: - 在 IDeviceManagementService 接口中更新了内存操作方法的注释,说明通常由其他操作自动调用 --- .../Management/IDeviceManagementService.cs | 3 ++ .../Management/DeviceManagementService.cs | 38 +++++++++++++++++-- .../Monitoring/DeviceMonitoringService.cs | 6 ++- DMS.WPF/Services/DeviceDataService.cs | 7 +--- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs index 60ae52f..3354ceb 100644 --- a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs +++ b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs @@ -42,16 +42,19 @@ public interface IDeviceManagementService /// /// 在内存中添加设备 + /// 注意:此方法通常由其他操作(如CreateDeviceWithDetailsAsync)自动调用,一般不需要直接调用 /// void AddDeviceToMemory(DeviceDto deviceDto); /// /// 在内存中更新设备 + /// 注意:此方法通常由其他操作(如UpdateDeviceAsync)自动调用,一般不需要直接调用 /// void UpdateDeviceInMemory(DeviceDto deviceDto); /// /// 在内存中删除设备 + /// 注意:此方法通常由其他操作(如DeleteDeviceByIdAsync)自动调用,一般不需要直接调用 /// void RemoveDeviceFromMemory(int deviceId); diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs index c444cf7..6a97871 100644 --- a/DMS.Application/Services/Management/DeviceManagementService.cs +++ b/DMS.Application/Services/Management/DeviceManagementService.cs @@ -47,7 +47,15 @@ public class DeviceManagementService : IDeviceManagementService /// public async Task CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto) { - return await _deviceAppService.CreateDeviceWithDetailsAsync(dto); + var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto); + + // 创建成功后,将设备添加到内存中 + if (result?.Device != null) + { + AddDeviceToMemory(result.Device); + } + + return result; } /// @@ -55,7 +63,15 @@ public class DeviceManagementService : IDeviceManagementService /// public async Task UpdateDeviceAsync(DeviceDto deviceDto) { - return await _deviceAppService.UpdateDeviceAsync(deviceDto); + var result = await _deviceAppService.UpdateDeviceAsync(deviceDto); + + // 更新成功后,更新内存中的设备 + if (result > 0 && deviceDto != null) + { + UpdateDeviceInMemory(deviceDto); + } + + return result; } /// @@ -63,7 +79,16 @@ public class DeviceManagementService : IDeviceManagementService /// public async Task 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; } /// @@ -72,6 +97,13 @@ public class DeviceManagementService : IDeviceManagementService public async Task ToggleDeviceActiveStateAsync(int id) { await _deviceAppService.ToggleDeviceActiveStateAsync(id); + + // 更新内存中的设备状态 + var device = await _deviceAppService.GetDeviceByIdAsync(id); + if (device != null) + { + UpdateDeviceInMemory(device); + } } /// diff --git a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs index 2f48abd..7dd8884 100644 --- a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs +++ b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs @@ -40,7 +40,11 @@ public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable { if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device)) { - _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device); + // 更新设备激活状态 - 同时更新数据库和内存 + _ = Task.Run(async () => + { + await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device); + }); } } diff --git a/DMS.WPF/Services/DeviceDataService.cs b/DMS.WPF/Services/DeviceDataService.cs index 269b929..480ed2a 100644 --- a/DMS.WPF/Services/DeviceDataService.cs +++ b/DMS.WPF/Services/DeviceDataService.cs @@ -103,8 +103,6 @@ public class DeviceDataService : IDeviceDataService //给界面添加设备 _dataStorageService.Devices.Add(addDto.Device.Id, _mapper.Map(addDto.Device)); - //更新数据中心 - _appDataCenterService.DeviceManagementService.AddDeviceToMemory(addDto.Device); // 给界面添加设备菜单 if (addDto.DeviceMenu != null) @@ -144,13 +142,11 @@ public class DeviceDataService : IDeviceDataService public async Task DeleteDevice(DeviceItemViewModel device) { - //从数据库删除设备相关数据 + //从数据库和内存中删除设备相关数据 if (!await _appDataCenterService.DeviceManagementService.DeleteDeviceByIdAsync(device.Id)) { return false; } - //从Application项目删除设备相关数据 - _appDataCenterService.DeviceManagementService.RemoveDeviceFromMemory(device.Id); // 从界面删除设备相关数据集 @@ -184,6 +180,7 @@ public class DeviceDataService : IDeviceDataService _mapper.Map(device, deviceDto); if (await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(deviceDto) > 0) { + // 更新数据库后会自动更新内存,无需额外操作 return true; }