tbf/TestBenchFramework/BenchControl/Network/Camera/Roi/Roi.cs

172 lines
3.8 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using Config.Entities;
namespace TBF.BenchControl.Network.Camera.Roi
{
public class Roi : ComponentBase, GenericDevices.IRoi, IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(Roi));
public override string ToString()
{
return string.Format("{0}({1})", this.GetType().Namespace.Substring(17), Cfg.ToString(1));
}
///
/// Cfg
///
readonly RoiCfg roiCfg;
public readonly CLP1611.Camera NetCamera;
public readonly Roi PreviousRoi;
public Telnet.TelnetClient Telnet;
///
/// ICamera interface functions
///
public GenericDevices.ICamera Camera { get { return NetCamera as GenericDevices.ICamera; } }
public double PulsesPerLtr { get { return (double)roiCfg.ProcParams.PulsesPerLtr; } }
public double LtrsPerPulse { get { return (PulsesPerLtr <= float.Epsilon) ? 1.0 : (1 / PulsesPerLtr); ; } }
public int WMPulses { get { return wmPulses; } }
public int WMRefPulses { get { return wmRefPulses; } }
public double WMVolume { get { return LtrsPerPulse * (double)wmPulses; } }
int wmPulses;
int wmRefPulses;
///
/// Roi specific
///
public bool Detected
{
get { return detected; }
set { detected = value; }
}
bool detected;
///
public Boxes.IntBox RoiX;
public Boxes.IntBox RoiY;
public Boxes.IntBox RoiWid;
public Boxes.IntBox RoiHgh;
public Roi()
{
Clear();
}
public Roi(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
roiCfg = cfg as RoiCfg;
NetCamera = TbfComponents.FindComponent(cfg.ParentName, components) as CLP1611.Camera;
PreviousRoi = TbfComponents.FindComponent(roiCfg.PreviousRoi, components) as Roi;
detected = false;
RoiX = new Boxes.IntBox();
RoiY = new Boxes.IntBox();
RoiWid = new Boxes.IntBox();
RoiHgh = new Boxes.IntBox();
Clear();
log.Debug(this.ToString());
}
#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)
{
RoiCfg tmpcfg = args.Cfg as RoiCfg;
if (tmpcfg != null && tmpcfg.Name.Equals(Name))
{
if (args.Command == CfgChangeCmd.CfgChange)
{
roiCfg.RoiParams = tmpcfg.RoiParams;
}
}
};
}
#endregion Configuration Change Handling
public void Clear()
{
wmPulses = 0;
wmRefPulses = 0;
}
public void TestCompleted() { }
public IOperation RoiDetectionOp()
{
if (NetCamera != null && NetCamera.Telnet != null)
{
Telnet = NetCamera.Telnet;
return new RoiDetectionOp(this, roiCfg.RoiParams, RoiX, RoiY, RoiWid, RoiHgh);
}
else
{
return null;
}
}
public IOperation MeasurementOp()
{
return null; /// TODO: To be implemented in a Camera
}
public IOperation ReadRegisterOp()
{
return this;
}
void UdatePulses()
{
}
/// <summary>Start this operation</summary>
public void Start()
{
UdatePulses();
}
/// <summary>Run this operation</summary>
/// <returns>eventDone</returns>
public Event Run()
{
UdatePulses();
return Event.ReadRegisterDone;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
}
}
}