tbf/TBF/Rig/Modbus/TempMeter/Groch/TempMeter.cs
2021-04-03 00:54:18 +02:00

204 lines
8.9 KiB
C#

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Config;
using TBF.Rig.ControlBoard.Legacy;
using TBF.Rig.Generic;
using TBF.Boxes;
using TBF.Resources;
namespace TBF.Rig.Modbus.TempMeter.Groch
{
public class TempMeter : ComponentBase, GenericDevices.ITempMeter, IDevice, 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)); }
public const int GrochAddress = 3;
readonly TempMeterCfg tempMtrCfg;
///
int Channel { get { return tempMtrCfg.Channel; } } /// 0 .. 7 (7 = TempMeterCfg.ChannelsCount-1)
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;
static int[] tempRaw = new int[TempMeterCfg.ChannelsCount];
public bool MsrmntAvailable { get { return true; } }
public double MeasuredVal { get { return ReadTemperature(); } }
public string AltString { get { return "Invalid format"; } }
int ticketNumber; /// 0 .. number of devices registered for regular polling - 1
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 override void Initialize()
{
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
if (Channel == 0)
{
ticketNumber = modbus.RegisterForPolling();
}
}
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 void RunDeviceBefore()
{
if (Channel != 0 ||
tempMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
tempMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
{
return;
}
///
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
///
if (modbus.ReceivedTelegrams[0].Count > 0)
{
byte[] telegram = modbus.ReceivedTelegrams[0].Dequeue();
if (telegram.Length == 28)
{
byte checksum = 0;
for (int i = 0; i < 27; i++) checksum += telegram[i];
if (telegram[0] == '\r' &&
telegram[1] == '\n' &&
telegram[2] == GrochAddress &&
telegram[3] == 28 &&
telegram[5] == 2 &&
telegram[26] == 3 &&
telegram[27] == checksum)
{
if (TempMeterCfg.ChannelsCount > 0) tempRaw[0] = telegram[10] + 256 * telegram[11];
if (TempMeterCfg.ChannelsCount > 1) tempRaw[1] = telegram[12] + 256 * telegram[13];
if (TempMeterCfg.ChannelsCount > 2) tempRaw[2] = telegram[14] + 256 * telegram[15];
if (TempMeterCfg.ChannelsCount > 3) tempRaw[3] = telegram[16] + 256 * telegram[17];
if (TempMeterCfg.ChannelsCount > 4) tempRaw[4] = telegram[18] + 256 * telegram[19];
if (TempMeterCfg.ChannelsCount > 5) tempRaw[5] = telegram[20] + 256 * telegram[21];
if (TempMeterCfg.ChannelsCount > 6) tempRaw[6] = telegram[22] + 256 * telegram[23];
if (TempMeterCfg.ChannelsCount > 7) tempRaw[7] = telegram[24] + 256 * telegram[25];
if (TempMeterCfg.ChannelsCount > 7)
{
log.WarnFormat("Raw temperature values: {0} {1} {2} {3} {4} {5} {6} {7}",
tempRaw[0], tempRaw[1], tempRaw[2], tempRaw[3], tempRaw[4], tempRaw[5], tempRaw[6], tempRaw[7]);
}
}
}
}
}
public void RunDeviceAfter()
{
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
if (Channel == 0 &&
tempMtrCfg.DebugLevel != Config.Entities.DebugMode.Simulate &&
tempMtrCfg.DebugLevel != Config.Entities.DebugMode.FailureDuringOperation &&
modbus.IsMyTurn(ticketNumber))
{
modbus.SendMessageGroch(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x10, 2, 1, 3, 6, 0x16 });
}
}
public void StopDevice() { }
public void StopDevice2() { }
public double ReadTemperature()
{
if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal)
{
double raw = Convert.ToDouble(tempRaw[Channel]);
double tempWOCorrection = (((A5 * raw + A4) * raw + A3) * raw + A2) * raw + A1;
return Config.Formulas.CorrectedValue(tempWOCorrection, 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);
}
}
}