180 lines
4.4 KiB
C#
180 lines
4.4 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.RoiForFixedStart
|
|
{
|
|
public class Roi : ComponentBase, GenericDevices.IRoiForFixedStart, 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
|
|
///
|
|
readonly RoiCfg roiCfg;
|
|
|
|
///
|
|
/// Camera and ICamera
|
|
///
|
|
public readonly CLP1611.Camera NetCamera;
|
|
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); } }
|
|
|
|
|
|
double beginWMState;
|
|
public double BeginWMState
|
|
{
|
|
get { return beginWMState; }
|
|
set { beginWMState = value; }
|
|
}
|
|
|
|
double endWMState;
|
|
public double EndWMState
|
|
{
|
|
get { return endWMState; }
|
|
set { endWMState = value; }
|
|
}
|
|
|
|
public double WMVolume { get { return endWMState - beginWMState; } }
|
|
|
|
public int WMPulses { get { return (int)(WMVolume / LtrsPerPulse); } }
|
|
|
|
int wmRefPulses;
|
|
public int WMRefPulses { get { return wmRefPulses; } }
|
|
|
|
|
|
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;
|
|
|
|
Clear();
|
|
|
|
StartChangeHandler();
|
|
|
|
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.BlackAndWhite = tmpcfg.BlackAndWhite;
|
|
roiCfg.LowResolution = tmpcfg.LowResolution;
|
|
roiCfg.ImageRotation = tmpcfg.ImageRotation;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#endregion Configuration Change Handling
|
|
|
|
public void Clear()
|
|
{
|
|
log.DebugFormat("{0}:Clear()", Name);
|
|
beginWMState = 0;
|
|
endWMState = 0;
|
|
wmRefPulses = 0;
|
|
}
|
|
|
|
public void TestCompleted()
|
|
{
|
|
log.DebugFormat("{0}:TestCompleted()", Name);
|
|
ReadPulses();
|
|
}
|
|
|
|
private void ReadPulses()
|
|
{
|
|
wmRefPulses = (int)StateMachine.ControlBoard.EtPulses(0);
|
|
}
|
|
|
|
|
|
public IOperation GrabImageOp(string imageFileName)
|
|
{
|
|
if (NetCamera != null && NetCamera.Telnet != null)
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
return NetCamera.GrabImagesOp(new string[] { imageFileName }, roiCfg.BlackAndWhite, roiCfg.LowResolution, roiCfg.ImageRotation);
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public IOperation GrabImageOp(string imageFileName, bool blackAndWhite,
|
|
bool lowResolution, Config.Entities.ImageRotation imageRotation)
|
|
{
|
|
if (NetCamera != null && NetCamera.Telnet != null)
|
|
{
|
|
/// TODO: Implement support for sharing one camera by multiple ROI-s
|
|
return NetCamera.GrabImagesOp(new string[] { imageFileName }, blackAndWhite, lowResolution, imageRotation);
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Events: Event.ReadRegisterDone
|
|
/// </summary>
|
|
/// <returns>ReadWaterMeter instance reference casted to IOperaton</returns>
|
|
public IOperation ReadRegisterOp()
|
|
{
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
ReadPulses();
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>eventDone</returns>
|
|
public Event Run()
|
|
{
|
|
ReadPulses();
|
|
return Event.ReadRegisterDone;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
}
|
|
}
|
|
}
|