初步完成变更新

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,6 +1,7 @@
using System.Collections.Concurrent;
using DMS.Application.DTOs;
using DMS.Application.DTOs.Events;
using DMS.Core.Models;
namespace DMS.Application.Interfaces;
@@ -305,5 +306,13 @@ public interface IDataCenterService
/// </summary>
event EventHandler<DataChangedEventArgs> DataChanged;
/// <summary>
/// 当变量值发生变化时触发
/// </summary>
event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
void OnVariableValueChanged(VariableValueChangedEventArgs e);
#endregion
}

View File

@@ -1,6 +1,7 @@
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.DTOs.Events;
using DMS.Core.Models;
using DMS.Application.Interfaces;
using DMS.Core.Interfaces;
using DMS.Core.Models;
@@ -83,6 +84,11 @@ public class DataCenterService : IDataCenterService
/// </summary>
public event EventHandler<DataChangedEventArgs> DataChanged;
/// <summary>
/// 当变量值发生变化时触发
/// </summary>
public event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
#endregion
/// <summary>
@@ -823,4 +829,16 @@ public class DataCenterService : IDataCenterService
}
#endregion
#region
/// <summary>
/// 触发变量值变更事件
/// </summary>
public virtual void OnVariableValueChanged(VariableValueChangedEventArgs e)
{
VariableValueChanged?.Invoke(this, e);
}
#endregion
}

View File

@@ -0,0 +1,45 @@
namespace DMS.Core.Models
{
/// <summary>
/// 变量值变更事件参数
/// </summary>
public class VariableValueChangedEventArgs : EventArgs
{
/// <summary>
/// 变量ID
/// </summary>
public int VariableId { get; set; }
/// <summary>
/// 变量名称
/// </summary>
public string VariableName { get; set; }
/// <summary>
/// 旧值
/// </summary>
public string OldValue { get; set; }
/// <summary>
/// 新值
/// </summary>
public string NewValue { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdateTime { get; set; }
/// <summary>
/// 构造函数
/// </summary>
public VariableValueChangedEventArgs(int variableId, string variableName, string oldValue, string newValue, DateTime updateTime)
{
VariableId = variableId;
VariableName = variableName;
OldValue = oldValue;
NewValue = newValue;
UpdateTime = updateTime;
}
}
}

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;

View File

@@ -1,7 +1,9 @@
using System.Collections.ObjectModel;
using System.Windows;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Core.Models;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
using DMS.Core.Models;
@@ -13,7 +15,7 @@ namespace DMS.WPF.Services;
/// 数据服务类,负责从数据库加载和管理各种数据,并提供数据变更通知。
/// 继承自ObservableRecipient可以接收消息实现IRecipient<LoadMessage>,处理加载消息。
/// </summary>
public partial class DataServices : ObservableObject
public partial class DataServices : ObservableObject, IDisposable
{
private readonly IMapper _mapper;
private readonly IDataCenterService _dataCenterService;
@@ -53,8 +55,24 @@ public partial class DataServices : ObservableObject
// MQTT列表变更事件当MQTT配置数据更新时触发。
// public event Action<List<Mqtt>> OnMqttListChanged;
// 设备IsActive状态变更事件当单个设备的IsActive状态改变时触发。
public event Action<Device, bool> OnDeviceIsActiveChanged;
/// <summary>
/// 处理变量值变更事件
/// </summary>
private void OnVariableValueChanged(object sender, VariableValueChangedEventArgs e)
{
// 在UI线程上更新变量值
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
// 查找并更新对应的变量
var variableToUpdate = Variables.FirstOrDefault(v => v.Id == e.VariableId);
if (variableToUpdate != null)
{
variableToUpdate.DataValue = e.NewValue;
variableToUpdate.DisplayValue = e.NewValue;
variableToUpdate.UpdatedAt = e.UpdateTime;
}
}));
}
/// <summary>
@@ -73,6 +91,9 @@ public partial class DataServices : ObservableObject
Menus = new ObservableCollection<MenuItemViewModel>();
MenuTrees = new ObservableCollection<MenuItemViewModel>();
// AllVariables = new ConcurrentDictionary<int, Variable>();
// 监听变量值变更事件
_dataCenterService.VariableValueChanged += OnVariableValueChanged;
}
@@ -358,4 +379,16 @@ public partial class DataServices : ObservableObject
Variables.Remove(variableItem);
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 取消事件订阅
if (_dataCenterService != null)
{
_dataCenterService.VariableValueChanged -= OnVariableValueChanged;
}
}
}