refactor:完成重构设备的添加,删除,更新。

This commit is contained in:
2025-10-22 14:06:16 +08:00
parent e995ec7207
commit 54d040b45f
76 changed files with 1028 additions and 1161 deletions

View File

@@ -15,15 +15,15 @@ namespace DMS.Application.Services.Management;
public class MqttManagementService : IMqttManagementService
{
private readonly IMqttAppService _mqttAppService;
private readonly IAppStorageService _appStorageService;
private readonly IAppDataService _appDataService;
private readonly IEventService _eventService;
public MqttManagementService(IMqttAppService mqttAppService,
IAppStorageService appStorageService,
IAppDataService appStorageService,
IEventService eventService)
{
_mqttAppService = mqttAppService;
_appStorageService = appStorageService;
_appDataService = appStorageService;
_eventService = eventService;
}
@@ -32,7 +32,7 @@ public class MqttManagementService : IMqttManagementService
/// </summary>
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
{
if (_appStorageService.MqttServers.TryGetValue(id,out var mqttServer))
if (_appDataService.MqttServers.TryGetValue(id,out var mqttServer))
{
return mqttServer;
}
@@ -44,7 +44,7 @@ public class MqttManagementService : IMqttManagementService
/// </summary>
public async Task<List<MqttServer>> GetAllMqttServersAsync()
{
return _appStorageService.MqttServers.Values.ToList();
return _appDataService.MqttServers.Values.ToList();
}
/// <summary>
@@ -67,7 +67,7 @@ public class MqttManagementService : IMqttManagementService
{
foreach (var mqttServer in mqttServers)
{
if (_appStorageService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
if (_appDataService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
{
// 比较旧值和新值,确定哪个属性发生了变化
var changedProperties = GetChangedProperties(mMqttServer, mqttServer);
@@ -99,7 +99,7 @@ public class MqttManagementService : IMqttManagementService
else
{
// 如果内存中不存在该MQTT服务器则直接添加
_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
_appDataService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer, MqttServerPropertyType.All));
}
@@ -120,7 +120,7 @@ public class MqttManagementService : IMqttManagementService
// 删除成功后从内存中移除MQTT服务器
if (result && mqttServer != null)
{
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
if (_appDataService.MqttServers.TryRemove(id, out var mqttServerFromCache))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerFromCache));
@@ -142,7 +142,7 @@ public class MqttManagementService : IMqttManagementService
// {
// foreach (var id in ids)
// {
// if (_appStorageService.MqttServers.TryRemove(id, out var mqttServer))
// if (_appDataService.MqttServers.TryRemove(id, out var mqttServer))
// {
// _eventService.RaiseMqttServerChanged(
// this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
@@ -168,7 +168,7 @@ public class MqttManagementService : IMqttManagementService
// 将MQTT服务器添加到内存中
if (_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
if (_appDataService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
{
_eventService.RaiseMqttServerChanged(
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer));
@@ -240,12 +240,12 @@ public class MqttManagementService : IMqttManagementService
/// </summary>
public async Task LoadAllMqttServersAsync()
{
_appStorageService.MqttServers.Clear();
_appDataService.MqttServers.Clear();
var mqttServers = await _mqttAppService.GetAllMqttServersAsync();
// 加载MQTT服务器数据到内存
foreach (var mqttServer in mqttServers)
{
_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
_appDataService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
}
}
}