添加 double 类型的变量值

This commit is contained in:
2025-09-13 08:24:06 +08:00
parent c173ab08d3
commit 5a39796f0e
5 changed files with 120 additions and 0 deletions

View File

@@ -12,7 +12,36 @@ public class VariableDto
public string Name { get; set; } public string Name { get; set; }
public string S7Address { get; set; } public string S7Address { get; set; }
public string DataValue { get; set; } public string DataValue { get; set; }
public double NumericValue { get; set; }
public string DisplayValue { get; set; } public string DisplayValue { get; set; }
/// <summary>
/// 更新数值属性根据DataValue的值进行转换。
/// </summary>
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 VariableTableDto? VariableTable { get; set; }
public List<VariableMqttAliasDto>? MqttAliases { get; set; } = new List<VariableMqttAliasDto>(); public List<VariableMqttAliasDto>? MqttAliases { get; set; } = new List<VariableMqttAliasDto>();
public SignalType SignalType { get; set; } public SignalType SignalType { get; set; }

View File

@@ -99,6 +99,40 @@ public class Variable
/// </summary> /// </summary>
public string DataValue { get; set; } public string DataValue { get; set; }
/// <summary>
/// 存储从设备读取到的最新数值。如果原始值是布尔类型则True转换为1.0False转换为0.0。
/// 此属性不应持久化到数据库,仅用于运行时。
/// </summary>
public double NumericValue { get; set; }
/// <summary>
/// 更新数值属性根据DataValue的值进行转换。
/// </summary>
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;
}
}
/// <summary> /// <summary>
/// 变量的通讯协议。 /// 变量的通讯协议。
/// </summary> /// </summary>

View File

@@ -81,6 +81,30 @@ namespace DMS.Infrastructure.Services
{ {
if (_session != null) 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(); await _session.CloseAsync();
_session = null; _session = null;
} }

View File

@@ -336,6 +336,7 @@ namespace DMS.Infrastructure.Services
// 更新变量值 // 更新变量值
variable.DataValue = newValue; variable.DataValue = newValue;
variable.DisplayValue = newValue; variable.DisplayValue = newValue;
variable.UpdateNumericValue(); // 更新数值属性
variable.UpdatedAt = DateTime.Now; variable.UpdatedAt = DateTime.Now;
_logger.LogDebug($"节点:{variable.OpcUaNodeId}值发生了变化:{newValue}"); _logger.LogDebug($"节点:{variable.OpcUaNodeId}值发生了变化:{newValue}");

View File

@@ -41,6 +41,12 @@ public partial class VariableItemViewModel : ObservableObject
[ObservableProperty] [ObservableProperty]
private string? _dataValue; private string? _dataValue;
/// <summary>
/// 获取或设置从设备读取到的数值。如果原始值是布尔类型则True转换为1.0False转换为0.0。
/// </summary>
[ObservableProperty]
private double _numericValue;
/// <summary> /// <summary>
/// 获取或设置用于在界面上显示的值。 /// 获取或设置用于在界面上显示的值。
/// 这可能是经过公式转换、格式化或添加了单位后的值。 /// 这可能是经过公式转换、格式化或添加了单位后的值。
@@ -48,6 +54,32 @@ public partial class VariableItemViewModel : ObservableObject
[ObservableProperty] [ObservableProperty]
private string? _displayValue; 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;
}
}
/// <summary> /// <summary>
/// 获取或设置此变量所属的变量表 (VariableTable) 的数据传输对象 (DTO)。 /// 获取或设置此变量所属的变量表 (VariableTable) 的数据传输对象 (DTO)。
/// 用于在界面上显示变量表的关联信息。 /// 用于在界面上显示变量表的关联信息。