/// /// Copyright (c) 2013-2023 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using TBF.Rig.Generic; using TBF.Boxes; using TBF.Resources; using TBF.UI.Calendar; namespace TBF.Rig.Elde.PressureMeter { public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, GenericDevices.IHasCalendarEvents { private static readonly ILog log = LogManager.GetLogger(typeof(PressureMeter)); public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } readonly PressureMeterCfg pressureMeterCfg; public readonly ControlBoardDev ControlBoard; public readonly int Idx0; /// 0..3 public readonly byte RS485Address; /// 0..255 public PressureMeter() { } public PressureMeter(Generic.IComponentCfg cfg, IList 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 == Common.MeretProtocol.Undefined) { throw new Exception("Metret protocol not defined"); } else if (pressureMeterCfg.Protocol == Common.MeretProtocol.Modbus) { ControlBoard.MeretProtocol |= (byte)(1 << Idx0); } } public override void Initialize() { log.FatalFormat("{0} initialized: {1}", Name, this); } public List GetCalendarEvents() { var calEvents = new List(); DateTime calibDue = pressureMeterCfg.CalibValidDate; if (calibDue > TBF.UI.Constants.MinDate) { /// Five weekly notifications foreach (var days in new int[] { -35, -28, -21, -14 }) { if (calibDue.Date.AddDays(days + 6) >= DateTime.Now.Date) { calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Notification, calibDue.Date.AddDays(days), Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } /// Five daily warnings foreach (var days in new int[] { -7, -6, -5, -4, -3, -2, -1 }) { if (calibDue.Date.AddDays(days) >= DateTime.Now.Date) { /// Weekly reminders (last 5 weeks) calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Warning, calibDue.Date.AddDays(days), Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } /// Calibration due date if (calibDue.Date >= DateTime.Now.Date) { calEvents.Add(new CalibrationDueDateEvent(calibDue.Date, Name, string.Format(Strings.Calibration_due_date_is_0, calibDue.Date.ToString(TBF.UI.Constants.DateFormat)))); } } return calEvents; } /// /// Returns the water pressure /// /// Pressure in mBar public float ReadPressure() { double rawPressure = Common.Units.ConvertFrom(Common.Unit.kPa, ControlBoard.Pressure(Idx0)); double correctedPressure = Config.Entities.MeasurementCorrection.CorrectedValue(rawPressure, Corrections); return (float)correctedPressure; } /// /// Events: PressureDone, Error /// /// Reference to a variable for the pressure in Bar /// ReadPressureOp instance reference casted to IOperaton public IOperation ReadPressureOp(ref FloatBox pressure) { return new ReadPressureOp(this, ref pressure); } /// /// Events: pressureDone, Error /// /// Reference to a variable for the pressure in Bar /// Event returned when measurement done /// ReadPressureOp instance reference casted to IOperaton public IOperation ReadPressureOp(ref FloatBox pressure, Event pressureDone) { return new ReadPressureOp(this, ref pressure, pressureDone); } } }