Files
UA-.NETStandard/SampleApplications/Samples/GDS/ClientControls/Controls/SelectGdsDialog.cs
T
2019-02-14 12:34:33 +02:00

128 lines
4.5 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 System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Opc.Ua.Gds.Client.Controls
{
public partial class SelectGdsDialog : Form
{
public SelectGdsDialog()
{
InitializeComponent();
Icon = ImageListControl.AppIcon;
}
private GlobalDiscoveryServerClient m_gds;
public string ShowDialog(IWin32Window owner, GlobalDiscoveryServerClient gds, IList<string> serverUrls)
{
m_gds = gds;
ServersListBox.Items.Clear();
foreach (var serverUrl in serverUrls)
{
ServersListBox.Items.Add(serverUrl);
}
ServerUrlTextBox.Text = gds.EndpointUrl;
UserNameCredentialsRB.Checked = true;
OkButton.Enabled = Uri.IsWellFormedUriString(ServerUrlTextBox.Text.Trim(), UriKind.Absolute);
if (base.ShowDialog(owner) != DialogResult.OK)
{
return null;
}
return ServerUrlTextBox.Text.Trim();
}
private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
{
ServerUrlTextBox.Text = ServersListBox.SelectedItem as string;
}
private void ServerUrlTextBox_TextChanged(object sender, EventArgs e)
{
OkButton.Enabled = Uri.IsWellFormedUriString(ServerUrlTextBox.Text.Trim(), UriKind.Absolute);
}
private void OkButton_Click(object sender, EventArgs e)
{
try
{
string url = ServerUrlTextBox.Text.Trim();
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
throw new ArgumentException("The URL is not valid: " + url, "ServerUrl");
}
try
{
Cursor = Cursors.WaitCursor;
var endpoint = CoreClientUtils.SelectEndpoint(url, true, 5000);
if (UserNameCredentialsRB.Checked)
{
if (endpoint.FindUserTokenPolicy(UserTokenType.UserName, (string)null) == null)
{
throw new ArgumentException("Server does not support username/password user identity tokens.");
}
var identity = new Opc.Ua.Client.Controls.UserNamePasswordDlg().ShowDialog(m_gds.AdminCredentials, "Provide GDS Administrator Credentials");
if (identity != null)
{
m_gds.AdminCredentials = identity;
}
}
m_gds.Connect(url);
}
finally
{
Cursor = Cursors.Default;
}
DialogResult = System.Windows.Forms.DialogResult.OK;
}
catch (Exception ex)
{
Opc.Ua.Client.Controls.ExceptionDlg.Show(Text, ex);
}
}
}
}