tbf/TestBenchFramework/BenchControl/Elde/Diverter/Diverter.cs

114 lines
3.1 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
namespace TBF.BenchControl.Elde.Diverter
{
public class Diverter : ComponentBase, GenericDevices.IDiverter
{
private static readonly ILog log = LogManager.GetLogger(typeof(Diverter));
public override string ToString() { return string.Format("Diverter({0})", Cfg.ToString(1)); }
readonly DiverterCfg diverterCfg;
readonly ControlBoardDev controlBoard;
2014-12-29 23:17:45 +00:00
readonly ulong mask; /// derived from bitPosition in the constructor
public bool State
{
get { return (controlBoard.Route & mask) != 0; }
}
public Diverter()
{
}
public Diverter(DiverterCfg cfg, IList<Generic.IComponent> components)
2015-03-24 01:45:36 +00:00
: base(cfg)
{
diverterCfg = cfg;
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);
2015-03-24 01:45:36 +00:00
/// Prepare data for SendCalibData() control board component method
2015-04-18 05:31:52 +00:00
controlBoard.DiverterEdge[0] = (uint)diverterCfg.StartingLevel;
controlBoard.DiverterEdge[1] = (uint)diverterCfg.StoppingLevel;
2015-03-24 01:45:36 +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)
{
}
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
}
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
}
}