88 lines
2.4 KiB
C#
88 lines
2.4 KiB
C#
///
|
|
/// Copyright (c) 2015-2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using TBF.BenchControl.GenericDevices;
|
|
using Dirichlet.Numerics;
|
|
|
|
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 UInt128 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 >= 128)
|
|
{
|
|
throw new ArgumentOutOfRangeException("position");
|
|
}
|
|
this.Mask = (((UInt128)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.Warn(this.ToString());
|
|
}
|
|
|
|
public void UpdateRoute(ref UInt128 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
|
|
}
|
|
}
|
|
}
|
|
}
|