/// /// Copyright (c) 2013-2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using TBF.Boxes; using TBF.Resources; namespace TBF.BenchControl.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("FlowMeter({0})", 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) { /// Apply correction var rangeCorrections = new List(); foreach (var corr in Corrections) { if (corr.RangeIx == rangeIx) rangeCorrections.Add(corr); } double correctedFlow = Config.Formulas.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; } log.Debug(this.ToString()); } /// /// IDevice interface functions /// public void Initialize() { if (flowMeterCfg.CalibValidDate != DateTime.MinValue && flowMeterCfg.CalibValidDate.Date < DateTime.Now.Date) { throw new Exception(string.Format("{0}: {1}", Name, Strings.Calibration_certificate_validity_expired)); } log.FatalFormat("Successfully initialized device {0}", Name); } public void RunDeviceBefore() { } public void RunDeviceAfter() { } public void StopDevice() { } public void StopDevice2() { } /// /// Calendar support /// public IList GetCalendarEvents() { DateTime calibrationDue = flowMeterCfg.CalibValidDate; IList calendarEvents = new List(); /// Calibration due date calendar event calendarEvents.Add(new TBF.UI.Calendar.CalibrationDueDateEvent(calibrationDue.Date, Name, string.Format(Strings.Calibration_due_date_is_0, calibrationDue.Date.ToString("dd.MM.yyyy")))); if ((calibrationDue != DateTime.MinValue) && (DateTime.Now.Date <= calibrationDue.AddDays(-7))) { /// Weekly reminders (last 5 weeks) calendarEvents.Add(new TBF.UI.Calendar.CalibrationReminderEvent(calibrationDue.Date.AddDays(-35), Name, string.Format(Strings.Calibration_due_date_is_0, calibrationDue.Date.ToString("dd.MM.yyyy")), false)); } if ((calibrationDue != DateTime.MinValue) && (DateTime.Now.Date <= calibrationDue.Date)) { /// Daily reminders (last 5 days) calendarEvents.Add(new TBF.UI.Calendar.CalibrationReminderEvent(calibrationDue.AddDays(-5), Name, string.Format(Strings.Calibration_due_date_is_0, calibrationDue.Date.ToString("dd.MM.yyyy")), true)); } return calendarEvents; } 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); } } }