tbf/TBF/Rig/Elde/Valve/Valve.cs
2021-10-26 19:38:09 +02:00

95 lines
3.1 KiB
C#

///
/// Copyright (c) 2013-2016 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using Dirichlet.Numerics;
using log4net;
using TBF.Rig.GenericDevices;
namespace TBF.Rig.Elde.Valve
{
public class Valve : GenericDevices.ValveBase, IValve
{
private static readonly ILog log = LogManager.GetLogger(typeof(Valve));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly ValveCfg valveCfg;
///
public ValveCategory Category { get { return valveCfg.Category; } }
public int Delay { get { return valveCfg.OpenCloseTime; } }
public bool Inverted { get { return valveCfg.Inverted; } }
public bool InvertCouple { get { return valveCfg.InvertCouple; } }
public int LagOpening { get { return valveCfg.LagOpening; } }
public int LagClosing { get { return valveCfg.LagClosing; } }
readonly ControlBoardDev controlBoard;
public bool State { get { return (controlBoard.Route & Mask) != 0; } }
public IValve CoupledTo { get { return coupledTo; } }
readonly IValve coupledTo;
public int BitPosition { get { return valveCfg.BitPosition; } }
public UInt128 Mask; /// derived from bitPosition in the constructor
public Valve() { }
public Valve(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
valveCfg = cfg as ValveCfg;
controlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components);
if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent");
if (!string.IsNullOrEmpty(valveCfg.CoupledToName))
{
coupledTo = (IValve)TbfComponents.FindComponent(valveCfg.CoupledToName, components);
if (coupledTo == null) throw new Exception("Cannot find " + valveCfg.CoupledToName);
}
}
public override void Initialize()
{
if (valveCfg.BitPosition < 0 || valveCfg.BitPosition >= 128)
{
throw new ArgumentOutOfRangeException("position");
}
this.Mask = (((UInt128)1) << valveCfg.BitPosition);
log.FatalFormat("{0} initialized: {1}", Name, this);
}
#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)
{
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
}
}