2025-09-09 13:35:16 +08:00
|
|
|
using AutoMapper;
|
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
using DMS.Core.Interfaces;
|
|
|
|
|
using System.Collections.Concurrent;
|
2025-09-16 14:42:23 +08:00
|
|
|
using DMS.Application.Events;
|
2025-09-16 12:29:09 +08:00
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-09-23 09:28:08 +08:00
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-09-23 06:51:29 +08:00
|
|
|
using DMS.Application.Services.Management;
|
2025-09-09 18:17:15 +08:00
|
|
|
using DMS.Core.Models;
|
2025-09-23 05:48:21 +08:00
|
|
|
using DMS.Core.Models.Triggers;
|
|
|
|
|
using DMS.Application.Services.Triggers;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
namespace DMS.Application.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据加载服务,负责从数据源加载数据到内存中
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DataLoaderService : IDataLoaderService
|
|
|
|
|
{
|
|
|
|
|
private readonly IRepositoryManager _repositoryManager;
|
|
|
|
|
private readonly IMapper _mapper;
|
2025-09-09 15:28:07 +08:00
|
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
2025-09-09 13:35:16 +08:00
|
|
|
private readonly IDeviceAppService _deviceAppService;
|
|
|
|
|
private readonly IVariableTableAppService _variableTableAppService;
|
|
|
|
|
private readonly IVariableAppService _variableAppService;
|
2025-10-05 17:07:17 +08:00
|
|
|
private readonly IMenuAppService _menuService;
|
2025-09-09 13:35:16 +08:00
|
|
|
private readonly IMqttAppService _mqttAppService;
|
|
|
|
|
private readonly INlogAppService _nlogAppService;
|
2025-10-11 18:07:01 +08:00
|
|
|
private readonly IMqttAliasManagementService _mqttAliasManagementService;
|
2025-09-23 05:48:21 +08:00
|
|
|
private readonly ITriggerManagementService _triggerManagementService; // 添加触发器管理服务
|
2025-10-05 11:21:05 +08:00
|
|
|
private readonly IEventService _eventService; // 添加事件服务
|
|
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
public const int LoadLogCount =100;
|
2025-09-09 13:35:16 +08:00
|
|
|
public DataLoaderService(
|
|
|
|
|
IRepositoryManager repositoryManager,
|
|
|
|
|
IMapper mapper,
|
2025-09-09 15:28:07 +08:00
|
|
|
IAppDataStorageService appDataStorageService,
|
2025-09-09 13:35:16 +08:00
|
|
|
IDeviceAppService deviceAppService,
|
|
|
|
|
IVariableTableAppService variableTableAppService,
|
|
|
|
|
IVariableAppService variableAppService,
|
2025-10-05 17:07:17 +08:00
|
|
|
IMenuAppService menuService,
|
2025-09-09 13:35:16 +08:00
|
|
|
IMqttAppService mqttAppService,
|
2025-09-23 05:48:21 +08:00
|
|
|
INlogAppService nlogAppService,
|
2025-10-11 18:07:01 +08:00
|
|
|
IMqttAliasManagementService mqttAliasManagementService,
|
2025-10-05 11:21:05 +08:00
|
|
|
ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数
|
|
|
|
|
IEventService eventService) // 添加事件服务参数
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
_repositoryManager = repositoryManager;
|
|
|
|
|
_mapper = mapper;
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService = appDataStorageService;
|
2025-09-09 13:35:16 +08:00
|
|
|
_deviceAppService = deviceAppService;
|
|
|
|
|
_variableTableAppService = variableTableAppService;
|
|
|
|
|
_variableAppService = variableAppService;
|
|
|
|
|
_menuService = menuService;
|
|
|
|
|
_mqttAppService = mqttAppService;
|
|
|
|
|
_nlogAppService = nlogAppService;
|
2025-10-11 18:07:01 +08:00
|
|
|
this._mqttAliasManagementService = mqttAliasManagementService;
|
2025-09-23 05:48:21 +08:00
|
|
|
_triggerManagementService = triggerManagementService; // 初始化触发器管理服务
|
2025-10-05 11:21:05 +08:00
|
|
|
_eventService = eventService; // 初始化事件服务
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有设备及其关联数据到内存中
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public async Task LoadAllDataToMemoryAsync()
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllDevicesAsync();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllVariableTablesAsync();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllVariablesAsync();
|
2025-09-09 13:35:16 +08:00
|
|
|
// 加载所有菜单
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllMenusAsync();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
// 加载所有MQTT服务器
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllMqttServersAsync();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
// 加载所有日志
|
2025-09-09 17:47:20 +08:00
|
|
|
await LoadAllNlogsAsync(LoadLogCount);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
// 获取变量MQTT别名
|
2025-10-11 18:07:01 +08:00
|
|
|
await _mqttAliasManagementService.LoadAllMqttAliasAsync();
|
2025-09-23 05:48:21 +08:00
|
|
|
|
|
|
|
|
// 加载所有触发器
|
|
|
|
|
await LoadAllTriggersAsync();
|
2025-09-09 17:47:20 +08:00
|
|
|
|
2025-10-05 11:21:05 +08:00
|
|
|
_eventService.RaiseLoadDataCompleted(this, new DataLoadCompletedEventArgs(true, "数据加载成功"));
|
2025-09-09 17:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 05:48:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有触发器数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllTriggersAsync()
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.Triggers.Clear();
|
|
|
|
|
var triggers = await _triggerManagementService.GetAllTriggersAsync();
|
|
|
|
|
// 加载触发器数据到内存
|
|
|
|
|
foreach (var trigger in triggers)
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.Triggers.TryAdd(trigger.Id, trigger);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-06 17:25:05 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有设备数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllDevicesAsync()
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.Devices.Clear();
|
|
|
|
|
var devices = await _repositoryManager.Devices.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
var devicesDtos = _mapper.Map<List<Device>>(devices);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
// 建立设备与变量表的关联
|
2025-09-09 17:47:20 +08:00
|
|
|
foreach (var deviceDto in devicesDtos)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
// 将设备添加到安全字典
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.Devices.TryAdd(deviceDto.Id, deviceDto);
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
2025-09-09 17:47:20 +08:00
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有变量表数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllVariableTablesAsync()
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.VariableTables.Clear();
|
|
|
|
|
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
var variableTableDtos = _mapper.Map<List<VariableTable>>(variableTables);
|
2025-09-09 13:35:16 +08:00
|
|
|
// 建立变量表与变量的关联
|
|
|
|
|
foreach (var variableTableDto in variableTableDtos)
|
|
|
|
|
{
|
2025-09-09 15:28:07 +08:00
|
|
|
if (_appDataStorageService.Devices.TryGetValue(variableTableDto.DeviceId, out var deviceDto))
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
variableTableDto.Device = deviceDto;
|
2025-09-09 17:47:20 +08:00
|
|
|
variableTableDto.Device.VariableTables.Add(variableTableDto);
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将变量表添加到安全字典
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.VariableTables.TryAdd(variableTableDto.Id, variableTableDto);
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
2025-09-09 17:47:20 +08:00
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有变量数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllVariablesAsync()
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.Variables.Clear();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
2025-09-09 17:47:20 +08:00
|
|
|
var variables = await _repositoryManager.Variables.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
var variableDtos = _mapper.Map<List<Variable>>(variables);
|
2025-09-09 13:35:16 +08:00
|
|
|
// 将变量添加到安全字典
|
|
|
|
|
foreach (var variableDto in variableDtos)
|
|
|
|
|
{
|
2025-09-09 17:47:20 +08:00
|
|
|
if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId,
|
|
|
|
|
out var variableTableDto))
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
|
|
|
|
variableDto.VariableTable = variableTableDto;
|
2025-09-09 17:47:20 +08:00
|
|
|
variableDto.VariableTable.Variables.Add(variableDto);
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
_appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto);
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有菜单数据
|
|
|
|
|
/// </summary>
|
2025-09-09 17:47:20 +08:00
|
|
|
public async Task LoadAllMenusAsync()
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
2025-09-09 17:47:20 +08:00
|
|
|
_appDataStorageService.Menus.Clear();
|
|
|
|
|
_appDataStorageService.MenuTrees.Clear();
|
2025-09-09 13:35:16 +08:00
|
|
|
var menus = await _repositoryManager.Menus.GetAllAsync();
|
2025-09-09 17:47:20 +08:00
|
|
|
// 将菜单添加到安全字典
|
2025-10-13 20:20:09 +08:00
|
|
|
foreach (var menuBean in menus)
|
2025-09-09 17:47:20 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
_appDataStorageService.Menus.TryAdd(menuBean.Id, menuBean);
|
2025-09-09 17:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有MQTT服务器数据
|
|
|
|
|
/// </summary>
|
2025-09-09 17:47:20 +08:00
|
|
|
public async Task LoadAllMqttServersAsync()
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
2025-09-09 17:47:20 +08:00
|
|
|
_appDataStorageService.MqttServers.Clear();
|
2025-10-06 19:32:45 +08:00
|
|
|
var mqttServers =await _mqttAppService.GetAllMqttServersAsync();
|
2025-09-09 17:47:20 +08:00
|
|
|
// 加载MQTT服务器数据到内存
|
2025-10-06 19:32:45 +08:00
|
|
|
foreach (var mqttServer in mqttServers)
|
2025-09-09 17:47:20 +08:00
|
|
|
{
|
2025-10-06 19:32:45 +08:00
|
|
|
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
2025-09-09 17:47:20 +08:00
|
|
|
}
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有日志数据
|
|
|
|
|
/// </summary>
|
2025-09-09 17:47:20 +08:00
|
|
|
public async Task LoadAllNlogsAsync(int count)
|
2025-09-09 13:35:16 +08:00
|
|
|
{
|
2025-09-09 17:47:20 +08:00
|
|
|
_appDataStorageService.Nlogs.Clear();
|
|
|
|
|
var nlogDtos =await _nlogAppService.GetLatestLogsAsync(count);
|
|
|
|
|
// 加载日志数据到内存
|
|
|
|
|
foreach (var nlogDto in nlogDtos)
|
|
|
|
|
{
|
|
|
|
|
_appDataStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
}
|
|
|
|
|
}
|