public void ReportPrediction(string contextId) { lock (lockStatus) { if (_process.CurrentState == State.WaitForCorrectPrediction || _process.CurrentState == State.Tracking) { if (contextId == null || contextId != _currentContextId) { // error notification var errorArgs = new StatusErrorNotificationEventArgs( StatusErrorNotificationCode.UnexpectedPrediction ); ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => StatusErrorNotification(this, errorArgs))); } else { // expected prediction (correct) var nextState = _process.MoveNext(Command.CorrectPrediction); //expected state: WaitForWrongPrediction _timer.Stop(); EnterState(nextState); } } else if (_process.CurrentState == State.WaitForWrongPrediction) { if (contextId == null || contextId != _currentContextId) { // expected prediction (wrong) var nextState = _process.MoveNext(Command.WrongPrediction); //expected state: WaitForConfirmation EnterState(nextState); var userInteractionArgs = new UserInteractionEventArgs { Code = UserInteractionCode.BetterContextFound, BetterContextFoundValue = contextId }; ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => UserInteraction(this, userInteractionArgs))); } } else if (_process.CurrentState == State.WaitForConfirmation) { // expected prediction var nextState = _process.MoveNext(Command.CorrectPrediction); //expected state: WaitForWrongPrediction EnterState(nextState); } } }
/// <summary> /// Timer event handler. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _timer.Stop(); State nextState = State.Unknown, leavingState = _process.CurrentState; UserInteractionEventArgs userInteractionArgs; StatusErrorNotificationEventArgs errorArgs; lock (lockStatus) { switch (leavingState) { case State.WaitForCorrectPrediction: // error notification errorArgs = new StatusErrorNotificationEventArgs( StatusErrorNotificationCode.TrackingSessionFailed ); ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => StatusErrorNotification(this, errorArgs))); nextState = _process.MoveNext(Command.None); //expected state: WaitForSelection break; case State.Tracking: userInteractionArgs = new UserInteractionEventArgs { Code = UserInteractionCode.TrackingSessionSucceded }; ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => UserInteraction(this, userInteractionArgs))); nextState = _process.MoveNext(Command.None); //expected state: WaitForWrongPrediction break; default: break; } EnterState(nextState); } }
private void EnterState(State state) { UserInteractionEventArgs userInteractionArgs; bool trackingStarted; lock (lockStatus) { switch (_process.CurrentState) { case State.Idle: // retrieve service status ServiceStatus serviceStatus = null; for (var attempts = Properties.Settings.Default.ClientRetries + 1; attempts > 0; attempts--) { try { serviceStatus = ServiceStatus; if (serviceStatus != null) { break; } } catch { } } if (serviceStatus != null && serviceStatus.ContextId != null) { CurrentContextId = serviceStatus.ContextId; // notify that AttiLA had a context userInteractionArgs = new UserInteractionEventArgs { Code = UserInteractionCode.PreviousContextFound, PreviousContextFoundValue = serviceStatus.ContextId }; ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => UserInteraction(this, userInteractionArgs))); } break; case State.WaitForSelection: //Eventually stop notifications _serviceClient.Silence(); break; case State.WaitForCorrectPrediction: _timer.Enabled = true; _timer.Interval = Properties.Settings.Default.ClientTimeout; // tracking start trackingStarted = false; for (var attempts = Properties.Settings.Default.ClientRetries + 1; attempts > 0; attempts--) { try { trackingStarted = _serviceClient.TrackingStart(CurrentContextId); if (trackingStarted) { break; } } catch { } } if (!trackingStarted) { throw new StatusException(Properties.Resources.MsgTrackingStartFailure) { Code = StatusExceptionCode.ServiceFailure }; } break; case State.WaitForWrongPrediction: _serviceClient.TrackingStop(); userInteractionArgs = new UserInteractionEventArgs { Code = UserInteractionCode.CurrentContextFound }; ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => UserInteraction(this, userInteractionArgs))); break; case State.WaitForConfirmation: // Do nothing break; case State.Tracking: _timer.Enabled = true; _timer.Interval = Properties.Settings.Default.ClientTimeout; // tracking start trackingStarted = false; for (var attempts = Properties.Settings.Default.ClientRetries + 1; attempts > 0; attempts--) { try { trackingStarted = _serviceClient.TrackingStart(CurrentContextId); if (trackingStarted) { break; } } catch { } } if (!trackingStarted) { throw new StatusException(Properties.Resources.MsgTrackingStartFailure) { Code = StatusExceptionCode.ServiceFailure }; } break; default: break; } } }
/// <summary> /// Inform about User context selection /// </summary> /// <param name="contextId"></param> /// <exception cref="SettingsException"></exception> /// <returns></returns> public bool ContextSelected(string contextId) { if (contextId == null) { throw new ArgumentNullException("contextId"); } if (!EntityService <Context> .IsValidObjectID(contextId)) { throw new ArgumentOutOfRangeException("contextId"); } lock (lockStatus) { State nextState; if (contextId != CurrentContextId) { // notify that the selection is different than the previous one var userInteractionArgs = new UserInteractionEventArgs { Code = UserInteractionCode.NewContextSelected }; ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => UserInteraction(this, userInteractionArgs))); CurrentContextId = contextId; // new context selected _timer.Stop(); nextState = _process.MoveNext(Command.Selection); //expected state: WaitForCorrectPrediction try { EnterState(nextState); } catch (StatusException se) { // error notification var errorArgs = new StatusErrorNotificationEventArgs( StatusErrorNotificationCode.TrackingSessionFailed, se ); ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => StatusErrorNotification(this, errorArgs))); } } else if (_process.CurrentState == State.WaitForConfirmation) { // confirmation nextState = _process.MoveNext(Command.Confirmation); //expected state: Tracking try { EnterState(nextState); } catch (StatusException se) { // error notification var errorArgs = new StatusErrorNotificationEventArgs( StatusErrorNotificationCode.TrackingSessionFailed, se ); ThreadPool.QueueUserWorkItem(new WaitCallback( (object o) => StatusErrorNotification(this, errorArgs))); } } else { // if the service is in Idle, need to be put in notification AwakeService(); } } return(true); }