2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using log4net;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Elde.Diverter
|
|
|
|
|
|
{
|
2014-12-31 12:46:33 +00:00
|
|
|
|
public class Diverter : ComponentBase, GenericDevices.IDiverter
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Diverter));
|
2014-12-31 12:46:33 +00:00
|
|
|
|
public override string ToString() { return string.Format("Diverter({0})", Cfg.ToString(1)); }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Enumeration of balances via static fields and methods
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
static int nextIdx = 0;
|
|
|
|
|
|
public static new void ResetStaticProperties()
|
|
|
|
|
|
{
|
|
|
|
|
|
nextIdx = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static int DivertersCount { get { return nextIdx; } }
|
|
|
|
|
|
public static GenericDevices.IDiverter[] Diverters;
|
|
|
|
|
|
public static int[] DiverterLevelsPct;
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
|
|
|
|
|
protected int diverterNr; /// 0-based diverter number
|
2015-11-27 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-31 12:46:33 +00:00
|
|
|
|
readonly DiverterCfg diverterCfg;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
readonly ControlBoardDev controlBoard;
|
2014-12-29 23:17:45 +00:00
|
|
|
|
readonly ulong mask; /// derived from bitPosition in the constructor
|
2015-08-14 16:14:08 +00:00
|
|
|
|
public int AdcNr { get { return diverterCfg.AdcNr; } }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2014-12-31 12:46:33 +00:00
|
|
|
|
public bool State
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (controlBoard.Route & mask) != 0; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-29 09:14:33 +00:00
|
|
|
|
public Diverter()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
public Diverter(DiverterCfg cfg, IList<Generic.IComponent> components)
|
2015-03-24 01:45:36 +00:00
|
|
|
|
: base(cfg)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2014-12-31 12:46:33 +00:00
|
|
|
|
diverterCfg = cfg;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
controlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
|
|
|
|
if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
|
|
|
|
|
|
|
|
if (cfg.BitPosition < 0 || cfg.BitPosition >= 64) throw new ArgumentOutOfRangeException("position");
|
2014-12-29 23:17:45 +00:00
|
|
|
|
this.mask = (((ulong)1) << cfg.BitPosition);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
diverterNr = nextIdx++;
|
|
|
|
|
|
///
|
|
|
|
|
|
if (Diverters == null || Diverters.Length < nextIdx)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenericDevices.IDiverter[] divertersSoFar = Diverters;
|
|
|
|
|
|
int[] levelsSoFar = DiverterLevelsPct;
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
Diverters = new GenericDevices.IDiverter[nextIdx];
|
|
|
|
|
|
DiverterLevelsPct = new int[nextIdx];
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
if (divertersSoFar != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < divertersSoFar.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Diverters[i] = divertersSoFar[i];
|
|
|
|
|
|
DiverterLevelsPct[i] = levelsSoFar[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
Diverters[nextIdx - 1] = this;
|
|
|
|
|
|
DiverterLevelsPct[nextIdx - 1] = diverterCfg.StartingLevel;
|
|
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
|
2015-12-02 13:10:54 +00:00
|
|
|
|
/// Prepare arguments of SendCalibData()
|
|
|
|
|
|
if (diverterNr >= 0 && diverterNr < 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.RegValveCalib[5 + diverterNr, 0] = (uint)diverterCfg.AdcValueToSink;
|
|
|
|
|
|
controlBoard.RegValveCalib[5 + diverterNr, 1] = (uint)diverterCfg.AdcValueToTank;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
log.Debug(this.ToString());
|
|
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Configuration Change Handling
|
|
|
|
|
|
|
|
|
|
|
|
public static void OnCfgChange(object sender, CfgChangeArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (CfgChangeHandler == null) return;
|
|
|
|
|
|
try { CfgChangeHandler(sender, args); }
|
|
|
|
|
|
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
|
|
|
|
|
|
|
|
|
|
|
|
public override void StartChangeHandler()
|
|
|
|
|
|
{
|
|
|
|
|
|
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
DiverterCfg tmpcfg = args.Cfg as DiverterCfg;
|
|
|
|
|
|
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
|
|
|
|
|
{
|
2015-06-12 14:39:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (args.Command == CfgChangeCmd.GetAdc1 || args.Command == CfgChangeCmd.GetAdc2)
|
|
|
|
|
|
{
|
|
|
|
|
|
int adc = (int)controlBoard.AnalogInputRaw(diverterCfg.AdcNr, 0);
|
|
|
|
|
|
// TOTEST
|
|
|
|
|
|
DiverterCfgCtrl.OnCmdResponse(this, new CmdResponseArgs(args.Command, diverterCfg.BitPosition, adc));
|
2015-03-24 01:45:36 +00:00
|
|
|
|
}
|
2015-06-12 16:02:19 +00:00
|
|
|
|
else if (args.Command == CfgChangeCmd.DivToTank)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Name.Contains("1")) /// TODO: Put flowmeter# into config
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.DiverterToTank(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.DiverterToTank(3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (args.Command == CfgChangeCmd.DivToSink)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Name.Contains("1")) /// TODO: Put flowmeter# into config
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.DiverterToSink(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.DiverterToSink(3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (args.Command == CfgChangeCmd.ReadDivTransition)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Name.Contains("1")) /// TODO: Put flowmeter# into config
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.ReadDiverterTransition(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
controlBoard.ReadDiverterTransition(3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion Configuration Change Handling
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|