初步完成变更新

This commit is contained in:
2025-09-05 07:03:47 +08:00
parent d23c8bbd90
commit 831f342b2c
6 changed files with 141 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
using DMS.Application.Interfaces;
using DMS.Infrastructure.Configuration;
using DMS.Infrastructure.Interfaces.Services;
using DMS.Infrastructure.Services;
@@ -15,7 +16,18 @@ namespace DMS.Infrastructure.Extensions
/// </summary>
public static IServiceCollection AddOpcUaServices(this IServiceCollection services)
{
// 注册配置选项
services.Configure<OpcUaServiceOptions>(
options => {
// 可以从配置文件或其他来源加载配置
});
// 注册服务
services.AddSingleton<IOpcUaServiceManager, OpcUaServiceManager>();
// 注册后台服务
services.AddHostedService<OptimizedOpcUaBackgroundService>();
return services;
}
}

View File

@@ -3,6 +3,7 @@ using System.Diagnostics;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.Infrastructure.Configuration;
using DMS.Infrastructure.Interfaces.Services;
using DMS.Infrastructure.Models;
@@ -18,6 +19,7 @@ namespace DMS.Infrastructure.Services
{
private readonly ILogger<OpcUaServiceManager> _logger;
private readonly IDataProcessingService _dataProcessingService;
private readonly IDataCenterService _dataCenterService;
private readonly OpcUaServiceOptions _options;
private readonly ConcurrentDictionary<int, DeviceContext> _deviceContexts;
private readonly SemaphoreSlim _semaphore;
@@ -26,10 +28,12 @@ namespace DMS.Infrastructure.Services
public OpcUaServiceManager(
ILogger<OpcUaServiceManager> logger,
IDataProcessingService dataProcessingService,
IDataCenterService dataCenterService,
IOptions<OpcUaServiceOptions> options)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
_dataCenterService = dataCenterService ?? throw new ArgumentNullException(nameof(dataCenterService));
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
_semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections);
@@ -287,11 +291,25 @@ namespace DMS.Infrastructure.Services
{
if (context.Variables.TryGetValue(opcUaNode.NodeId.ToString(), out var variable))
{
// 保存旧值
var oldValue = variable.DataValue;
var newValue = opcUaNode.Value.ToString();
// 更新变量值
variable.DataValue = opcUaNode.Value.ToString();
variable.DisplayValue = opcUaNode.Value.ToString();
variable.DataValue = newValue;
variable.DisplayValue = newValue;
variable.UpdatedAt = DateTime.Now;
// 触发变量值变更事件
var eventArgs = new VariableValueChangedEventArgs(
variable.Id,
variable.Name,
oldValue,
newValue,
variable.UpdatedAt);
_dataCenterService.OnVariableValueChanged( eventArgs);
// 推送到数据处理队列
await _dataProcessingService.EnqueueAsync(variable);
break;