tbf/TestBenchFramework/BenchControl/Cameras/CameraRoi/CameraRoi.cs

137 lines
4.6 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using log4net;
using Config.Entities;
using TBF.BenchControl;
using TBF.BenchControl.Generic;
using TBF.Boxes;
namespace TBF.BenchControl.Cameras.CameraRoi
{
/// <summary>
/// IdcCamera object represents all IDC cameras.
/// Camera ID is an argument of most methods.
/// </summary>
public class CameraRoi : ComponentBase, GenericDevices.ICameraRoi, GenericDevices.IRegisterReader
{
private static readonly ILog log = LogManager.GetLogger(typeof(CameraRoi));
public override string ToString() { return string.Format("CameraRoi({0})", Cfg.ToString(1)); }
readonly CameraRoiCfg cameraRoiCfg;
readonly GenericDevices.ICamera camera;
public GenericDevices.ICamera Camera { get { return camera; } }
public double PulsesPerLtr { get { return (double)cameraRoiCfg.ProcParams.GradPerLtr; } }
public double LtrsPerPulse { get { return ltrsPerPulse; } }
readonly double ltrsPerPulse;
public int WMPulses { get { return wmPulses; } }
public int WMRefPulses { get { return wmRefPulses; } }
public double WMVolume { get { return ltrsPerPulse * (double)wmPulses; } }
int wmPulses;
int wmRefPulses;
public CameraRoi()
{
}
public CameraRoi(CameraRoiCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
cameraRoiCfg = cfg;
ltrsPerPulse = (PulsesPerLtr <= float.Epsilon) ? 0 : (1 / PulsesPerLtr);
if (cfg.DebugLevel != DebugMode.Off)
{
camera = TbfComponents.FindComponent(cfg.ParentName, components) as GenericDevices.ICamera;
if ((camera == null) && (cfg.DebugLevel != DebugMode.Inherit))
{
throw new Exception("Cannot find " + Name + " parent");
}
}
log.Debug(this.ToString());
}
public void Clear()
{
wmPulses = 0;
wmRefPulses = 0;
}
public void TestCompleted()
{
/// TODO: Implement
}
/// <summary>
/// Events: Event.ReadRegisterDone, Event.Error
/// </summary>
/// <param name="pulses">Reference to a variable for the water meter pulses</param>
/// <returns>ReadWaterMeter instance reference casted to IOperaton</returns>
public IOperation ReadRegisterOp(ref IntBox pulses)
{
return new ReadRegisterOp(this, ref pulses);
}
/// <summary>
/// Events: Event.ReadRegisterDone, Event.Error
/// </summary>
/// <param name="pulses">Reference to a variable for the water meter pulses</param>
/// <param name="pulses">Reference to a variable for the related reference flow meter pulses</param>
/// <returns>ReadWaterMeter instance reference casted to IOperaton</returns>
public IOperation ReadRegisterOp(ref IntBox pulses, ref IntBox refPulses)
{
return new ReadRegisterOp(this, ref pulses, ref refPulses);
}
/// <summary>
/// Events: ReadReferenceDone, Error
/// </summary>
/// <param name="pulses">Reference to a variable for the water meter pulses</param>
/// <returns>ReadWaterMeter instance reference casted to IOperaton</returns>
public IOperation ReadReferenceForRegisterOp(ref IntBox pulses, ref IntBox refPulses)
{
return null; /// not implemented
}
/// <summary>
/// Watermeter wheel/arrow detection process.
/// </summary>
/// <param name="roiId">Region of interest (1..4)</param>
/// <returns>Reference to operation object instance</returns>
public IOperation DetectOp()
{
RoiProcedureParams pp = cameraRoiCfg.ProcParams;
return camera.DetectOp(cameraRoiCfg.RoiNr,
new Cameras.IdcCamera.Command.Detect(cameraRoiCfg.RoiNr, 0, pp.IRoiX, pp.IRoiY, pp.IRoiW, pp.IRoiH,
pp.CX, pp.CY, pp.ORoiW, pp.ORoiH, pp.R1, pp.R2, pp.R3,
pp.Ang1, pp.Ang2, pp.AngStep, pp.CCW, (pp.CCW ? -30 : 30),
pp.SequenceLen, pp.SequenceTiming, pp.BackgroundCorr,
pp.BrightnessRoi * camera.Brightness));
}
/// <summary>
/// Watermeter wheel/arrow angle measurement process.
/// </summary>
/// <param name="roiId">Region of interest (1..4)</param>
/// <returns>Reference to operation object instance</returns>
public IOperation MeasureOp()
{
RoiProcedureParams pp = cameraRoiCfg.ProcParams;
return camera.MeasureOp(cameraRoiCfg.RoiNr,
new Cameras.IdcCamera.Command.Measure(cameraRoiCfg.RoiNr, true, 0, pp.IRoiX, pp.IRoiY, pp.IRoiW, pp.IRoiH,
pp.CX, pp.CY, pp.ORoiW, pp.ORoiH, pp.MinMvmt, pp.MaxMvmt,
pp.BrightnessRoi * camera.Brightness));
}
}
}