tbf/TestBenchFramework/BenchControl/Elde/ValveEx/Valve.cs

87 lines
2.4 KiB
C#

///
/// Copyright (c) 2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.Elde.ValveEx
{
public class Valve : ComponentBase, Generic.IComponent
{
private static readonly ILog log = LogManager.GetLogger(typeof(Valve));
public override string ToString() { return string.Format("ValveEx({0})", Cfg.ToString(1)); }
readonly ValveCfg valveCfg;
public readonly ulong Mask; /// derived from bitPosition in the constructor
///
public readonly IValve[] InputValves;
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 (valveCfg.BitPosition < 0 || valveCfg.BitPosition >= 64)
{
throw new ArgumentOutOfRangeException("position");
}
this.Mask = (((ulong)1) << valveCfg.BitPosition);
InputValves = new IValve[valveCfg.InputValvesCount];
InputValves[0] = TbfComponents.FindComponent(valveCfg.Input1, components) as IValve;
InputValves[1] = TbfComponents.FindComponent(valveCfg.Input2, components) as IValve;
if (InputValves.Length > 2)
InputValves[2] = TbfComponents.FindComponent(valveCfg.Input3, components) as IValve;
if (InputValves.Length > 3)
InputValves[3] = TbfComponents.FindComponent(valveCfg.Input4, components) as IValve;
if (InputValves.Length > 4)
InputValves[4] = TbfComponents.FindComponent(valveCfg.Input5, components) as IValve;
log.Debug(this.ToString());
}
public void UpdateRoute(ref ulong route)
{
bool output;
if (valveCfg.LogicalOp == ValveExOperation.And)
{
output = true;
foreach (var valve in InputValves)
{
if (valve != null) output = output && valve.State;
}
}
else if (valveCfg.LogicalOp == ValveExOperation.Or)
{
output = false;
foreach (var valve in InputValves)
{
if (valve != null) output = output || valve.State;
}
}
else
return;
if (output != valveCfg.InvertOutput)
{
route = route | Mask; /// Set the output bit to true
}
else
{
route = route & (~Mask); /// Reset the output bit to false
}
}
}
}