/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// 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 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; } } /// /// Pump power in [%] in range 0 .. 100.0f /// private float power; public float Power { get { return power; } } public Pump() { } /// /// Constructor /// /// Pump configuration object /// List of all components (used to find the control board) 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 TurnOn(float powerArg) { log.InfoFormat("{0}.TurnOn({1})", Name, powerArg); this.power = powerArg; controlBoard.SetFMFreq(FMIndex, powerArg); } public void TurnOff() { log.InfoFormat("{0}.TurnOff()", Name); this.power = 0.0f; controlBoard.SetFMFreq(FMIndex, 0.0f); } /// /// The currently running operation. /// public enum CurrentOp { None, TurnOn, TurnOff, } /// CurrentOp currentOp; float powerForOp; /// /// 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.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; } } }