/// /// 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.IRoi, 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 double PulsesPerLtr { get { return (double)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 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() { } 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(); 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.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 && NetCamera.Telnet != null) { detectedImageName = Path.Combine(Program.TempImagesDir, string.Format("{0}-det-{1}.jpg", RoiCfg.Name, StateMachine.Time)); return new RoiDetectionOp(this, NetCamera, detectedImageName, RoiX, RoiY, RoiWid, RoiHgh); } else return null; } public IOperation GrabImageOp(string imageFileName, bool blackAndWhite, bool lowResolution, 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; } 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 }, false, true, ImageRotation.None); } else return null; } /// /// Updates wmPulses, caled from 'Start' and 'Run' functions of 'ReadRegisterOp' /// void UdatePulses() { if (detected && roiHandle != 0) { cameraPulses = NetCamera.GetResult(roiHandle, out cameraTime); } } /// /// Note: Measurement operations of cameras should preceeede ReadRegister operations of ROI-s /// public IOperation ReadRegisterOp() { return this; } /// Start this operation public void Start() { UdatePulses(); Clear(); volumeStart = WMVolume; timestampStart = WMTestTime; } /// Run this operation /// eventDone public Event Run() { UdatePulses(); volumeEnd = WMVolume; timestampEnd = WMTestTime; return Event.ReadRegisterDone; } /// Stop this operation public void Stop() { } } }