/// /// Copyright (c) 2017-2021 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Text; using log4net; using Common; using Config.Entities; using NHibernate.Util; using TBF.Rig.Generic; using TBF.Rig.GenericDevices; namespace TBF.Rig.Network.Camera.RoiForFixedStartKeyence { public class Roi : ComponentBase, GenericDevices.IRegReaderStillCamera { /// 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})", ClassName, Cfg.ToString(-1)); } readonly RoiCfg roiCfg; /// /// Camera and ICamera /// public readonly KeyenceIV3G120.Camera FtpCamera; public GenericDevices.ICamera Camera { get { return FtpCamera 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 RegisterReaderType RegisterReaderType { get { return RegisterReaderType.Manual; } } public double PulsesPerLtr { get { return roiCfg.ProcParams.PulsesPerLtr; } set { roiCfg.ProcParams.PulsesPerLtr = value; } } public double LtrsPerPulse { get { return (PulsesPerLtr <= float.Epsilon) ? 1.0 : (1 / PulsesPerLtr); } } public string QuantityUnits { get; set; } 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); } } public int WMRefPulses { get { return StateMachine.ControlBoardMain.RefPulses; ; } } public Roi() { } public Roi(Generic.IComponentCfg cfg, IList components) : base(cfg) { roiCfg = cfg as RoiCfg; FtpCamera = TbfComponents.FindComponent(cfg.ParentName, components) as KeyenceIV3G120.Camera; log.Warn(this.ToString()); } public override void Initialize() { Clear(); } #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.TriggerName = tmpcfg.TriggerName; } } }; } #endregion Configuration Change Handling public void Clear() { log.DebugFormat("{0}:Clear()", Name); beginWMState = 0; endWMState = 0; } public static IValve Valve(string name) { /// Load the list of components from the database var session = TBF.DB.CreateSession(DBKind.Config); var cmptnEntities = session.QueryOver().OrderBy(x => x.ItemNr).Asc.List(); IList TbfComponents = Rig.TbfComponents.LoadComponentsFromDB(cmptnEntities); IList valves = Rig.BuiltIn.ValveBase.AllValves(TbfComponents); if (valves.Count == 0) { return null; } foreach (var valve in valves) { if (valve.Name == name) { return valve; } } return valves[0]; } public IOperation GrabImageOp(string imageFileName) { return GrabImageOp(imageFileName, true, true, ImageRotation.None); } public IOperation GrabImageOp(string imageFileName, bool blackAndWhite, bool lowResolution, ImageRotation imageRotation) { if (FtpCamera != null) { /// TODO: Implement support for sharing one camera by multiple ROI-s return FtpCamera.GrabImagesOp(new string[] { imageFileName }, blackAndWhite, lowResolution, imageRotation); } else return null; } public IValve GetValve() { if (roiCfg?.TriggerName != null) { IValve valve = Valve(roiCfg.TriggerName); if (valve != null) { return valve; } } return null; } } }