mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-22 01:26:20 +08:00
39dca2c103
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.
439 lines
14 KiB
C#
439 lines
14 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 WriteValueListCtrl : Opc.Ua.Client.Controls.BaseListCtrl
|
|
{
|
|
public WriteValueListCtrl()
|
|
{
|
|
InitializeComponent();
|
|
SetColumns(m_ColumnNames);
|
|
}
|
|
|
|
#region Private Fields
|
|
private Session m_session;
|
|
|
|
/// <summary>
|
|
/// The columns to display in the control.
|
|
/// </summary>
|
|
private readonly object[][] m_ColumnNames = new object[][]
|
|
{
|
|
new object[] { "Name", HorizontalAlignment.Left, null },
|
|
new object[] { "NodeId", HorizontalAlignment.Left, null },
|
|
new object[] { "Attribute", HorizontalAlignment.Left, "Value" },
|
|
new object[] { "IndexRange", HorizontalAlignment.Left, "" },
|
|
new object[] { "Value", HorizontalAlignment.Left, "" },
|
|
new object[] { "Status", HorizontalAlignment.Left, "" },
|
|
new object[] { "Timestamp", HorizontalAlignment.Left, "" }
|
|
};
|
|
#endregion
|
|
|
|
#region Public Interface
|
|
/// <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, WriteValueCollection values)
|
|
{
|
|
if (session == null) throw new ArgumentNullException("session");
|
|
|
|
Clear();
|
|
|
|
m_session = session;
|
|
|
|
if (values != null)
|
|
{
|
|
foreach (WriteValue value in values)
|
|
{
|
|
AddItem(value);
|
|
}
|
|
}
|
|
|
|
AdjustColumns();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a value to the control.
|
|
/// </summary>
|
|
public void AddValue(ReferenceDescription reference)
|
|
{
|
|
Node node = m_session.NodeCache.Find(reference.NodeId) as Node;
|
|
|
|
if (node == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
WriteValue value = new WriteValue();
|
|
|
|
value.NodeId = node.NodeId;
|
|
value.AttributeId = Attributes.Value;
|
|
value.IndexRange = null;
|
|
|
|
// read the display name for non-variables.
|
|
if ((node.NodeClass & (NodeClass.Variable | NodeClass.VariableType)) == 0)
|
|
{
|
|
value.AttributeId = Attributes.DisplayName;
|
|
}
|
|
|
|
value.Value = GetDefaultValue(value.NodeId, value.AttributeId);
|
|
|
|
AddItem(value);
|
|
AdjustColumns();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Returns the items in the control.
|
|
/// </summary>
|
|
public WriteValueCollection GetValues()
|
|
{
|
|
WriteValueCollection values = new WriteValueCollection();
|
|
|
|
foreach (ListViewItem item in ItemsLV.Items)
|
|
{
|
|
WriteValue value = item.Tag as WriteValue;
|
|
|
|
if (value != null)
|
|
{
|
|
value.Value.StatusCode = StatusCodes.Good;
|
|
value.Value.ServerTimestamp = DateTime.MinValue;
|
|
value.Value.SourceTimestamp = DateTime.MinValue;
|
|
|
|
values.Add(value);
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a default value for the node.
|
|
/// </summary>
|
|
private DataValue GetDefaultValue(NodeId nodeId, uint attributeId)
|
|
{
|
|
// find the node.
|
|
Node node = m_session.NodeCache.Find(nodeId) as Node;
|
|
|
|
if (node == null)
|
|
{
|
|
return new DataValue();
|
|
}
|
|
|
|
// read the current attribute value.
|
|
DataValue value = new DataValue();
|
|
|
|
ServiceResult result = node.Read(null, attributeId, value);
|
|
|
|
if (ServiceResult.IsBad(result))
|
|
{
|
|
value.Value = GuiUtils.GetDefaultValue(Attributes.GetDataTypeId(attributeId), ValueRanks.Scalar);
|
|
}
|
|
|
|
// update the value attribute.
|
|
if (attributeId == Attributes.Value)
|
|
{
|
|
try
|
|
{
|
|
return m_session.ReadValue(node.NodeId);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
VariableNode variable = node as VariableNode;
|
|
|
|
if (variable != null)
|
|
{
|
|
value.Value = GuiUtils.GetDefaultValue(variable.DataType, variable.ValueRank);
|
|
}
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
#endregion
|
|
|
|
#region Overridden Methods
|
|
/// <see cref="BaseListCtrl.PickItems" />
|
|
protected override void PickItems()
|
|
{
|
|
base.PickItems();
|
|
EditValueMI_Click(this, null);
|
|
}
|
|
|
|
/// <see cref="BaseListCtrl.EnableMenuItems" />
|
|
protected override void EnableMenuItems(ListViewItem clickedItem)
|
|
{
|
|
NewMI.Enabled = true;
|
|
EditMI.Enabled = ItemsLV.SelectedItems.Count == 1;
|
|
EditValueMI.Enabled = ItemsLV.SelectedItems.Count == 1;
|
|
DeleteMI.Enabled = ItemsLV.SelectedItems.Count > 0;
|
|
}
|
|
|
|
/// <see cref="BaseListCtrl.UpdateItem" />
|
|
protected override void UpdateItem(ListViewItem listItem, object item)
|
|
{
|
|
WriteValue value = item as WriteValue;
|
|
|
|
if (value == null)
|
|
{
|
|
base.UpdateItem(listItem, item);
|
|
return;
|
|
}
|
|
|
|
if (value.Value == null)
|
|
{
|
|
value.Value = new DataValue();
|
|
}
|
|
|
|
if (value.Value.Value == null)
|
|
{
|
|
value.Value = GetDefaultValue(value.NodeId, value.AttributeId);
|
|
}
|
|
|
|
Node node = m_session.NodeCache.Find(value.NodeId) as Node;
|
|
|
|
if (node != null)
|
|
{
|
|
listItem.SubItems[0].Text = String.Format("{0}", node);
|
|
}
|
|
else
|
|
{
|
|
listItem.SubItems[0].Text = String.Format("{0}", value.NodeId);
|
|
}
|
|
|
|
listItem.SubItems[1].Text = String.Format("{0}", value.NodeId);
|
|
listItem.SubItems[2].Text = String.Format("{0}", Attributes.GetBrowseName(value.AttributeId));
|
|
listItem.SubItems[3].Text = String.Format("{0}", value.IndexRange);
|
|
listItem.SubItems[4].Text = String.Format("{0}", value.Value.Value);
|
|
listItem.SubItems[5].Text = String.Format("{0}", value.Value.StatusCode);
|
|
listItem.SubItems[6].Text = String.Format("{0}", value.Value.SourceTimestamp);
|
|
|
|
listItem.Tag = item;
|
|
listItem.ImageKey = "DataType";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles a drop event.
|
|
/// </summary>
|
|
protected override void ItemsLV_DragDrop(object sender, DragEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
ReferenceDescription reference = e.Data.GetData(typeof(ReferenceDescription)) as ReferenceDescription;
|
|
|
|
if (reference == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
AddValue(reference);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Event Handlers
|
|
private void NewMI_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
WriteValue value = new WriteValue();
|
|
|
|
if (new WriteValueEditDlg().ShowDialog(m_session, value))
|
|
{
|
|
AddItem(value);
|
|
}
|
|
|
|
AdjustColumns();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
|
|
private void EditMI_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
WriteValue[] values = GetSelectedItems(typeof(WriteValue)) as WriteValue[];
|
|
|
|
if (values == null || values.Length != 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (new WriteValueEditDlg().ShowDialog(m_session, values[0]))
|
|
{
|
|
Node node = m_session.NodeCache.Find(values[0].NodeId) as Node;
|
|
|
|
if (node != null)
|
|
{
|
|
DataValue value = new DataValue();
|
|
|
|
ServiceResult result = node.Read(null, values[0].AttributeId, value);
|
|
|
|
if (ServiceResult.IsGood(result))
|
|
{
|
|
values[0].Value = value;
|
|
}
|
|
}
|
|
|
|
UpdateItem(ItemsLV.SelectedItems[0], values[0]);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
|
|
private void EditValueMI_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
WriteValue[] values = GetSelectedItems(typeof(WriteValue)) as WriteValue[];
|
|
|
|
if (values == null || values.Length != 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
NodeId datatypeId = Attributes.GetDataTypeId(values[0].AttributeId);
|
|
int valueRank = Attributes.GetValueRank(values[0].AttributeId);
|
|
|
|
if (values[0].AttributeId == Attributes.Value)
|
|
{
|
|
VariableNode node = m_session.NodeCache.Find(values[0].NodeId) as VariableNode;
|
|
|
|
if (node != null)
|
|
{
|
|
datatypeId = node.DataType;
|
|
valueRank = node.ValueRank;
|
|
}
|
|
}
|
|
|
|
bool useIndexRange = false;
|
|
object value = null;
|
|
|
|
// check IndexRange for arrays
|
|
if (values[0].AttributeId == Attributes.Value && valueRank > 0)
|
|
{
|
|
NumericRange indexRange;
|
|
ServiceResult result = NumericRange.Validate(values[0].IndexRange, out indexRange);
|
|
|
|
if (ServiceResult.IsGood(result) && indexRange != NumericRange.Empty)
|
|
{
|
|
useIndexRange = true;
|
|
}
|
|
}
|
|
|
|
if (useIndexRange)
|
|
{
|
|
value = new ComplexValueEditDlg().ShowDialog(values[0]);
|
|
|
|
WriteValue writeValue = value as WriteValue;
|
|
|
|
if (writeValue != null)
|
|
{
|
|
value = writeValue.Value.Value;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
value = GuiUtils.EditValue(m_session, values[0].Value.Value, datatypeId, valueRank);
|
|
}
|
|
|
|
if (value != null)
|
|
{
|
|
values[0].Value.Value = value;
|
|
values[0].Value.StatusCode = StatusCodes.Good;
|
|
|
|
UpdateItem(ItemsLV.SelectedItems[0], values[0]);
|
|
|
|
AdjustColumns();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
|
|
private void DeleteMI_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
List<ListViewItem> itemsToDelete = new List<ListViewItem>();
|
|
|
|
foreach (ListViewItem item in ItemsLV.SelectedItems)
|
|
{
|
|
itemsToDelete.Add(item);
|
|
}
|
|
|
|
foreach (ListViewItem item in itemsToDelete)
|
|
{
|
|
item.Remove();
|
|
}
|
|
|
|
AdjustColumns();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|