/// /// Copyright (c) 2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using Common; using Config.Entities; 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("{0}({1})", ClassName, 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; } } public SchematicDrawing.IDrawingItem DrawingItem { get { return tempMtrCfg as SchematicDrawing.IDrawingItem; } } Common.Modbus modbus; static int[] tempRaw = new int[TempMeterCfg.ChannelsCount]; public bool MsrmntAvailable { get { return true; } } public double MeasuredVal { get { return ReadTemperature(); } } public double MsrdValLimLo { get { return Units.ConvertFrom(MsrdUnit, tempMtrCfg.MsrdValLimLo); } } public double MsrdValLimHi { get { return Units.ConvertFrom(MsrdUnit, tempMtrCfg.MsrdValLimHi); } } public string MsrdFormat { get { return tempMtrCfg.MsrdFormat; } } public Unit MsrdUnit { get { return tempMtrCfg.MsrdUnit; } } 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 components) : base(cfg) { tempMtrCfg = cfg as TempMeterCfg; } public override void Initialize() { if (DebugLevel == DebugMode.Normal) { /// Only TempMeter with Channel == 0 communicates via the parent Modbus component if (Channel == 0) { modbus = TbfComponents.FindComponent(tempMtrCfg.ParentName) as Common.Modbus; if (modbus == null) throw new Exception("Cannot find " + Name + " parent"); modbus.ComponentNames[0] = Name; ticketNumber = modbus.RegisterForPolling(); } log.FatalFormat("{0} initialized: {1}", Name, this); } else { log.FatalFormat("{0} simulated: {1}", Name, this); } } public IList GetCalendarEvents() { DateTime calibrationDue = tempMtrCfg.CalibValidDate; IList calendarEvents = new List(); 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 (DebugLevel == DebugMode.Normal && Channel == 0) { /// 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 (DebugLevel == DebugMode.Normal && Channel == 0 && modbus.IsMyTurn(ticketNumber)) { modbus.SendMessageGroch(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x10, 2, 1, 3, 6, 0x16 }, Name); } } public void StopDevice() { } public void StopDevice2() { } public double ReadTemperature() { if (Cfg.DebugLevel == DebugMode.Normal) { double raw = Convert.ToDouble(tempRaw[Channel]); double tempWOCorrection = (((A5 * raw + A4) * raw + A3) * raw + A2) * raw + A1; return MeasurementCorrection.CorrectedValue(tempWOCorrection, Corrections); } else if (Cfg.DebugLevel == DebugMode.Simulate) { return DefaultTemp; } else { return 0; } } /// /// Events: TempDone, Error /// /// Reference to a variable for the temperature in Celsius /// ReadTempOp instance reference casted to IOperaton public IOperation ReadTempOp(ref DoubleBox temp) { return new ReadTempOp(this, ref temp); } /// /// 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) { return new ReadTempOp(this, ref temp, tempDone); } } }