refactor:将AppDataCenterService改为AppCenterService,将AppDataStorageService改为AppStorageService,将触发器的增删改成合并
This commit is contained in:
@@ -17,8 +17,8 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
private readonly ILogger<MqttBackgroundService> _logger;
|
||||
private readonly IMqttServiceManager _mqttServiceManager;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly ConcurrentDictionary<int, MqttServer> _mqttServers;
|
||||
private readonly SemaphoreSlim _reloadSemaphore = new(0);
|
||||
|
||||
@@ -26,14 +26,14 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
ILogger<MqttBackgroundService> logger,
|
||||
IMqttServiceManager mqttServiceManager,
|
||||
IEventService eventService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppDataCenterService appDataCenterService)
|
||||
IAppStorageService appStorageService,
|
||||
IAppCenterService appCenterService)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_mqttServiceManager = mqttServiceManager ?? throw new ArgumentNullException(nameof(mqttServiceManager));
|
||||
_eventService = eventService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
||||
_appStorageService = appStorageService;
|
||||
_appCenterService = appCenterService ?? throw new ArgumentNullException(nameof(appCenterService));
|
||||
_mqttServers = new ConcurrentDictionary<int, MqttServer>();
|
||||
|
||||
_eventService.OnLoadDataCompleted += OnLoadDataCompleted;
|
||||
@@ -186,7 +186,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
_mqttServers.Clear();
|
||||
|
||||
// 从数据服务中心获取所有激活的MQTT服务器
|
||||
var mqttServers = _appDataStorageService.MqttServers.Values.ToList();
|
||||
var mqttServers = _appStorageService.MqttServers.Values.ToList();
|
||||
|
||||
foreach (var mqttServer in mqttServers)
|
||||
{
|
||||
|
||||
@@ -28,14 +28,14 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
/// <summary>
|
||||
/// 与该MQTT服务器关联的所有变量MQTT别名
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<int, MqttAlias> VariableMqttAliases { get; set; }
|
||||
public ConcurrentDictionary<int, MqttAlias> MqttAliases { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public MqttDeviceContext()
|
||||
{
|
||||
VariableMqttAliases = new ConcurrentDictionary<int, MqttAlias>();
|
||||
MqttAliases = new ConcurrentDictionary<int, MqttAlias>();
|
||||
ReconnectAttempts = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
{
|
||||
private readonly ILogger<MqttServiceManager> _logger;
|
||||
private readonly IDataProcessingService _dataProcessingService;
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppCenterService _appDataCenterService;
|
||||
private readonly IMqttServiceFactory _mqttServiceFactory;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
public MqttServiceManager(
|
||||
ILogger<MqttServiceManager> logger,
|
||||
IDataProcessingService dataProcessingService,
|
||||
IAppDataCenterService appDataCenterService,
|
||||
IAppCenterService appDataCenterService,
|
||||
IMqttServiceFactory mqttServiceFactory,
|
||||
IEventService eventService)
|
||||
{
|
||||
@@ -91,16 +91,16 @@ namespace DMS.Infrastructure.Services.Mqtt
|
||||
/// <summary>
|
||||
/// 更新MQTT服务器变量别名
|
||||
/// </summary>
|
||||
public void UpdateVariableMqttAliases(int mqttServerId, List<MqttAlias> variableMqttAliases)
|
||||
public void UpdateMqttAliases(int mqttServerId, List<MqttAlias> mqttAliases)
|
||||
{
|
||||
if (_mqttContexts.TryGetValue(mqttServerId, out var context))
|
||||
{
|
||||
context.VariableMqttAliases.Clear();
|
||||
foreach (var alias in variableMqttAliases)
|
||||
context.MqttAliases.Clear();
|
||||
foreach (var alias in mqttAliases)
|
||||
{
|
||||
context.VariableMqttAliases.AddOrUpdate(alias.Id, alias, (key, oldValue) => alias);
|
||||
context.MqttAliases.AddOrUpdate(alias.Id, alias, (key, oldValue) => alias);
|
||||
}
|
||||
_logger.LogInformation("已更新MQTT服务器 {MqttServerId} 的变量别名列表,共 {Count} 个别名", mqttServerId, variableMqttAliases.Count);
|
||||
_logger.LogInformation("已更新MQTT服务器 {MqttServerId} 的变量别名列表,共 {Count} 个别名", mqttServerId, mqttAliases.Count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
{
|
||||
private readonly ILogger<OpcUaServiceManager> _logger;
|
||||
private readonly IDataProcessingService _dataProcessingService;
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly OpcUaServiceOptions _options;
|
||||
private readonly ConcurrentDictionary<int, DeviceContext> _deviceContexts;
|
||||
@@ -33,15 +33,15 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
ILogger<OpcUaServiceManager> logger,
|
||||
IDataProcessingService dataProcessingService,
|
||||
IEventService eventService,
|
||||
IAppDataCenterService appDataCenterService,
|
||||
IAppCenterService appCenterService,
|
||||
IOptions<OpcUaServiceOptions> options)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_dataProcessingService
|
||||
= dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
||||
_eventService = eventService;
|
||||
_appDataCenterService
|
||||
= appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
||||
_appCenterService
|
||||
= appCenterService ?? throw new ArgumentNullException(nameof(appCenterService));
|
||||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
|
||||
_semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections);
|
||||
|
||||
@@ -14,22 +14,22 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
/// </summary>
|
||||
public class OptimizedOpcUaBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IOpcUaServiceManager _opcUaServiceManager;
|
||||
private readonly ILogger<OptimizedOpcUaBackgroundService> _logger;
|
||||
private readonly SemaphoreSlim _reloadSemaphore = new SemaphoreSlim(0);
|
||||
|
||||
public OptimizedOpcUaBackgroundService(
|
||||
IAppDataCenterService appDataCenterService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppCenterService appCenterService,
|
||||
IAppStorageService appStorageService,
|
||||
IEventService eventService,
|
||||
IOpcUaServiceManager opcUaServiceManager,
|
||||
ILogger<OptimizedOpcUaBackgroundService> logger)
|
||||
{
|
||||
_appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appCenterService = appCenterService ?? throw new ArgumentNullException(nameof(appCenterService));
|
||||
_appStorageService = appStorageService;
|
||||
_opcUaServiceManager = opcUaServiceManager ?? throw new ArgumentNullException(nameof(opcUaServiceManager));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_eventService = eventService;
|
||||
@@ -60,7 +60,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
if (stoppingToken.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
if (_appDataStorageService.Devices.IsEmpty)
|
||||
if (_appStorageService.Devices.IsEmpty)
|
||||
{
|
||||
_logger.LogInformation("没有可用的OPC UA设备,等待设备列表更新...");
|
||||
continue;
|
||||
@@ -96,7 +96,7 @@ namespace DMS.Infrastructure.Services.OpcUa
|
||||
try
|
||||
{
|
||||
// 获取所有活动的OPC UA设备
|
||||
var opcUaDevices = _appDataStorageService.Devices.Values
|
||||
var opcUaDevices = _appStorageService.Devices.Values
|
||||
.Where(d => d.Protocol == ProtocolType.OpcUa )
|
||||
.ToList();
|
||||
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace DMS.Infrastructure.Services.S7;
|
||||
/// </summary>
|
||||
public class OptimizedS7BackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IDataProcessingService _dataProcessingService;
|
||||
private readonly IS7ServiceManager _s7ServiceManager;
|
||||
@@ -38,15 +38,15 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
/// 构造函数,注入数据服务和数据处理服务。
|
||||
/// </summary>
|
||||
public OptimizedS7BackgroundService(
|
||||
IAppDataCenterService appDataCenterService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppCenterService appCenterService,
|
||||
IAppStorageService appStorageService,
|
||||
IEventService eventService,
|
||||
IDataProcessingService dataProcessingService,
|
||||
IS7ServiceManager s7ServiceManager,
|
||||
ILogger<OptimizedS7BackgroundService> logger)
|
||||
{
|
||||
_appDataCenterService = appDataCenterService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appCenterService = appCenterService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
_dataProcessingService = dataProcessingService;
|
||||
_s7ServiceManager = s7ServiceManager;
|
||||
@@ -76,7 +76,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
break;
|
||||
}
|
||||
|
||||
if (_appDataStorageService.Devices.IsEmpty)
|
||||
if (_appStorageService.Devices.IsEmpty)
|
||||
{
|
||||
_logger.LogInformation("没有可用的S7设备,等待设备列表更新...");
|
||||
continue;
|
||||
@@ -124,7 +124,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
_variablesByPollingInterval.Clear();
|
||||
_logger.LogInformation("开始加载S7变量....");
|
||||
|
||||
var s7Devices = _appDataStorageService
|
||||
var s7Devices = _appStorageService
|
||||
.Devices.Values.Where(d => d.Protocol == ProtocolType.S7 && d.IsActive == true)
|
||||
.ToList();
|
||||
|
||||
@@ -161,7 +161,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
/// </summary>
|
||||
private async Task ConnectS7ServiceAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var s7Devices = _appDataStorageService
|
||||
var s7Devices = _appStorageService
|
||||
.Devices.Values.Where(d => d.Protocol == ProtocolType.S7 && d.IsActive == true)
|
||||
.ToList();
|
||||
|
||||
@@ -207,7 +207,7 @@ public class OptimizedS7BackgroundService : BackgroundService
|
||||
private async Task PollVariablesForDeviceAsync(S7DeviceContext context, List<Variable> variables,
|
||||
CancellationToken stoppingToken)
|
||||
{
|
||||
if (!_appDataStorageService.Devices.TryGetValue(context.Device.Id, out var device))
|
||||
if (!_appStorageService.Devices.TryGetValue(context.Device.Id, out var device))
|
||||
{
|
||||
_logger.LogWarning($"轮询时没有找到设备ID:{context.Device.Id}");
|
||||
return;
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace DMS.Infrastructure.Services.S7
|
||||
private readonly ILogger<S7ServiceManager> _logger;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IDataProcessingService _dataProcessingService;
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppCenterService _appCenterService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IS7ServiceFactory _s7ServiceFactory;
|
||||
private readonly ConcurrentDictionary<int, S7DeviceContext> _deviceContexts;
|
||||
private readonly SemaphoreSlim _semaphore;
|
||||
@@ -30,17 +30,17 @@ namespace DMS.Infrastructure.Services.S7
|
||||
ILogger<S7ServiceManager> logger,
|
||||
IEventService eventService,
|
||||
IDataProcessingService dataProcessingService,
|
||||
IAppDataCenterService appDataCenterService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppCenterService appCenterService,
|
||||
IAppStorageService appStorageService,
|
||||
IS7ServiceFactory s7ServiceFactory)
|
||||
{
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_eventService = eventService;
|
||||
_dataProcessingService
|
||||
= dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
||||
_appDataCenterService
|
||||
= appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService));
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appCenterService
|
||||
= appCenterService ?? throw new ArgumentNullException(nameof(appCenterService));
|
||||
_appStorageService = appStorageService;
|
||||
_s7ServiceFactory = s7ServiceFactory ?? throw new ArgumentNullException(nameof(s7ServiceFactory));
|
||||
_deviceContexts = new ConcurrentDictionary<int, S7DeviceContext>();
|
||||
_semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10
|
||||
@@ -54,7 +54,7 @@ namespace DMS.Infrastructure.Services.S7
|
||||
// if (_deviceContexts.TryGetValue(e.DeviceId, out var s7DeviceContext))
|
||||
// {
|
||||
|
||||
// var variables = _appDataStorageService.Variables.Values.Where(v => e.VariableIds.Contains(v.Id))
|
||||
// var variables = _appStorageService.Variables.Values.Where(v => e.VariableIds.Contains(v.Id))
|
||||
// .ToList();
|
||||
// foreach (var variable in variables)
|
||||
// {
|
||||
|
||||
Reference in New Issue
Block a user