183 lines
8.0 KiB
C#
183 lines
8.0 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using SchematicDrawing;
|
|
using TBF.Rig.ControlBoard.Uni;
|
|
using TBF.Boxes;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.Rig.Uni.FlowMeter
|
|
{
|
|
public class FlowMeter : ComponentBase, GenericDevices.IFlowMeter, GenericDevices.IHasCalendarEvents, IDrawingItCmpntWithMeasuredVal
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(FlowMeter));
|
|
public override string ToString() { return string.Format("FlowMeter({0})", Cfg.ToString(-1)); }
|
|
|
|
readonly FlowMeterCfg flowMeterCfg;
|
|
public int Idx1 { get { return (flowMeterCfg != null) ? flowMeterCfg.Idx1 : 0; } }
|
|
public double NominalFlow { get { return (flowMeterCfg != null) ? flowMeterCfg.NominalFlow : 0; } }
|
|
public double LtrPerPulse { get { return (flowMeterCfg != null) ? flowMeterCfg.NominalFlow / 7200.0 : 0; } } /// Nominal frequency is 2000 Hz
|
|
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 SchematicDrawing.IDrawingItem DrawingItem { get { return flowMeterCfg as SchematicDrawing.IDrawingItem; } }
|
|
|
|
public bool MsrmntAvailable
|
|
{
|
|
get {
|
|
if ((uniCB.State & (ulong)StatusP.TestWithRefFlowmtr) == 0) return false; /// Not a test with flow meter
|
|
|
|
if (uniCB.FlowmeterId == Idx1) return true; /// Flow meter is active, ID matches
|
|
|
|
if (uniCB.FlowmeterId == 0)
|
|
{
|
|
/// Measurement with parallel flow meters is active
|
|
switch (Idx1)
|
|
{
|
|
case 1: return (uniCB.State & (ulong)StatusP.Flowmtr1Active) != 0;
|
|
case 2: return (uniCB.State & (ulong)StatusP.Flowmtr2Active) != 0;
|
|
case 3: return (uniCB.State & (ulong)StatusP.Flowmtr3Active) != 0;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
public double MeasuredVal { get { return NominalFlow * ReadFrequency() / 2000.0; } }
|
|
public string AltString { get { return string.Format("Calibration expires on {0:dd.MM.yyyy}", flowMeterCfg.CalibValidDate); } }
|
|
///
|
|
public double ReadFlow() { return NominalFlow * ReadFrequency() / 2000.0; }
|
|
///
|
|
public double ReadFrequency()
|
|
{
|
|
if (uniCB.FlowmeterId == Idx1) return uniCB.RefFrequency;
|
|
|
|
if (uniCB.FlowmeterId == 0)
|
|
{
|
|
switch (Idx1)
|
|
{
|
|
case 1: return (uniCB.State & (ulong)StatusP.Flowmtr1Active) != 0 ? uniCB.Data.ReferenceFreq[1] : 0;
|
|
case 2: return (uniCB.State & (ulong)StatusP.Flowmtr2Active) != 0 ? uniCB.Data.ReferenceFreq[2] : 0;
|
|
case 3: return (uniCB.State & (ulong)StatusP.Flowmtr3Active) != 0 ? uniCB.Data.ReferenceFreq[3] : 0;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
public double LtrPerPulseCorrected(double flow, int rangeIx)
|
|
{
|
|
/// Apply correction
|
|
var rangeCorrections = new List<Config.Entities.MeasurementCorrection>();
|
|
foreach (var corr in Corrections)
|
|
{
|
|
if (corr.RangeIx == rangeIx) rangeCorrections.Add(corr);
|
|
}
|
|
double correctedFlow = Config.Formulas.CorrectedValue(flow, rangeCorrections);
|
|
return (LtrPerPulse * correctedFlow / flow);
|
|
}
|
|
|
|
|
|
readonly UniCB uniCB;
|
|
|
|
|
|
public FlowMeter() { }
|
|
|
|
public FlowMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
flowMeterCfg = cfg as FlowMeterCfg;
|
|
|
|
uniCB = TbfComponents.FindComponent(cfg.ParentName, components) as UniCB;
|
|
if (uniCB == null) throw new Exception(string.Format("Cannot find {0} (a parent of {1})", cfg.ParentName, Name));
|
|
|
|
log.Warn(this.ToString());
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
for (int r = 0; r <= 5; r++)
|
|
{
|
|
if (flowMeterCfg.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));
|
|
}
|
|
}
|
|
|
|
TBF.UiBridge.Bridge.MeasurementRequestHandler += delegate(object sender, TBF.UiBridge.MeasurementRequestArgs args)
|
|
{
|
|
if (args.Name == Name) { uniCB.MeasureFlow(true, Idx1); } /// true = command is comming form the UI
|
|
};
|
|
|
|
log.FatalFormat("Successfully initialized device {0}", Name);
|
|
}
|
|
|
|
///
|
|
/// Calendar support
|
|
///
|
|
public IList<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
|
|
{
|
|
DateTime calibrationDue = flowMeterCfg.CalibValidDate;
|
|
IList<Config.CalendarEvent.ICalendarEvent> calendarEvents = new List<Config.CalendarEvent.ICalendarEvent>();
|
|
|
|
if (calibrationDue > TBF.UI.Constants.MinDate)
|
|
{
|
|
/// 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(TBF.UI.Constants.DateFormat))));
|
|
if (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(TBF.UI.Constants.DateFormat)),
|
|
false));
|
|
}
|
|
if (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(TBF.UI.Constants.DateFormat)),
|
|
true));
|
|
}
|
|
}
|
|
return calendarEvents;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Events: FlowDone, Error
|
|
/// </summary>
|
|
/// <param name="flow">Reference to a variable for the flow in Bar</param>
|
|
/// <returns>ReadFlowOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadFlowOp(ref DoubleBox flow)
|
|
{
|
|
return new ReadFlowOp(this, ref flow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: flowDone, Error
|
|
/// </summary>
|
|
/// <param name="flow">Reference to a variable for the flow in Bar</param>
|
|
/// <param name="flowDone">Event returned when measurement done</param>
|
|
/// <returns>ReadFlowOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadFlowOp(ref DoubleBox flow, Event flowDone)
|
|
{
|
|
return new ReadFlowOp(this, ref flow, flowDone);
|
|
}
|
|
}
|
|
}
|