2025-07-26 10:05:43 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.DTOs.Events;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.Core.Enums;
|
|
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Opc.Ua;
|
|
|
|
|
|
using Opc.Ua.Client;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
using DMS.Core.Interfaces;
|
2025-08-25 20:16:57 +08:00
|
|
|
|
using DMS.Infrastructure.Helper;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
using DMS.Infrastructure.Models;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.Infrastructure.Services;
|
|
|
|
|
|
|
|
|
|
|
|
public class OpcUaBackgroundService : BackgroundService
|
|
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private readonly IDataCenterService _dataCenterService;
|
2025-09-04 13:40:07 +08:00
|
|
|
|
private readonly IDataProcessingService _dataProcessingService;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
|
|
|
|
|
|
// private readonly IDataProcessingService _dataProcessingService;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
private readonly ILogger<OpcUaBackgroundService> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
// 存储 OPC UA 设备,键为设备Id,值为会话对象。
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private readonly ConcurrentDictionary<int, DeviceDto> _opcUaDevices = new();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
// 存储 OPC UA 会话,键为终结点 URL,值为会话对象。
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private readonly ConcurrentDictionary<DeviceDto, OpcUaService> _opcUaServices;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
// 存储 OPC UA 订阅,键为终结点 URL,值为订阅对象。
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, Subscription> _opcUaSubscriptions;
|
|
|
|
|
|
|
|
|
|
|
|
// 存储活动的 OPC UA 变量,键为变量的OpcNodeId
|
2025-09-04 13:40:07 +08:00
|
|
|
|
private readonly ConcurrentDictionary<string, VariableDto> _opcUaVariables;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
// 储存所有要轮询更新的变量,键是Device.Id,值是这个设备所有要轮询的变量
|
|
|
|
|
|
private readonly ConcurrentDictionary<int, List<Variable>> _opcUaPollVariablesByDeviceId;
|
|
|
|
|
|
|
|
|
|
|
|
// 储存所有要订阅更新的变量,键是Device.Id,值是这个设备所有要轮询的变量
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private readonly ConcurrentDictionary<int, List<VariableDto>> _opcUaVariablesByDeviceId;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
private readonly SemaphoreSlim _reloadSemaphore = new SemaphoreSlim(0);
|
|
|
|
|
|
|
|
|
|
|
|
// OPC UA 轮询间隔(毫秒)
|
|
|
|
|
|
private readonly int _opcUaPollIntervalMs = 100;
|
|
|
|
|
|
|
|
|
|
|
|
// OPC UA 订阅发布间隔(毫秒)
|
|
|
|
|
|
private readonly int _opcUaSubscriptionPublishingIntervalMs = 1000;
|
|
|
|
|
|
|
|
|
|
|
|
// OPC UA 订阅采样间隔(毫秒)
|
|
|
|
|
|
private readonly int _opcUaSubscriptionSamplingIntervalMs = 1000;
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟 PollingIntervals,实际应用中可能从配置或数据库加载
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private static readonly Dictionary<PollLevelType, TimeSpan> PollingIntervals
|
|
|
|
|
|
= new Dictionary<PollLevelType, TimeSpan>
|
|
|
|
|
|
{
|
|
|
|
|
|
{ PollLevelType.TenMilliseconds, TimeSpan.FromMilliseconds((int)PollLevelType.TenMilliseconds) },
|
|
|
|
|
|
{ PollLevelType.HundredMilliseconds, TimeSpan.FromMilliseconds((int)PollLevelType.HundredMilliseconds) },
|
|
|
|
|
|
{
|
|
|
|
|
|
PollLevelType.FiveHundredMilliseconds,
|
|
|
|
|
|
TimeSpan.FromMilliseconds((int)PollLevelType.FiveHundredMilliseconds)
|
|
|
|
|
|
},
|
|
|
|
|
|
{ PollLevelType.OneSecond, TimeSpan.FromMilliseconds((int)PollLevelType.OneSecond) },
|
|
|
|
|
|
{ PollLevelType.FiveSeconds, TimeSpan.FromMilliseconds((int)PollLevelType.FiveSeconds) },
|
|
|
|
|
|
{ PollLevelType.TenSeconds, TimeSpan.FromMilliseconds((int)PollLevelType.TenSeconds) },
|
|
|
|
|
|
{ PollLevelType.TwentySeconds, TimeSpan.FromMilliseconds((int)PollLevelType.TwentySeconds) },
|
|
|
|
|
|
{ PollLevelType.ThirtySeconds, TimeSpan.FromMilliseconds((int)PollLevelType.ThirtySeconds) },
|
|
|
|
|
|
{ PollLevelType.OneMinute, TimeSpan.FromMilliseconds((int)PollLevelType.OneMinute) },
|
|
|
|
|
|
{ PollLevelType.ThreeMinutes, TimeSpan.FromMilliseconds((int)PollLevelType.ThreeMinutes) },
|
|
|
|
|
|
{ PollLevelType.FiveMinutes, TimeSpan.FromMilliseconds((int)PollLevelType.FiveMinutes) },
|
|
|
|
|
|
{ PollLevelType.TenMinutes, TimeSpan.FromMilliseconds((int)PollLevelType.TenMinutes) },
|
|
|
|
|
|
{ PollLevelType.ThirtyMinutes, TimeSpan.FromMilliseconds((int)PollLevelType.ThirtyMinutes) }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-04 13:40:07 +08:00
|
|
|
|
public OpcUaBackgroundService(IDataCenterService dataCenterService,IDataProcessingService dataProcessingService, ILogger<OpcUaBackgroundService> logger)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_dataCenterService = dataCenterService;
|
2025-09-04 13:40:07 +08:00
|
|
|
|
_dataProcessingService = dataProcessingService;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
_logger = logger;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_opcUaServices = new ConcurrentDictionary<DeviceDto, OpcUaService>();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
_opcUaSubscriptions = new ConcurrentDictionary<string, Subscription>();
|
2025-09-04 13:40:07 +08:00
|
|
|
|
_opcUaVariables = new ConcurrentDictionary<string, VariableDto>();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
_opcUaPollVariablesByDeviceId = new ConcurrentDictionary<int, List<Variable>>();
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_opcUaVariablesByDeviceId = new ConcurrentDictionary<int, List<VariableDto>>();
|
|
|
|
|
|
|
|
|
|
|
|
_dataCenterService.DataLoadCompleted += DataLoadCompleted;
|
|
|
|
|
|
}
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private void DataLoadCompleted(object? sender, DataLoadCompletedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_reloadSemaphore.Release();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("OPC UA 后台服务正在启动。");
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _reloadSemaphore.WaitAsync(stoppingToken); // Wait for a reload signal
|
|
|
|
|
|
|
|
|
|
|
|
if (stoppingToken.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
if (_dataCenterService.Devices.IsEmpty)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("没有可用的OPC UA设备,等待设备列表更新...");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var isLoaded = LoadVariables();
|
|
|
|
|
|
if (!isLoaded)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("加载变量过程中发生了错误,停止后面的操作。");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await ConnectOpcUaServiceAsync(stoppingToken);
|
|
|
|
|
|
await SetupOpcUaSubscriptionAsync(stoppingToken);
|
|
|
|
|
|
_logger.LogInformation("OPC UA 后台服务已启动。");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("OPC UA 后台服务已停止。");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, $"OPC UA 后台服务运行中发生了错误:{e.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
await DisconnectAllOpcUaSessionsAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从数据库加载所有活动的 OPC UA 变量,并进行相应的连接和订阅管理。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private bool LoadVariables()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_opcUaDevices.Clear();
|
|
|
|
|
|
_opcUaPollVariablesByDeviceId.Clear();
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_opcUaVariablesByDeviceId.Clear();
|
2025-09-04 13:40:07 +08:00
|
|
|
|
_opcUaVariables.Clear();
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("开始加载OPC UA变量....");
|
2025-09-03 19:58:02 +08:00
|
|
|
|
var opcUaDevices = _dataCenterService
|
|
|
|
|
|
.Devices.Values.Where(d => d.Protocol == ProtocolType.OpcUa && d.IsActive == true)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
.ToList();
|
2025-09-03 19:58:02 +08:00
|
|
|
|
int totalVariableCount = 0;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
foreach (var opcUaDevice in opcUaDevices)
|
|
|
|
|
|
{
|
|
|
|
|
|
_opcUaDevices.AddOrUpdate(opcUaDevice.Id, opcUaDevice, (key, oldValue) => opcUaDevice);
|
|
|
|
|
|
|
|
|
|
|
|
//查找设备中所有要订阅的变量
|
2025-09-03 19:58:02 +08:00
|
|
|
|
var variableDtos = opcUaDevice.VariableTables?.SelectMany(vt => vt.Variables)
|
|
|
|
|
|
.Where(vd => vd.IsActive == true &&
|
|
|
|
|
|
vd.Protocol == ProtocolType.OpcUa)
|
|
|
|
|
|
.ToList();
|
2025-09-04 13:40:07 +08:00
|
|
|
|
foreach (var variableDto in variableDtos)
|
|
|
|
|
|
{
|
|
|
|
|
|
_opcUaVariables.TryAdd(variableDto.OpcUaNodeId,variableDto);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
totalVariableCount += variableDtos.Count;
|
|
|
|
|
|
_opcUaVariablesByDeviceId.AddOrUpdate(opcUaDevice.Id, variableDtos, (key, oldValue) => variableDtos);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(
|
2025-09-03 19:58:02 +08:00
|
|
|
|
$"OPC UA 变量加载成功,共加载OPC UA设备:{opcUaDevices.Count}个,变量数:{totalVariableCount}");
|
2025-07-26 10:05:43 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, $"加载OPC UA变量的过程中发生了错误:{e.Message}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接到 OPC UA 服务器并订阅或轮询指定的变量。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private async Task ConnectOpcUaServiceAsync(CancellationToken stoppingToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (stoppingToken.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var connectTasks = new List<Task>();
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历_opcUaDevices中的所有设备,尝试连接
|
|
|
|
|
|
foreach (var device in _opcUaDevices.Values.ToList())
|
|
|
|
|
|
{
|
|
|
|
|
|
connectTasks.Add(ConnectSingleOpcUaDeviceAsync(device, stoppingToken));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(connectTasks);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接单个OPC UA设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="device">要连接的设备。</param>
|
|
|
|
|
|
/// <param name="stoppingToken">取消令牌。</param>
|
2025-09-03 19:58:02 +08:00
|
|
|
|
private async Task ConnectSingleOpcUaDeviceAsync(DeviceDto device, CancellationToken stoppingToken = default)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
// Check if already connected
|
2025-09-03 19:58:02 +08:00
|
|
|
|
if (_opcUaServices.TryGetValue(device, out var existOpcUaService))
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
if (existOpcUaService.IsConnected)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation($"已连接到 OPC UA 服务器: {device.OpcUaServerUrl}");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"开始连接OPC UA服务器: {device.Name} ({device.OpcUaServerUrl})");
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
OpcUaService opcUaService = new OpcUaService();
|
|
|
|
|
|
await opcUaService.ConnectAsync(device.OpcUaServerUrl);
|
|
|
|
|
|
if (!opcUaService.IsConnected)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger.LogWarning($"创建OPC UA会话失败: {device.OpcUaServerUrl}");
|
|
|
|
|
|
return; // 连接失败,直接返回
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_opcUaServices.AddOrUpdate(device, opcUaService, (key, oldValue) => opcUaService);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
_logger.LogInformation($"已连接到OPC UA服务器: {device.Name} ({device.OpcUaServerUrl})");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(e, $"OPC UA服务连接 {device.Name} ({device.OpcUaServerUrl}) 过程中发生错误:{e.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// /// <summary>
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// /// 读取单个 OPC UA 变量并处理其数据。
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// /// </summary>
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// /// <param name="session">OPC UA 会话。</param>
|
|
|
|
|
|
// /// <param name="variable">要读取的变量。</param>
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// /// <param name="stoppingToken">取消令牌。</param>
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// private async Task ReadAndProcessOpcUaVariableAsync(Session session, Variable variable,
|
|
|
|
|
|
// CancellationToken stoppingToken)
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// {
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// var nodesToRead = new ReadValueIdCollection
|
|
|
|
|
|
// {
|
|
|
|
|
|
// new ReadValueId
|
|
|
|
|
|
// {
|
|
|
|
|
|
// NodeId = new NodeId(variable.OpcUaNodeId),
|
|
|
|
|
|
// AttributeId = Attributes.Value
|
|
|
|
|
|
// }
|
|
|
|
|
|
// };
|
2025-09-03 19:58:02 +08:00
|
|
|
|
//
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// try
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// {
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// var readResponse = await session.ReadAsync(null, 0, TimestampsToReturn.Both, nodesToRead, stoppingToken);
|
|
|
|
|
|
// var result = readResponse.Results?.FirstOrDefault();
|
|
|
|
|
|
// if (result == null) return;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
//
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// if (!StatusCode.IsGood(result.StatusCode))
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// {
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// _logger.LogWarning($"读取 OPC UA 变量 {variable.Name} ({variable.OpcUaNodeId}) 失败: {result.StatusCode}");
|
|
|
|
|
|
// return;
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// }
|
|
|
|
|
|
//
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// await UpdateAndEnqueueVariable(variable, result.Value);
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// }
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadSessionIdInvalid)
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// {
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// _logger.LogError(ex, $"OPC UA会话ID无效,变量: {variable.Name} ({variable.OpcUaNodeId})。正在尝试重新连接...");
|
|
|
|
|
|
//
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// }
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// catch (Exception ex)
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// {
|
2025-09-04 13:40:07 +08:00
|
|
|
|
// _logger.LogError(ex, $"轮询OPC UA变量 {variable.Name} ({variable.OpcUaNodeId}) 时发生未知错误: {ex.Message}");
|
2025-09-03 19:58:02 +08:00
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新变量数据,并将其推送到数据处理队列。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variable">要更新的变量。</param>
|
|
|
|
|
|
/// <param name="value">读取到的数据值。</param>
|
2025-09-04 13:40:07 +08:00
|
|
|
|
private async Task UpdateAndEnqueueVariable(VariableDto variable, object value)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新变量的原始数据值和显示值。
|
|
|
|
|
|
variable.DataValue = value.ToString();
|
|
|
|
|
|
variable.DisplayValue = value.ToString(); // 或者根据需要进行格式化
|
2025-07-27 21:08:58 +08:00
|
|
|
|
variable.UpdatedAt = DateTime.Now;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// Console.WriteLine($"OpcUa后台服务轮询变量:{variable.Name},值:{variable.DataValue}");
|
|
|
|
|
|
// 将更新后的数据推入处理队列。
|
2025-09-04 13:40:07 +08:00
|
|
|
|
await _dataProcessingService.EnqueueAsync(variable);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, $"更新变量 {variable.Name} 并入队失败:{ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置 OPC UA 订阅并添加监控项。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="stoppingToken">取消令牌。</param>
|
|
|
|
|
|
private async Task SetupOpcUaSubscriptionAsync(CancellationToken stoppingToken)
|
|
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
foreach (var opcUaServiceKayValuePair in _opcUaServices)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
var device = opcUaServiceKayValuePair.Key;
|
|
|
|
|
|
var opcUaService = opcUaServiceKayValuePair.Value;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
if (_opcUaVariablesByDeviceId.TryGetValue(device.Id, out var opcUaVariables))
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
var variableGroup = opcUaVariables.GroupBy(variable => variable.PollLevel);
|
|
|
|
|
|
foreach (var vGroup in variableGroup)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
var pollLevelType = vGroup.Key;
|
|
|
|
|
|
var opcUaNodes
|
|
|
|
|
|
= vGroup.Select(variableDto => new OpcUaNode() { NodeId = variableDto.OpcUaNodeId })
|
|
|
|
|
|
.ToList();
|
2025-09-04 13:40:07 +08:00
|
|
|
|
|
|
|
|
|
|
PollingIntervals.TryGetValue(pollLevelType, out var pollLevel);
|
|
|
|
|
|
opcUaService.SubscribeToNode(opcUaNodes,HandleDataChanged,10000,1000);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
2025-09-03 19:58:02 +08:00
|
|
|
|
}
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
2025-09-03 19:58:02 +08:00
|
|
|
|
}
|
2025-07-26 10:05:43 +08:00
|
|
|
|
|
2025-09-04 13:40:07 +08:00
|
|
|
|
private async void HandleDataChanged(OpcUaNode opcUaNode)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-04 13:40:07 +08:00
|
|
|
|
if (_opcUaVariables.TryGetValue(opcUaNode.NodeId.ToString(), out var variabelDto))
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-04 13:40:07 +08:00
|
|
|
|
if (opcUaNode.Value == null)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-04 13:40:07 +08:00
|
|
|
|
return;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
2025-09-04 13:40:07 +08:00
|
|
|
|
await UpdateAndEnqueueVariable(variabelDto, opcUaNode.Value);
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 13:40:07 +08:00
|
|
|
|
|
2025-07-26 10:05:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 断开所有 OPC UA 会话。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private async Task DisconnectAllOpcUaSessionsAsync()
|
|
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
if (_opcUaServices.IsEmpty)
|
2025-07-26 10:05:43 +08:00
|
|
|
|
return;
|
|
|
|
|
|
_logger.LogInformation("正在断开所有 OPC UA 会话...");
|
|
|
|
|
|
var closeTasks = new List<Task>();
|
|
|
|
|
|
|
2025-09-03 19:58:02 +08:00
|
|
|
|
foreach (var device in _opcUaServices.Keys.ToList())
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
closeTasks.Add(Task.Run(async () =>
|
|
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
_logger.LogInformation($"正在断开 OPC UA 会话: {device.Name}");
|
|
|
|
|
|
if (_opcUaServices.TryRemove(device, out var opcUaService))
|
2025-07-26 10:05:43 +08:00
|
|
|
|
{
|
2025-09-03 19:58:02 +08:00
|
|
|
|
await opcUaService.DisconnectAsync();
|
|
|
|
|
|
_logger.LogInformation($"已从 OPC UA 服务器断开连接: {device.Name}");
|
2025-07-26 10:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(closeTasks);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|