/// /// Copyright (c) 2013-2020 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using Dirichlet.Numerics; using log4net; using TBF.Rig.GenericDevices; namespace TBF.Rig.BuiltIn.Valve { /// /// Plain valve, open/close state is controlled by 1 bit from the route /// public class Valve : ValveBase, IValve, SchematicDrawing.IDrawingItCmpnt { private static readonly ILog log = LogManager.GetLogger(typeof(Valve)); public override string ToString() { return string.Format("Valve({0})", Cfg.ToString(-1)); } readonly ValveCfg valveCfg; public SchematicDrawing.IDrawingItem DrawingItem { get { return valveCfg as SchematicDrawing.IDrawingItem; } } readonly TBF.Rig.ControlBoard.IControlBoard controlBoard; public ValveCategory Category { get { return valveCfg.Category; } } public int Delay { get { return valveCfg.OpenCloseTime; } } public bool Inverted { get { return valveCfg.Inverted; } } public IValve CoupledTo { get { return coupledTo; } } readonly IValve coupledTo; public bool InvertCouple { get { return valveCfg.InvertCouple; } } public int LagOpening { get { return valveCfg.LagOpening; } } public int LagClosing { get { return valveCfg.LagClosing; } } public readonly UInt128 Mask; /// derived from bitPosition in the constructor public int BitPosition { get { return valveCfg.BitNr; } } public Valve() { } public Valve(Generic.IComponentCfg cfg, IList components) : base(cfg) { valveCfg = cfg as ValveCfg; controlBoard = TbfComponents.FindComponent(cfg.ParentName, components) as TBF.Rig.ControlBoard.IControlBoard; if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent"); if (valveCfg.BitNr < 0 || valveCfg.BitNr >= 128) { throw new ArgumentOutOfRangeException("position"); } this.Mask = (((UInt128)1) << valveCfg.BitNr); if (valveCfg.CoupledToName != null) { coupledTo = (IValve)TbfComponents.FindComponent(valveCfg.CoupledToName, components); } log.Warn(this.ToString()); } public bool State { get { return (controlBoard.Route & Mask) != 0; } } #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 CfgChangeHandler; public override void StartChangeHandler() { CfgChangeHandler += delegate(object sender, CfgChangeArgs args) { ValveCfg tmpcfg = args.Cfg as ValveCfg; if (tmpcfg != null && tmpcfg.Name.Equals(Name)) { if (args.Command == CfgChangeCmd.CfgChange) { valveCfg.OpenCloseTime = tmpcfg.OpenCloseTime; valveCfg.LagOpening = tmpcfg.LagOpening; valveCfg.LagClosing = tmpcfg.LagClosing; } } }; } #endregion Configuration Change Handling } }