tbf/TestBenchFramework/BenchControl/MettlerToledo/TankDraining.cs

145 lines
3.5 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.GenericDevices;
using TBF.Resources;
namespace TBF.BenchControl.MettlerToledo
{
public enum DrainState
{
None,
OpeningDV_Start,
OpeningDV_Run,
OpeningDV_Stop,
Draining,
ClosingDV_Start,
ClosingDV_Run,
ClosingDV_Stop,
TankClosed,
}
public class TankDraining : ComponentBase, ITankDraining, ISequenceCondition
{
private static readonly ILog log = LogManager.GetLogger(typeof(TankDraining));
readonly GenericDevices.ITankDrainingCfg tankDrainingCfg;
private IValve drainValve;
public GenericDevices.IValve DrainValve { get { return drainValve; } }
private IOperation openTheDrainValveOp;
private IOperation closeTheDrainValveOp;
private IOperation waitTankEmptyOp;
DrainState drainState;
public TankDraining()
: base()
{
openTheDrainValveOp = null;
closeTheDrainValveOp = null;
waitTankEmptyOp = null;
}
public TankDraining(Generic.IComponentCfg cfg)
: base(cfg)
{
tankDrainingCfg = cfg as GenericDevices.ITankDrainingCfg;
if (tankDrainingCfg == null) throw new Exception(string.Format("Cannot create component {0}", cfg.Name));
openTheDrainValveOp = null;
closeTheDrainValveOp = null;
waitTankEmptyOp = null;
}
protected void Initalize()
{
drainValve = TbfComponents.FindComponent(tankDrainingCfg.DrainValve) as IValve;
openTheDrainValveOp = new Elde.SetValvesOp(StateMachine.ControlBoard, drainValve, null);
closeTheDrainValveOp = new Elde.SetValvesOp(StateMachine.ControlBoard, null, drainValve);
waitTankEmptyOp = new WaitTankEmptyOp(this);
}
protected void RunBefore()
{
switch (drainState)
{
case DrainState.OpeningDV_Start:
openTheDrainValveOp.Start();
drainState = DrainState.OpeningDV_Run;
return;
case DrainState.OpeningDV_Run:
if (openTheDrainValveOp.Run() == Event.ValvesSet) drainState = DrainState.OpeningDV_Stop;
return;
case DrainState.OpeningDV_Stop:
openTheDrainValveOp.Stop();
return;
case DrainState.Draining:
if (IsEmpty()) drainState = DrainState.ClosingDV_Start;
return;
case DrainState.ClosingDV_Start:
closeTheDrainValveOp.Start();
drainState = DrainState.ClosingDV_Run;
return;
case DrainState.ClosingDV_Run:
if (closeTheDrainValveOp.Run() == Event.ValvesSet) drainState = DrainState.ClosingDV_Stop;
return;
case DrainState.ClosingDV_Stop:
closeTheDrainValveOp.Stop();
return;
default:
return;
}
}
protected void RunAfter() { } /// Not used
public virtual string GetName() { return string.Empty; }
public virtual bool IsEmpty() { return false; }
public int ConditionsCount { get { return 3; } }
/// Strings are added to the combo-box for transition sequence condition selection
public string ConditionName(int i)
{
switch (i)
{
case 0: return string.Format(Strings._0_open_the_drain_valve, Name);
case 1: return string.Format(Strings._0_close_the_drain_valve, Name);
case 2: return string.Format(Strings._0_wait_until_the_tank_is_empty, Name);
default: return string.Empty;
}
}
/// Operation to be executed as a part of a transition sequence
public IOperation ConditionOp(int i)
{
switch (i)
{
case 0: return openTheDrainValveOp;
case 1: return closeTheDrainValveOp;
case 2: return waitTankEmptyOp;
default: return null;
}
}
}
}