protected void GetEnpoints_Click(object sender, EventArgs e) { if (discoveryTextBox.Text != "") { bool foundEndpoints = false; endpointListView.Items.Clear(); Session["Urltext"] = discoveryTextBox.Text; //The local discovery URL for the discovery server string discoveryUrl = discoveryTextBox.Text; try { ApplicationDescriptionCollection servers = myClientHelperAPI.FindServers(discoveryUrl); foreach (ApplicationDescription ad in servers) { foreach (string url in ad.DiscoveryUrls) { try { EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url); foundEndpoints = foundEndpoints || endpoints.Count > 0; List <string> enps = new List <string>(); foreach (EndpointDescription ep in endpoints) { string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42); string key = "[" + ad.ApplicationName + "] " + " [" + ep.SecurityMode + "] " + " [" + securityPolicy + "] " + " [" + ep.EndpointUrl + "]"; enps.Add(key); endpointListView.Items.Add(key); } Session["endpointlist"] = enps; } catch (ServiceResultException sre) { //If an url in ad.DiscoveryUrls can not be reached, myClientHelperAPI will throw an Exception ClientScript.RegisterStartupScript(this.GetType(), "Alert", "mess('error','Oops...'," + sre.ToString() + ")", true); } } if (!foundEndpoints) { ClientScript.RegisterStartupScript(this.GetType(), "Alert", "mess('warning','Oops...','Could not get any Endpoints!')", true); } } } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "Alert", "mess('error','Oops...'," + ex.ToString() + ")", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "Alert", "mess('warning','Warning...','You forget write endpoint Url!')", true); } }
public List <EndpointDes> FindServer(string serverUrl) { List <EndpointDes> endpointListView = new List <EndpointDes>(); _EndpointDescriptions.Clear(); ApplicationDescriptionCollection servers = uAClient.FindServers(serverUrl); foreach (ApplicationDescription ad in servers) { foreach (string url in ad.DiscoveryUrls) { EndpointDescriptionCollection endpoints = uAClient.GetEndpoints(url); foreach (EndpointDescription ep in endpoints) { string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42); EndpointDes endpointDes = new EndpointDes() { ID = Guid.NewGuid().ToString(), Description = "[" + ad.ApplicationName + "] " + " [" + ep.SecurityMode + "] " + " [" + securityPolicy + "] " + " [" + ep.EndpointUrl + "]", OpcEndpointDescription = ep }; endpointListView.Add(endpointDes); _EndpointDescriptions.Add(endpointDes.ID, endpointDes); } } } return(endpointListView); }
private void EndpointButton_Click(object sender, EventArgs e) { endpointListView.Items.Clear(); //The local discovery URL for the discovery server string discoveryUrl = discoveryTextBox.Text; try { ApplicationDescriptionCollection servers = myClientHelperAPI.FindServers(discoveryUrl); foreach (ApplicationDescription ad in servers) { foreach (string url in ad.DiscoveryUrls) { EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url); foreach (EndpointDescription ep in endpoints) { string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42); endpointListView.Items.Add("[" + ad.ApplicationName + "] " + " [" + ep.SecurityMode + "] " + " [" + securityPolicy + "] " + " [" + ep.EndpointUrl + "]").Tag = ep; } } } } catch (Exception ex) { if (BGW_OPCUA.IsBusy) { BGW_OPCUA.CancelAsync(); } MessageBox.Show(ex.Message, "Error"); } }
private void DiscoveryB_Click(object sender, EventArgs e) { bool foundEndpoints = false; DiscoveredServersLV.Items.Clear(); //Create discovery URL for the discovery client string serverUrl = null; if (!String.IsNullOrEmpty(DiscoveryUrlTB.Text) & !String.IsNullOrEmpty(DiscoveryPortTB.Text)) { serverUrl = "opc.tcp://" + DiscoveryUrlTB.Text + ":" + DiscoveryPortTB.Text; } else if (String.IsNullOrEmpty(DiscoveryUrlTB.Text) & !String.IsNullOrEmpty(DiscoveryPortTB.Text)) { serverUrl = "opc.tcp://localhost:" + DiscoveryPortTB.Text; } else if (!String.IsNullOrEmpty(DiscoveryUrlTB.Text) & String.IsNullOrEmpty(DiscoveryPortTB.Text)) { serverUrl = "opc.tcp://" + DiscoveryUrlTB.Text + ":" + "4840"; } else { serverUrl = "opc.tcp://localhost:4840"; } //Get the endpoints try { ApplicationDescriptionCollection servers = myHelperApi.FindServers(serverUrl); foreach (ApplicationDescription ad in servers) { foreach (string url in ad.DiscoveryUrls) { EndpointDescriptionCollection endpoints = myHelperApi.GetEndpoints(url); foundEndpoints = foundEndpoints || endpoints.Count > 0; foreach (EndpointDescription ep in endpoints) { string securityPolicy = ep.SecurityPolicyUri.Remove(0, 43); if (!DiscoveredServersLV.Items.ContainsKey(ep.Server.ApplicationName.Text)) { string[] row = { ep.Server.ApplicationName.Text, ep.EndpointUrl, ep.SecurityMode + "-" + securityPolicy }; ListViewItem listViewItem = new ListViewItem(row); DiscoveredServersLV.Items.Add(listViewItem).Tag = ep; DiscoveredServersLV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); } } } if (!foundEndpoints) { MessageBox.Show("Could not get any Endpoints", "Error"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
public int Connect() { try { List <object> Listtmp = new List <object>(); ApplicationDescriptionCollection servers = null; servers = m_Server.FindServers(discoveryUrl.ToString()); //for (int i = 0; i < servers.Count; i++) //{ // // Create discovery client and get the available endpoints. EndpointDescriptionCollection endpoints = null; // string sUrl; // sUrl = servers[i].DiscoveryUrls[0]; // discoveryUrl = new Uri(sUrl); endpoints = m_Server.GetEndpoints(discoveryUrl.ToString()); // Create wrapper and fill the combobox. for (int j = 0; j < endpoints.Count; j++) { // Create endpoint wrapper. EndpointWrapper wrapper = new EndpointWrapper(endpoints[j]); Listtmp.Add(wrapper); // Add it to the combobox. // UrlCB.Items.Add(wrapper); } if (Listtmp != null) { // Call connect with URL //m_Server.Connect(Listtmp[0].ToString(), "none", MessageSecurityMode.None, false, "", ""); m_Server.Connect(((EndpointWrapper)Listtmp[0]).Endpoint, false, "", ""); } else { m_Connected = false; return(-1); } //} //servers[i].ApplicationName; m_Connected = true; return(0); } catch { //MessageBox.Show(ex.Message); if (m_Connected) { // Disconnect from server. Disconnect(); } m_Connected = false; return(-1); } }
//private EndpointDescription CreateEndpointDescription(string url, string secPolicy, MessageSecurityMode msgSecMode) //{ // // create the endpoint description. // EndpointDescription endpointDescription = new EndpointDescription(); // // submit the url of the endopoint // endpointDescription.EndpointUrl = url; // // specify the security policy to use. // endpointDescription.SecurityPolicyUri = secPolicy; // endpointDescription.SecurityMode = msgSecMode; // // specify the transport profile. // endpointDescription.TransportProfileUri = Profiles.UaTcpTransport; // return endpointDescription; //} //url="opc.tcp://192.168.0.15:4840" private EndpointDescription getEndpointDescription(string url) { EndpointDescription ret = null; EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url); if (endpoints != null && endpoints.Count > 0) { ret = endpoints[0]; } return(ret); }
/// <summary> /// Event handlers called by the UI /// </summary>, #region UserInteractionHandlers private void EndpointButton_Click(object sender, EventArgs e) { bool foundEndpoints = false; endpointListView.Items.Clear(); //The local discovery URL for the discovery server string discoveryUrl = discoveryTextBox.Text; try { ApplicationDescriptionCollection servers = myClientHelperAPI.FindServers(discoveryUrl); foreach (ApplicationDescription ad in servers) { foreach (string url in ad.DiscoveryUrls) { try { EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url); foundEndpoints = foundEndpoints || endpoints.Count > 0; foreach (EndpointDescription ep in endpoints) { string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42); string key = "[" + ad.ApplicationName + "] " + " [" + ep.SecurityMode + "] " + " [" + securityPolicy + "] " + " [" + ep.EndpointUrl + "]"; if (!endpointListView.Items.ContainsKey(key)) { endpointListView.Items.Add(key, key, 0).Tag = ep; } } } catch (ServiceResultException sre) { //If an url in ad.DiscoveryUrls can not be reached, myClientHelperAPI will throw an Exception MessageBox.Show(sre.Message, "Error"); } } if (!foundEndpoints) { MessageBox.Show("Could not get any Endpoints", "Error"); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
private void UrlCB_DropDown(object sender, EventArgs e) { try { Uri discoveryUrl = null; // Create the uri from hostname. string sUrl = "opc.tcp://" + label1.Text + textBox1.Text + ":4840"; // Has the port been entered by the user? // Create the uri itself. discoveryUrl = new Uri(sUrl); // Clear all items of the ComboBox. UrlCB.Items.Clear(); UrlCB.Text = ""; // Look for servers ApplicationDescriptionCollection servers = null; //Discovery discovery = new Discovery(); servers = m_Server.FindServers(discoveryUrl.ToString()); // Populate the drop down list with the endpoints for the available servers. for (int iServer = 0; iServer < servers.Count; iServer++) { try { // Create discovery client and get the available endpoints. EndpointDescriptionCollection endpoints = null; sUrl = servers[iServer].DiscoveryUrls[0]; discoveryUrl = new Uri(sUrl); endpoints = m_Server.GetEndpoints(discoveryUrl.ToString()); // Create wrapper and fill the combobox. for (int i = 0; i < endpoints.Count; i++) { // Create endpoint wrapper. EndpointWrapper wrapper = new EndpointWrapper(endpoints[i]); // Add it to the combobox. UrlCB.Items.Add(wrapper); } // Update status label. toolStripStatusLabel.Text = "GetEndpoints succeeded for " + servers[iServer].ApplicationName; btn_Connect.Enabled = true; } catch (Exception) { // Update status label. toolStripStatusLabel.Text = "GetEndpoints failed for " + servers[iServer].ApplicationName; } } } catch (Exception ex) { // Update status label. toolStripStatusLabel.Text = "FindServers failed:" + ex.ToString(); } }
/// <summary> /// Expands the drop down list of the ComboBox to display available servers and endpoints. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void UrlCB_DropDown(object sender, EventArgs e) { try { Uri discoveryUrl = null; // Check the text property of the Server textbox if (NodeTB.Text.Length == 0) { // Set the uri of the local discovery server by default. discoveryUrl = new Uri("opc.tcp://localhost:4840"); } else { // Create the uri from hostname. string sUrl; // Has the port been entered by the user? char seperator = ':'; string[] strPortCheck = NodeTB.Text.Split(seperator); if (strPortCheck.Length > 1) { sUrl = "opc.tcp://" + NodeTB.Text; } else { sUrl = "opc.tcp://" + NodeTB.Text + ":4840"; } // Create the uri itself. discoveryUrl = new Uri(sUrl); } // Set wait cursor. Cursor = Cursors.WaitCursor; // Clear all items of the ComboBox. UrlCB.Items.Clear(); UrlCB.Text = ""; // Look for servers ApplicationDescriptionCollection servers = null; //Discovery discovery = new Discovery(); servers = m_Server.FindServers(discoveryUrl.ToString()); // Populate the drop down list with the endpoints for the available servers. for (int iServer = 0; iServer < servers.Count; iServer++) { try { // Create discovery client and get the available endpoints. EndpointDescriptionCollection endpoints = null; string sUrl; sUrl = servers[iServer].DiscoveryUrls[0]; discoveryUrl = new Uri(sUrl); endpoints = m_Server.GetEndpoints(discoveryUrl.ToString()); // Create wrapper and fill the combobox. for (int i = 0; i < endpoints.Count; i++) { // Create endpoint wrapper. EndpointWrapper wrapper = new EndpointWrapper(endpoints[i]); // Add it to the combobox. UrlCB.Items.Add(wrapper); } // Update status label. toolStripStatusLabel.Text = "GetEndpoints succeeded for " + servers[iServer].ApplicationName; toolStripStatusLabel.Image = global::Siemens.OpcUA.Client.Properties.Resources.success; } catch (Exception) { // Update status label. toolStripStatusLabel.Text = "GetEndpoints failed for " + servers[iServer].ApplicationName; toolStripStatusLabel.Image = global::Siemens.OpcUA.Client.Properties.Resources.error; } } // Set default cursor. Cursor = Cursors.Default; } catch (Exception ex) { // Update status label. toolStripStatusLabel.Text = "FindServers failed:" + ex.ToString(); toolStripStatusLabel.Image = global::Siemens.OpcUA.Client.Properties.Resources.error; } }