Files
tbf/TBF/BenchControl/Elde/PressureMeter/PressureMeter.cs
T

124 lines
5.3 KiB
C#

///
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl.ControlBoard.Legacy;
using TBF.BenchControl.Generic;
using TBF.Boxes;
using TBF.Resources;
namespace TBF.BenchControl.Elde.PressureMeter
{
public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, GenericDevices.IHasCalendarEvents, SchematicDrawing.IDrawingItemCmpnt
{
private static readonly ILog log = LogManager.GetLogger(typeof(PressureMeter));
public override string ToString() { return string.Format("PressureMeter({0})", Cfg.ToString(1)); }
readonly PressureMeterCfg pressureMeterCfg;
public SchematicDrawing.IDrawingItemCfg DrawingItemCfg { get { return pressureMeterCfg as SchematicDrawing.IDrawingItemCfg; } }
public readonly ControlBoardDev ControlBoard;
public readonly int Idx0; /// 0..3
public readonly byte RS485Address; /// 0..255
public PressureMeter()
{
}
public PressureMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
pressureMeterCfg = cfg as PressureMeterCfg;
ControlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components);
if (ControlBoard == null) throw new Exception("Cannot find " + Name + " parent");
Idx0 = pressureMeterCfg.Idx0;
RS485Address = pressureMeterCfg.RS485Address;
/// Prepare data for SendCalibData() control board component method
ControlBoard.MeretRS485Address[Idx0] = RS485Address;
if (pressureMeterCfg.Protocol == Config.Entities.MeretProtocol.Undefined)
{
throw new Exception("Metret protocol not defined");
}
else if (pressureMeterCfg.Protocol == Config.Entities.MeretProtocol.Modbus)
{
ControlBoard.MeretProtocol |= (byte)(1 << Idx0);
}
log.Debug(this.ToString());
}
public IList<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
{
DateTime calibrationDue = pressureMeterCfg.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>
/// Returns the water pressure and convert from kPa to default units
/// </summary>
/// <returns>Pressure</returns>
public float ReadPressure()
{
double rawPressure = Config.Units.ConvertFrom(Config.Unit.kPa, ControlBoard.Pressure(Idx0));
double correctedPressure = Config.Formulas.CorrectedValue(rawPressure, Corrections);
return (float)correctedPressure;
}
/// <summary>
/// Events: PressureDone, Error
/// </summary>
/// <param name="pressure">Reference to a variable for the pressure in Bar</param>
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
public IOperation ReadPressureOp(ref FloatBox pressure)
{
return new ReadPressureOp(this, ref pressure);
}
/// <summary>
/// Events: pressureDone, Error
/// </summary>
/// <param name="pressure">Reference to a variable for the pressure in Bar</param>
/// <param name="pressureDone">Event returned when measurement done</param>
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
public IOperation ReadPressureOp(ref FloatBox pressure, Event pressureDone)
{
return new ReadPressureOp(this, ref pressure, pressureDone);
}
}
}