tbf/TBF/BenchControl/Uni/FlowMeter/FlowMeter.cs

201 lines
8.0 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.ControlBoard.Uni;
using TBF.Boxes;
using TBF.Resources;
namespace TBF.BenchControl.Uni.FlowMeter
{
public class FlowMeter : ComponentBase, Generic.IDevice, GenericDevices.IFlowMeter, GenericDevices.IHasCalendarEvents, SchematicDrawing.IDrawingItCmpnt
{
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 SchematicDrawing.IDrawingItem DrawingItem { get { return flowMeterCfg as SchematicDrawing.IDrawingItem; } }
readonly CBoard cBoard;
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<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);
}
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<Generic.IComponent> components)
: base(cfg)
{
flowMeterCfg = cfg as FlowMeterCfg;
cBoard = TbfComponents.FindComponent(cfg.ParentName, components) as CBoard;
if (cBoard == null) throw new Exception(string.Format("Cannot find {0} (a parent of {1})", cfg.ParentName, Name));
log.Warn(this.ToString());
}
///
/// IDevice interface functions
///
public 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("Successfully initialized device {0}", Name);
}
public void RunDeviceBefore() { }
public void RunDeviceAfter() { }
public void StopDevice() { }
public void StopDevice2() { }
///
/// 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;
}
public bool IsActive
{
get
{
if ((cBoard.State & (ulong)StatusP.TestWithRefFlowmtr) == 0)
{
/// Not a test with flow meter
return false;
}
else if ((int)(cBoard.State & (ulong)StatusP.FlowmtrNrMask) == Idx1)
{
/// Flow meter is active, ID matches
return true;
}
else if ((int)(cBoard.State & (ulong)StatusP.FlowmtrNrMask) == 0)
{
/// Measurement with parallel flow meters is active
switch (Idx1)
{
case 1: return (cBoard.State & (ulong)StatusP.Flowmtr1Active) != 0;
case 2: return (cBoard.State & (ulong)StatusP.Flowmtr2Active) != 0;
case 3: return (cBoard.State & (ulong)StatusP.Flowmtr3Active) != 0;
default: return false;
}
}
else
{
return false;
}
}
}
public double ReadFrequency()
{
if ((int)(cBoard.State & (ulong)StatusP.FlowmtrNrMask) == Idx1)
{
return cBoard.RefFrequency;
}
else if (Idx1 == 1 && (cBoard.State & (ulong)StatusP.Flowmtr1Active) != 0)
{
return cBoard.Data.ReferenceFreq[1];
}
else if (Idx1 == 2 && (cBoard.State & (ulong)StatusP.Flowmtr2Active) != 0)
{
return cBoard.Data.ReferenceFreq[2];
}
else if (Idx1 == 3 && (cBoard.State & (ulong)StatusP.Flowmtr3Active) != 0)
{
return cBoard.Data.ReferenceFreq[3];
}
else
{
return 0;
}
}
public double ReadFlow()
{
return flowMeterCfg.NominalFlow * ReadFrequency() / 2000.0;
}
/// <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);
}
}
}