/// <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:48010"); } 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 + ":48010"; } // 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(); discovery.FindServers(discoveryUrl, ref servers); // 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); discovery.GetEndpoints(discoveryUrl, ref endpoints); // 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; } }
/// <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) { // get discovery url string sUrl; // Check the text property of the Server textbox if (NodeTB.Text.Length == 0) { // Set the uri of the local discovery server by default. sUrl = "opc.tcp://localhost:4840"; } else { // Has the port been entered by the user? char seperator = ':'; string[] strPortCheck = NodeTB.Text.Split(seperator); if (strPortCheck.Length > 1) { sUrl = NodeTB.Text; } else { sUrl = NodeTB.Text + ":4840"; } } // Set wait cursor. Cursor = Cursors.WaitCursor; // Clear all items of the ComboBox. UrlCB.Items.Clear(); UrlCB.Text = ""; // Look for servers List <ApplicationDescription> serverList = null; using (Discovery discovery = new Discovery(m_Application)) { try { serverList = discovery.FindServers(sUrl); } catch (Exception exception) { // Update status label. toolStripStatusLabel.Text = "FindServers failed:" + exception.Message; toolStripStatusLabel.Image = RabbitMQ_SendClient.Properties.Resources.error; // Set default cursor. Cursor = Cursors.Default; return; } bool bGetEndpointsError = false; bool bIgnoreError = false; string errorText = ""; List <EndpointWrapper> lstEndpointWrappers = new List <EndpointWrapper>(); // Populate the drop down list with the endpoints for the available servers. foreach (ApplicationDescription server in serverList) { if (server.ApplicationType == ApplicationType.Client || server.ApplicationType == ApplicationType.DiscoveryServer) { continue; } try { StringCollection lstEndpoint = new StringCollection(); foreach (string discoveryUrl in server.DiscoveryUrls) { // Call GetEndpoints List <EndpointDescription> lstEndpoints = null; try { lstEndpoints = discovery.GetEndpoints(discoveryUrl); foreach (EndpointDescription endpoint in lstEndpoints) { // build display name for combo EndpointWrapper endpointWrap = new EndpointWrapper(endpoint); if (!lstEndpoint.Contains(endpointWrap.ToString())) { lstEndpointWrappers.Add(endpointWrap); lstEndpoint.Add(endpointWrap.ToString()); bIgnoreError = true; } } } catch (Exception) { continue; } } } catch (Exception exception) { // Update status label. errorText = "GetEndpoints failed. Error: " + exception.Message; bGetEndpointsError = true; } } // error occured during get endpoints if (bGetEndpointsError && !bIgnoreError) { // Update status label. toolStripStatusLabel.Text = "GetEndpoints failed. Error: " + errorText; toolStripStatusLabel.Image = RabbitMQ_SendClient.Properties.Resources.error; } // add list of endpoints else { UrlCB.Items.AddRange(lstEndpointWrappers.ToArray()); } } // Set default cursor. Cursor = Cursors.Default; }