231 lines
10 KiB
C#
231 lines
10 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using Common;
|
|
using Config.Entities;
|
|
using SchematicDrawing;
|
|
using TBF.Rig.ControlBoard.Uni;
|
|
using TBF.Boxes;
|
|
using TBF.Resources;
|
|
|
|
|
|
namespace TBF.Rig.Uni.FlowMeter
|
|
{
|
|
public class FlowMeter : ComponentBase, GenericDevices.IFlowMeterSingle, GenericDevices.IHasCalendarEvents, IDrawingItCmpntWithMeasuredVal
|
|
{
|
|
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;
|
|
|
|
public int Idx1 { get { return (flowMeterCfg != null) ? flowMeterCfg.Idx1 : 0; } }
|
|
public double NominalFlow { get { return (flowMeterCfg != null) ? flowMeterCfg.NominalFlow : 0; } }
|
|
public double NominalFreq { get { return (flowMeterCfg != null) ? flowMeterCfg.NominalFreq : 2000; } }
|
|
public double LtrPerPulse { get { return (flowMeterCfg != null) ? flowMeterCfg.NominalFlow / (3.6 * NominalFreq) : 0; } }
|
|
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 string MsrdFormat { get { return flowMeterCfg.MsrdFormat; } }
|
|
public Unit MsrdUnit { get { return flowMeterCfg.MsrdUnit; } }
|
|
public double MsrdValLimLo { get { return flowMeterCfg.MsrdValLimLo; } set { flowMeterCfg.MsrdValLimLo = value; } } /// in [m3/h]
|
|
public double MsrdValLimHi { get { return flowMeterCfg.MsrdValLimHi; } set { flowMeterCfg.MsrdValLimHi = value; } } /// in [m3/h]
|
|
|
|
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() / NominalFreq; } }
|
|
public string AltString { get { return string.Format("Calibration expires on {0:dd.MM.yyyy}", flowMeterCfg.CalibValidDate); } }
|
|
///
|
|
public double ReadFlow() { return NominalFlow * ReadFrequency() / NominalFreq; }
|
|
///
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Determine flow meter range
|
|
/// </summary>
|
|
/// <param name="tempRngLo">Lower boundary of temperature</param>
|
|
/// <param name="tempRngHi">Upper boundary of temperature</param>
|
|
/// <returns>flow meter range (0..5) or -1 if no range fits the specified temperature range</returns>
|
|
public int GetRange(float tempRngLo, float tempRngHi)
|
|
{
|
|
for (int rng = 0; rng <= 5; rng++)
|
|
{
|
|
if (RangeEnabled(rng) && GetTempLo(rng) <= tempRngLo && tempRngHi <= GetTempHi(rng))
|
|
{
|
|
return rng;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public double LtrPerPulseCorrected(double flow, float temperature)
|
|
{
|
|
if (flow == 0) return LtrPerPulse;
|
|
|
|
/// Apply correction
|
|
int rng = GetRange(temperature, temperature);
|
|
double correctedFlow = (measurementCorrections == null || rng < 0) ? flow : CorrectedFlow(flow, rng);
|
|
return (LtrPerPulse * correctedFlow / flow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Apply flow measurement correction
|
|
/// </summary>
|
|
/// <param name="flow">Measured flow in [m3/h]</param>
|
|
/// <param name="rng">Tange 0..5</param>
|
|
/// <returns>Corrected flow in [m3/h]</returns>
|
|
public double CorrectedFlow(double flow, int rng)
|
|
{
|
|
return MeasurementCorrection.CorrectedValue(flow, measurementCorrections[rng]);
|
|
}
|
|
|
|
|
|
readonly UniCB uniCB;
|
|
IList<MeasurementCorrection>[] measurementCorrections;
|
|
|
|
|
|
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));
|
|
|
|
measurementCorrections = new IList<MeasurementCorrection>[6];
|
|
for (int i = 0; i < measurementCorrections.Length; i++) measurementCorrections[i] = new List<MeasurementCorrection>();
|
|
foreach (var corr in Corrections)
|
|
{
|
|
if (corr.RangeIx >= 0 && corr.RangeIx < measurementCorrections.Length) measurementCorrections[corr.RangeIx].Add(corr);
|
|
}
|
|
|
|
log.Warn(this.ToString());
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
MsrdValLimLo = 0;
|
|
MsrdValLimHi = NominalFlow;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|