tbf/TBF/Rig/Keithley/TempMeter/TempMeter.cs

203 lines
8.0 KiB
C#

using Common;
using log4net;
using SchematicDrawing;
///
/// Copyright (c) 2016-2020 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using TBF.Boxes;
using TBF.Resources;
using TBF.Rig.Keithley.Multimeter_2010_RS232;
namespace TBF.Rig.Keithley.TempMeter
{
public class TempMeter : ComponentBase, GenericDevices.ITempMeter, GenericDevices.IHasCalendarEvents, IDrawingItCmpntWithMeasuredVal
{
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 SchematicDrawing.IDrawingItem DrawingItem { get { return tempMtrCfg as SchematicDrawing.IDrawingItem; } }
public string MsrdFormat { get { return tempMtrCfg.MsrdFormat; } }
public Unit MsrdUnit { get { return tempMtrCfg.MsrdUnit; } }
public double MsrdValLimLo { get { return Units.ConvertFrom(MsrdUnit, tempMtrCfg.MsrdValLimLo); } }
public double MsrdValLimHi { get { return Units.ConvertFrom(MsrdUnit, tempMtrCfg.MsrdValLimHi); } }
public bool MsrmntAvailable { get { return true; } }
public double MeasuredVal { get { return ReadTemperature(); } }
public string AltString { get { return string.Empty; } }
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<DoubleBox, IOperation> operations;
Dictionary<DoubleBox, IOperation> operations2;
public bool IsOffline
{
get { return tempMtrCfg.IsOffline; }
set { tempMtrCfg.IsOffline = value; }
}
int invalidStreak = 0;
const int OfflineAfterInvalid = 5;
public TempMeter() { }
public TempMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
tempMtrCfg = cfg as TempMeterCfg;
Multimeter = (Multimeter)TbfComponents.FindComponent(cfg.ParentName, components);
if (Multimeter == null) throw new Exception("Cannot find " + Name + " parent");
log.Warn(this.ToString());
}
public override void Initialize()
{
operations = new Dictionary<DoubleBox, IOperation>();
operations2 = new Dictionary<DoubleBox, IOperation>();
}
///
/// Calendar support
///
public IList<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
{
DateTime calibrationDue = tempMtrCfg.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 double ReadTemperature()
{
// In non-Normal modes do not perform offline detection here
if (Cfg.DebugLevel != DebugMode.Normal)
{
if (Cfg.DebugLevel == DebugMode.Simulate)
return 20.0;
return 0;
}
double resistance;
try
{
resistance = Multimeter.ReadResistance(tempMtrCfg.Channel);
}
catch (Exception ex)
{
invalidStreak++;
if (invalidStreak >= OfflineAfterInvalid)
IsOffline = true;
log.WarnFormat("Keithley TempMeter {0} ch={1}: ReadResistance failed ({2}/{3}): {4}",
Name, Channel, invalidStreak, OfflineAfterInvalid, ex.Message);
return 0; // or keep last value, or double.NaN
}
// Valid resistance => online
invalidStreak = 0;
IsOffline = false;
double temperature;
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;
}
/// <summary>
/// Events: TempDone, Error
/// </summary>
/// <param name="temp">Reference to a variable for the temperature in Celsius</param>
/// <returns>ReadTempOp instance reference casted to IOperaton</returns>
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;
}
}
/// <summary>
/// Events: tempDone, Error
/// </summary>
/// <param name="temp">Reference to a variable for the temperature in Celsius</param>
/// <param name="tempDone">Event returned when measurement done</param>
/// <returns>ReadTempOp instance reference casted to IOperaton</returns>
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;
}
}
}
}