修复数据类型属性不统一的问题

This commit is contained in:
2025-09-02 16:45:24 +08:00
parent b770abe3f9
commit 6d7636d664
16 changed files with 88 additions and 76 deletions

View File

@@ -27,7 +27,7 @@ public class VariableDto
public double AlarmMaxValue { get; set; }
public double AlarmDeadband { get; set; }
public ProtocolType Protocol { get; set; }
public CSharpDataType CSharpDataType { get; set; }
public DataType DataType { get; set; }
public string ConversionFormula { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }

View File

@@ -12,6 +12,7 @@ public class VariableTableDto
public string Description { get; set; }
public bool IsActive { get; set; }
public int DeviceId { get; set; }
public DeviceDto Device { get; set; }
public ProtocolType Protocol { get; set; }
public List<VariableDto> Variables { get; set; } = new();
}

View File

@@ -99,6 +99,7 @@ public class DeviceAppService : IDeviceAppService
throw new InvalidOperationException($"添加设备变量表失败,设备:{device.Name},变量表:{variableTable.Name}");
}
_mapper.Map(addVariableTable,dto.VariableTable);
dto.VariableTable.Device = dto.Device;
// 假设有设备菜单
if (dto.VariableTableMenu != null)

View File

@@ -9,7 +9,7 @@ namespace DMS.Core.Enums
/// <summary>
/// 定义了C#中常用的数据类型。
/// </summary>
public enum CSharpDataType
public enum DataType
{
// 基本数值类型
Bool,

View File

@@ -107,7 +107,7 @@ public class Variable
/// <summary>
/// 变量的数据类型。
/// </summary>
public CSharpDataType CSharpDataType { get; set; }
public DataType DataType { get; set; }
/// <summary>
/// 数值转换公式,例如 "+3*5"。

View File

@@ -73,7 +73,7 @@ namespace DMS.Infrastructure.UnitTests
dbVariable.AlarmMaxValue = 100;
dbVariable.AlarmDeadband = 1;
dbVariable.Protocol = 0;
dbVariable.CSharpDataType = 0;
dbVariable.DataType = 0;
dbVariable.CreatedAt = DateTime.Now;
dbVariable.UpdatedAt = DateTime.Now;
dbVariable.IsModified = false;
@@ -156,7 +156,7 @@ namespace DMS.Infrastructure.UnitTests
.RuleFor(v => v.AlarmMaxValue, f => f.Random.Double(50.0, 100.0))
.RuleFor(v => v.AlarmDeadband, f => f.Random.Double(0.0, 1.0))
.RuleFor(v => v.Protocol, f => f.PickRandom<ProtocolType>())
.RuleFor(v => v.CSharpDataType, f => f.PickRandom(Enum.GetValues<CSharpDataType>()))
.RuleFor(v => v.DataType, f => f.PickRandom(Enum.GetValues<DataType>()))
.RuleFor(v => v.OpcUaNodeId, f => $"ns=2;s=My.Variable{f.Random.Int(1, 100)}")
.RuleFor(v => v.ConversionFormula, f => "x * 1.0")
.RuleFor(v => v.UpdatedBy, f => f.Name.FullName())

View File

@@ -1,7 +1,6 @@
using DMS.Core.Enums;
using SqlSugar;
using SqlSugar.DbConvert;
using CSharpDataType = SqlSugar.CSharpDataType;
namespace DMS.Infrastructure.Entities;
@@ -113,7 +112,7 @@ public class DbVariable
/// 变量的数据类型。
/// </summary>
[SugarColumn(ColumnDataType="varchar(20)",SqlParameterDbType=typeof(EnumToStringConvert))]
public CSharpDataType CSharpDataType { get; set; }
public DataType DataType { get; set; }
/// <summary>
/// 数值转换公式 (例如: "+3*5"),可以为空。

View File

@@ -40,7 +40,7 @@ namespace DMS.Infrastructure.Models
/// <summary>
/// 数据类型
/// </summary>
public CSharpDataType DataType { get; set; }
public DataType DataType { get; set; }
/// <summary>
/// 返回节点的字符串表示形式。

View File

@@ -250,7 +250,7 @@ public class ExcelService : IExcelService
{
DMS.Core.Models.Variable variable = new DMS.Core.Models.Variable();
variable.Name = dataRow["Name"].ToString();
variable.CSharpDataType = (DMS.Core.Enums.CSharpDataType)Enum.Parse(typeof(DMS.Core.Enums.CSharpDataType), SiemensHelper.S7ToCSharpTypeString(dataRow["Data Type"].ToString()), true);
variable.DataType = (DMS.Core.Enums.DataType)Enum.Parse(typeof(DMS.Core.Enums.DataType), SiemensHelper.S7ToCSharpTypeString(dataRow["Data Type"].ToString()), true);
variable.SignalType = SignalType.OtherASignal;
var exS7Addr = dataRow["Logical Address"].ToString();
if (exS7Addr.StartsWith("%"))

View File

@@ -626,42 +626,42 @@ namespace DMS.Infrastructure.Services
/// </summary>
/// <param name="dataTypeId">数据类型NodeId</param>
/// <returns>数据类型的友好名称</returns>
private CSharpDataType GetDataTypeName(NodeId dataTypeId)
private DataType GetDataTypeName(NodeId dataTypeId)
{
if (dataTypeId == null)
return CSharpDataType.Unknown;
return DataType.Unknown;
// 使用OPC UA内置的类型映射
switch (dataTypeId.Identifier.ToString())
{
case "1": return CSharpDataType.Bool; // Boolean
case "2": return CSharpDataType.SByte; // SByte
case "3": return CSharpDataType.Byte; // Byte
case "4": return CSharpDataType.Short; // Int16
case "5": return CSharpDataType.UShort; // UInt16
case "6": return CSharpDataType.Int; // Int32
case "7": return CSharpDataType.UInt; // UInt32
case "8": return CSharpDataType.Long; // Int64
case "9": return CSharpDataType.ULong; // UInt64
case "10": return CSharpDataType.Float; // Float
case "11": return CSharpDataType.Double; // Double
case "12": return CSharpDataType.String; // String
case "13": return CSharpDataType.DateTime; // DateTime
case "14": return CSharpDataType.Guid; // Guid
case "15": return CSharpDataType.ByteArray; // ByteString
case "16": return CSharpDataType.Object; // XmlElement
case "17": return CSharpDataType.Object; // NodeId
case "18": return CSharpDataType.Object; // ExpandedNodeId
case "19": return CSharpDataType.Object; // StatusCode
case "20": return CSharpDataType.Object; // QualifiedName
case "21": return CSharpDataType.Object; // LocalizedText
case "22": return CSharpDataType.Object; // ExtensionObject
case "23": return CSharpDataType.Object; // DataValue
case "24": return CSharpDataType.Object; // Variant
case "25": return CSharpDataType.Object; // DiagnosticInfo
case "1": return DataType.Bool; // Boolean
case "2": return DataType.SByte; // SByte
case "3": return DataType.Byte; // Byte
case "4": return DataType.Short; // Int16
case "5": return DataType.UShort; // UInt16
case "6": return DataType.Int; // Int32
case "7": return DataType.UInt; // UInt32
case "8": return DataType.Long; // Int64
case "9": return DataType.ULong; // UInt64
case "10": return DataType.Float; // Float
case "11": return DataType.Double; // Double
case "12": return DataType.String; // String
case "13": return DataType.DateTime; // DateTime
case "14": return DataType.Guid; // Guid
case "15": return DataType.ByteArray; // ByteString
case "16": return DataType.Object; // XmlElement
case "17": return DataType.Object; // NodeId
case "18": return DataType.Object; // ExpandedNodeId
case "19": return DataType.Object; // StatusCode
case "20": return DataType.Object; // QualifiedName
case "21": return DataType.Object; // LocalizedText
case "22": return DataType.Object; // ExtensionObject
case "23": return DataType.Object; // DataValue
case "24": return DataType.Object; // Variant
case "25": return DataType.Object; // DiagnosticInfo
default:
// 对于自定义数据类型返回Unknown
return CSharpDataType.Unknown;
return DataType.Unknown;
}
}

View File

@@ -287,7 +287,7 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
if (existingItem.AlarmMaxValue != dto.AlarmMaxValue) existingItem.AlarmMaxValue = dto.AlarmMaxValue;
if (existingItem.AlarmDeadband != dto.AlarmDeadband) existingItem.AlarmDeadband = dto.AlarmDeadband;
if (existingItem.Protocol != dto.Protocol) existingItem.Protocol = dto.Protocol;
if (existingItem.CSharpDataType != dto.CSharpDataType) existingItem.CSharpDataType = dto.CSharpDataType;
if (existingItem.DataType != dto.DataType) existingItem.DataType = dto.DataType;
if (existingItem.ConversionFormula != dto.ConversionFormula)
existingItem.ConversionFormula = dto.ConversionFormula;
if (existingItem.CreatedAt != dto.CreatedAt) existingItem.CreatedAt = dto.CreatedAt;

View File

@@ -331,7 +331,7 @@ public partial class ImportOpcUaDialogViewModel : DialogViewModelBase<List<Varia
Name = child.DisplayName, // 变量名称
OpcUaNodeId = child.NodeId.ToString(), // OPC UA节点ID
Protocol = ProtocolType.OpcUa, // 协议类型
CSharpDataType = child.DataType, // C#数据类型
DataType = child.DataType, // C#数据类型
IsActive = true // 默认激活状态
});
}

View File

@@ -143,7 +143,7 @@ public partial class VariableItemViewModel : ObservableObject
/// 获取或设置变量在C#中对应的数据类型。
/// </summary>
[ObservableProperty]
private CSharpDataType _cSharpDataType;
private DataType _dataType;
/// <summary>
/// 获取或设置值的转换公式。

View File

@@ -277,6 +277,11 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
{
try
{
if (CurrentVariableTable.Device==null)
{
NotificationHelper.ShowError("当前变量表的Device对象为空请检查。");
return;
}
// 检查OPC UA Endpoint URL是否已设置
string opcUaEndpointUrl = CurrentVariableTable.Device.OpcUaServerUrl;
if (string.IsNullOrEmpty(opcUaEndpointUrl))
@@ -287,47 +292,53 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
// 显示OPC UA导入对话框让用户选择要导入的变量
ImportOpcUaDialogViewModel importOpcUaDialogViewModel = App.Current.Services.GetRequiredService<ImportOpcUaDialogViewModel>();
importOpcUaDialogViewModel.EndpointUrl = opcUaEndpointUrl; // 设置Endpoint URL
var importedVariables = await _dialogService.ShowDialogAsync(importOpcUaDialogViewModel);
if (importedVariables == null || !importedVariables.Any())
{
return; // 用户取消或没有选择任何变量
}
//var importedVariableDtos = _mapper.Map<List<VariableDto>>(importedVariables);
//foreach (var variableDto in importedVariableDtos)
//{
// variableDto.CreatedAt = DateTime.Now;
// variableDto.UpdatedAt = DateTime.Now;
// variableDto.VariableTableId = CurrentVariableTable.Id;
// variableDto.Protocol = ProtocolType.OpcUa; // 确保协议类型正确
//}
// 将导入的变量转换为DTO并设置必要的属性
var importedVariableDtos = _mapper.Map<List<VariableDto>>(importedVariables);
foreach (var variableDto in importedVariableDtos)
{
variableDto.CreatedAt = DateTime.Now;
variableDto.UpdatedAt = DateTime.Now;
variableDto.VariableTableId = CurrentVariableTable.Id;
}
//var existList = await _variableAppService.FindExistingVariablesAsync(importedVariableDtos);
//if (existList.Count > 0)
//{
// // 拼接要删除的变量名称,用于确认提示
// var existNames = string.Join("、", existList.Select(v => v.Name));
// var confrimDialogViewModel
// = new ConfirmDialogViewModel("存在已经添加的变量", $"变量名称:{existNames},已经存在,是否跳过继续添加其他的变量。取消则不添加任何变量", "继续");
// var res = await _dialogService.ShowDialogAsync(confrimDialogViewModel);
// if (!res) return;
// // 从导入列表中删除已经存在的变量
// importedVariableDtos.RemoveAll(variableDto => existList.Contains(variableDto));
//}
// 检查是否存在同名变量
var existList = await _variableAppService.FindExistingVariablesAsync(importedVariableDtos);
if (existList.Count > 0)
{
// 拼接要删除的变量名称,用于确认提示
var existNames = string.Join("、", existList.Select(v => v.Name));
var confirmDialogViewModel = new ConfirmDialogViewModel("存在已经添加的变量", $"变量名称:{existNames},已经存在,是否跳过继续添加其他的变量。取消则不添加任何变量", "继续");
var res = await _dialogService.ShowDialogAsync(confirmDialogViewModel);
if (!res) return;
// 从导入列表中删除已经存在的变量
importedVariableDtos.RemoveAll(variableDto => existList.Contains(variableDto));
}
//if (importedVariableDtos.Count != 0)
//{
// var isSuccess = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos);
// if (isSuccess)
// {
// _variableItemList.AddRange(_mapper.Map<List<VariableItemViewModel>>(importedVariableDtos));
// NotificationHelper.ShowSuccess($"从OPC UA服务器导入变量成功共导入变量{importedVariableDtos.Count}个");
// }
//}
//else
//{
// NotificationHelper.ShowSuccess($"列表中没有要添加的变量了。");
//}
// 如果还有变量需要导入,则执行导入操作
if (importedVariableDtos.Count != 0)
{
var isSuccess = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos);
if (isSuccess)
{
_variableItemList.AddRange(_mapper.Map<List<VariableItemViewModel>>(importedVariableDtos));
NotificationHelper.ShowSuccess($"从OPC UA服务器导入变量成功共导入变量{importedVariableDtos.Count}个");
}
else
{
NotificationHelper.ShowError("从OPC UA服务器导入变量失败");
}
}
else
{
NotificationHelper.ShowSuccess("列表中没有要添加的变量了。");
}
}
catch (Exception e)
{

View File

@@ -25,7 +25,7 @@
<ex:EnumBindingSource x:Key="ProtocolType" EnumType="{x:Type en:ProtocolType}" />
<ex:EnumBindingSource x:Key="SignalType" EnumType="{x:Type enums:SignalType}" />
<ex:EnumBindingSource x:Key="PollLevelType" EnumType="{x:Type enums:PollLevelType}" />
<ex:EnumBindingSource x:Key="CSharpDataType" EnumType="{x:Type enums:CSharpDataType}" />
<ex:EnumBindingSource x:Key="CSharpDataType" EnumType="{x:Type enums:DataType}" />
</ui:ContentDialog.Resources>
<ScrollViewer VerticalScrollBarVisibility="Auto">

View File

@@ -251,7 +251,7 @@
IsReadOnly="True"
Visibility="{Binding Source={StaticResource proxy}, Path=Data.IsOpcUaProtocolSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
<DataGridTextColumn
Binding="{Binding CSharpDataType}"
Binding="{Binding DataType}"
Header="数据类型"
IsReadOnly="True" />
<DataGridTemplateColumn Header="信号类型" SortMemberPath="SignalType">