///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.Generic;
using TBF.Boxes;
namespace TBF.BenchControl.Modbus.TankSelector
{
public class TankSelector : ComponentBase, IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(TankSelector));
public override string ToString() { return string.Format("TankSelector({0})", Cfg.ToString(1)); }
readonly TankSelectorCfg tankSelectorCfg;
readonly TBF.BenchControl.Modbus.QuidoRS.QuidoRS quidoRS;
readonly TBF.BenchControl.Modbus.Easytherm.Easytherm easytherm;
readonly Elde.ControlBoardDev controlBoard;
public enum CurrentOp
{
None,
SelectTank,
}
CurrentOp currentOp;
///
/// Quido digital out board outputs assignment
///
private enum QuidoInputs
{
None = 0,
SelectColdTank = 0x0001,
SelectWarmTank = 0x0002,
SelectHotTank = 0x0004,
PumpToColdTank = 0x0008,
PumpToWarmTank = 0x0010,
PumpToHotTank = 0x0020,
Mixing = 0x0040,
HeatingHotTank = 0x0080,
HeatingWarmTank = 0x0100,
CoolingHotTank = 0x0200,
CoolingWarmTank = 0x0400,
CoolingColdTank = 0x0800,
}
public TankSelector()
{
}
public TankSelector(Generic.IComponentCfg cfg, IList components)
: base(cfg)
{
tankSelectorCfg = cfg as TankSelectorCfg;
quidoRS = (TBF.BenchControl.Modbus.QuidoRS.QuidoRS)TbfComponents.FindComponent(cfg.ParentName, components);
if (quidoRS == null) throw new Exception(string.Format("Cannot find parent '{0}' of the component {1}", cfg.ParentName, Name));
controlBoard = (Elde.ControlBoardDev)TbfComponents.FindComponent(tankSelectorCfg.ControlBoard, components);
if (controlBoard == null) throw new Exception(string.Format("Cannot find control board '{0}' for the component {1}", tankSelectorCfg.ControlBoard, Name));
log.Debug(this.ToString());
}
///
/// Set outputs of QuidoRS board
///
/// output value
public void SetOutputs(uint outputs)
{
quidoRS.SetOutputs((ushort)outputs);
}
///
/// Events: SetOpututsDone, Error
///
/// Reference to a variable for the pressure in Bar
/// SetOutputsOp instance reference casted to IOperaton
//public IOperation SetOutputsOp(ushort outValue)
//{
// return new SetOutputsOp(this, outValue);
//}
///
/// Events: pressureDone, Error
///
/// Reference to a variable for the pressure in Bar
/// Event returned when SetOutput is done
/// SetOutputsOp instance reference casted to IOperaton
//public IOperation ReadPressureOp(ushort outValue, Event setOutputsDone)
//{
// return new SetOutputsOp(this, outValue, setOutputsDone);
//}
int tankNo;
///
public IOperation SelectTankOp(int tankNo)
{
if (currentOp != CurrentOp.None)
{
throw new Exception("Cannot start operation 'SelectTankOp'");
}
this.tankNo = tankNo;
currentOp = CurrentOp.SelectTank;
return this;
}
/// Start this operation
public void Start()
{
switch (currentOp)
{
case CurrentOp.SelectTank:
SetOutputs((tankNo==1) ? 1u : ((tankNo==2) ? 2u : ((tankNo==3) ? 3u : 0)));
break;
default:
break;
}
}
/// Run this operation
public Event Run()
{
switch (currentOp)
{
case CurrentOp.SelectTank:
return Event.Done;
default:
return Event.Done;
}
}
/// Stop this operation
public void Stop()
{
currentOp = CurrentOp.None;
}
}
}