/// <summary> /// Track an observer so that it may be unsubscribed at the specified acquisition step. /// </summary> /// <param name="observer">The observer to track.</param> /// <param name="unregisterStep">The acquisition step when the observer should unsubscribed.</param> protected void RegisterObserver(IDisposable observer, AcquisitionStep unregisterStep) { if (observer == null) { throw new ArgumentNullException("observer"); } _observers.Add(Tuple.Create(observer, unregisterStep)); }
public AcquisitionActionResult(string sequenceId, AcquisitionStep acquisitionStep, string agentId, string agentName) { if (string.IsNullOrEmpty(agentId)) { throw new ArgumentNullException("agentId"); } if (string.IsNullOrEmpty(agentName)) { throw new ArgumentNullException("agentName"); } this.SequenceId = sequenceId; this.AcquisitionStep = acquisitionStep; this.AgentId = agentId; this.AgentName = agentName; this.IsSuccessful = true; this.Timestamp = DateTimePrecise.Now; }
protected async Task <AcquisitionActionResult> MakeStateTransition <TParameter>(AcquisitionStep step, Func <Func <Task <bool> >, Func <Task>, Task> moduleAction, TParameter parameters, Func <TParameter, AcquisitionActionResult, Task <AcquisitionActionResult> > agentAction) where TParameter : AcquisitionParameter { if (moduleAction == null) { throw new ArgumentNullException("moduleAction"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (agentAction == null) { throw new ArgumentNullException("agentAction"); } var result = new AcquisitionActionResult(parameters.SequenceId, step, this.Id, this.DisplayData.Name) { MachineName = Environment.MachineName }; // action to accomplish the state transition on the agent // if it succeeds, the module action will then be executed Func <Task <bool> > before = async() => { try { await OnBeforeStateTransition(step, parameters, result).ConfigureAwait(false); result = await agentAction(parameters, result).ConfigureAwait(false); // Il faut désabonner les observers avant de faire la transition d'état, // car le code qui réagit aux événements assume que le provider n'est pas dans l'état de fin d'observation. // Les observers doivent également devenir inactifs, même si la transition d'état a planté. for (int i = _observers.Count - 1; i >= 0; i--) { if (_observers[i].Item2 == step) { _observers[i].Item1.Dispose(); _observers.RemoveAt(i); } } return(result.IsSuccessful); } catch { this.OperationalState |= OperationalAgentStates.InternalAgentError; throw; } }; try { if (this.OperationalState != OperationalAgentStates.None) { throw new InvalidOperationException(string.Format("{0} -> agent is not operational.", this.DisplayData.Name)); } await moduleAction(before, null).ConfigureAwait(false); } catch (InvalidStateTransitionException ex) { Log.Error().Exception(ex).WithAgent(this.Id).Write(); result.Exception = ex; result.IsSuccessful = false; } catch (Exception ex) { if (this.ProviderState == ProviderState.Failed) { this.OperationalState |= OperationalAgentStates.ModuleError; } result.Exception = ex; result.IsSuccessful = false; } result.ProviderState = this.ProviderState; await OnAfterStateTransition(step, parameters, result).ConfigureAwait(false); if (result.IsSuccessful) { Log.Debug().Message("Transition to AcquisitionStep '{0}' successful.", step).WithAgent(this.Id).Write(); } else { var logBuilder = Log.Warn().Message("Transition to AcquisitionStep '{0}' failed.", step); if (result.Exception != null) { logBuilder = logBuilder.Exception(result.Exception); } logBuilder.WithAgent(this.Id).Write(); } return(result); }
protected virtual Task OnAfterStateTransition(AcquisitionStep step, AcquisitionParameter parameters, AcquisitionActionResult result) { Log.Debug().Message("After transition to AcquisitionStep '{0}'.", step).WithAgent(this.Id).Write(); return(Task.FromResult(0)); }