/// <summary> /// Adds the row. /// </summary> /// <param name="credential">The credential.</param> /// <param name="server">The server.</param> /// <returns>ServersTableRow.</returns> public ServersTableRow AddRow(ConnectData credential, Opc.Da.Server server) { ServersTableRow mRow = this.NewServersTableRow(); mRow.LicenseKey = credential.LicenseKey; if (credential.Credentials != null) { mRow.Domain = credential.Credentials.Domain; mRow.Password = credential.Credentials.Password; mRow.UserName = credential.Credentials.UserName; } ServerStatus status = server.GetStatus(); mRow.CurrentTime = status.CurrentTime; mRow.ProductVersion = status.ProductVersion; mRow.SpecificationDescription = server.PreferedSpecyfication.Description; mRow.SpecificationID = server.PreferedSpecyfication.ID; mRow.URLString = server.Url.ToString(); mRow.VendorInfo = status.VendorInfo; this.AddServersTableRow(mRow); foreach (var loc in server.SupportedLocales) { ((AddressSpaceDataBase)this.DataSet).LocalesTable.AddRow(mRow.ID, loc); } return(mRow); }
private Connection CreateConnection(string serverUrl) { ServiceManager.LogMessage("Connecting to {0}...", serverUrl); URL url = new URL(serverUrl); Opc.Da.Server server = null; try { server = new Opc.Da.Server(new OpcCom.Factory(), url); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { server.Connect(); Opc.Da.ServerStatus serverStatus = server.GetStatus(); if (serverStatus.ServerState != Opc.Da.serverState.running) { throw new Exception(String.Format("Bad server state ({0})", serverStatus.ServerState)); } Connection connection = new Connection(serverUrl, server); server = null; stopwatch.Stop(); ServiceManager.LogMessage("Successful created new connection {0}: {1} ({2} ms)", connection.GetUID(), serverUrl, stopwatch.ElapsedMilliseconds); Counter counter = counters[serverUrl]; Interlocked.Increment(ref counter.numConnections); return(connection); } catch (Exception ex) { stopwatch.Stop(); ServiceManager.LogMessage("Failed create new connection to {0}: {1} ({2} ms)", serverUrl, ex.Message, stopwatch.ElapsedMilliseconds); throw new FaultException(ex.Message, new FaultCode("E_FAIL")); } } finally { if (server != null) { if (server.IsConnected) { server.Disconnect(); } server.Dispose(); } } }
void ReadMeasurement(PolledMeasurement measurement) { var item = new OpcDa.Item { ItemName = measurement.Path }; if (l_server == null || l_server.GetStatus().ServerState != OpcDa.serverState.running) { l_logger.ErrorFormat(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); throw new Exception(string.Format("Server not connected. Cannot read path {0}.", measurement.Name)); } var result = l_server.Read(new[] { item })[0]; if (result == null) { l_logger.Error("the server replied with an empty response!!!"); throw new Exception("the server replied with an empty response");//if any item cannot read, throw exeption } if (result.ResultID.ToString() != "S_OK") { l_logger.ErrorFormat(string.Format("Invalid response from the server. (Response Status: {0}, Opc Tag: {1})", result.ResultID, measurement.Path)); throw new Exception(string.Format("Invalid response from the server. (Response Status: {0}, Opc Tag: {1})", result.ResultID, measurement.Path)); } OpcMetric metric = new OpcMetric() { Measurement = measurement, Opcstatus = result.ResultID.ToString(), OpcValue = result.Value, Timestamp = result.Timestamp }; if (OpcHelper.QualifyMetric(ref metric, l_cache)) { l_cache[metric.Measurement.Name] = metric.OpcValue; l_metricCollection.Add(metric); } }
public override bool Initialize(string connectionString, int newPollerId) { bool result = false; ConnString = connectionString; try { PollerId = newPollerId; url = new Opc.URL(connectionString); server = null; var fact = new OpcCom.Factory(); server = new Opc.Da.Server(fact, null); server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential())); server.ServerShutdown += Shutdown; _tags = new List <Opc.Da.Item>(); Browse(); _groupWriteState = new Opc.Da.SubscriptionState(); _groupWriteState.Name = "GroupWrite"; _groupWriteState.Active = false; groupWrite = (Opc.Da.Subscription)server.CreateSubscription(_groupWriteState); Activated = true; logger.Logged("Info", "Состояние сервера: " + server.GetStatus().StatusInfo, "", ""); result = true; } catch (Exception ex) { logger.Logged("Error", "Не удалось подключиться к OPC DA серверу " + connectionString + ": " + ex.Message, "", ""); // logger.Logged("Warn", "Повторная попытка через 5 секунд...", "", ""); //Thread.Sleep(5000); } return(result); }
private void Timer1OnTick(object sender, EventArgs e) { DisplayPanelWatch.Text = DateTime.Now.ToShortTimeString(); DisplayPanelWatch.ForeColor = Color.Black; DisplayPanelWatch.Visible = true; DisplayPanelWatch.Font = new Font("Segoe UI Symbo", 12F, FontStyle.Regular); try { _server.GetStatus(); DisplayPanelServerError.Text = string.Empty; if (!Trends.Visible) { DisplayPanelWatch.Visible = true; Trends.Visible = true; //Toolbar.Visible = true; DisplayPanel.Visible = Status; DisplayPanelServerError.Visible = false; _status = true; } if (_repConnectionWorker.IsBusy) { _repConnectionWorker.CancelAsync(); } } catch (Exception) { CheckTheConnectionStatus(); _status = false; if (Trends.Visible) { Trends.Visible = false; //Toolbar.Visible = false; DisplayPanel.Visible = Status; DisplayPanelServerError.Visible = false; DisplayPanelWatch.Visible = false; } } try { if (_status) { //Toolbar.Visible = true; Trends.Visible = true; DisplayPanel.Visible = Status; OffConnectionDisplayPanelIndicator.Visible = false; OnConnectionDisplayPanelIndicator.Visible = true; OnConnectionDisplayPanelIndicator.ForeColor = Color.Black; OnConnectionDisplayPanelIndicator.Font = new Font("Segoe UI Symbo", 12F, FontStyle.Regular); DisplayPanelServerError.Visible = false; DisplayPanelServerError.Text = string.Empty; } else { OffConnectionDisplayPanelIndicator.Visible = true; DisplayPanelServerError.Visible = true; OnConnectionDisplayPanelIndicator.Visible = false; DisplayPanelServerError.Font = new Font("Segoe UI Symbo", 12F, FontStyle.Regular); OffConnectionDisplayPanelIndicator.Font = new Font("Segoe UI Symbo", 12F, FontStyle.Regular); DisplayPanelServerError.Text = @"| Ошибка соединения: Идет попытка востановления соединения, пожалуйста подождите"; DisplayPanel.ForeColor = Color.PaleVioletRed; OffConnectionDisplayPanelIndicator.ForeColor = Color.PaleVioletRed; } } catch (Exception) { } }
public void Ping() { localId = server.GetLocale(); supportedLocales = server.GetSupportedLocales(); status = server.GetStatus(); }