2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2018-01-24 12:42:07 +00:00
|
|
|
|
/// Copyright (c) 2013-2018 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using log4net;
|
2016-12-12 16:25:12 +00:00
|
|
|
|
using Dirichlet.Numerics;
|
2017-07-20 11:49:16 +00:00
|
|
|
|
using TBF.BenchControl.Generic;
|
2017-08-22 13:11:21 +00:00
|
|
|
|
using TBF.Resources;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Elde.Diverter
|
|
|
|
|
|
{
|
2017-08-22 13:11:21 +00:00
|
|
|
|
public class Diverter : ComponentBase, IDevice, GenericDevices.IDiverter, GenericDevices.ISequenceCondition
|
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;
|
2017-07-27 06:14:02 +00:00
|
|
|
|
public static int[] DivStartingLevelPct;
|
|
|
|
|
|
public static int[] DivStoppingLevelPct;
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2018-03-09 11:25:58 +00:00
|
|
|
|
readonly int diverterNr0; /// 0-based diverter number
|
|
|
|
|
|
public int DiverterNr { get { return diverterNr0 + 1; } } /// 1-based public 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;
|
2017-08-22 13:11:21 +00:00
|
|
|
|
readonly UInt128 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
|
|
|
|
|
2017-08-22 13:11:21 +00:00
|
|
|
|
readonly IOperation diverterToTankOp;
|
|
|
|
|
|
readonly IOperation diverterToSinkOp;
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-05 12:32:36 +00:00
|
|
|
|
public Diverter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
2015-03-24 01:45:36 +00:00
|
|
|
|
: base(cfg)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2016-08-05 12:32:36 +00:00
|
|
|
|
diverterCfg = cfg as DiverterCfg;
|
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");
|
|
|
|
|
|
|
2016-08-05 12:32:36 +00:00
|
|
|
|
if (diverterCfg.BitPosition < 0 || diverterCfg.BitPosition >= 64) throw new ArgumentOutOfRangeException("position");
|
2016-12-12 16:25:12 +00:00
|
|
|
|
this.mask = (((UInt128)1) << diverterCfg.BitPosition);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-03-09 11:25:58 +00:00
|
|
|
|
diverterNr0 = nextIdx++;
|
2015-11-27 15:41:40 +00:00
|
|
|
|
///
|
|
|
|
|
|
if (Diverters == null || Diverters.Length < nextIdx)
|
|
|
|
|
|
{
|
|
|
|
|
|
GenericDevices.IDiverter[] divertersSoFar = Diverters;
|
2017-07-27 06:14:02 +00:00
|
|
|
|
int[] startingLevelsSoFar = DivStartingLevelPct;
|
|
|
|
|
|
int[] stoppingLevelsSoFar = DivStoppingLevelPct;
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
Diverters = new GenericDevices.IDiverter[nextIdx];
|
2017-07-27 06:14:02 +00:00
|
|
|
|
DivStartingLevelPct = new int[nextIdx];
|
|
|
|
|
|
DivStoppingLevelPct = 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];
|
2017-07-27 06:14:02 +00:00
|
|
|
|
DivStartingLevelPct[i] = startingLevelsSoFar[i];
|
|
|
|
|
|
DivStoppingLevelPct[i] = stoppingLevelsSoFar[i];
|
2015-11-27 15:41:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-02 13:10:54 +00:00
|
|
|
|
|
2015-11-27 15:41:40 +00:00
|
|
|
|
Diverters[nextIdx - 1] = this;
|
2017-07-27 06:14:02 +00:00
|
|
|
|
DivStartingLevelPct[nextIdx - 1] = diverterCfg.StartingLevel;
|
|
|
|
|
|
DivStoppingLevelPct[nextIdx - 1] = diverterCfg.StoppingLevel;
|
2015-11-27 15:41:40 +00:00
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
|
2015-12-02 13:10:54 +00:00
|
|
|
|
/// Prepare arguments of SendCalibData()
|
2017-07-27 06:14:02 +00:00
|
|
|
|
if (diverterCfg.AdcNr >= 0 && diverterCfg.AdcNr < controlBoard.RegValveCalib.GetLength(0))
|
2015-12-02 13:10:54 +00:00
|
|
|
|
{
|
2017-07-27 06:14:02 +00:00
|
|
|
|
controlBoard.RegValveCalib[diverterCfg.AdcNr, 0] = (uint)diverterCfg.AdcValueToSink;
|
|
|
|
|
|
controlBoard.RegValveCalib[diverterCfg.AdcNr, 1] = (uint)diverterCfg.AdcValueToTank;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("Unable to set {0} ADC levels: RegValveCalib array size={1}, diverter AdcNr={2}",
|
|
|
|
|
|
Name, controlBoard.RegValveCalib.GetLength(0), diverterCfg.AdcNr);
|
2015-12-02 13:10:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-22 13:11:21 +00:00
|
|
|
|
diverterToTankOp = new SwitchDiverterOp(controlBoard, true, Name.Contains("1") ? 1 : 3);
|
|
|
|
|
|
diverterToSinkOp = new SwitchDiverterOp(controlBoard, false, Name.Contains("1") ? 1 : 3);
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
log.Debug(this.ToString());
|
|
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-07-20 11:49:16 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// IDevice interface implementation
|
|
|
|
|
|
///
|
|
|
|
|
|
public void Initialize() { }
|
|
|
|
|
|
public void RunDeviceBefore()
|
|
|
|
|
|
{
|
|
|
|
|
|
int adc = (int)StateMachine.ControlBoard.AnalogInputRaw(diverterCfg.AdcNr, 0);
|
|
|
|
|
|
Handlers.OnAdcChanged(this, new CmdResponseArgs(CfgChangeCmd.GetAdc, diverterCfg.BitPosition, adc));
|
|
|
|
|
|
}
|
|
|
|
|
|
public void RunDeviceAfter() { }
|
|
|
|
|
|
public void StopDevice() { }
|
2017-12-05 18:26:07 +00:00
|
|
|
|
public void StopDevice2() { }
|
2017-07-20 11:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-08-22 13:11:21 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// ISequenceCondition interface implementation
|
|
|
|
|
|
///
|
|
|
|
|
|
public int ConditionsCount { get { return 2; } }
|
|
|
|
|
|
|
|
|
|
|
|
/// Returned strings are added to the combo-box
|
|
|
|
|
|
public string ConditionName(int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (i == 0) return string.Format("{0} {1}", Name, Strings.Start);
|
|
|
|
|
|
else return string.Format("{0} {1}", Name, Strings.End);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IOperation ConditionOp(int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (i == 0) return diverterToTankOp;
|
|
|
|
|
|
else return diverterToSinkOp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
2018-02-01 22:04:15 +00:00
|
|
|
|
{
|
2018-03-07 12:01:53 +00:00
|
|
|
|
int diverterNr = int.Parse(Name.Substring(3));
|
2018-03-16 02:04:16 +00:00
|
|
|
|
#if GENESIS || PETERSBURG_50 || PETERSBURG_200 || FUZHOU_40 || FUZHOU150 || FUZHOU300
|
2018-03-07 12:01:53 +00:00
|
|
|
|
int flowMtrNr = diverterNr + 1;
|
|
|
|
|
|
#elif BADGER_STREDNA_TRAT
|
|
|
|
|
|
int flowMtrNr = (diverterNr == 1) ? 1 : ((diverterNr == 2) ? 9 : 20);
|
2018-03-16 02:04:16 +00:00
|
|
|
|
#else
|
2018-03-07 12:01:53 +00:00
|
|
|
|
int flowMtrNr = (diverterNr == 1) ? 1 : 3;
|
2018-01-24 12:42:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (args.Command == CfgChangeCmd.CfgChange)
|
2015-03-24 01:45:36 +00:00
|
|
|
|
{
|
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)
|
|
|
|
|
|
{
|
2018-01-24 12:42:07 +00:00
|
|
|
|
controlBoard.DiverterToTank(flowMtrNr);
|
2015-06-12 16:02:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (args.Command == CfgChangeCmd.DivToSink)
|
|
|
|
|
|
{
|
2018-01-24 12:42:07 +00:00
|
|
|
|
controlBoard.DiverterToSink(flowMtrNr);
|
2015-06-12 16:02:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (args.Command == CfgChangeCmd.ReadDivTransition)
|
|
|
|
|
|
{
|
2018-01-24 12:42:07 +00:00
|
|
|
|
controlBoard.ReadDiverterTransition(flowMtrNr);
|
2015-06-12 16:02:19 +00:00
|
|
|
|
}
|
2015-03-24 01:45:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion Configuration Change Handling
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|