tbf/TBF/Rig/Elde/TempMeter/TempMeter.cs
2023-04-19 12:06:16 +02:00

141 lines
5.3 KiB
C#

///
/// Copyright (c) 2013-2023 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Common;
using TBF.Rig.Generic;
using TBF.Boxes;
using TBF.Resources;
using TBF.UI.Calendar;
namespace TBF.Rig.Elde.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;
public readonly ControlBoardDev ControlBoard;
public int Idx0 { get { return tempMtrCfg.Idx0; } } /// 0..15
public double A1 { get { return tempMtrCfg.A1; } }
public double A2 { get { return tempMtrCfg.A2; } }
public double A3 { get { return tempMtrCfg.A3; } }
public double A4 { get { return tempMtrCfg.A4; } }
public double A5 { get { return tempMtrCfg.A5; } }
public TempMeter() { }
public TempMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
tempMtrCfg = cfg as TempMeterCfg;
ControlBoard = (ControlBoardDev)TbfComponents.FindComponent(cfg.ParentName, components);
if (ControlBoard == null) throw new Exception("Cannot find " + Name + " parent");
/// Prepare data for SendCalibData() control board component method
ControlBoard.TempCalibData[Idx0, 0] = (float)A1;
ControlBoard.TempCalibData[Idx0, 1] = (float)A2;
ControlBoard.TempCalibData[Idx0, 2] = (float)A3;
ControlBoard.TempCalibData[Idx0, 3] = (float)A4;
ControlBoard.TempCalibData[Idx0, 4] = (float)A5;
}
public override void Initialize()
{
log.FatalFormat("{0} initialized: {1}", Name, this);
}
public List<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
{
var calEvents = new List<Config.CalendarEvent.ICalendarEvent>();
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()
{
if (Cfg.DebugLevel == DebugMode.Normal)
{
return Config.Entities.MeasurementCorrection.CorrectedValue((double)ControlBoard.Temperature(Idx0), Corrections);
}
else if (Cfg.DebugLevel == DebugMode.Simulate)
{
return tempMtrCfg.DefaultTemperature;
}
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);
}
}
}