/// /// 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 Config.Entities.RegisterReaderType RegisterReaderType { get { return Config.Entities.RegisterReaderType.Manual; } } public double PulsesPerLtr { get { return 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 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 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) { return GrabImageOp(imageFileName, roiCfg.BlackAndWhite, roiCfg.LowResolution, roiCfg.ImageRotation); } public IOperation GrabImageOp(string imageFileName, bool blackAndWhite, bool lowResolution, Config.Entities.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; } /// /// Events: Event.ReadRegisterDone /// /// ReadWaterMeter instance reference casted to IOperaton public IOperation ReadRegisterOp() { return this; } /// Start this operation public void Start() { ReadPulses(); } /// Run this operation /// eventDone public Event Run() { ReadPulses(); return Event.ReadRegisterDone; } /// Stop this operation public void Stop() { } } }