From 7fad87fff542e1ea8ccd8ff18e8945c77a85a276 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Tue, 9 Sep 2025 15:57:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=98=E9=87=8F=E5=80=BC?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ILogManagementService.cs | 2 +- .../IMenuManagementService.cs | 2 +- .../IMqttManagementService.cs | 2 +- .../Services/IVariableManagementService.cs | 8 +++++++ .../Services/VariableManagementService.cs | 24 +++++++++++++++---- .../Services/OpcUaServiceManager.cs | 4 +++- DMS.WPF/Services/DataEventService.cs | 12 +++++----- DMS.WPF/Services/VariableTableDataService.cs | 7 ++++-- 8 files changed, 44 insertions(+), 17 deletions(-) rename DMS.Application/{Services => Interfaces}/ILogManagementService.cs (96%) rename DMS.Application/{Services => Interfaces}/IMenuManagementService.cs (97%) rename DMS.Application/{Services => Interfaces}/IMqttManagementService.cs (96%) diff --git a/DMS.Application/Services/ILogManagementService.cs b/DMS.Application/Interfaces/ILogManagementService.cs similarity index 96% rename from DMS.Application/Services/ILogManagementService.cs rename to DMS.Application/Interfaces/ILogManagementService.cs index eaa36c6..8737a1e 100644 --- a/DMS.Application/Services/ILogManagementService.cs +++ b/DMS.Application/Interfaces/ILogManagementService.cs @@ -1,7 +1,7 @@ using DMS.Application.DTOs; using DMS.Application.DTOs.Events; -namespace DMS.Application.Services; +namespace DMS.Application.Interfaces; public interface ILogManagementService { diff --git a/DMS.Application/Services/IMenuManagementService.cs b/DMS.Application/Interfaces/IMenuManagementService.cs similarity index 97% rename from DMS.Application/Services/IMenuManagementService.cs rename to DMS.Application/Interfaces/IMenuManagementService.cs index 4217f0c..b9586c0 100644 --- a/DMS.Application/Services/IMenuManagementService.cs +++ b/DMS.Application/Interfaces/IMenuManagementService.cs @@ -1,6 +1,6 @@ using DMS.Application.DTOs; -namespace DMS.Application.Services; +namespace DMS.Application.Interfaces; public interface IMenuManagementService { diff --git a/DMS.Application/Services/IMqttManagementService.cs b/DMS.Application/Interfaces/IMqttManagementService.cs similarity index 96% rename from DMS.Application/Services/IMqttManagementService.cs rename to DMS.Application/Interfaces/IMqttManagementService.cs index 11a0af9..c7f2238 100644 --- a/DMS.Application/Services/IMqttManagementService.cs +++ b/DMS.Application/Interfaces/IMqttManagementService.cs @@ -1,6 +1,6 @@ using DMS.Application.DTOs; -namespace DMS.Application.Services; +namespace DMS.Application.Interfaces; public interface IMqttManagementService { diff --git a/DMS.Application/Services/IVariableManagementService.cs b/DMS.Application/Services/IVariableManagementService.cs index 8a1267a..c4df21f 100644 --- a/DMS.Application/Services/IVariableManagementService.cs +++ b/DMS.Application/Services/IVariableManagementService.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; using DMS.Application.DTOs.Events; +using DMS.Core.Models; namespace DMS.Application.Services; @@ -56,8 +57,15 @@ public interface IVariableManagementService /// void RemoveVariableFromMemory(int variableId, ConcurrentDictionary variableTables); + void VariableValueChanged(VariableValueChangedEventArgs eventArgs); + /// /// 当变量数据发生变化时触发 /// event EventHandler OnVariableChanged; + + /// + /// 当变量数据发生变化时触发 + /// + event EventHandler OnVariableValueChanged; } \ No newline at end of file diff --git a/DMS.Application/Services/VariableManagementService.cs b/DMS.Application/Services/VariableManagementService.cs index 1397e79..cc07e6f 100644 --- a/DMS.Application/Services/VariableManagementService.cs +++ b/DMS.Application/Services/VariableManagementService.cs @@ -25,7 +25,13 @@ public class VariableManagementService : IVariableManagementService /// public event EventHandler OnVariableChanged; - public VariableManagementService(IVariableAppService variableAppService,IAppDataStorageService appDataStorageService) + /// + /// 当变量数据发生变化时触发 + /// + public event EventHandler OnVariableValueChanged; + + public VariableManagementService(IVariableAppService variableAppService, + IAppDataStorageService appDataStorageService) { _variableAppService = variableAppService; _appDataStorageService = appDataStorageService; @@ -102,14 +108,16 @@ public class VariableManagementService : IVariableManagementService if (_appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto)) { - OnVariableChanged?.Invoke(this,new VariableChangedEventArgs(DataChangeType.Added, variableDto, variableTableDto)); + OnVariableChanged?.Invoke( + this, new VariableChangedEventArgs(DataChangeType.Added, variableDto, variableTableDto)); } } /// /// 在内存中更新变量 /// - public void UpdateVariableInMemory(VariableDto variableDto, ConcurrentDictionary variableTables) + public void UpdateVariableInMemory(VariableDto variableDto, + ConcurrentDictionary variableTables) { VariableTableDto variableTableDto = null; if (variableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) @@ -118,7 +126,8 @@ public class VariableManagementService : IVariableManagementService } _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); - OnVariableChanged?.Invoke(this,new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); + OnVariableChanged?.Invoke( + this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); } /// @@ -135,8 +144,13 @@ public class VariableManagementService : IVariableManagementService variableTable.Variables.Remove(variableDto); } - OnVariableChanged?.Invoke(this,new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + OnVariableChanged?.Invoke( + this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); } } + public void VariableValueChanged(VariableValueChangedEventArgs eventArgs) + { + OnVariableValueChanged?.Invoke(this, eventArgs); + } } \ No newline at end of file diff --git a/DMS.Infrastructure/Services/OpcUaServiceManager.cs b/DMS.Infrastructure/Services/OpcUaServiceManager.cs index dc45f31..77e5e81 100644 --- a/DMS.Infrastructure/Services/OpcUaServiceManager.cs +++ b/DMS.Infrastructure/Services/OpcUaServiceManager.cs @@ -299,6 +299,8 @@ namespace DMS.Infrastructure.Services variable.DataValue = newValue; variable.DisplayValue = newValue; variable.UpdatedAt = DateTime.Now; + + _logger.LogDebug($"节点:{variable.OpcUaNodeId}值发生了变化:{newValue}"); // 触发变量值变更事件 var eventArgs = new VariableValueChangedEventArgs( @@ -308,7 +310,7 @@ namespace DMS.Infrastructure.Services newValue, variable.UpdatedAt); - // _appDataCenterService.OnVariableValueChanged( eventArgs); + _appDataCenterService.VariableManagementService.VariableValueChanged( eventArgs); // 推送到数据处理队列 await _dataProcessingService.EnqueueAsync(variable); diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index 1aefca9..448057e 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -37,7 +37,7 @@ public class DataEventService : IDataEventService // 监听变量值变更事件 - _appDataCenterService.VariableManagementService.OnVariableChanged += OnVariableValueChanged; + _appDataCenterService.VariableManagementService.OnVariableValueChanged += OnVariableValueChanged; _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; // 监听日志变更事件 // _appDataCenterService.OnLogChanged += _logDataService.OnNlogChanged; @@ -60,18 +60,18 @@ public class DataEventService : IDataEventService /// /// 处理变量值变更事件。 /// - private void OnVariableValueChanged(object? sender, VariableChangedEventArgs e) + private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) { // 在UI线程上更新变量值 App.Current.Dispatcher.BeginInvoke(new Action(() => { // 查找并更新对应的变量 - var variableToUpdate = _dataStorageService.Variables.FirstOrDefault(v => v.Id == e.Variable.Id); + var variableToUpdate = _dataStorageService.Variables.FirstOrDefault(v => v.Id == e.VariableId); if (variableToUpdate != null) { - variableToUpdate.DataValue = e.Variable.DataValue; - variableToUpdate.DisplayValue = e.Variable.DisplayValue; - variableToUpdate.UpdatedAt = e.Variable.UpdatedAt; + variableToUpdate.DataValue = e.NewValue; + variableToUpdate.DisplayValue = e.NewValue; + variableToUpdate.UpdatedAt = e.UpdateTime; } })); } diff --git a/DMS.WPF/Services/VariableTableDataService.cs b/DMS.WPF/Services/VariableTableDataService.cs index 6b71d8f..b00502d 100644 --- a/DMS.WPF/Services/VariableTableDataService.cs +++ b/DMS.WPF/Services/VariableTableDataService.cs @@ -32,9 +32,12 @@ public class VariableTableDataService : IVariableTableDataService public void LoadAllVariableTables() { - foreach (var variableTable in _dataStorageService.VariableTables) + foreach (var device in _dataStorageService.Devices) { - _dataStorageService.VariableTables.Add(variableTable); + foreach (var variableTable in device.VariableTables) + { + _dataStorageService.VariableTables.Add(variableTable); + } } }