/// /// Copyright (c) 2013-2023 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using Config.Entities; using TBF.Boxes; using TBF.Resources; using TBF.UI.Calendar; namespace TBF.Rig.Elde.FlowMeter { public class FlowMeter : ComponentBase, Generic.IDevice, GenericDevices.IFlowMeter, GenericDevices.IHasCalendarEvents { private static readonly ILog log = LogManager.GetLogger(typeof(FlowMeter)); public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } readonly FlowMeterCfg flowMeterCfg; readonly ControlBoardDev controlBoard; public int Idx1 { get { return flowMeterCfg.Idx1; } } public double NominalFlow { get { return flowMeterCfg.NominalFlow; } } public double LtrPerPulse { get { return flowMeterCfg.NominalFlow / 7200.0; } } /// Nominal frequency is 2000 Hz public double LtrPerPulseCorrected(double flow, int rangeIx) { if (flow == 0) return LtrPerPulse; /// To avoid division by zero /// Apply correction var rangeCorrections = new List(); foreach (var corr in Corrections) { if (corr.RangeIx == rangeIx) rangeCorrections.Add(corr); } double correctedFlow = MeasurementCorrection.CorrectedValue(flow, rangeCorrections); return (LtrPerPulse * correctedFlow / flow); } public bool RangeEnabled(int ix) { return flowMeterCfg.RangeEnabled(ix); } public double GetTempLo(int ix) { return flowMeterCfg.GetTempLo(ix); } public double GetTempHi(int ix) { return flowMeterCfg.GetTempHi(ix); } public double GetPressLo(int ix) { return flowMeterCfg.GetPressLo(ix); } public double GetPressHi(int ix) { return flowMeterCfg.GetPressHi(ix); } public FlowMeter() { } public FlowMeter(Generic.IComponentCfg cfg, IList components) : base(cfg) { flowMeterCfg = cfg as FlowMeterCfg; controlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components); if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent"); /// Prepare data for SendCalibData() control board component method if (Idx1 < controlBoard.EtCalib.Length) { controlBoard.EtCalib[Idx1] = (float)flowMeterCfg.NominalFlow; } } /// /// IDevice interface functions /// public override void Initialize() { for (int r = 0; r <= 5; r++) { if (RangeEnabled(r) && flowMeterCfg.GetCalibValidDate(r) > DateTime.MinValue && flowMeterCfg.GetCalibValidDate(r).Date < DateTime.Now.Date) { throw new Exception(string.Format("{0}/r{1}: {2}", Name, r, Strings.Calibration_certificate_validity_expired)); } } log.FatalFormat("{0} initialized: {1}", Name, this); } public void RunDeviceBefore() { } public void RunDeviceAfter() { } public void StopDevice() { } public void StopDevice2() { } /// /// Calendar support /// public List GetCalendarEvents() { var calEvents = new List(); DateTime calibDue = flowMeterCfg.CalibValidDate; if (calibDue > TBF.UI.Constants.MinDate) { /// Five weekly notifications foreach (var days in new int[] { -35, -28, -21, -14 }) { if (calibDue.Date.AddDays(days + 6) >= DateTime.Now.Date) { calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Notification, calibDue.Date.AddDays(days), Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } /// Five daily warnings foreach (var days in new int[] { -7, -6, -5, -4, -3, -2, -1 }) { if (calibDue.Date.AddDays(days) >= DateTime.Now.Date) { /// Weekly reminders (last 5 weeks) calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Warning, calibDue.Date.AddDays(days), Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } /// Calibration due date if (calibDue.Date >= DateTime.Now.Date) { calEvents.Add(new CalibrationDueDateEvent(calibDue.Date, Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } return calEvents; } public double ReadFlow() { return controlBoard.ReferenceFlow; } public double ReadFrequency() { return controlBoard.ReferenceFreq; } /// /// Events: FlowDone, Error /// /// Reference to a variable for the flow in Bar /// ReadFlowOp instance reference casted to IOperaton public IOperation ReadFlowOp(ref DoubleBox flow) { return new ReadFlowOp(this, controlBoard, ref flow); } /// /// Events: flowDone, Error /// /// Reference to a variable for the flow in Bar /// Event returned when measurement done /// ReadFlowOp instance reference casted to IOperaton public IOperation ReadFlowOp(ref DoubleBox flow, Event flowDone) { return new ReadFlowOp(this, controlBoard, ref flow, flowDone); } } }