Files
tbf/TBF/BenchControl/Modbus/TempMeter/Groch/TempMeter.cs
T

126 lines
5.3 KiB
C#

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Config;
using TBF.BenchControl.ControlBoard.Legacy;
using TBF.BenchControl.Generic;
using TBF.Boxes;
using TBF.Resources;
namespace TBF.BenchControl.Modbus.TempMeter.Groch
{
public class TempMeter : ComponentBase, GenericDevices.ITempMeter, GenericDevices.IHasCalendarEvents, SchematicDrawing.IDrawingItCmpntWithMeasuredVal
{
private static readonly ILog log = LogManager.GetLogger(typeof(TempMeter));
public override string ToString() { return string.Format("TempMeter({0})", Cfg.ToString(1)); }
readonly TempMeterCfg tempMtrCfg;
///
int Idx0 { get { return tempMtrCfg.Idx0; } } /// 0..15
double A1 { get { return tempMtrCfg.A1; } }
double A2 { get { return tempMtrCfg.A2; } }
double A3 { get { return tempMtrCfg.A3; } }
double A4 { get { return tempMtrCfg.A4; } }
double A5 { get { return tempMtrCfg.A5; } }
double DefaultTemp { get { return tempMtrCfg != null ? Units.ConvertFrom(tempMtrCfg.MsrdUnit, tempMtrCfg.DefaultTemp) : 0; } }
double MsrdValLimLo { get { return Units.ConvertFrom(tempMtrCfg.MsrdUnit, tempMtrCfg.MsrdValLimLo); } }
double MsrdValLimHi { get { return Units.ConvertFrom(tempMtrCfg.MsrdUnit, tempMtrCfg.MsrdValLimHi); } }
public SchematicDrawing.IDrawingItem DrawingItem { get { return tempMtrCfg as SchematicDrawing.IDrawingItem; } }
readonly Common.Modbus modbus;
public double MeasuredVal { get { return ReadTemperature(); } }
public string AltString { get { return "Invalid format"; } }
public TempMeter()
{
}
public TempMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
tempMtrCfg = cfg as TempMeterCfg;
modbus = TbfComponents.FindComponent(cfg.ParentName, components) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
log.Warn(this.ToString());
}
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()
{
if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal)
{
return 0; // Config.Formulas.CorrectedValue((double)ControlBoard.Temperature(Idx0), Corrections);
}
else if (Cfg.DebugLevel == Config.Entities.DebugMode.Simulate)
{
return DefaultTemp;
}
else
{
return 0;
}
}
/// <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)
{
return new ReadTempOp(this, ref temp);
}
/// <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)
{
return new ReadTempOp(this, ref temp, tempDone);
}
}
}