using System; using System.Collections.Generic; using log4net; namespace TBF.BenchControl.Elde.PumpWithFM { public class Pump : ComponentBase, GenericDevices.IPumpFM, GenericDevices.IValve, IOperation { private static readonly ILog log = LogManager.GetLogger(typeof(Pump)); public override string ToString() { return string.Format("PumpWithFM({0})", Cfg.ToString(1)); } readonly PumpCfg pumpCfg; public int Delay { get { return pumpCfg.Delay; } } public ValveCategory Category { get { return ValveCategory.Feeding; } } /// Pump category is Feeding public float Power { get { return pumpCfg.TestParams.Power; } } public GenericDevices.IValve CoupledTo { get { return null; } } public bool InvertCouple { get { return false; } } public int LagOpening { get { return 0; } } public int LagClosing { get { return 0; } } readonly ControlBoardDev controlBoard; readonly int bitPosition; /// 0 .. 63 int FMIndex; /// 0 .. FMPumpsCount-1 public readonly ulong Mask; /// derived from bitPosition in the constructor public bool State { get { return (controlBoard.Route & Mask) != 0; } } public float powerForOp; public Pump(PumpCfg cfg, IList components) : base(cfg) { pumpCfg = cfg; controlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components); if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent"); bitPosition = pumpCfg.BitPosition; Mask = (((ulong)1) << pumpCfg.BitPosition); FMIndex = pumpCfg.FMIndex; log.Debug(this.ToString()); } public void TurnOnDfltPower() { controlBoard.SetFMFreq(FMIndex, Power); } public void TurnOn(float powerArg) { controlBoard.SetFMFreq(FMIndex, powerArg); } public void TurnOff() { controlBoard.SetFMFreq(FMIndex, 0.0f); } /// /// The currently running operation. /// public enum CurrentOp { None, TurnOnDfltPower, TurnOn, TurnOff, } /// CurrentOp currentOp; /// /// Turn the pump frequency inverter on. /// public IOperation TurnOnDfltPowerOp() { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.TurnOnDfltPower; return this; } /// /// Turn the pump frequency inverter on. /// public IOperation TurnOnOp(float powerArg) { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.TurnOn; powerForOp = powerArg; return this; } /// /// Turn the pump frequency inverter on. /// public IOperation TurnOffOp() { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.TurnOff; return this; } /// Start the operation public void Start() { switch (currentOp) { case CurrentOp.TurnOnDfltPower: TurnOnDfltPower(); return; case CurrentOp.TurnOn: TurnOn(powerForOp); return; case CurrentOp.TurnOff: default: TurnOff(); return; } } /// Run the operation public Event Run() { return Event.TurnPumpOnOffDone; } /// Stop the operation public void Stop() { currentOp = CurrentOp.None; } } }