tbf/TBF/Rig/BuiltIn/PumpTandem/Pump.cs

190 lines
5.4 KiB
C#

///
/// Copyright (c) 2013-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using Dirichlet.Numerics;
using log4net;
using Common;
namespace TBF.Rig.BuiltIn.PumpTandem
{
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("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly PumpCfg pumpCfg;
GenericDevices.IPump pump1;
GenericDevices.IPump pump2;
GenericDevices.IPumpFM pumpFm1;
GenericDevices.IPumpFM pumpFm2;
public bool State { get { return pump1.State || pump2.State; } }
/// Pump power in [%] in range 0 .. 100.0
public double Power
{
get { return (pump1.Power + pump2.Power) / 2; }
}
public int Delay { get { return Math.Max(pump1.Delay, pump2.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; } }
UInt128 mask1;
UInt128 mask2;
public UInt128 Mask { get { return mask1 | mask2; } } /// derived from masks of two pumps
public Pump() { }
public Pump(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
pumpCfg = cfg as PumpCfg;
}
public override void Initialize()
{
if (pumpCfg.DebugLevel == DebugMode.Normal)
{
pump1 = TbfComponents.FindComponent(pumpCfg.Pump1, StateMachine.Components) as GenericDevices.IPump;
if (pump1 == null) throw new Exception(string.Format("Cannot find component {0}", pumpCfg.Pump1 != null ? pumpCfg.Pump1 : "<null>"));
pump2 = TbfComponents.FindComponent(pumpCfg.Pump2, StateMachine.Components) as GenericDevices.IPump;
if (pump2 == null) throw new Exception(string.Format("Cannot find component {0}", pumpCfg.Pump2 != null ? pumpCfg.Pump2 : "<null>"));
pumpFm1 = pump1 as GenericDevices.IPumpFM;
pumpFm2 = pump2 as GenericDevices.IPumpFM;
if (pump1 is BuiltIn.Pump.Pump) { mask1 = (pump1 as BuiltIn.Pump.Pump).Mask; }
else if (pump1 is Danfoss.VLT2800.Pump) { mask1 = (pump1 as Danfoss.VLT2800.Pump).Mask; }
else if (pump1 is Modbus.PumpFM.DanfossVLT.Pump) { mask1 = (pump1 as Modbus.PumpFM.DanfossVLT.Pump).Mask; }
else if (pump1 is Modbus.PumpFM.Grundfoss.Pump) { mask1 = (pump1 as Modbus.PumpFM.Grundfoss.Pump).Mask; }
else { mask1 = 0; }
if (pump2 is BuiltIn.Pump.Pump) { mask2 = (pump2 as BuiltIn.Pump.Pump).Mask; }
else if (pump2 is Danfoss.VLT2800.Pump) { mask2 = (pump2 as Danfoss.VLT2800.Pump).Mask; }
else if (pump2 is Modbus.PumpFM.DanfossVLT.Pump) { mask2 = (pump2 as Modbus.PumpFM.DanfossVLT.Pump).Mask; }
else if (pump2 is Modbus.PumpFM.Grundfoss.Pump) { mask2 = (pump2 as Modbus.PumpFM.Grundfoss.Pump).Mask; }
else { mask2 = 0; }
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
}
public void TurnOn(double powerArg)
{
if ((pumpFm1 != null) && (pumpFm2 != null))
{
pumpFm1.TurnOn(powerArg);
pumpFm2.TurnOn(powerArg);
}
else if ((pumpFm1 != null) && (pumpFm2 == null))
{
// TODO
if (powerArg >= 50)
{
pumpFm1.TurnOn(2 * (powerArg - 50.0));
//pump2.
}
else
{
pumpFm1.TurnOn(2 * powerArg);
//pump2.
}
// TODO
}
}
public void TurnOff()
{
if ((pumpFm1 != null) && (pumpFm2 != null))
{
pumpFm1.TurnOff();
pumpFm2.TurnOff();
}
else
{
// TODO
}
}
/// <summary>
/// The currently running operation.
/// </summary>
public enum CurrentOp
{
None,
TurnOn,
TurnOff,
}
///
CurrentOp currentOp;
double powerForOp;
/// <summary>
/// Turn the pump frequency inverter on.
/// </summary>
public IOperation TurnOnOp(double 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;
}
}
}