From ddfb12420458e7c2c54d2c757a54e3c20515a380 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 9 Sep 2025 17:47:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=BD=BD=E5=8A=A0=E8=BD=BD=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IAppDataStorageService.cs | 5 + .../Interfaces/IDataLoaderService.cs | 12 +- .../Services/AppDataStorageService.cs | 6 + DMS.Application/Services/DataLoaderService.cs | 178 +++++++++--------- DMS.WPF/Views/SplashWindow.xaml | 6 +- 5 files changed, 110 insertions(+), 97 deletions(-) diff --git a/DMS.Application/Interfaces/IAppDataStorageService.cs b/DMS.Application/Interfaces/IAppDataStorageService.cs index 9ba9e0f..abc9c15 100644 --- a/DMS.Application/Interfaces/IAppDataStorageService.cs +++ b/DMS.Application/Interfaces/IAppDataStorageService.cs @@ -39,4 +39,9 @@ public interface IAppDataStorageService /// 安全字典,用于存储所有日志数据 /// ConcurrentDictionary Nlogs { get; } + + /// + /// 安全字典,用于存储所有MQTT变量别名的数据 + /// + ConcurrentDictionary VariableMqttAliases { get; } } \ No newline at end of file diff --git a/DMS.Application/Interfaces/IDataLoaderService.cs b/DMS.Application/Interfaces/IDataLoaderService.cs index bb241bf..425f855 100644 --- a/DMS.Application/Interfaces/IDataLoaderService.cs +++ b/DMS.Application/Interfaces/IDataLoaderService.cs @@ -17,32 +17,32 @@ public interface IDataLoaderService /// /// 异步加载所有设备数据 /// - Task> LoadAllDevicesAsync(); + Task LoadAllDevicesAsync(); /// /// 异步加载所有变量表数据 /// - Task> LoadAllVariableTablesAsync(); + Task LoadAllVariableTablesAsync(); /// /// 异步加载所有变量数据 /// - Task> LoadAllVariablesAsync(); + Task LoadAllVariablesAsync(); /// /// 异步加载所有菜单数据 /// - Task> LoadAllMenusAsync(); + Task LoadAllMenusAsync(); /// /// 异步加载所有MQTT服务器数据 /// - Task> LoadAllMqttServersAsync(); + Task LoadAllMqttServersAsync(); /// /// 异步加载所有日志数据 /// - Task> LoadAllNlogsAsync(int count); + Task LoadAllNlogsAsync(int count); /// /// 当数据加载完成时触发 diff --git a/DMS.Application/Services/AppDataStorageService.cs b/DMS.Application/Services/AppDataStorageService.cs index 57fabc9..09d6a9f 100644 --- a/DMS.Application/Services/AppDataStorageService.cs +++ b/DMS.Application/Services/AppDataStorageService.cs @@ -35,6 +35,12 @@ public class AppDataStorageService : IAppDataStorageService /// 安全字典,用于存储所有MQTT服务器数据 /// public ConcurrentDictionary MqttServers { get; } = new(); + + + /// + /// 安全字典,用于存储所有MQTT变量别名的数据 + /// + public ConcurrentDictionary VariableMqttAliases { get; } = new(); /// /// 安全字典,用于存储所有日志数据 diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index 0e829ad..464e25b 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -21,12 +21,13 @@ public class DataLoaderService : IDataLoaderService private readonly IMenuService _menuService; private readonly IMqttAppService _mqttAppService; private readonly INlogAppService _nlogAppService; - + /// /// 当数据加载完成时触发 /// public event EventHandler OnLoadDataCompleted; + public const int LoadLogCount =100; public DataLoaderService( IRepositoryManager repositoryManager, IMapper mapper, @@ -50,149 +51,148 @@ public class DataLoaderService : IDataLoaderService } - /// /// 异步加载所有设备及其关联数据到内存中 /// public async Task LoadAllDataToMemoryAsync() { - // 清空现有数据 - _appDataStorageService.Devices.Clear(); - _appDataStorageService.VariableTables.Clear(); - _appDataStorageService.Variables.Clear(); - _appDataStorageService.Menus.Clear(); - _appDataStorageService.MenuTrees.Clear(); - _appDataStorageService.MqttServers.Clear(); - _appDataStorageService.Nlogs.Clear(); - // 加载所有设备 - var deviceDtos = await LoadAllDevicesAsync(); + await LoadAllDevicesAsync(); - // 加载所有变量表 - var variableTableDtos = await LoadAllVariableTablesAsync(); - - // 加载所有变量 - var variableDtos = await LoadAllVariablesAsync(); + await LoadAllVariableTablesAsync(); + await LoadAllVariablesAsync(); // 加载所有菜单 - var menuDtos = await LoadAllMenusAsync(); + await LoadAllMenusAsync(); // 加载所有MQTT服务器 - var mqttServerDtos = await LoadAllMqttServersAsync(); + await LoadAllMqttServersAsync(); // 加载所有日志 - var nlogDtos = await LoadAllNlogsAsync(100); + await LoadAllNlogsAsync(LoadLogCount); // 获取变量MQTT别名 + await LoadAllVariableMqttAliases(); + + OnLoadDataCompleted?.Invoke(this, new DataLoadCompletedEventArgs(true, "数据加载成功")); + } + + private async Task LoadAllVariableMqttAliases() + { + var variableMqttAliases = await _repositoryManager.VariableMqttAliases.GetAllAsync(); - - // 建立设备与变量表的关联 - foreach (var deviceDto in deviceDtos) - { - deviceDto.VariableTables = variableTableDtos - .Where(vt => vt.DeviceId == deviceDto.Id) - .ToList(); - - // 将设备添加到安全字典 - _appDataStorageService.Devices.TryAdd(deviceDto.Id, deviceDto); - } - - // 建立变量表与变量的关联 - foreach (var variableTableDto in variableTableDtos) - { - variableTableDto.Variables = variableDtos - .Where(v => v.VariableTableId == variableTableDto.Id) - .ToList(); - if (_appDataStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var deviceDto)) - { - variableTableDto.Device = deviceDto; - } - - // 将变量表添加到安全字典 - _appDataStorageService.VariableTables.TryAdd(variableTableDto.Id, variableTableDto); - } - - // 加载MQTT服务器数据到内存 - foreach (var mqttServerDto in mqttServerDtos) - { - _appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto); - } - - // 加载日志数据到内存 - foreach (var nlogDto in nlogDtos) - { - _appDataStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto); - } - - // 将变量添加到安全字典 - foreach (var variableDto in variableDtos) - { - if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTableDto)) - { - variableDto.VariableTable = variableTableDto; - } - - _appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto); - } - - // 将菜单添加到安全字典 - foreach (var menuDto in menuDtos) - { - _appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto); - } - - OnLoadDataCompleted?.Invoke(this,new DataLoadCompletedEventArgs(true,"数据加载成功")); + var variableMqttAliasDtos = _mapper.Map>(variableMqttAliases); + foreach (var variableMqttAliasDto in variableMqttAliasDtos) + { + _appDataStorageService.VariableMqttAliases.TryAdd(variableMqttAliasDto.Id, variableMqttAliasDto); + } } /// /// 异步加载所有设备数据 /// - public async Task> LoadAllDevicesAsync() + public async Task LoadAllDevicesAsync() { + _appDataStorageService.Devices.Clear(); var devices = await _repositoryManager.Devices.GetAllAsync(); - return _mapper.Map>(devices); + var devicesDtos = _mapper.Map>(devices); + + // 建立设备与变量表的关联 + foreach (var deviceDto in devicesDtos) + { + // 将设备添加到安全字典 + _appDataStorageService.Devices.TryAdd(deviceDto.Id, deviceDto); + } } /// /// 异步加载所有变量表数据 /// - public async Task> LoadAllVariableTablesAsync() + public async Task LoadAllVariableTablesAsync() { + _appDataStorageService.VariableTables.Clear(); var variableTables = await _repositoryManager.VariableTables.GetAllAsync(); - return _mapper.Map>(variableTables); + var variableTableDtos = _mapper.Map>(variableTables); + // 建立变量表与变量的关联 + foreach (var variableTableDto in variableTableDtos) + { + if (_appDataStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var deviceDto)) + { + variableTableDto.Device = deviceDto; + variableTableDto.Device.VariableTables.Add(variableTableDto); + } + + // 将变量表添加到安全字典 + _appDataStorageService.VariableTables.TryAdd(variableTableDto.Id, variableTableDto); + } } /// /// 异步加载所有变量数据 /// - public async Task> LoadAllVariablesAsync() + public async Task LoadAllVariablesAsync() { + _appDataStorageService.Variables.Clear(); + var variables = await _repositoryManager.Variables.GetAllAsync(); - return _mapper.Map>(variables); + var variableDtos = _mapper.Map>(variables); + // 将变量添加到安全字典 + foreach (var variableDto in variableDtos) + { + if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, + out var variableTableDto)) + { + variableDto.VariableTable = variableTableDto; + variableDto.VariableTable.Variables.Add(variableDto); + } + + _appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto); + } } /// /// 异步加载所有菜单数据 /// - public async Task> LoadAllMenusAsync() + public async Task LoadAllMenusAsync() { + _appDataStorageService.Menus.Clear(); + _appDataStorageService.MenuTrees.Clear(); var menus = await _repositoryManager.Menus.GetAllAsync(); - return _mapper.Map>(menus); + var menuDtos = _mapper.Map>(menus); + // 将菜单添加到安全字典 + foreach (var menuDto in menuDtos) + { + _appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto); + } + } /// /// 异步加载所有MQTT服务器数据 /// - public async Task> LoadAllMqttServersAsync() + public async Task LoadAllMqttServersAsync() { - return await _mqttAppService.GetAllMqttServersAsync(); + _appDataStorageService.MqttServers.Clear(); + var mqttServerDtos =await _mqttAppService.GetAllMqttServersAsync(); + // 加载MQTT服务器数据到内存 + foreach (var mqttServerDto in mqttServerDtos) + { + _appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto); + } } /// /// 异步加载所有日志数据 /// - public async Task> LoadAllNlogsAsync(int count) + public async Task LoadAllNlogsAsync(int count) { - return await _nlogAppService.GetLatestLogsAsync(count); + _appDataStorageService.Nlogs.Clear(); + var nlogDtos =await _nlogAppService.GetLatestLogsAsync(count); + // 加载日志数据到内存 + foreach (var nlogDto in nlogDtos) + { + _appDataStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto); + } + } } \ No newline at end of file diff --git a/DMS.WPF/Views/SplashWindow.xaml b/DMS.WPF/Views/SplashWindow.xaml index 2c47bc5..55b0e47 100644 --- a/DMS.WPF/Views/SplashWindow.xaml +++ b/DMS.WPF/Views/SplashWindow.xaml @@ -3,15 +3,17 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:vm="clr-namespace:DMS.WPF.ViewModels" + d:DataContext="{d:DesignInstance vm:SplashViewModel}" xmlns:local="clr-namespace:DMS.WPF.Views" mc:Ignorable="d" - Title="SplashWindow" Height="400" Width="600" + Title="正在启动..." Height="400" Width="600" WindowStyle="None" WindowStartupLocation="CenterScreen" AllowsTransparency="True" Background="Transparent"> - +