/// /// 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 /// public 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); ; } } int wmPulses; public int WMPulses { get { return wmPulses; } } 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 /// /// 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 DetectedImageName { get { return detectedImageName; } } string detectedImageName; /// private int roiHandle; /// public void RegisterRoiToCamera() { roiHandle = Camera.RegisterRoi(string.Format("-roi 1 {0} {1} {2} bkgnd.bmp", RoiX.Val, RoiY.Val, RoiCfg.RoiParams)); } public Roi() { Clear(); } public Roi(Generic.IComponentCfg cfg, IList 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 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; RoiCfg.DetectionImagesCount = tmpcfg.DetectionImagesCount; RoiCfg.LoadImages = tmpcfg.LoadImages; RoiCfg.SaveImages = tmpcfg.SaveImages; RoiCfg.DetectionPeriod1 = tmpcfg.DetectionPeriod1; RoiCfg.DetectionPeriod2 = tmpcfg.DetectionPeriod2; } } }; } #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; detectedImageName = string.Format("{0}{1}-det-{2}.jpg", Program.ImagesDir, RoiCfg.Name, StateMachine.Time); return new RoiDetectionOp(this, RoiX, RoiY, RoiWid, RoiHgh); } else { return null; } } /// /// Updates wmPulses, caled from 'Start' and 'Run' functions of 'ReadRegisterOp' /// void UdatePulses() { if (detected && roiHandle != 0) { wmPulses = NetCamera.GetResult(roiHandle); } } /// /// Note: Measurement operations of cameras should preceeede ReadRegister operations of ROI-s /// public IOperation ReadRegisterOp() { return this; } /// Start this operation public void Start() { UdatePulses(); } /// Run this operation /// eventDone public Event Run() { UdatePulses(); return Event.ReadRegisterDone; } /// Stop this operation public void Stop() { } } }