tbf/TestBenchFramework/BenchControl/Elde/PumpWithFM/Pump.cs

148 lines
3.7 KiB
C#

///
/// 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; }
}
/// <summary>
/// Pump power in [%] in range 0 .. 100.0f
/// </summary>
private float power;
public float Power
{
get { return power; }
}
public Pump()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="cfg">Pump configuration object</param>
/// <param name="components">List of all components (used to find the control board)</param>
public Pump(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
pumpCfg = cfg as PumpCfg;
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);
}
/// <summary>
/// The currently running operation.
/// </summary>
public enum CurrentOp
{
None,
TurnOn,
TurnOff,
}
///
CurrentOp currentOp;
float powerForOp;
/// <summary>
/// Turn the pump frequency inverter on.
/// </summary>
public IOperation TurnOnOp(float powerArg)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.TurnOn;
powerForOp = powerArg;
return this;
}
/// <summary>
/// Turn the pump frequency inverter on.
/// </summary>
public IOperation TurnOffOp()
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.TurnOff;
return this;
}
/// <summary>Start the operation</summary>
public void Start()
{
switch (currentOp)
{
case CurrentOp.TurnOn:
TurnOn(powerForOp);
return;
case CurrentOp.TurnOff:
default:
TurnOff();
return;
}
}
/// <summary>Run the operation</summary>
public Event Run()
{
return Event.TurnPumpOnOffDone;
}
/// <summary>Stop the operation</summary>
public void Stop()
{
currentOp = CurrentOp.None;
}
}
}