Example #1
0
        private static string FormatState(ConnectionStates state)
        {
            switch (state)
            {
            case _reConnecting:
                return("Reconnecting");

            case _none:
                return("None");

            default:
                return(state.ToString());
            }
        }
 void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
 {
     // display state changes
     messages.Add(string.Format("[State Change] {0} => {1}", oldState.ToString(), newState.ToString()));
 }
 void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
 {
     // display state changes
     messages.Add(string.Format("[State Change] {0} => {1}", oldState.ToString(), newState.ToString()));
 }
Example #4
0
        private void SetState(ConnectionStates state, string errorReason = null)
        {
            HTTPManager.Logger.Information("HubConnection", "SetState - from State: " + this.State.ToString() + " to State: " + state.ToString() + " errorReason: " + errorReason ?? string.Empty);

            if (this.State == state)
            {
                return;
            }

            this.State = state;

            switch (state)
            {
            case ConnectionStates.Initial:
            case ConnectionStates.Authenticating:
            case ConnectionStates.Negotiating:
            case ConnectionStates.CloseInitiated:
                break;

            case ConnectionStates.Connected:
                try
                {
                    if (this.OnConnected != null)
                    {
                        this.OnConnected(this);
                    }
                }
                catch (Exception ex)
                {
                    HTTPManager.Logger.Exception("HubConnection", "Exception in OnConnected user code!", ex);
                }
                break;

            case ConnectionStates.Closed:
                if (string.IsNullOrEmpty(errorReason))
                {
                    if (this.OnClosed != null)
                    {
                        try
                        {
                            this.OnClosed(this);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnClosed user code!", ex);
                        }
                    }
                }
                else
                {
                    if (this.OnError != null)
                    {
                        try
                        {
                            this.OnError(this, errorReason);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnError user code!", ex);
                        }
                    }
                }
                break;
            }
        }
 private void signalRConnection_OnStateChanged(Connection manager, ConnectionStates oldState, ConnectionStates newState)
 {
     this.messages.Add($"[State Change] {oldState.ToString()} => {newState.ToString()}");
 }
Example #6
0
    private void _connection_OnStateChanged(Connection connection, ConnectionStates oldState, ConnectionStates newState)
    {
        string msg = string.Format("[State Change] {0} => {1}", oldState.ToString(), newState.ToString());

        Debug.Log(msg);
    }
        private async void APIEventHandler(ConnectionStates State)
        {
            ConnectionState = State;

            switch (ConnectionState)
            {
            case ConnectionStates.Initializing:
            case ConnectionStates.Initialized:
            case ConnectionStates.Discovering:
            case ConnectionStates.Authenticating:
            case ConnectionStates.RetrievingData:
            case ConnectionStates.Disconnecting:
                LogStatus(State.ToString());
                break;

            case ConnectionStates.NotConnected:
                if (IsConnecting || IsConnected)
                {
                    AdapterNotConnected();
                    LogStatus(State.ToString());
                }
                break;

            case ConnectionStates.Connecting:
                if (BlueFire.IsReconnecting)
                {
                    if (!IsConnecting)
                    {
                        AdapterReconnecting();
                    }
                }
                LogStatus(State.ToString());
                break;

            case ConnectionStates.Authenticated:
                if (!IsConnected)
                {
                    await AdapterConnected();

                    LogStatus(State.ToString());
                }
                break;

            case ConnectionStates.KeyTurnedOn:
                LogKeyState();
                await GetTruckData();     // get data if key is turned on after app is started

                break;

            case ConnectionStates.KeyTurnedOff:
                LogKeyState();
                break;

            case ConnectionStates.Disconnected:
                if ((IsConnecting || IsConnected) && !BlueFire.IsReconnecting)
                {
                    AdapterDisconnected();
                }
                LogStatus(State.ToString());
                break;

            case ConnectionStates.Reconnecting:
                if (!IsConnecting)
                {
                    AdapterReconnecting();
                }
                LogStatus(State.ToString());
                break;

            case ConnectionStates.Reconnected:
                if (IsConnecting)
                {
                    AdapterReconnected();
                    LogStatus(State.ToString());
                }
                break;

            case ConnectionStates.NotReconnected:
                if (IsConnecting)
                {
                    AdapterNotReconnected();
                }
                LogStatus(State.ToString());
                break;

            case ConnectionStates.CANStarting:
                LogMessage("The Adapter is connected to the " + BlueFire.CanBusToString() + ".");
                await GetTruckData();

                break;

            case ConnectionStates.J1708Restarting:
                LogMessage("J1708 is restarting.");
                await GetTruckData();

                break;

            case ConnectionStates.DataAvailable:
                if (IsConnected)
                {
                    CheckData();
                    LogStatus(State.ToString());
                }
                break;

            case ConnectionStates.CANFilterFull:
                LogMessage("The CAN Filter is Full. Some data will not be retrieved.");
                LogStatus(State.ToString());
                break;

            case ConnectionStates.ConnectTimeout:
            case ConnectionStates.AdapterTimeout:
                if (IsConnecting || IsConnected)
                {
                    AdapterNotConnected();
                    LogStatus(State.ToString());
                    LogMessage("The Adapter Timed Out.");
                }
                break;

            case ConnectionStates.Notification:
                LogNotification();
                break;

            case ConnectionStates.AdapterMessage:
                LogAdapterMessage();
                break;

            case ConnectionStates.DataError:
                LogError();
                break;

            case ConnectionStates.SystemError:
                LogError();
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.NotAuthenticated:
                LogMessage("You are not authorized to access this adapter. Check your adapter security settings.");
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.NotFound:
                LogMessage("A valid adapter was not found. Check your adapter connection settings.");
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.IncompatibleAPI:
                LogMessage("The API is not compatible with this App.");
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.IncompatibleAdapter:
                LogMessage("The Adapter is not compatible with this API.");
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.IncompatibleSecurity:
                LogMessage("App Security is not compatible with this API.");
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;

            case ConnectionStates.NoAdapter:
            case ConnectionStates.BluetoothNA:
                AdapterNotConnected();
                LogStatus(State.ToString());
                break;
            }

            // Allow App to stop the service
            await Task.Delay(10);
        }
        private void SetState(ConnectionStates state, string errorReason = null)
        {
            if (string.IsNullOrEmpty(errorReason))
            {
                HTTPManager.Logger.Information("HubConnection", "SetState - from State: '" + this.State.ToString() + "' to State: '" + state.ToString() + "'");
            }
            else
            {
                HTTPManager.Logger.Information("HubConnection", "SetState - from State: '" + this.State.ToString() + "' to State: '" + state.ToString() + "' errorReason: '" + errorReason + "'");
            }

            if (this.State == state)
            {
                return;
            }


            this.State = state;

            switch (state)
            {
            case ConnectionStates.Initial:
            case ConnectionStates.Authenticating:
            case ConnectionStates.Negotiating:
            case ConnectionStates.CloseInitiated:
                break;

            case ConnectionStates.Connected:
                try
                {
                    if (this.OnConnected != null)
                    {
                        this.OnConnected(this);
                    }
                }
                catch (Exception ex)
                {
                    HTTPManager.Logger.Exception("HubConnection", "Exception in OnConnected user code!", ex);
                }

                HTTPManager.Heartbeats.Subscribe(this);
                this.lastMessageSent = DateTime.UtcNow;
                break;

            case ConnectionStates.Closed:
                if (string.IsNullOrEmpty(errorReason))
                {
                    if (this.OnClosed != null)
                    {
                        try
                        {
                            this.OnClosed(this);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnClosed user code!", ex);
                        }
                    }
                }
                else
                {
                    if (this.OnError != null)
                    {
                        try
                        {
                            this.OnError(this, errorReason);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnError user code!", ex);
                        }
                    }
                }
                HTTPManager.Heartbeats.Unsubscribe(this);
                break;
            }
        }
Example #9
0
        private void SetState(ConnectionStates state, string errorReason = null)
        {
            if (string.IsNullOrEmpty(errorReason))
            {
                HTTPManager.Logger.Information("HubConnection", "SetState - from State: '" + this.State.ToString() + "' to State: '" + state.ToString() + "'");
            }
            else
            {
                HTTPManager.Logger.Information("HubConnection", "SetState - from State: '" + this.State.ToString() + "' to State: '" + state.ToString() + "' errorReason: '" + errorReason + "'");
            }

            if (this.State == state)
            {
                return;
            }

            var previousState = this.State;

            this.State = state;

            switch (state)
            {
            case ConnectionStates.Initial:
            case ConnectionStates.Authenticating:
            case ConnectionStates.Negotiating:
            case ConnectionStates.CloseInitiated:
                break;

            case ConnectionStates.Reconnecting:
                HTTPManager.Heartbeats.Subscribe(this);
                break;

            case ConnectionStates.Connected:
                // If reconnectStartTime isn't its default value we reconnected
                if (this.reconnectStartTime != DateTime.MinValue)
                {
                    try
                    {
                        if (this.OnReconnected != null)
                        {
                            this.OnReconnected(this);
                        }
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("HubConnection", "OnReconnected", ex);
                    }
                }
                else
                {
                    try
                    {
                        if (this.OnConnected != null)
                        {
                            this.OnConnected(this);
                        }
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("HubConnection", "Exception in OnConnected user code!", ex);
                    }
                }

                HTTPManager.Heartbeats.Subscribe(this);
                this.lastMessageSent = DateTime.UtcNow;

                // Clean up reconnect related fields
                this.currentContext     = new RetryContext();
                this.reconnectStartTime = DateTime.MinValue;
                this.reconnectAt        = DateTime.MinValue;

                break;

            case ConnectionStates.Closed:
                // No errorReason? It's an expected closure.
                if (string.IsNullOrEmpty(errorReason))
                {
                    if (this.OnClosed != null)
                    {
                        try
                        {
                            this.OnClosed(this);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnClosed user code!", ex);
                        }
                    }
                }
                else
                {
                    // If possible, try to reconnect
                    if (this.ReconnectPolicy != null && (previousState == ConnectionStates.Connected || this.reconnectStartTime != DateTime.MinValue))
                    {
                        // It's the first attempt after a successful connection
                        if (this.reconnectStartTime == DateTime.MinValue)
                        {
                            this.reconnectStartTime = DateTime.UtcNow;

                            try
                            {
                                if (this.OnReconnecting != null)
                                {
                                    this.OnReconnecting(this, errorReason);
                                }
                            }
                            catch (Exception ex)
                            {
                                HTTPManager.Logger.Exception("HubConnection", "SetState - ConnectionStates.Reconnecting", ex);
                            }
                        }

                        RetryContext context = new RetryContext
                        {
                            ElapsedTime        = DateTime.UtcNow - this.reconnectStartTime,
                            PreviousRetryCount = this.currentContext.PreviousRetryCount,
                            RetryReason        = errorReason
                        };

                        TimeSpan?nextAttempt = null;
                        try
                        {
                            nextAttempt = this.ReconnectPolicy.GetNextRetryDelay(context);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "ReconnectPolicy.GetNextRetryDelay", ex);
                        }

                        // No more reconnect attempt, we are closing
                        if (nextAttempt == null)
                        {
                            HTTPManager.Logger.Warning("HubConnecction", "No more reconnect attempt!");

                            // Clean up everything
                            this.currentContext     = new RetryContext();
                            this.reconnectStartTime = DateTime.MinValue;
                            this.reconnectAt        = DateTime.MinValue;
                        }
                        else
                        {
                            HTTPManager.Logger.Information("HubConnecction", "Next reconnect attempt after " + nextAttempt.Value.ToString());

                            this.currentContext = context;
                            this.currentContext.PreviousRetryCount += 1;

                            this.reconnectAt = DateTime.UtcNow + nextAttempt.Value;

                            this.SetState(ConnectionStates.Reconnecting);

                            return;
                        }
                    }

                    if (this.OnError != null)
                    {
                        try
                        {
                            this.OnError(this, errorReason);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HubConnection", "Exception in OnError user code!", ex);
                        }
                    }
                }

                HTTPManager.Heartbeats.Unsubscribe(this);
                break;
            }
        }