tbf/TBF/BenchControl/TestMethods/RoiDetection/RoiDetectionSeq.cs
2018-09-06 15:25:48 +02:00

286 lines
11 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using Config.Entities;
using TBF.Resources;
using TBF.UiBridge;
using System.IO;
namespace TBF.BenchControl.TestMethods.RoiDetection
{
public class RoiDetectionSeq : Sequences.SequenceBase
{
private static readonly ILog log = LogManager.GetLogger(typeof(RoiDetectionSeq));
public IList<Event> Execute(Test test, GenericDevices.ICameraDisplay display, int repetitionNr, bool isLastRepetition)
{
Elde.ControlBoardDev cBrd = StateMachine.ControlBoard;
IList<Event> e = new List<Event>(); /// Events from currently running operations
Event retVal = Event.Done;
/// Operations
checkUiOp = new Operations.CheckUIOp(true);
processDataLoggingOp = null;
/// LtrPerRefPulse and totalPulses are only to make time estimates
LtrPerRefPulse = outPath.FlowMeter.LtrPerPulse; /// [ltr/pulse], nominal flow in [m3/h]
/// Notes:
/// float timeHr = volumeLtr / (1000.0f * targetFlow);
/// float timeSec = 3600.0f * timeHr;
/// int refPulses = (int)(timeSec * (2000.0f * targetFlow / pOut.FlowMeter.NominalFlow));
int totalPulses = (int)(test.Volume / LtrPerRefPulse + 0.5f);
//====================
// Start the test
//====================
Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, repetitionNr, inPath, benchPath, outPath, sensPath, totalPulses));
string testName = Results.Utils.GetTestName(test.Name, test.Repeats, repetitionNr);
TestStartTime = DateTime.Now;
//-------------------------------------------------
Bridge.OnActivity(this, Strings.Starting_the_pump);
//-------------------------------------------------
///
if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(test.PumpPower);
State.Create(string.Format("{0}({1}) : Starting the pump", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.AddOperation(inPath.Pump != null ? cBrd.SetValvesOp(inPath.Pump, null) : null)
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; }
if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; }
}
while (e.Contains(Event.ValvesBusy));
if (test.TimePump2StartV > 0)
{
State.Create(string.Format("{0}({1}) : Waiting after the pump started", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(new Operations.TimerOp(test.TimePump2StartV))
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnProcessData(this, new ProcessDataEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; }
}
while (!e.Contains(Event.TimerExpired));
}
if (benchPath.StopBFValve != null)
{
State.Create(string.Format("{0}({1}) : Opening the stop backflow valve", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(StateMachine.ControlBoard.SetValvesOp(benchPath.StopBFValve, null))
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; }
if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; }
}
while (!e.Contains(Event.ValvesSet));
}
if (test.TimeBeforeFlow > 0)
{
State.Create(string.Format("{0}({1}) : Waiting before flow setting process starts", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(new Operations.TimerOp(test.TimeBeforeFlow))
.AddOperation(StateMachine.ControlBoard.UpdateTankWeightOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnProcessData(this, new ProcessDataEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; }
}
while (!e.Contains(Event.TimerExpired));
}
setting_flow:
//------------------------------------------------
Bridge.OnActivity(this, Strings.Setting_the_flow);
//------------------------------------------------
State.Create(string.Format("{0}({1}) : Setting the flow", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(outPath.RegulValve.SetFlowOp(outPath.FlowMeter, test.Qfrom, test.Qto, outPath.PidCoef, RefFlow, 600)) /// timeout = 10 min.
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.FlowSetting));
if (e.Contains(Event.OpArgumentError)) { retVal = Event.OpArgumentError; goto stopTest; }
if (TestAndLogUiCmdStop(test,e)) { retVal = Event.UiCmdStop; goto stopTest; }
if (e.Contains(Event.RegulValveTimeOut))
{
Bridge.OnError(this, Strings.Flow_adjustment_failed);
retVal = Event.UiCmdStop;
goto stopTest;
}
if (e.Contains(Event.Next)) goto flow_set;
}
while (!e.Contains(Event.FlowReached));
flow_set:
int time = StateMachine.Time;
double currentFlow = RefFlow.Val;
string[] imageNames = new string[sensPath.RegisterReaders.Length];
for (int i = 0; i < sensPath.RegisterReaders.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null)
{
roi.ClearRoi();
}
else
{
imageNames[i] = null;
}
}
detection_loop:
//---------------------------------------------------------
Bridge.OnActivity(this, Strings.ROI_Detection_in_Progress);
//---------------------------------------------------------
State detection = State.Create(string.Format("{0}({1}) : Detection of ROI-s", test.Method, test.Name))
.AddOperation(checkUiOp);
for (int i = 0; i < sensPath.RegisterReaders.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null && !roi.Detected)
{
detection.AddOperation(roi.RoiDetectionOp());
imageNames[i] = roi.ImageFileNamePrefix + "detected.jpg";
}
}
detection.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Config.Entities.Progress.Test));
if (e.Contains(Event.Error))
{
retVal = Event.Error;
goto stopTest;
}
if (TestAndLogUiCmdStop(test,e))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
}
while (e.Contains(Event.CameraBusy));
//-----------------------------------------------------------------------------------------------------
if (e.Contains(Event.RoiDetectionPreviousFailed)) Bridge.OnActivity(this, Strings.Camera_error);
else if (e.Contains(Event.RoiDetectionFailed)) Bridge.OnActivity(this, Strings.Detection_failed);
else Bridge.OnActivity(this, Strings.Detection_completed);
//-----------------------------------------------------------------------------------------------------
log.WarnFormat("display: {0}", (display != null) ? display.Name : "-none-");
log.Warn("imageNames:");
for (int i = 0; i < imageNames.Length; i++)
{
if (!string.IsNullOrEmpty(imageNames[i])) log.WarnFormat(" " + imageNames[i]);
}
string title = Strings.Select_images_and_press_Retry_to_retry_RoI_detection;
State.Create(string.Format("{0}({1}) : Select images and press Retry to retry RoI detection", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation((display == null) ? null : display.ShowStaticImageOp(imageNames, title))
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (TestAndLogUiCmdStop(test, e))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
}
while (e.Contains(Event.ModelessFormIsOpen));
if (e.Contains(Event.Retry))
{
for (int i = 0; i < display.SelectedImages.Length; i++)
{
GenericDevices.IRoi roi = sensPath.RegisterReaders[i] as GenericDevices.IRoi;
if (roi != null && display.SelectedImages[i])
{
roi.ClearRoi();
}
}
goto detection_loop;
}
else if (e.Contains(Event.Abort))
{
retVal = Event.UiCmdStop;
goto stopTest;
}
//---------------------------------------------------
Bridge.OnActivity(this, Strings.Detection_completed);
//---------------------------------------------------
stopTest:
///
/// Quit this sequence
///
if (isLastRepetition || retVal == Event.UiCmdStop || retVal == Event.OpArgumentError
|| retVal == Event.Error || retVal == Event.ConfigurationError)
{
State.Create(string.Format("{0}({1}) : Stopping diverter, gate, etc.", test.Method, test.Name))
.AddOperation(checkUiOp)
.AddOperation(cBrd.StopPreviousOp())
.EnterState();
do
{
e = StateMachine.WaitRunDevsRunOps();
if (TestAndLogUiCmdStop(test, e)) goto stopTest;
}
while (!e.Contains(Event.PreviousStopped));
}
/// Create a list with one item 'retVal' (default is Event.Done) and return it
IList<Event> retList = new List<Event>(1);
retList.Add(retVal);
return retList;
}
}
}