初步完成变更新
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Application.DTOs.Events;
|
using DMS.Application.DTOs.Events;
|
||||||
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Application.Interfaces;
|
namespace DMS.Application.Interfaces;
|
||||||
|
|
||||||
@@ -305,5 +306,13 @@ public interface IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
event EventHandler<DataChangedEventArgs> DataChanged;
|
event EventHandler<DataChangedEventArgs> DataChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量值发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
|
||||||
|
|
||||||
|
void OnVariableValueChanged(VariableValueChangedEventArgs e);
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Application.DTOs.Events;
|
using DMS.Application.DTOs.Events;
|
||||||
|
using DMS.Core.Models;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Core.Interfaces;
|
using DMS.Core.Interfaces;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
@@ -83,6 +84,11 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<DataChangedEventArgs> DataChanged;
|
public event EventHandler<DataChangedEventArgs> DataChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量值发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<VariableValueChangedEventArgs> VariableValueChanged;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -823,4 +829,16 @@ public class DataCenterService : IDataCenterService
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 事件触发方法
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发变量值变更事件
|
||||||
|
/// </summary>
|
||||||
|
public virtual void OnVariableValueChanged(VariableValueChangedEventArgs e)
|
||||||
|
{
|
||||||
|
VariableValueChanged?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
45
DMS.Core/Models/VariableValueChangedEventArgs.cs
Normal file
45
DMS.Core/Models/VariableValueChangedEventArgs.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Infrastructure.Configuration;
|
using DMS.Infrastructure.Configuration;
|
||||||
using DMS.Infrastructure.Interfaces.Services;
|
using DMS.Infrastructure.Interfaces.Services;
|
||||||
using DMS.Infrastructure.Services;
|
using DMS.Infrastructure.Services;
|
||||||
@@ -15,6 +16,17 @@ namespace DMS.Infrastructure.Extensions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static IServiceCollection AddOpcUaServices(this IServiceCollection services)
|
public static IServiceCollection AddOpcUaServices(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
// 注册配置选项
|
||||||
|
services.Configure<OpcUaServiceOptions>(
|
||||||
|
options => {
|
||||||
|
// 可以从配置文件或其他来源加载配置
|
||||||
|
});
|
||||||
|
|
||||||
|
// 注册服务
|
||||||
|
services.AddSingleton<IOpcUaServiceManager, OpcUaServiceManager>();
|
||||||
|
|
||||||
|
// 注册后台服务
|
||||||
|
services.AddHostedService<OptimizedOpcUaBackgroundService>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Diagnostics;
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
|
using DMS.Core.Models;
|
||||||
using DMS.Infrastructure.Configuration;
|
using DMS.Infrastructure.Configuration;
|
||||||
using DMS.Infrastructure.Interfaces.Services;
|
using DMS.Infrastructure.Interfaces.Services;
|
||||||
using DMS.Infrastructure.Models;
|
using DMS.Infrastructure.Models;
|
||||||
@@ -18,6 +19,7 @@ namespace DMS.Infrastructure.Services
|
|||||||
{
|
{
|
||||||
private readonly ILogger<OpcUaServiceManager> _logger;
|
private readonly ILogger<OpcUaServiceManager> _logger;
|
||||||
private readonly IDataProcessingService _dataProcessingService;
|
private readonly IDataProcessingService _dataProcessingService;
|
||||||
|
private readonly IDataCenterService _dataCenterService;
|
||||||
private readonly OpcUaServiceOptions _options;
|
private readonly OpcUaServiceOptions _options;
|
||||||
private readonly ConcurrentDictionary<int, DeviceContext> _deviceContexts;
|
private readonly ConcurrentDictionary<int, DeviceContext> _deviceContexts;
|
||||||
private readonly SemaphoreSlim _semaphore;
|
private readonly SemaphoreSlim _semaphore;
|
||||||
@@ -26,10 +28,12 @@ namespace DMS.Infrastructure.Services
|
|||||||
public OpcUaServiceManager(
|
public OpcUaServiceManager(
|
||||||
ILogger<OpcUaServiceManager> logger,
|
ILogger<OpcUaServiceManager> logger,
|
||||||
IDataProcessingService dataProcessingService,
|
IDataProcessingService dataProcessingService,
|
||||||
|
IDataCenterService dataCenterService,
|
||||||
IOptions<OpcUaServiceOptions> options)
|
IOptions<OpcUaServiceOptions> options)
|
||||||
{
|
{
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
_dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService));
|
||||||
|
_dataCenterService = dataCenterService ?? throw new ArgumentNullException(nameof(dataCenterService));
|
||||||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||||
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
|
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
|
||||||
_semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections);
|
_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))
|
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.DataValue = newValue;
|
||||||
variable.DisplayValue = opcUaNode.Value.ToString();
|
variable.DisplayValue = newValue;
|
||||||
variable.UpdatedAt = DateTime.Now;
|
variable.UpdatedAt = DateTime.Now;
|
||||||
|
|
||||||
|
// 触发变量值变更事件
|
||||||
|
var eventArgs = new VariableValueChangedEventArgs(
|
||||||
|
variable.Id,
|
||||||
|
variable.Name,
|
||||||
|
oldValue,
|
||||||
|
newValue,
|
||||||
|
variable.UpdatedAt);
|
||||||
|
|
||||||
|
_dataCenterService.OnVariableValueChanged( eventArgs);
|
||||||
|
|
||||||
// 推送到数据处理队列
|
// 推送到数据处理队列
|
||||||
await _dataProcessingService.EnqueueAsync(variable);
|
await _dataProcessingService.EnqueueAsync(variable);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Core.Models;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Core.Enums;
|
using DMS.Core.Enums;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
@@ -13,7 +15,7 @@ namespace DMS.WPF.Services;
|
|||||||
/// 数据服务类,负责从数据库加载和管理各种数据,并提供数据变更通知。
|
/// 数据服务类,负责从数据库加载和管理各种数据,并提供数据变更通知。
|
||||||
/// 继承自ObservableRecipient,可以接收消息;实现IRecipient<LoadMessage>,处理加载消息。
|
/// 继承自ObservableRecipient,可以接收消息;实现IRecipient<LoadMessage>,处理加载消息。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class DataServices : ObservableObject
|
public partial class DataServices : ObservableObject, IDisposable
|
||||||
{
|
{
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
private readonly IDataCenterService _dataCenterService;
|
private readonly IDataCenterService _dataCenterService;
|
||||||
@@ -53,8 +55,24 @@ public partial class DataServices : ObservableObject
|
|||||||
// MQTT列表变更事件,当MQTT配置数据更新时触发。
|
// MQTT列表变更事件,当MQTT配置数据更新时触发。
|
||||||
// public event Action<List<Mqtt>> OnMqttListChanged;
|
// public event Action<List<Mqtt>> OnMqttListChanged;
|
||||||
|
|
||||||
// 设备IsActive状态变更事件,当单个设备的IsActive状态改变时触发。
|
/// <summary>
|
||||||
public event Action<Device, bool> OnDeviceIsActiveChanged;
|
/// 处理变量值变更事件
|
||||||
|
/// </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>
|
/// <summary>
|
||||||
@@ -73,6 +91,9 @@ public partial class DataServices : ObservableObject
|
|||||||
Menus = new ObservableCollection<MenuItemViewModel>();
|
Menus = new ObservableCollection<MenuItemViewModel>();
|
||||||
MenuTrees = new ObservableCollection<MenuItemViewModel>();
|
MenuTrees = new ObservableCollection<MenuItemViewModel>();
|
||||||
// AllVariables = new ConcurrentDictionary<int, Variable>();
|
// AllVariables = new ConcurrentDictionary<int, Variable>();
|
||||||
|
|
||||||
|
// 监听变量值变更事件
|
||||||
|
_dataCenterService.VariableValueChanged += OnVariableValueChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -358,4 +379,16 @@ public partial class DataServices : ObservableObject
|
|||||||
|
|
||||||
Variables.Remove(variableItem);
|
Variables.Remove(variableItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 释放资源
|
||||||
|
/// </summary>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// 取消事件订阅
|
||||||
|
if (_dataCenterService != null)
|
||||||
|
{
|
||||||
|
_dataCenterService.VariableValueChanged -= OnVariableValueChanged;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user