mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-25 03:43:05 +08:00
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.
274 lines
9.3 KiB
C#
274 lines
9.3 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 Opc.Ua.Client;
|
|
using Opc.Ua.Client.ComplexTypes;
|
|
using Opc.Ua.Client.Controls;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Opc.Ua.Sample.Controls
|
|
{
|
|
public partial class SessionOpenDlg : Form
|
|
{
|
|
#region Constructors
|
|
public SessionOpenDlg()
|
|
{
|
|
InitializeComponent();
|
|
this.Icon = ClientUtils.GetAppIcon();
|
|
}
|
|
#endregion
|
|
|
|
#region Private Fields
|
|
private Session m_session;
|
|
private const string m_BrowseCertificates = "<Browse...>";
|
|
private static long m_Counter = 0;
|
|
private IList<string> m_preferredLocales;
|
|
private bool m_checkDomain = true;
|
|
#endregion
|
|
|
|
#region Public Interface
|
|
/// <summary>
|
|
/// Displays the dialog.
|
|
/// </summary>
|
|
public bool ShowDialog(Session session, IList<string> preferredLocales)
|
|
{
|
|
if (session == null) throw new ArgumentNullException("session");
|
|
|
|
m_session = session;
|
|
m_preferredLocales = preferredLocales;
|
|
|
|
UserIdentityTypeCB.Items.Clear();
|
|
|
|
foreach (UserTokenPolicy policy in session.Endpoint.UserIdentityTokens)
|
|
{
|
|
UserIdentityTypeCB.Items.Add(policy.TokenType);
|
|
}
|
|
|
|
if (UserIdentityTypeCB.Items.Count == 0)
|
|
{
|
|
UserIdentityTypeCB.Items.Add(UserTokenType.UserName);
|
|
}
|
|
|
|
UserIdentityTypeCB.SelectedIndex = 0;
|
|
|
|
SessionNameTB.Text = session.SessionName;
|
|
|
|
if (String.IsNullOrEmpty(SessionNameTB.Text))
|
|
{
|
|
SessionNameTB.Text = Utils.Format("MySession {0}", Utils.IncrementIdentifier(ref m_Counter));
|
|
}
|
|
|
|
if (session.Identity != null)
|
|
{
|
|
UserIdentityTypeCB.SelectedItem = session.Identity.TokenType;
|
|
}
|
|
|
|
if (ShowDialog() != DialogResult.OK)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
private void UserIdentityTypeCB_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
UserTokenType tokenType = (UserTokenType)UserIdentityTypeCB.SelectedItem;
|
|
|
|
UserNameCB.Items.Clear();
|
|
|
|
UserNameCB.Enabled = true;
|
|
PasswordTB.Enabled = true;
|
|
|
|
// allow use to browse certificate stores.
|
|
if (tokenType == UserTokenType.Certificate)
|
|
{
|
|
UserNameCB.Items.Add(m_BrowseCertificates);
|
|
UserNameCB.SelectedIndex = 0;
|
|
}
|
|
|
|
// populate list.
|
|
foreach (IUserIdentity identity in m_session.IdentityHistory)
|
|
{
|
|
if (identity.TokenType == tokenType)
|
|
{
|
|
UserNameCB.Items.Add(identity.DisplayName);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
|
|
private void OkBTN_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// construct the user identity.
|
|
IUserIdentity identity = null;
|
|
|
|
if ((UserTokenType)UserIdentityTypeCB.SelectedItem == UserTokenType.UserName)
|
|
{
|
|
string username = (string)UserNameCB.SelectedItem;
|
|
|
|
if (String.IsNullOrEmpty(username))
|
|
{
|
|
username = UserNameCB.Text;
|
|
}
|
|
|
|
if (!String.IsNullOrEmpty(username) || !String.IsNullOrEmpty(PasswordTB.Text))
|
|
{
|
|
identity = new UserIdentity(username, PasswordTB.Text);
|
|
}
|
|
}
|
|
|
|
Cursor = Cursors.WaitCursor;
|
|
|
|
ThreadPool.QueueUserWorkItem(Open, new object[] { m_session, SessionNameTB.Text, identity, m_preferredLocales, m_checkDomain });
|
|
|
|
CancelBTN.Enabled = false;
|
|
OkBTN.Enabled = false;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reports the results of the open session operation.
|
|
/// </summary>
|
|
private void OpenComplete(object e)
|
|
{
|
|
if (InvokeRequired)
|
|
{
|
|
BeginInvoke(new WaitCallback(OpenComplete), e);
|
|
return;
|
|
}
|
|
|
|
if (IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
Cursor = Cursors.Default;
|
|
|
|
ServiceResultException sre = e as ServiceResultException;
|
|
if (sre != null)
|
|
{
|
|
if (m_checkDomain && sre.StatusCode == StatusCodes.BadCertificateHostNameInvalid)
|
|
{
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
buffer.AppendFormat(sre.Message);
|
|
buffer.AppendFormat("\r\n\r\nRetry without certificate hostname validation?");
|
|
|
|
DialogResult result = MessageBox.Show(
|
|
buffer.ToString(),
|
|
"Exception: BadCertificateHostNameInvalid",
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Warning);
|
|
|
|
if (result == DialogResult.Yes)
|
|
{
|
|
m_checkDomain = false;
|
|
OkBTN.Enabled = true;
|
|
OkBTN.PerformClick();
|
|
return;
|
|
}
|
|
|
|
DialogResult = DialogResult.OK;
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (e != null)
|
|
{
|
|
GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), (Exception)e);
|
|
}
|
|
|
|
if (m_session.Connected && m_session.SessionTimeout < 1000)
|
|
{
|
|
DialogResult result = MessageBox.Show(
|
|
"Warning: the session time out might be too small: " + m_session.SessionTimeout,
|
|
"Session revised timeout",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Warning);
|
|
}
|
|
|
|
DialogResult = DialogResult.OK;
|
|
}
|
|
finally
|
|
{
|
|
CancelBTN.Enabled = true;
|
|
OkBTN.Enabled = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously open the session.
|
|
/// </summary>
|
|
private void Open(object state)
|
|
{
|
|
try
|
|
{
|
|
Session session = ((object[])state)[0] as Session;
|
|
string sessionName = ((object[])state)[1] as string;
|
|
IUserIdentity identity = ((object[])state)[2] as IUserIdentity;
|
|
IList<string> preferredLocales = ((object[])state)[3] as IList<string>;
|
|
bool? checkDomain = ((object[])state)[4] as bool?;
|
|
|
|
// open the session.
|
|
session.Open(sessionName, (uint)session.SessionTimeout, identity, preferredLocales, checkDomain ?? true);
|
|
|
|
var typeSystemLoader = new ComplexTypeSystem(session);
|
|
typeSystemLoader.Load().Wait();
|
|
|
|
OpenComplete(null);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
OpenComplete(exception);
|
|
}
|
|
}
|
|
}
|
|
}
|