diff --git a/DMS.Application/DTOs/VariableDto.cs b/DMS.Application/DTOs/VariableDto.cs index 2a0b2c3..b39ae39 100644 --- a/DMS.Application/DTOs/VariableDto.cs +++ b/DMS.Application/DTOs/VariableDto.cs @@ -12,7 +12,36 @@ public class VariableDto public string Name { get; set; } public string S7Address { get; set; } public string DataValue { get; set; } + public double NumericValue { get; set; } public string DisplayValue { get; set; } + + /// + /// 更新数值属性,根据DataValue的值进行转换。 + /// + public void UpdateNumericValue() + { + if (string.IsNullOrEmpty(DataValue)) + { + NumericValue = 0.0; + return; + } + + // 尝试将字符串转换为数值 + if (double.TryParse(DataValue, out double numericValue)) + { + NumericValue = numericValue; + } + // 如果是布尔值 + else if (bool.TryParse(DataValue, out bool boolValue)) + { + NumericValue = boolValue ? 1.0 : 0.0; + } + // 如果无法转换,保持为0.0 + else + { + NumericValue = 0.0; + } + } public VariableTableDto? VariableTable { get; set; } public List? MqttAliases { get; set; } = new List(); public SignalType SignalType { get; set; } diff --git a/DMS.Core/Models/Variable.cs b/DMS.Core/Models/Variable.cs index 69b55fa..a041509 100644 --- a/DMS.Core/Models/Variable.cs +++ b/DMS.Core/Models/Variable.cs @@ -99,6 +99,40 @@ public class Variable /// public string DataValue { get; set; } + /// + /// 存储从设备读取到的最新数值。如果原始值是布尔类型,则True转换为1.0,False转换为0.0。 + /// 此属性不应持久化到数据库,仅用于运行时。 + /// + public double NumericValue { get; set; } + + /// + /// 更新数值属性,根据DataValue的值进行转换。 + /// + public void UpdateNumericValue() + { + if (string.IsNullOrEmpty(DataValue)) + { + NumericValue = 0.0; + return; + } + + // 尝试将字符串转换为数值 + if (double.TryParse(DataValue, out double numericValue)) + { + NumericValue = numericValue; + } + // 如果是布尔值 + else if (bool.TryParse(DataValue, out bool boolValue)) + { + NumericValue = boolValue ? 1.0 : 0.0; + } + // 如果无法转换,保持为0.0 + else + { + NumericValue = 0.0; + } + } + /// /// 变量的通讯协议。 /// diff --git a/DMS.Infrastructure/Services/OpcUaService.cs b/DMS.Infrastructure/Services/OpcUaService.cs index 04e3d5d..9db6ec4 100644 --- a/DMS.Infrastructure/Services/OpcUaService.cs +++ b/DMS.Infrastructure/Services/OpcUaService.cs @@ -81,6 +81,30 @@ namespace DMS.Infrastructure.Services { if (_session != null) { + // 取消所有订阅 + if (_subscription != null) + { + try + { + // 删除服务器上的订阅 + _subscription.Delete(true); + // 从会话中移除订阅 + _session.RemoveSubscription(_subscription); + // 释放订阅资源 + _subscription.Dispose(); + _subscription = null; + } + catch (Exception ex) + { + Console.WriteLine($"取消订阅时发生错误: {ex.Message}"); + // 即使取消订阅失败,也继续关闭会话 + } + } + + // 清理订阅节点跟踪字典 + _subscribedNodes.Clear(); + + // 关闭会话 await _session.CloseAsync(); _session = null; } diff --git a/DMS.Infrastructure/Services/OpcUaServiceManager.cs b/DMS.Infrastructure/Services/OpcUaServiceManager.cs index 4d77a8c..69d40c7 100644 --- a/DMS.Infrastructure/Services/OpcUaServiceManager.cs +++ b/DMS.Infrastructure/Services/OpcUaServiceManager.cs @@ -336,6 +336,7 @@ namespace DMS.Infrastructure.Services // 更新变量值 variable.DataValue = newValue; variable.DisplayValue = newValue; + variable.UpdateNumericValue(); // 更新数值属性 variable.UpdatedAt = DateTime.Now; _logger.LogDebug($"节点:{variable.OpcUaNodeId}值发生了变化:{newValue}"); diff --git a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs index 469d684..842ac98 100644 --- a/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/VariableItemViewModel.cs @@ -41,6 +41,12 @@ public partial class VariableItemViewModel : ObservableObject [ObservableProperty] private string? _dataValue; + /// + /// 获取或设置从设备读取到的数值。如果原始值是布尔类型,则True转换为1.0,False转换为0.0。 + /// + [ObservableProperty] + private double _numericValue; + /// /// 获取或设置用于在界面上显示的值。 /// 这可能是经过公式转换、格式化或添加了单位后的值。 @@ -48,6 +54,32 @@ public partial class VariableItemViewModel : ObservableObject [ObservableProperty] private string? _displayValue; + partial void OnDataValueChanged(string? oldValue, string? newValue) + { + // 当DataValue发生变化时,更新NumericValue + if (string.IsNullOrEmpty(newValue)) + { + NumericValue = 0.0; + return; + } + + // 尝试将字符串转换为数值 + if (double.TryParse(newValue, out double numericValue)) + { + NumericValue = numericValue; + } + // 如果是布尔值 + else if (bool.TryParse(newValue, out bool boolValue)) + { + NumericValue = boolValue ? 1.0 : 0.0; + } + // 如果无法转换,保持为0.0 + else + { + NumericValue = 0.0; + } + } + /// /// 获取或设置此变量所属的变量表 (VariableTable) 的数据传输对象 (DTO)。 /// 用于在界面上显示变量表的关联信息。