using System; using System.Collections.Generic; using System.Text; using log4net; using TBF.Resources; using TBF.UiBridge; namespace TBF.BenchControl.TestMethods.CameraRoiDetection { public class RoiDetectionSeq : Sequences.SequenceBase { private static readonly ILog log = LogManager.GetLogger(typeof(RoiDetectionSeq)); private static readonly ILog allResults = LogManager.GetLogger("AllResults"); private static readonly ILog summaryResults = LogManager.GetLogger("SummaryResults"); public IList Execute(Entities.Test test) { Elde.ControlBoardDev cBrd = StateMachine.ControlBoard; IList e; /// Events from currently running operations //-------------------------------- State.Create("RoiDetection : Starting the detection sequence") .AddOperation(checkUiOp) .AddOperation(cBrd.StopPreviousOp()) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) goto stop; } while (!e.Contains(Event.PreviousStopped)); //==================================== /// Start the test, initialize test results Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, 1, inPath, benchPath, outPath, sensPath, 0)); Entities.TestResult tstRslt = new Entities.TestResult(test, 1, Entities.MetersKind.Single); tstRslt.DoNotEvaluate = true; tstRslt.DoNotPublish = true; //==================================== // Transition or SetRoute - Start //==================================== switch (Transition(transitionStart, TransitionContext.TestStart)) { case Event.Error: goto error; case Event.UiCmdStop: goto stop; } //------------------------------------------------ Bridge.OnActivity(this, Strings.Setting_the_flow); //------------------------------------------------ State.Create("RoiDetection : Starting the pump") .AddOperation(checkUiOp) .AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOn() : null) .AddOperation(cBrd.SetValvesOp(inPath.Pump, null)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) goto stop; } while (!e.Contains(Event.ValvesSet)); //-------------------------------- State.Create("RoiDetection : Setting the flow") .AddOperation(checkUiOp) .AddOperation(outPath.RegulValve.SetFlowOp(outPath.FlowMeter, test.Qfrom, test.Qto, refFlow, 600)) /// timeout = 10 min. .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) goto stop; if (e.Contains(Event.RegulValveTimeOut)) goto error; if (e.Contains(Event.Next)) goto flow_set; } while (!e.Contains(Event.FlowReached)); flow_set: int time = StateMachine.Time; float currentFlow = refFlow.Val; //------------------------------------------------ Bridge.OnActivity(this, Strings.ROI_Detection_in_Progress); //------------------------------------------------ State detection = State.Create("RoiDetection : Detection of ROI-s") .AddOperation(checkUiOp); /// Extract cameras from the current sensors path, add operations to the detection state for (int i = 0; i < sensPath.RegisterReaders.Length; i++) { GenericDevices.ICameraRoi cameraRoi = sensPath.RegisterReaders[i] as GenericDevices.ICameraRoi; if (cameraRoi != null) { detection.AddOperation(cameraRoi.DetectOp()); } } detection.EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) goto error; if (e.Contains(Event.UiCmdStop)) goto stop; } while (e.Contains(Event.CameraBusy)); if (e.Contains(Event.CameraOpFailed)) goto error; /// TODO: resolve in some other way //------------------------------------------------ Bridge.OnActivity(this, Strings.Detection_completed); //------------------------------------------------ State.Create("RoiDetection : Stopping the pump") .AddOperation(checkUiOp) .AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null) .AddOperation(cBrd.SetValvesOp(null, inPath.Pump)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) goto stop; } while (!e.Contains(Event.ValvesSet)); //-------------------------------- State.Create("RoiDetection : Stopping diverter, gate, etc.") .AddOperation(checkUiOp) .AddOperation(cBrd.StopPreviousOp()) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) goto stop; } while (!e.Contains(Event.PreviousStopped)); ///----------------------/// /// Quit this sequence /// ///----------------------/// /// Stop the pump State.Create("RoiDetection : Detection of ROI-s completed -> Stopping the pump") .AddOperation(checkUiOp) .AddOperation(cBrd.SetValvesOp(null, inPath.Pump)) .AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) goto error; } while (!e.Contains(Event.ValvesSet) || ((inPath.Pump is GenericDevices.IPumpFM) && !e.Contains(Event.TurnPumpOnOffDone))); /// Transition or SetRoute - End switch (Transition(transitionStop, TransitionContext.TestEnd)) { case Event.Error: goto error; case Event.UiCmdStop: goto stop; } /// Create the return value: a list with one event Event.Done IList retval = new List(); retval.Add(Event.Done); return retval; //==================================== stop: cBrd.SetFMFreq(0, 0); /// Stop FM controlled pumps State.Create("RoiDetection : User selected STOP -> Closing the valves") .AddOperation(checkUiOp) .AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null) .AddOperation(cBrd.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) goto error; } while (!e.Contains(Event.ValvesSet)); IList retval2 = new List(); retval2.Add(Event.UiCmdStop); return retval2; //==================================== error: cBrd.SetFMFreq(0, 0); /// Stop FM controlled pumps State.Create("RoiDetection : ERROR -> Closing the valves") .AddOperation(checkUiOp) .AddOperation((inPath != null && inPath.Pump is GenericDevices.IPumpFM) ? (inPath.Pump as GenericDevices.IPumpFM).TurnOff() : null) .AddOperation(cBrd.SetValvesOp(StateMachine.DefaultValvesOpen, StateMachine.DefaultValvesClose)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) goto error; } while (!e.Contains(Event.ValvesSet)); IList retval3 = new List(); retval3.Add(Event.Error); return retval3; } } }