mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-11-05 01:53:58 +08:00
可指定OPCUA节点数据类型
This commit is contained in:
@@ -5,7 +5,10 @@ internal class OPCUATag : BaseDataVariableState
|
||||
public OPCUATag(NodeState parent) : base(parent)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 变量数据类型
|
||||
/// </summary>
|
||||
public Type NETDataType { get; set; }
|
||||
/// <summary>
|
||||
/// 变量Id
|
||||
/// </summary>
|
||||
|
||||
@@ -2,6 +2,8 @@ using Mapster;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using NewLife.Reflection;
|
||||
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Server;
|
||||
|
||||
@@ -234,7 +236,16 @@ public class ThingsGatewayNodeManager : CustomNodeManager2
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
_idTags[nodeId].Value = value;
|
||||
object newValue;
|
||||
try
|
||||
{
|
||||
newValue=Convert.ChangeType(value, _idTags[nodeId].NETDataType);
|
||||
}
|
||||
catch
|
||||
{
|
||||
newValue= value;
|
||||
}
|
||||
_idTags[nodeId].Value = newValue;
|
||||
_idTags[nodeId].Timestamp = dateTime;
|
||||
_idTags[nodeId].ClearChangeMasks(SystemContext, false);
|
||||
}
|
||||
@@ -286,6 +297,7 @@ public class ThingsGatewayNodeManager : CustomNodeManager2
|
||||
variable.ValueRank = ValueRanks.Scalar;
|
||||
variable.Id = variableRunTime.Id;
|
||||
variable.DataType = DataNodeType(variableRunTime);
|
||||
variable.NETDataType = NETDataNodeType(variableRunTime);
|
||||
var level = ProtectTypeTrans(variableRunTime);
|
||||
variable.AccessLevel = level;
|
||||
variable.UserAccessLevel = level;
|
||||
@@ -391,6 +403,43 @@ public class ThingsGatewayNodeManager : CustomNodeManager2
|
||||
return DataTypeIds.TimeString;
|
||||
return DataTypeIds.ObjectNode;
|
||||
}
|
||||
/// <summary>
|
||||
/// 网关转OPC数据类型
|
||||
/// </summary>
|
||||
/// <param name="variableRunTime"></param>
|
||||
/// <returns></returns>
|
||||
private Type NETDataNodeType(CollectVariableRunTime variableRunTime)
|
||||
{
|
||||
Type tp;
|
||||
var str = GetPropertyValue(variableRunTime, nameof(OPCUAServerVariableProperty.DataTypeEnum));
|
||||
if (Enum.TryParse<DataTypeEnum>(str, out DataTypeEnum result))
|
||||
{
|
||||
tp = result.GetNetType();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (variableRunTime.Value != null)
|
||||
{
|
||||
tp = variableRunTime.Value.GetType();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (variableRunTime.ReadExpressions.IsNullOrEmpty())
|
||||
{
|
||||
tp = variableRunTime.DataTypeEnum.GetNetType();
|
||||
}
|
||||
else
|
||||
{
|
||||
var tp1 = variableRunTime.DataTypeEnum.GetNetType();
|
||||
var data = variableRunTime.ReadExpressions.GetExpressionsResult(GetDefaultValue(tp1));
|
||||
tp = data.GetType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tp;
|
||||
}
|
||||
|
||||
private ServiceResult OnWriteDataValue(ISystemContext context, NodeState node, NumericRange indexRange, QualifiedName dataEncoding, ref object value, ref StatusCode statusCode, ref DateTime timestamp)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ public class VariableData
|
||||
/// <inheritdoc cref="CollectVariableRunTime.RawValue"/>
|
||||
public string rawValue { get; set; }
|
||||
/// <inheritdoc cref="CollectVariableRunTime.Value"/>
|
||||
public string value { get; set; }
|
||||
public object value { get; set; }
|
||||
/// <inheritdoc cref="CollectVariableRunTime.ChangeTime"/>
|
||||
public DateTime changeTime { get; set; }
|
||||
/// <inheritdoc cref="CollectVariableRunTime.CollectTime"/>
|
||||
|
||||
Reference in New Issue
Block a user