Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a complex type system client library extension for custom enumerations and structure types based on a server dictionary or DataTypeDefinition using emitted classes at runtime. - Custom types are added to the session factory, not to the global factory. Types are loaded when the session connects by adding ` var complexTypeSystem = new ComplexTypeSystem(session); await complexTypeSystem.Load(); ` - Custom type properties can be accessed with an accessor [propertyname] or [index]. Direct access through the properties is possible, but some cases do not update the optional or union flags correctly. - Support includes V1.03 dictionaries and V1.04 DataTypeDefinitions (--> known issues). - NetCoreComplexClient can be used for quick testing servers. It prints out all custom types with content and subscribes to all. Also includes sample how to write back complex type variables and how to output JSON. - The UA Sample Client and the Quickstart Data Type client can be used for testing with UI. Many issues were adressed to be able to read and write complex types, however some support is limited, e.g. only editing simple fields is currently working (help wanted). - Caveat: The complex type libaries for .Net Core must be built for .netcoreapp, so it needs a seperate nuget package for the complex type client extension. - Support for a factory type builder class to allow to plugin other type builder methods (e.g. Codedom etc...) - Added JSON test case and include the complex types client in the testing shell script, so the complex types lib is tested on mac/linux too. - Few fixes in the JSON encoder. Finding enough servers with custom types is an issue, so if you find servers with unsupported custom types please provide feedback. Known limitations: - Only V1.03 OPC binary dictionaries are supported (no XML Schema dictionaries). - V1.03 dictionary structure definitions are only supported if they can be mapped to a V1.04 DataTypeDefinition. Nested types and cross dictionary refs should work. - OptionSet is only partially implemented. Only shows the byte arrays, not the flags. - Opaque Types in dictionaries are ignored.
- Loading branch information
Showing
110 changed files
with
4,951 additions
and
12,386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
807 changes: 807 additions & 0 deletions
807
SampleApplications/SDK/Opc.Ua.Client.ComplexTypes/ComplexTypeSystem.cs
Large diffs are not rendered by default.
Oops, something went wrong.
295 changes: 295 additions & 0 deletions
295
SampleApplications/SDK/Opc.Ua.Client.ComplexTypes/DataTypeDefinitionExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,295 @@ | ||
/* ======================================================================== | ||
* Copyright (c) 2005-2019 The OPC Foundation, Inc. All rights reserved. | ||
* | ||
* OPC Foundation MIT License 1.00 | ||
* | ||
* Permission is hereby granted, free of charge, to any person | ||
* obtaining a copy of this software and associated documentation | ||
* files (the "Software"), to deal in the Software without | ||
* restriction, including without limitation the rights to use, | ||
* copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following | ||
* conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
* OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* The complete license agreement can be found here: | ||
* http://opcfoundation.org/License/MIT/1.00/ | ||
* ======================================================================*/ | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml; | ||
|
||
namespace Opc.Ua.Client.ComplexTypes | ||
{ | ||
/// <summary> | ||
/// Extensions to convert binary schema type definitions to DataTypeDefinitions. | ||
/// </summary> | ||
public static class DataTypeDefinitionExtension | ||
{ | ||
#region Public Extensions | ||
/// <summary> | ||
/// Convert a binary schema type definition to a | ||
/// StructureDefinition. | ||
/// </summary> | ||
/// <remarks> | ||
/// Support for: | ||
/// - Structures, structures with optional fields and unions. | ||
/// - Nested types and typed arrays with length field. | ||
/// The converter has the following known restrictions: | ||
/// - Support only for V1.03 structured types which can be mapped to the V1.04 | ||
/// structured type definition. | ||
/// The following dictionary tags cause bail out for a structure: | ||
/// - use of a terminator of length in bytes | ||
/// - an array length field is not a direct predecessor of the array | ||
/// - The switch value of a union is not the first field. | ||
/// - The selector bits of optional fields are not stored in a 32 bit variable | ||
/// and do not add up to 32 bits. | ||
/// </remarks> | ||
public static StructureDefinition ToStructureDefinition( | ||
this Schema.Binary.StructuredType structuredType, | ||
ExpandedNodeId defaultEncodingId, | ||
IList<INode> typeDictionary, | ||
NamespaceTable namespaceTable) | ||
{ | ||
var structureDefinition = new StructureDefinition() | ||
{ | ||
BaseDataType = null, | ||
DefaultEncodingId = ExpandedNodeId.ToNodeId(defaultEncodingId, namespaceTable), | ||
Fields = new StructureFieldCollection(), | ||
StructureType = StructureType.Structure | ||
}; | ||
|
||
bool isSupportedType = true; | ||
bool hasBitField = false; | ||
bool isUnionType = false; | ||
|
||
foreach (var field in structuredType.Field) | ||
{ | ||
// check for yet unsupported properties | ||
if (field.IsLengthInBytes || | ||
field.Terminator != null) | ||
{ | ||
isSupportedType = false; | ||
} | ||
|
||
if (field.SwitchValue != 0) | ||
{ | ||
isUnionType = true; | ||
} | ||
|
||
if (field.TypeName.Namespace == Namespaces.OpcBinarySchema || | ||
field.TypeName.Namespace == Namespaces.OpcUa) | ||
{ | ||
if (field.TypeName.Name == "Bit") | ||
{ | ||
hasBitField = true; | ||
continue; | ||
} | ||
} | ||
if (field.Length != 0) | ||
{ | ||
isSupportedType = false; | ||
} | ||
} | ||
|
||
// test forbidden combinations | ||
if (!isSupportedType) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"The structure definition uses a Terminator or LengthInBytes, which are not supported."); | ||
} | ||
|
||
if (isUnionType && hasBitField) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"The structure definition combines a Union and a bit filed, both of which are not supported in a single structure."); | ||
} | ||
|
||
if (isUnionType) | ||
{ | ||
structureDefinition.StructureType = StructureType.Union; | ||
} | ||
|
||
if (hasBitField) | ||
{ | ||
structureDefinition.StructureType = StructureType.StructureWithOptionalFields; | ||
} | ||
|
||
byte switchFieldBitPosition = 0; | ||
Int32 dataTypeFieldPosition = 0; | ||
var switchFieldBits = new Dictionary<string, byte>(); | ||
// convert fields | ||
foreach (var field in structuredType.Field) | ||
{ | ||
// consume optional bits | ||
if (field.TypeName.IsXmlBitType()) | ||
{ | ||
var count = structureDefinition.Fields.Count; | ||
if (count == 0 && | ||
switchFieldBitPosition < 32) | ||
{ | ||
structureDefinition.StructureType = StructureType.StructureWithOptionalFields; | ||
byte fieldLength = (byte)((field.Length == 0) ? 1u : field.Length); | ||
switchFieldBits[field.Name] = switchFieldBitPosition; | ||
switchFieldBitPosition += fieldLength; | ||
} | ||
else | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"Options for bit selectors must be 32 bit in size, use the Int32 datatype and must be the first element in the structure."); | ||
} | ||
continue; | ||
} | ||
|
||
if (switchFieldBitPosition != 0 && | ||
switchFieldBitPosition != 32) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"Bitwise option selectors must have 32 bits."); | ||
} | ||
|
||
var dataTypeField = new StructureField() | ||
{ | ||
Name = field.Name, | ||
Description = null, | ||
DataType = field.TypeName.ToNodeId(typeDictionary, namespaceTable), | ||
IsOptional = false, | ||
MaxStringLength = 0, | ||
ArrayDimensions = null, | ||
ValueRank = -1 | ||
}; | ||
|
||
if (field.LengthField != null) | ||
{ | ||
// handle array length | ||
var lastField = structureDefinition.Fields.Last(); | ||
if (lastField.Name != field.LengthField) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"The length field must precede the type field of an array."); | ||
} | ||
lastField.Name = field.Name; | ||
lastField.DataType = field.TypeName.ToNodeId(typeDictionary, namespaceTable); | ||
lastField.ValueRank = 1; | ||
} | ||
else | ||
{ | ||
if (isUnionType) | ||
{ | ||
// ignore the switchfield | ||
if (field.SwitchField == null) | ||
{ | ||
if (structureDefinition.Fields.Count != 0) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"The switch field of a union must be the first field in the complex type."); | ||
} | ||
continue; | ||
} | ||
if (structureDefinition.Fields.Count != dataTypeFieldPosition) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
"The count of the switch field of the union member is not matching the field position."); | ||
} | ||
dataTypeFieldPosition++; | ||
} | ||
else | ||
{ | ||
if (field.SwitchField != null) | ||
{ | ||
dataTypeField.IsOptional = true; | ||
byte value; | ||
if (!switchFieldBits.TryGetValue(field.SwitchField, out value)) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadNotSupported, | ||
$"The switch field for {field.SwitchField} does not exist."); | ||
} | ||
} | ||
} | ||
structureDefinition.Fields.Add(dataTypeField); | ||
} | ||
} | ||
|
||
return structureDefinition; | ||
} | ||
|
||
/// <summary> | ||
/// Test for special Bit type used in the binary schema structure definition. | ||
/// </summary> | ||
private static bool IsXmlBitType(this XmlQualifiedName typeName) | ||
{ | ||
if (typeName.Namespace == Namespaces.OpcBinarySchema || | ||
typeName.Namespace == Namespaces.OpcUa) | ||
{ | ||
if (typeName.Name == "Bit") | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Look up the node id for a qualified name of a type | ||
/// in a binary schema type definition. | ||
/// </summary> | ||
private static NodeId ToNodeId( | ||
this XmlQualifiedName typeName, | ||
IList<INode> typeCollection, | ||
NamespaceTable namespaceTable) | ||
{ | ||
if (typeName.Namespace == Namespaces.OpcBinarySchema || | ||
typeName.Namespace == Namespaces.OpcUa) | ||
{ | ||
switch (typeName.Name) | ||
{ | ||
case "CharArray": return DataTypeIds.String; | ||
case "Variant": return DataTypeIds.BaseDataType; | ||
case "ExtensionObject": return DataTypeIds.Structure; | ||
} | ||
var internalField = typeof(DataTypeIds).GetField(typeName.Name); | ||
if (internalField == null) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadDataTypeIdUnknown, | ||
$"The type {typeName.Name} was not found in the internal type factory."); | ||
} | ||
return (NodeId)internalField.GetValue(typeName.Name); | ||
} | ||
else | ||
{ | ||
var referenceId = typeCollection.Where(t => | ||
t.DisplayName == typeName.Name && | ||
t.NodeId.NamespaceUri == typeName.Namespace).FirstOrDefault(); | ||
if (referenceId == null) | ||
{ | ||
// TODO: servers may have multiple dictionaries with different | ||
// target namespace but types are stored for all in the same namespace. | ||
// Find a way to find the right namespace here | ||
referenceId = typeCollection.Where(t => | ||
t.DisplayName == typeName.Name).FirstOrDefault(); | ||
if (referenceId == null) | ||
{ | ||
throw new ServiceResultException(StatusCodes.BadDataTypeIdUnknown, | ||
$"The type {typeName.Name} in namespace {typeName.Namespace} was not found."); | ||
} | ||
} | ||
return ExpandedNodeId.ToNodeId(referenceId.NodeId, namespaceTable); | ||
} | ||
} | ||
#endregion | ||
} | ||
}//namespace |
Oops, something went wrong.