271 lines
7.3 KiB
C#
271 lines
7.3 KiB
C#
///
|
|
/// Copyright (c) 2017 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using log4net;
|
|
using Config.Entities;
|
|
|
|
namespace TBF.BenchControl.Network.Camera.Roi
|
|
{
|
|
public class Roi : ComponentBase, GenericDevices.IRegReaderLiveCamera, IOperation
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
|
|
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
|
|
///
|
|
public readonly RoiCfg RoiCfg;
|
|
|
|
public readonly CLP1611.Camera NetCamera;
|
|
public readonly Roi PreviousRoi;
|
|
|
|
///
|
|
/// ICamera interface functions
|
|
///
|
|
public GenericDevices.ICamera Camera { get { return NetCamera as GenericDevices.ICamera; } }
|
|
|
|
public int Position
|
|
{
|
|
get
|
|
{
|
|
int firstDigitPos = Name.IndexOfAny(new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' });
|
|
int position;
|
|
return (firstDigitPos < 0) ? 0 : (int.TryParse(Name.Substring(firstDigitPos), out position) ? position : 0);
|
|
}
|
|
}
|
|
public Config.Entities.RegisterReaderType RegisterReaderType { get { return Config.Entities.RegisterReaderType.Camera; } }
|
|
public double PulsesPerLtr { get { return RoiCfg.ProcParams.PulsesPerLtr; } }
|
|
public double LtrsPerPulse { get { return (PulsesPerLtr <= float.Epsilon) ? 1.0 : (1 / PulsesPerLtr); } }
|
|
|
|
int cameraPulses;
|
|
int pulsesDelta; /// = (WMPulses - cameraPulses) ... WMPulses = cameraPulses + pulsesDelta, Clear() calculates pulsesDelta
|
|
public int WMPulses { get { return cameraPulses + pulsesDelta; } }
|
|
|
|
long cameraTime;
|
|
long timeDelta; /// = (WMTestTime - cameraTime) ... WMTestTime = cameraTime + timeDelta, Clear() calculates timeDelta
|
|
public double WMTestTime { get { return (cameraTime + timeDelta) / 1000.0; } }
|
|
|
|
int wmRefPulses;
|
|
public int WMRefPulses { get { return wmRefPulses; } }
|
|
|
|
public double WMVolume { get { return LtrsPerPulse * (double)WMPulses; } }
|
|
|
|
public double BeginWMState { get { return 0; } } /// Always 0
|
|
public double EndWMState { get { return WMVolume; } } /// Derived from WMVolume
|
|
|
|
|
|
double volumeStart;
|
|
public double VolumeStart { get { return volumeStart; } } /// in l
|
|
|
|
double volumeEnd;
|
|
public double VolumeEnd { get { return volumeEnd; } } /// in l
|
|
|
|
double timestampStart;
|
|
public double TimestampStart { get { return timestampStart; } } /// in s
|
|
|
|
double timestampEnd;
|
|
public double TimestampEnd { get { return timestampEnd; } } /// in s
|
|
|
|
///
|
|
/// Roi specific
|
|
///
|
|
public bool Detected { get { return detected; } }
|
|
bool detected;
|
|
///
|
|
public Boxes.IntBox RoiX;
|
|
public Boxes.IntBox RoiY;
|
|
public Boxes.IntBox RoiWid;
|
|
public Boxes.IntBox RoiHgh;
|
|
///
|
|
public void ClearRoi()
|
|
{
|
|
detected = false;
|
|
}
|
|
///
|
|
public void SetRoi(int x, int y, int wid, int hgh)
|
|
{
|
|
RoiX.Val = x;
|
|
RoiY.Val = y;
|
|
RoiWid.Val = wid;
|
|
RoiHgh.Val = hgh;
|
|
detected = true;
|
|
}
|
|
///
|
|
public string ImageFileNamePrefix { get { return imgFileNamePrefix; } }
|
|
string imgFileNamePrefix;
|
|
///
|
|
private int roiHandle;
|
|
///
|
|
public void RegisterRoiToCamera()
|
|
{
|
|
if (RoiCfg.DebugLevel == DebugMode.Simulate)
|
|
{
|
|
roiHandle = Camera.RegisterRoi(string.Format("-roi 1 300 200 134 134 66 66 20 66 0 0 360 360 1 50 0 0 270 0 bkgnd.bmp"));
|
|
}
|
|
else
|
|
{
|
|
roiHandle = Camera.RegisterRoi(string.Format("-roi 1 {0} {1} {2} bkgnd.bmp", RoiX.Val, RoiY.Val, RoiCfg.RoiParams));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Roi()
|
|
{
|
|
}
|
|
|
|
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();
|
|
|
|
//StartChangeHandler(); /// already done in StateMachine.InitializeBoardEtc(...)
|
|
|
|
log.Warn(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.LoadImages = tmpcfg.LoadImages;
|
|
RoiCfg.SaveImages = tmpcfg.SaveImages;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
|
|
public void Clear()
|
|
{
|
|
pulsesDelta = -cameraPulses;
|
|
timeDelta = -cameraTime;
|
|
wmRefPulses = 0;
|
|
}
|
|
|
|
public void TestCompleted() { }
|
|
|
|
|
|
public IOperation RoiDetectionOp()
|
|
{
|
|
if (NetCamera != null && RoiCfg.DebugLevel != DebugMode.Simulate)
|
|
{
|
|
imgFileNamePrefix = Path.Combine(Program.TempImagesDir, string.Format("t{0}-{1}-", StateMachine.Time, RoiCfg.Name));
|
|
return new RoiDetectionOp(this, NetCamera, imgFileNamePrefix, RoiX, RoiY, RoiWid, RoiHgh);
|
|
}
|
|
else
|
|
{
|
|
return null; /// When NetCamera == null or ROI is in simulation mode
|
|
}
|
|
}
|
|
|
|
|
|
public IOperation GrabImageOp(string imageFileName, bool blackAndWhite, bool lowResolution, ImageRotation imageRotation)
|
|
{
|
|
if (NetCamera != null)
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
return NetCamera.GrabImagesOp(new string[] { imageFileName }, blackAndWhite, lowResolution, imageRotation);
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
|
|
public IOperation GrabImageOp(string imageFileName)
|
|
{
|
|
if (NetCamera != null)
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
return NetCamera.GrabImagesOp(new string[] { imageFileName }, false, true, ImageRotation.None);
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Updates wmPulses, caled from 'Start' and 'Run' functions of 'ReadRegisterOp'
|
|
/// </summary>
|
|
void UdatePulses()
|
|
{
|
|
if (detected && roiHandle != 0)
|
|
{
|
|
cameraPulses = NetCamera.GetResult(roiHandle, out cameraTime);
|
|
}
|
|
|
|
wmRefPulses = StateMachine.ControlBoard.EtPulses(0);
|
|
}
|
|
|
|
///
|
|
/// Note: Measurement operations of cameras should preceeede ReadRegister operations of ROI-s
|
|
///
|
|
public IOperation ReadRegisterOp()
|
|
{
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
UdatePulses();
|
|
Clear();
|
|
volumeStart = WMVolume;
|
|
timestampStart = WMTestTime;
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>eventDone</returns>
|
|
public Event Run()
|
|
{
|
|
UdatePulses();
|
|
volumeEnd = WMVolume;
|
|
timestampEnd = WMTestTime;
|
|
return Event.ReadRegisterDone;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
}
|
|
}
|
|
}
|