diff --git a/DMS.Application/Interfaces/IDataLoaderService.cs b/DMS.Application/Interfaces/IDataLoaderService.cs index 851ca72..ed5c8e4 100644 --- a/DMS.Application/Interfaces/IDataLoaderService.cs +++ b/DMS.Application/Interfaces/IDataLoaderService.cs @@ -14,35 +14,5 @@ public interface IDataLoaderService /// Task LoadAllDataToMemoryAsync(); - /// - /// 异步加载所有设备数据 - /// - Task LoadAllDevicesAsync(); - - /// - /// 异步加载所有变量表数据 - /// - Task LoadAllVariableTablesAsync(); - - /// - /// 异步加载所有变量数据 - /// - Task LoadAllVariablesAsync(); - - /// - /// 异步加载所有菜单数据 - /// - Task LoadAllMenusAsync(); - - /// - /// 异步加载所有MQTT服务器数据 - /// - Task LoadAllMqttServersAsync(); - - /// - /// 异步加载所有日志数据 - /// - Task LoadAllNlogsAsync(int count); - } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs index 2d0cc5f..a906f8c 100644 --- a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs +++ b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs @@ -6,7 +6,7 @@ namespace DMS.Application.Interfaces.Management; public interface IDeviceManagementService { - + /// /// 异步根据ID获取设备DTO。 @@ -35,7 +35,9 @@ public interface IDeviceManagementService /// Task ToggleDeviceActiveStateAsync(int id); - + /// + /// 异步加载所有设备数据到内存中。 + /// + Task LoadAllDevicesAsync(); - } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/ILogManagementService.cs b/DMS.Application/Interfaces/Management/ILogManagementService.cs index d840b4f..8c5bec4 100644 --- a/DMS.Application/Interfaces/Management/ILogManagementService.cs +++ b/DMS.Application/Interfaces/Management/ILogManagementService.cs @@ -44,4 +44,9 @@ public interface ILogManagementService /// 当日志数据发生变化时触发 /// event EventHandler OnLogChanged; + + /// + /// 异步加载所有日志数据到内存中。 + /// + Task LoadAllNlogsAsync(int count); } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IMenuManagementService.cs b/DMS.Application/Interfaces/Management/IMenuManagementService.cs index 1c446ca..e344956 100644 --- a/DMS.Application/Interfaces/Management/IMenuManagementService.cs +++ b/DMS.Application/Interfaces/Management/IMenuManagementService.cs @@ -50,4 +50,9 @@ public interface IMenuManagementService /// 当菜单数据发生变化时触发 /// event EventHandler MenuChanged; + + /// + /// 异步加载所有菜单数据到内存中。 + /// + Task LoadAllMenusAsync(); } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IMqttManagementService.cs b/DMS.Application/Interfaces/Management/IMqttManagementService.cs index 76e59f3..a98aa6d 100644 --- a/DMS.Application/Interfaces/Management/IMqttManagementService.cs +++ b/DMS.Application/Interfaces/Management/IMqttManagementService.cs @@ -19,4 +19,9 @@ public interface IMqttManagementService Task GetMqttServerByIdAsync(int id); Task UpdateMqttServerAsync(MqttServer mqttServer); Task UpdateMqttServersAsync(List mqttServers); + + /// + /// 异步加载所有MQTT服务器数据到内存中。 + /// + Task LoadAllMqttServersAsync(); } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/ITriggerManagementService.cs b/DMS.Application/Interfaces/Management/ITriggerManagementService.cs index 57dd286..0f8fef7 100644 --- a/DMS.Application/Interfaces/Management/ITriggerManagementService.cs +++ b/DMS.Application/Interfaces/Management/ITriggerManagementService.cs @@ -49,5 +49,10 @@ namespace DMS.Application.Interfaces.Management /// 变量 ID /// 该变量关联的触发器定义列表 Task> GetTriggersForVariableAsync(int variableId); + + /// + /// 异步加载所有触发器数据 + /// + Task LoadAllTriggersAsync(); } } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IVariableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableManagementService.cs index a9a3ea9..beb6a51 100644 --- a/DMS.Application/Interfaces/Management/IVariableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableManagementService.cs @@ -50,4 +50,9 @@ public interface IVariableManagementService /// 查找已存在的变量。 /// Task> FindExistingVariablesAsync(IEnumerable variablesToCheck); + + /// + /// 异步加载所有变量数据到内存中。 + /// + Task LoadAllVariablesAsync(); } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs index d7d532a..8960a42 100644 --- a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs @@ -30,4 +30,9 @@ public interface IVariableTableManagementService /// 异步删除一个变量表。 /// Task DeleteVariableTableAsync(int id); + + /// + /// 异步加载所有变量表数据到内存中。 + /// + Task LoadAllVariableTablesAsync(); } \ No newline at end of file diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index 50b81c9..25a9bad 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -1,9 +1,6 @@ -using AutoMapper; using DMS.Application.Interfaces; -using DMS.Core.Interfaces; using System.Collections.Concurrent; using DMS.Application.Events; -using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; using DMS.Application.Services.Management; using DMS.Core.Models; @@ -17,48 +14,38 @@ namespace DMS.Application.Services; /// public class DataLoaderService : IDataLoaderService { - private readonly IRepositoryManager _repositoryManager; - private readonly IMapper _mapper; - private readonly IAppStorageService _appStorageService; - private readonly IDeviceAppService _deviceAppService; - private readonly IVariableTableAppService _variableTableAppService; - private readonly IVariableAppService _variableAppService; - private readonly IMenuAppService _menuService; - private readonly IMqttAppService _mqttAppService; - private readonly INlogAppService _nlogAppService; + private readonly IMqttManagementService _mqttManagementService; private readonly IMqttAliasManagementService _mqttAliasManagementService; private readonly ITriggerManagementService _triggerManagementService; // 添加触发器管理服务 private readonly IEventService _eventService; // 添加事件服务 + private readonly IDeviceManagementService _deviceManagementService; // 添加设备管理服务 + private readonly IVariableTableManagementService _variableTableManagementService; // 添加变量表管理服务 + private readonly IVariableManagementService _variableManagementService; // 添加变量管理服务 + private readonly IMenuManagementService _menuManagementService; // 添加菜单管理服务 + private readonly ILogManagementService _logManagementService; // 添加日志管理服务 public const int LoadLogCount =100; - public DataLoaderService( - IRepositoryManager repositoryManager, - IMapper mapper, - IAppStorageService appStorageService, - IDeviceAppService deviceAppService, - IVariableTableAppService variableTableAppService, - IVariableAppService variableAppService, - IMenuAppService menuService, - IMqttAppService mqttAppService, - INlogAppService nlogAppService, - IMqttAliasManagementService mqttAliasManagementService, - ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数 - IEventService eventService) // 添加事件服务参数 + public DataLoaderService(IMqttManagementService mqttManagementService, + IMqttAliasManagementService mqttAliasManagementService, + ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数 + IEventService eventService, // 添加事件服务参数 + IDeviceManagementService deviceManagementService, // 添加设备管理服务参数 + IVariableTableManagementService variableTableManagementService, // 添加变量表管理服务参数 + IVariableManagementService variableManagementService, // 添加变量管理服务参数 + IMenuManagementService menuManagementService, // 添加菜单管理服务参数 + ILogManagementService logManagementService) // 添加日志管理服务参数 { - _repositoryManager = repositoryManager; - _mapper = mapper; - _appStorageService = appStorageService; - _deviceAppService = deviceAppService; - _variableTableAppService = variableTableAppService; - _variableAppService = variableAppService; - _menuService = menuService; - _mqttAppService = mqttAppService; - _nlogAppService = nlogAppService; - this._mqttAliasManagementService = mqttAliasManagementService; + _mqttManagementService = mqttManagementService; + _mqttAliasManagementService = mqttAliasManagementService; _triggerManagementService = triggerManagementService; // 初始化触发器管理服务 _eventService = eventService; // 初始化事件服务 + _deviceManagementService = deviceManagementService; // 初始化设备管理服务 + _variableTableManagementService = variableTableManagementService; // 初始化变量表管理服务 + _variableManagementService = variableManagementService; // 初始化变量管理服务 + _menuManagementService = menuManagementService; // 初始化菜单管理服务 + _logManagementService = logManagementService; // 初始化日志管理服务 } @@ -68,149 +55,27 @@ public class DataLoaderService : IDataLoaderService public async Task LoadAllDataToMemoryAsync() { - await LoadAllDevicesAsync(); + await _deviceManagementService.LoadAllDevicesAsync(); - await LoadAllVariableTablesAsync(); + await _variableTableManagementService.LoadAllVariableTablesAsync(); - await LoadAllVariablesAsync(); + await _variableManagementService.LoadAllVariablesAsync(); // 加载所有菜单 - await LoadAllMenusAsync(); + await _menuManagementService.LoadAllMenusAsync(); // 加载所有MQTT服务器 - await LoadAllMqttServersAsync(); + await _mqttManagementService.LoadAllMqttServersAsync(); // 加载所有日志 - await LoadAllNlogsAsync(LoadLogCount); + await _logManagementService.LoadAllNlogsAsync(LoadLogCount); // 获取变量MQTT别名 await _mqttAliasManagementService.LoadAllMqttAliasAsync(); // 加载所有触发器 - await LoadAllTriggersAsync(); + await _triggerManagementService.LoadAllTriggersAsync(); _eventService.RaiseLoadDataCompleted(this, new DataLoadCompletedEventArgs(true, "数据加载成功")); } - /// - /// 异步加载所有触发器数据 - /// - public async Task LoadAllTriggersAsync() - { - _appStorageService.Triggers.Clear(); - var triggers = _triggerManagementService.GetAllTriggersAsync(); - // 加载触发器数据到内存 - foreach (var trigger in triggers) - { - _appStorageService.Triggers.TryAdd(trigger.Id, trigger); - } - } - - - - /// - /// 异步加载所有设备数据 - /// - public async Task LoadAllDevicesAsync() - { - _appStorageService.Devices.Clear(); - var devices = await _repositoryManager.Devices.GetAllAsync(); - var devicesDtos = _mapper.Map>(devices); - - // 建立设备与变量表的关联 - foreach (var deviceDto in devicesDtos) - { - // 将设备添加到安全字典 - _appStorageService.Devices.TryAdd(deviceDto.Id, deviceDto); - } - } - - /// - /// 异步加载所有变量表数据 - /// - public async Task LoadAllVariableTablesAsync() - { - _appStorageService.VariableTables.Clear(); - var variableTables = await _repositoryManager.VariableTables.GetAllAsync(); - var variableTableDtos = _mapper.Map>(variableTables); - // 建立变量表与变量的关联 - foreach (var variableTableDto in variableTableDtos) - { - if (_appStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var deviceDto)) - { - variableTableDto.Device = deviceDto; - variableTableDto.Device.VariableTables.Add(variableTableDto); - } - - // 将变量表添加到安全字典 - _appStorageService.VariableTables.TryAdd(variableTableDto.Id, variableTableDto); - } - } - - /// - /// 异步加载所有变量数据 - /// - public async Task LoadAllVariablesAsync() - { - _appStorageService.Variables.Clear(); - - var variables = await _repositoryManager.Variables.GetAllAsync(); - var variableDtos = _mapper.Map>(variables); - // 将变量添加到安全字典 - foreach (var variableDto in variableDtos) - { - if (_appStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, - out var variableTableDto)) - { - variableDto.VariableTable = variableTableDto; - variableDto.VariableTable.Variables.Add(variableDto); - } - - _appStorageService.Variables.TryAdd(variableDto.Id, variableDto); - } - } - - /// - /// 异步加载所有菜单数据 - /// - public async Task LoadAllMenusAsync() - { - _appStorageService.Menus.Clear(); - _appStorageService.MenuTrees.Clear(); - var menus = await _repositoryManager.Menus.GetAllAsync(); - // 将菜单添加到安全字典 - foreach (var menuBean in menus) - { - _appStorageService.Menus.TryAdd(menuBean.Id, menuBean); - } - - } - - /// - /// 异步加载所有MQTT服务器数据 - /// - public async Task LoadAllMqttServersAsync() - { - _appStorageService.MqttServers.Clear(); - var mqttServers =await _mqttAppService.GetAllMqttServersAsync(); - // 加载MQTT服务器数据到内存 - foreach (var mqttServer in mqttServers) - { - _appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer); - } - } - - /// - /// 异步加载所有日志数据 - /// - public async Task LoadAllNlogsAsync(int count) - { - _appStorageService.Nlogs.Clear(); - var nlogDtos =await _nlogAppService.GetLatestLogsAsync(count); - // 加载日志数据到内存 - foreach (var nlogDto in nlogDtos) - { - _appStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto); - } - - } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs index 0f96621..cdc8145 100644 --- a/DMS.Application/Services/Management/DeviceManagementService.cs +++ b/DMS.Application/Services/Management/DeviceManagementService.cs @@ -128,7 +128,20 @@ public class DeviceManagementService : IDeviceManagementService } } - - + /// + /// 异步加载所有设备数据到内存中。 + /// + public async Task LoadAllDevicesAsync() + { + _appStorageService.Devices.Clear(); + var devices = await _deviceAppService.GetAllDevicesAsync(); + + // 建立设备与变量表的关联 + foreach (var device in devices) + { + // 将设备添加到安全字典 + _appStorageService.Devices.TryAdd(device.Id, device); + } + } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/LogManagementService.cs b/DMS.Application/Services/Management/LogManagementService.cs index 9e7fc28..c46cd75 100644 --- a/DMS.Application/Services/Management/LogManagementService.cs +++ b/DMS.Application/Services/Management/LogManagementService.cs @@ -89,5 +89,18 @@ public class LogManagementService : ILogManagementService } } + /// + /// 异步加载所有日志数据到内存中。 + /// + public async Task LoadAllNlogsAsync(int count) + { + _appStorageService.Nlogs.Clear(); + var nlogDtos = await _nlogAppService.GetLatestLogsAsync(count); + // 加载日志数据到内存 + foreach (var nlogDto in nlogDtos) + { + _appStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto); + } + } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/MenuManagementService.cs b/DMS.Application/Services/Management/MenuManagementService.cs index 09355af..4e7d54f 100644 --- a/DMS.Application/Services/Management/MenuManagementService.cs +++ b/DMS.Application/Services/Management/MenuManagementService.cs @@ -160,4 +160,19 @@ public class MenuManagementService : IMenuManagementService { _eventService.RaiseMenuChanged(this, e); } + + /// + /// 异步加载所有菜单数据到内存中。 + /// + public async Task LoadAllMenusAsync() + { + _appStorageService.Menus.Clear(); + _appStorageService.MenuTrees.Clear(); + var menus = await _menuService.GetAllMenusAsync(); + // 将菜单添加到安全字典 + foreach (var menuBean in menus) + { + _appStorageService.Menus.TryAdd(menuBean.Id, menuBean); + } + } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/MqttManagementService.cs b/DMS.Application/Services/Management/MqttManagementService.cs index 36ce157..052a845 100644 --- a/DMS.Application/Services/Management/MqttManagementService.cs +++ b/DMS.Application/Services/Management/MqttManagementService.cs @@ -233,4 +233,18 @@ public class MqttManagementService : IMqttManagementService return changedProperties; } + + /// + /// 异步加载所有MQTT服务器数据到内存中。 + /// + public async Task LoadAllMqttServersAsync() + { + _appStorageService.MqttServers.Clear(); + var mqttServers = await _mqttAppService.GetAllMqttServersAsync(); + // 加载MQTT服务器数据到内存 + foreach (var mqttServer in mqttServers) + { + _appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer); + } + } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/TriggerManagementService.cs b/DMS.Application/Services/Management/TriggerManagementService.cs index c3c0956..211c92d 100644 --- a/DMS.Application/Services/Management/TriggerManagementService.cs +++ b/DMS.Application/Services/Management/TriggerManagementService.cs @@ -41,7 +41,8 @@ namespace DMS.Application.Services.Management _appStorageService.Triggers.TryGetValue(id, out var trigger); return trigger; } - + + /// /// 创建一个新的触发器定义 /// @@ -125,6 +126,9 @@ namespace DMS.Application.Services.Management return null; } + /// + /// 异步加载所有触发器数据 + /// public async Task LoadAllTriggersAsync() { _appStorageService.Triggers.Clear(); diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index f9df09e..f4ee821 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -275,4 +275,26 @@ public class VariableManagementService : IVariableManagementService } + /// + /// 异步加载所有变量数据到内存中。 + /// + public async Task LoadAllVariablesAsync() + { + _appStorageService.Variables.Clear(); + + var variables = await _variableAppService.GetAllVariablesAsync(); + // 将变量添加到安全字典 + foreach (var variable in variables) + { + if (_appStorageService.VariableTables.TryGetValue(variable.VariableTableId, + out var variableTable)) + { + variable.VariableTable = variableTable; + variableTable.Variables.Add(variable); + } + + _appStorageService.Variables.TryAdd(variable.Id, variable); + } + } + } \ No newline at end of file diff --git a/DMS.Application/Services/Management/VariableTableManagementService.cs b/DMS.Application/Services/Management/VariableTableManagementService.cs index 93c866e..536b186 100644 --- a/DMS.Application/Services/Management/VariableTableManagementService.cs +++ b/DMS.Application/Services/Management/VariableTableManagementService.cs @@ -131,6 +131,27 @@ public class VariableTableManagementService : IVariableTableManagementService } + /// + /// 异步加载所有变量表数据到内存中。 + /// + public async Task LoadAllVariableTablesAsync() + { + _appStorageService.VariableTables.Clear(); + var variableTables = await _variableTableAppService.GetAllVariableTablesAsync(); + // 建立变量表与变量的关联 + foreach (var variableTable in variableTables) + { + if (_appStorageService.Devices.TryGetValue(variableTable.DeviceId, out var device)) + { + variableTable.Device = device; + if (device.VariableTables == null) + device.VariableTables = new List(); + device.VariableTables.Add(variableTable); + } + // 将变量表添加到安全字典 + _appStorageService.VariableTables.TryAdd(variableTable.Id, variableTable); + } + } } \ No newline at end of file