private void ReestablishConnection() { _logger.Trace(); _reconnectionHandler = new SessionReconnectHandler(); State = OpcState.Connecting; _reconnectionHandler.BeginReconnect(_session, _config.ReconnectAttemptInterval, OnSessionReconnectComplete); }
private void OnStateChanged(OpcState lastState) { _logger.Trace($"{lastState.ToString()}->{State.ToString()}"); OpcStateChanged?.Invoke(this, new OpcStateChangedEventArgs() { LastState = lastState, State = this.State }); }
private void _session_KeepAlive(Session session, KeepAliveEventArgs e) { _logger.Trace(); if (e != null && e.Status != null) { if (!Object.ReferenceEquals(session, _session)) { return; } if (ServiceResult.IsBad(e.Status)) { _logger.Warn($"Connection to OPC server lost on keepalive interval"); State = OpcState.ConnectionLost; _session.KeepAlive -= _session_KeepAlive; ReestablishConnection(); } } }
public void CloseConnection() { try { _session?.Close(); _session?.Dispose(); _subscription.DeleteItems(); _subscription.Delete(true); _subscription.Dispose(); } catch (Exception e) { _logger.Warn($"Exception while closing connection to OPC server: {e.ToString()}"); } finally { State = OpcState.Disconnected; _session = null; _subscription = null; } }
public async Task ConnectAsync(string url, string sessionName = "BreanosOpcConnectorSession") { _logger.Trace(ConnectorLogging.Process((nameof(url), url), (nameof(sessionName), sessionName))); while (State != OpcState.Connected) { try { _session = await Session.Create(CreateConfig(), new ConfiguredEndpoint(null, new EndpointDescription(url)), true, sessionName, (uint)_config.SessionTimeout, null, null); State = OpcState.Connected; } catch (Exception e) { _logger.Error($"Exception while creating a session with an OPC server: {e.ToString()}"); State = OpcState.Disconnected; await Task.Delay(_config.ReconnectAttemptInterval); } } _session.KeepAliveInterval = _config.KeepAliveInterval; _session.KeepAlive += _session_KeepAlive; _session.PublishError += _session_PublishError; }
private void OnSessionReconnectComplete(object sender, EventArgs e) { _logger.Trace(); if (_reconnectionHandler == null) { _logger.Error($"ReconnectComplete called but reconnect handler was null"); State = OpcState.Disconnected; return; } if (!Object.ReferenceEquals(sender, _reconnectionHandler)) { _logger.Info($"Current reconnectHandler did not match the sender of this method"); return; } _session = _reconnectionHandler.Session; _session.KeepAliveInterval = _config.KeepAliveInterval; _session.KeepAlive += _session_KeepAlive; _session.PublishError += _session_PublishError; _reconnectionHandler.Dispose(); _reconnectionHandler = null; State = OpcState.ConnectedAndListening; }