Skip to content

Commit

Permalink
Enhance GetBuiltintype to support simple datatypes (#1488)
Browse files Browse the repository at this point in the history
* Return the correct BuiltInType for Simple numeric type ids in nanespace 0

* Added unit test BuiltInTypeOfSimpleDataTypes
  • Loading branch information
mrsuciu committed Aug 19, 2021
1 parent 105d211 commit 198cec6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Stack/Opc.Ua.Core/Types/Utils/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,39 @@ public static BuiltInType GetBuiltInType(NodeId datatypeId)
{
return BuiltInType.Null;
}
switch ((uint)datatypeId.Identifier)
{
// subtype of DateTime
case DataTypes.UtcTime: return BuiltInType.DateTime;
// subtype of ByteString
case DataTypes.ApplicationInstanceCertificate:
case DataTypes.AudioDataType:
case DataTypes.ContinuationPoint:
case DataTypes.Image:
case DataTypes.ImageBMP:
case DataTypes.ImageGIF:
case DataTypes.ImageJPG:
case DataTypes.ImagePNG: return BuiltInType.ByteString;
// subtype of NodeId
case DataTypes.SessionAuthenticationToken: return BuiltInType.NodeId;
// subtype of Double
case DataTypes.Duration: return BuiltInType.Double;
// subtype of UInt32
case DataTypes.IntegerId:
case DataTypes.Index:
case DataTypes.VersionTime:
case DataTypes.Counter: return BuiltInType.UInt32;
// subtype of UInt64
case DataTypes.BitFieldMaskDataType: return BuiltInType.UInt64;
// subtype of String
case DataTypes.DateString:
case DataTypes.DecimalString:
case DataTypes.DurationString:
case DataTypes.LocaleId:
case DataTypes.NormalizedString:
case DataTypes.NumericRange:
case DataTypes.TimeString: return BuiltInType.String;
}

return (BuiltInType)Enum.ToObject(typeof(BuiltInType), datatypeId.Identifier);
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/Opc.Ua.Core.Tests/Types/Utils/UtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using NUnit.Framework;
Expand Down Expand Up @@ -222,6 +225,49 @@ public void ExponentialEntityExpansionProcessing()
TestContext.Out.WriteLine(ex.Message);

}

/// <summary>
/// Test the built-in type of the Simple data types
/// </summary>
[Test]
public void BuiltInTypeOfSimpleDataTypes()

{
Assert.AreEqual(BuiltInType.DateTime, TypeInfo.GetBuiltInType(DataTypeIds.UtcTime));

List<string> bnList = new List<string> { BrowseNames.ApplicationInstanceCertificate, BrowseNames.AudioDataType,
BrowseNames.ContinuationPoint, BrowseNames.Image,
BrowseNames.ImageBMP, BrowseNames.ImageGIF,
BrowseNames.ImageJPG, BrowseNames.ImagePNG };
foreach (string name in bnList)
{
var staticValue = typeof(DataTypeIds).GetFields(BindingFlags.Public | BindingFlags.Static).Where(f => f.Name == name).First().GetValue(null);
Assert.AreEqual(BuiltInType.ByteString, TypeInfo.GetBuiltInType((NodeId)staticValue));
}

Assert.AreEqual(BuiltInType.NodeId, TypeInfo.GetBuiltInType(DataTypeIds.SessionAuthenticationToken));
Assert.AreEqual(BuiltInType.Double, TypeInfo.GetBuiltInType(DataTypeIds.Duration));

bnList = new List<string> { BrowseNames.IntegerId, BrowseNames.Index, BrowseNames.VersionTime, BrowseNames.Counter };
foreach (string name in bnList)
{
var staticValue = typeof(DataTypeIds).GetFields(BindingFlags.Public | BindingFlags.Static).Where(f => f.Name == name).First().GetValue(null);
Assert.AreEqual(BuiltInType.UInt32, TypeInfo.GetBuiltInType((NodeId)staticValue));
}

Assert.AreEqual(BuiltInType.UInt64, TypeInfo.GetBuiltInType(DataTypeIds.BitFieldMaskDataType));

bnList = new List<string> { BrowseNames.DateString, BrowseNames.DecimalString,
BrowseNames.DurationString, BrowseNames.LocaleId,
BrowseNames.NormalizedString, BrowseNames.NumericRange,
BrowseNames.TimeString };
foreach (string name in bnList)
{
var staticValue = typeof(DataTypeIds).GetFields(BindingFlags.Public | BindingFlags.Static).Where(f => f.Name == name).First().GetValue(null);
Assert.AreEqual(BuiltInType.String, TypeInfo.GetBuiltInType((NodeId)staticValue));
}
}

#endregion
}

Expand Down

0 comments on commit 198cec6

Please sign in to comment.