Files
UA-.NETStandard/SampleApplications/Samples/Controls.Net4/Common/AttributeListCtrl.cs
T
Martin Regen 39dca2c103 Complex Types client library (#849)
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.
2019-11-20 16:05:24 +01:00

584 lines
19 KiB
C#

/* ========================================================================
* 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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Opc.Ua.Client;
using Opc.Ua.Client.Controls;
namespace Opc.Ua.Sample.Controls
{
public partial class AttributeListCtrl : Opc.Ua.Client.Controls.BaseListCtrl
{
public AttributeListCtrl()
{
InitializeComponent();
SetColumns(m_ColumnNames);
}
#region Private Fields
private Session m_session;
private NodeId m_nodeId;
private bool m_readOnly;
/// <summary>
/// The columns to display in the control.
/// </summary>
private readonly object[][] m_ColumnNames = new object[][]
{
new object[] { "Field", HorizontalAlignment.Left, null },
new object[] { "Value", HorizontalAlignment.Left, null },
new object[] { "Status", HorizontalAlignment.Left, "Good" }
};
#endregion
#region Public Interface
/// <summary>
///
/// </summary>
public bool ReadOnly
{
get { return m_readOnly; }
set { m_readOnly = value; }
}
/// <summary>
/// Clears the contents of the control,
/// </summary>
public void Clear()
{
ItemsLV.Items.Clear();
AdjustColumns();
}
/// <summary>
/// Sets the nodes in the control.
/// </summary>
public void Initialize(Session session, ExpandedNodeId nodeId)
{
if (session == null) throw new ArgumentNullException("session");
Clear();
if (nodeId == null)
{
return;
}
m_session = session;
m_nodeId = (NodeId)nodeId;
INode node = m_session.NodeCache.Find(m_nodeId);
if (node != null && (node.NodeClass & (NodeClass.Variable | NodeClass.Object)) != 0)
{
AddReferences(ReferenceTypeIds.HasTypeDefinition, BrowseDirection.Forward);
AddReferences(ReferenceTypeIds.HasModellingRule, BrowseDirection.Forward);
}
AddAttributes();
AddProperties();
AdjustColumns();
}
#endregion
#region NodeField Class
/// <summary>
/// A field associated with a node.
/// </summary>
private class NodeField
{
public string Name;
public object Value;
public StatusCode StatusCode;
public DiagnosticInfo DiagnosticInfo;
public ReadValueId ValueId;
}
#endregion
#region Private Methods
/// <summary>
/// Adds the attributes to the control.
/// </summary>
private void AddAttributes()
{
// build list of attributes to read.
ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
foreach (uint attributeId in Attributes.GetIdentifiers())
{
ReadValueId valueId = new ReadValueId();
valueId.NodeId = m_nodeId;
valueId.AttributeId = attributeId;
valueId.IndexRange = null;
valueId.DataEncoding = null;
nodesToRead.Add(valueId);
}
// read attributes.
DataValueCollection values;
DiagnosticInfoCollection diagnosticInfos;
m_session.Read(
null,
0,
TimestampsToReturn.Neither,
nodesToRead,
out values,
out diagnosticInfos);
ClientBase.ValidateResponse(values, nodesToRead);
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);
// update control.
for (int ii = 0; ii < nodesToRead.Count; ii++)
{
// check if node supports attribute.
if (values[ii].StatusCode == StatusCodes.BadAttributeIdInvalid)
{
continue;
}
NodeField field = new NodeField();
field.ValueId = nodesToRead[ii];
field.Name = Attributes.GetBrowseName(nodesToRead[ii].AttributeId);
field.Value = values[ii].Value;
field.StatusCode = values[ii].StatusCode;
if (diagnosticInfos != null && diagnosticInfos.Count > ii)
{
field.DiagnosticInfo = diagnosticInfos[ii];
}
AddItem(field, "SimpleItem", -1);
}
}
/// <summary>
/// Adds the properties to the control.
/// </summary>
private void AddProperties()
{
// build list of properties to read.
ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
Browser browser = new Browser(m_session);
browser.BrowseDirection = BrowseDirection.Forward;
browser.ReferenceTypeId = ReferenceTypeIds.HasProperty;
browser.IncludeSubtypes = true;
browser.NodeClassMask = (int)NodeClass.Variable;
browser.ContinueUntilDone = true;
ReferenceDescriptionCollection references = browser.Browse(m_nodeId);
foreach (ReferenceDescription reference in references)
{
ReadValueId valueId = new ReadValueId();
valueId.NodeId = (NodeId)reference.NodeId;
valueId.AttributeId = Attributes.Value;
valueId.IndexRange = null;
valueId.DataEncoding = null;
nodesToRead.Add(valueId);
}
// check for empty list.
if (nodesToRead.Count == 0)
{
return;
}
// read values.
DataValueCollection values;
DiagnosticInfoCollection diagnosticInfos;
m_session.Read(
null,
0,
TimestampsToReturn.Neither,
nodesToRead,
out values,
out diagnosticInfos);
ClientBase.ValidateResponse(values, nodesToRead);
ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);
// update control.
for (int ii = 0; ii < nodesToRead.Count; ii++)
{
NodeField field = new NodeField();
field.ValueId = nodesToRead[ii];
field.Name = references[ii].ToString();
field.Value = values[ii].Value;
field.StatusCode = values[ii].StatusCode;
if (diagnosticInfos != null && diagnosticInfos.Count > ii)
{
field.DiagnosticInfo = diagnosticInfos[ii];
}
AddItem(field, "Property", -1);
}
}
/// <summary>
/// Adds the targets of references to the control.
/// </summary>
private void AddReferences(NodeId referenceTypeId, BrowseDirection browseDirection)
{
// fetch the attributes for the reference type.
INode referenceType = m_session.NodeCache.Find(referenceTypeId);
if (referenceType == null)
{
return;
}
// browse for the references.
Browser browser = new Browser(m_session);
browser.BrowseDirection = browseDirection;
browser.ReferenceTypeId = referenceTypeId;
browser.IncludeSubtypes = true;
browser.NodeClassMask = 0;
browser.ContinueUntilDone = true;
ReferenceDescriptionCollection references = browser.Browse(m_nodeId);
// add results to list.
foreach (ReferenceDescription reference in references)
{
NodeField field = new NodeField();
field.Name = referenceType.ToString();
field.Value = reference.ToString();
field.StatusCode = StatusCodes.Good;
AddItem(field, "ReferenceType", -1);
}
}
/// <summary>
/// Formats the value of an attribute.
/// </summary>
private string FormatAttributeValue(uint attributeId, object value)
{
switch (attributeId)
{
case Attributes.NodeClass:
{
if (value != null)
{
return String.Format("{0}", Enum.ToObject(typeof(NodeClass), value));
}
return "(null)";
}
case Attributes.DataType:
{
NodeId datatypeId = value as NodeId;
if (datatypeId != null)
{
INode datatype = m_session.NodeCache.Find(datatypeId);
if (datatype != null)
{
return String.Format("{0}", datatype.DisplayName.Text);
}
else
{
return String.Format("{0}", datatypeId);
}
}
return String.Format("{0}", value);
}
case Attributes.ValueRank:
{
int? valueRank = value as int?;
if (valueRank != null)
{
switch (valueRank.Value)
{
case ValueRanks.Scalar: return "Scalar";
case ValueRanks.OneDimension: return "OneDimension";
case ValueRanks.OneOrMoreDimensions: return "OneOrMoreDimensions";
case ValueRanks.Any: return "Any";
default:
{
return String.Format("{0}", valueRank.Value);
}
}
}
return String.Format("{0}", value);
}
case Attributes.MinimumSamplingInterval:
{
double? minimumSamplingInterval = value as double?;
if (minimumSamplingInterval != null)
{
if (minimumSamplingInterval.Value == MinimumSamplingIntervals.Indeterminate)
{
return "Indeterminate";
}
else if (minimumSamplingInterval.Value == MinimumSamplingIntervals.Continuous)
{
return "Continuous";
}
return String.Format("{0}", minimumSamplingInterval.Value);
}
return String.Format("{0}", value);
}
case Attributes.AccessLevel:
case Attributes.UserAccessLevel:
{
byte accessLevel = Convert.ToByte(value);
StringBuilder bits = new StringBuilder();
if ((accessLevel & AccessLevels.CurrentRead) != 0)
{
bits.Append("Readable");
}
if ((accessLevel & AccessLevels.CurrentWrite) != 0)
{
if (bits.Length > 0)
{
bits.Append(" | ");
}
bits.Append("Writeable");
}
if ((accessLevel & AccessLevels.HistoryRead) != 0)
{
if (bits.Length > 0)
{
bits.Append(" | ");
}
bits.Append("History");
}
if ((accessLevel & AccessLevels.HistoryWrite) != 0)
{
if (bits.Length > 0)
{
bits.Append(" | ");
}
bits.Append("History Update");
}
if (bits.Length == 0)
{
bits.Append("No Access");
}
return String.Format("{0}", bits);
}
case Attributes.EventNotifier:
{
byte notifier = Convert.ToByte(value);
StringBuilder bits = new StringBuilder();
if ((notifier & EventNotifiers.SubscribeToEvents) != 0)
{
bits.Append("Subscribe");
}
if ((notifier & EventNotifiers.HistoryRead) != 0)
{
if (bits.Length > 0)
{
bits.Append(" | ");
}
bits.Append("History");
}
if ((notifier & EventNotifiers.HistoryWrite) != 0)
{
if (bits.Length > 0)
{
bits.Append(" | ");
}
bits.Append("History Update");
}
if (bits.Length == 0)
{
bits.Append("No Access");
}
return String.Format("{0}", bits);
}
default:
{
return String.Format("{0}", value);
}
}
}
#endregion
#region Overridden Methods
/// <see cref="BaseListCtrl.GetDataToDrag" />
protected override object GetDataToDrag()
{
ReadValueIdCollection valueIds = new ReadValueIdCollection();
foreach (ListViewItem listItem in ItemsLV.SelectedItems)
{
NodeField field = listItem.Tag as NodeField;
if (field != null && field.ValueId != null)
{
valueIds.Add(field.ValueId);
}
}
return valueIds;
}
/// <see cref="BaseListCtrl.PickItems" />
protected override void PickItems()
{
base.PickItems();
EditMI_Click(this, null);
}
/// <see cref="BaseListCtrl.EnableMenuItems" />
protected override void EnableMenuItems(ListViewItem clickedItem)
{
RefreshMI.Enabled = true;
NodeField[] items = GetSelectedItems(typeof(NodeField)) as NodeField[];
if (items != null && items.Length > 0)
{
ViewMI.Enabled = items.Length == 1;
}
}
/// <see cref="BaseListCtrl.UpdateItem" />
protected override void UpdateItem(ListViewItem listItem, object item)
{
NodeField field = item as NodeField;
if (field == null)
{
base.UpdateItem(listItem, item);
return;
}
Array array = field.Value as Array;
listItem.SubItems[0].Text = String.Format("{0}", field.Name);
if (array == null)
{
if (field.ValueId != null)
{
listItem.SubItems[1].Text = FormatAttributeValue(field.ValueId.AttributeId, field.Value);
}
else
{
listItem.SubItems[1].Text = String.Format("{0}", field.Value);
}
}
else
{
listItem.SubItems[1].Text = String.Format("{0}[{1}]", field.Value.GetType().GetElementType().Name, array.Length);
}
listItem.SubItems[2].Text = String.Format("{0}", field.StatusCode);
listItem.Tag = item;
}
#endregion
private void EditMI_Click(object sender, EventArgs e)
{
try
{
NodeField[] items = GetSelectedItems(typeof(NodeField)) as NodeField[];
if (items != null && items.Length == 1)
{
object value = GuiUtils.EditValue(m_session, items[0].Value);
if (!m_readOnly)
{
if (value != null)
{
items[0].Value = value;
UpdateItem(ItemsLV.SelectedItems[0], items[0]);
}
}
}
}
catch (Exception exception)
{
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
}
}
}
}