/// /// Copyright (c) 2016-2023 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using Config; using log4net; using TBF.Boxes; using TBF.Rig.Keithley.Multimeter_2010_RS232; using TBF.Resources; using TBF.UI.Calendar; namespace TBF.Rig.Keithley.TempMeter { public class TempMeter : ComponentBase, GenericDevices.ITempMeter, GenericDevices.IHasCalendarEvents { private static readonly ILog log = LogManager.GetLogger(typeof(TempMeter)); public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } readonly TempMeterCfg tempMtrCfg; /// configuration public readonly Multimeter Multimeter; /// parent component public int Channel { get { return tempMtrCfg.Channel; } } /// 1..5 /// Dictionaries to maintain separate operations for each FloatBox argument Dictionary operations; Dictionary operations2; public TempMeter() { } public TempMeter(Generic.IComponentCfg cfg, IList components) : base(cfg) { tempMtrCfg = cfg as TempMeterCfg; Multimeter = (Multimeter)TbfComponents.FindComponent(cfg.ParentName, components); if (Multimeter == null) throw new Exception("Cannot find " + Name + " parent"); } public override void Initialize() { operations = new Dictionary(); operations2 = new Dictionary(); log.FatalFormat("{0} initialized: {1}", Name, this); } /// /// Calendar support /// public List GetCalendarEvents() { var calEvents = new List(); DateTime calibDue = tempMtrCfg.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; } public double ReadTemperature() { double resistance = Multimeter.ReadResistance(tempMtrCfg.Channel); double temperature = 0; if (resistance > 0 && resistance < 2000) { if (tempMtrCfg.UseITS90) { temperature = Formulas.PlatinumResistanceTM_ITS90_R2T(resistance, tempMtrCfg.R001C, tempMtrCfg.a7, tempMtrCfg.b7, tempMtrCfg.c7); } else { temperature = Formulas.PlatinumResistanceTM_ITS27_R2T(resistance, tempMtrCfg.R0, tempMtrCfg.A, tempMtrCfg.B); } log.DebugFormat("Ch = {0}, R = {1} Ohm, T = {2} °C", Channel, resistance.ToString("F3"), temperature.ToString("F3")); } return temperature; } /// /// Events: TempDone, Error /// /// Reference to a variable for the temperature in Celsius /// ReadTempOp instance reference casted to IOperaton public IOperation ReadTempOp(ref DoubleBox temp) { IOperation operation; if (operations.TryGetValue(temp, out operation)) { return operation; /// Operation already available } else { /// Create a new operation and store it in the dictionary operation = new ReadTempOp(this, ref temp); operations.Add(temp, operation); return operation; } } /// /// Events: tempDone, Error /// /// Reference to a variable for the temperature in Celsius /// Event returned when measurement done /// ReadTempOp instance reference casted to IOperaton public IOperation ReadTempOp(ref DoubleBox temp, Event tempDone) { IOperation operation; if (operations2.TryGetValue(temp, out operation)) { return operation; /// Operation already available } else { /// Create a new operation and store it in the dictionary operation = new ReadTempOp(this, ref temp, tempDone); operations2.Add(temp, operation); return operation; } } } }