2018-09-14 10:13:15 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2018 Sensus Slovensko a.s.
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-09-23 09:46:40 +00:00
|
|
|
|
using Common;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
using log4net;
|
2021-10-26 09:23:57 +00:00
|
|
|
|
using TBF.Rig.Generic;
|
|
|
|
|
|
using TBF.Rig.GenericDevices;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
using TBF.Boxes;
|
2021-10-26 09:23:57 +00:00
|
|
|
|
using TBF.Rig.Hart.Common;
|
2020-05-06 07:45:23 +00:00
|
|
|
|
using TBF.Resources;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2021-10-26 09:23:57 +00:00
|
|
|
|
namespace TBF.Rig.Hart.Nivotrack
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Root component for Modbus communication via serial port (RS485)
|
|
|
|
|
|
/// </summary>
|
2020-05-06 07:45:23 +00:00
|
|
|
|
public class Nivotrack : ComponentBase, IDevice, ILevelMeter, IOperation, GenericDevices.IHasCalendarEvents
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-11-14 14:49:43 +00:00
|
|
|
|
const int StableReadingsCount = 9;
|
|
|
|
|
|
|
2018-09-14 10:13:15 +00:00
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Nivotrack));
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-11-14 14:49:43 +00:00
|
|
|
|
readonly NivotrackCfg nivotrackCfg;
|
|
|
|
|
|
readonly IHart hart;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-11-12 17:26:23 +00:00
|
|
|
|
public double Level { get { return ReadLevel(); } }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-11-14 14:49:43 +00:00
|
|
|
|
double rawLevel_mm;
|
|
|
|
|
|
bool isRawLevelValid;
|
|
|
|
|
|
|
|
|
|
|
|
/// For stable measurement calculation
|
|
|
|
|
|
double maxSDev;
|
|
|
|
|
|
double[] levelReadings;
|
|
|
|
|
|
double[] sortedLevelReadings;
|
|
|
|
|
|
int currentReadingsCount;
|
|
|
|
|
|
|
|
|
|
|
|
bool measurementCompleted; /// stableMeasurementCompleted
|
2018-10-24 14:28:56 +00:00
|
|
|
|
|
2018-11-14 14:49:43 +00:00
|
|
|
|
DoubleBox levelBox;
|
|
|
|
|
|
///
|
|
|
|
|
|
enum CurrentOp
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
None,
|
|
|
|
|
|
ReadLevel,
|
2018-11-13 12:51:42 +00:00
|
|
|
|
ReadStableLevel,
|
|
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
CurrentOp currentOp;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public Nivotrack() { }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
|
|
|
|
|
|
/// Connection settings: 19200 Bd 8-bits No-parity 1-stop-bit Flow control: none or hardware.
|
|
|
|
|
|
/// </summary>
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public Nivotrack(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
2018-09-14 10:13:15 +00:00
|
|
|
|
: base(cfg)
|
|
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
nivotrackCfg = cfg as NivotrackCfg;
|
|
|
|
|
|
|
|
|
|
|
|
hart = (IHart)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
|
|
|
|
if (hart == null) throw new Exception("Cannot find " + Name + " parent");
|
2021-04-07 07:57:42 +00:00
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
2018-11-14 14:49:43 +00:00
|
|
|
|
levelReadings = new double[StableReadingsCount];
|
|
|
|
|
|
sortedLevelReadings = new double[StableReadingsCount];
|
2021-04-07 07:57:42 +00:00
|
|
|
|
rawLevel_mm = 0;
|
|
|
|
|
|
isRawLevelValid = false;
|
|
|
|
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
|
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2020-05-06 07:45:23 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Calendar support
|
|
|
|
|
|
///
|
|
|
|
|
|
public IList<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime calibrationDue = nivotrackCfg.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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
void Detect()
|
|
|
|
|
|
{
|
|
|
|
|
|
byte bcnt = (byte)0; /// block count
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = new byte[5];
|
|
|
|
|
|
data[0] = (byte)Mark.ShortFrameMaster2Slave;
|
|
|
|
|
|
data[1] = (byte)((int)Master.Primary + (byte)nivotrackCfg.Address);
|
|
|
|
|
|
data[2] = (byte)Cmd.ReadUniqueID;
|
|
|
|
|
|
data[3] = bcnt;
|
|
|
|
|
|
data[4] = 0; /// reserved for chehcksum
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
hart.SendMessage(5, data);
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
void RequestProcessValue()
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte bcnt = (byte)0; /// block count
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = new byte[5];
|
|
|
|
|
|
data[0] = (byte)Mark.ShortFrameMaster2Slave;
|
|
|
|
|
|
data[1] = (byte)((int)Master.Primary + (byte)nivotrackCfg.Address);
|
|
|
|
|
|
data[2] = (byte)Cmd.ReadPrimaryVar;
|
|
|
|
|
|
data[3] = bcnt;
|
|
|
|
|
|
data[4] = 0; /// reserved for chehcksum
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
hart.SendMessage(5, data);
|
|
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceBefore()
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if (nivotrackCfg.DebugLevel == DebugMode.Simulate) return;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if (hart.ReceivedTelegrams[nivotrackCfg.Address].Count > 0)
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = hart.ReceivedTelegrams[nivotrackCfg.Address].Dequeue();
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if ((data.Length == 12) && (data[0] == (byte)Mark.ShortFrameSlave2Master)
|
|
|
|
|
|
&& (data[2] == (byte)Cmd.ReadPrimaryVar)
|
|
|
|
|
|
&& (data[3] == 7))
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// A frame with a status and a primary variable
|
|
|
|
|
|
///
|
|
|
|
|
|
ushort state = (ushort)(256 * data[4] + data[5]);
|
|
|
|
|
|
byte unitsID = data[6];
|
2018-11-12 17:26:23 +00:00
|
|
|
|
byte[] floatData = new byte[] { data[10], data[9], data[8], data[7] };
|
|
|
|
|
|
rawLevel_mm = System.BitConverter.ToSingle(floatData, 0);
|
2018-11-14 14:49:43 +00:00
|
|
|
|
isRawLevelValid = true;
|
|
|
|
|
|
log.InfoFormat("{0} : Received water level = {1} mm, State = {2}", Name, rawLevel_mm, state.ToString("X4"));
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
else if ((data.Length == 7) && (data[0] == (byte)Mark.ShortFrameSlave2Master)
|
|
|
|
|
|
&& (data[2] == (byte)Cmd.ReadPrimaryVar)
|
|
|
|
|
|
&& (data[3] == 2))
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// A frame with a status only
|
|
|
|
|
|
///
|
|
|
|
|
|
ushort state = (ushort)(256 * data[4] + data[5]);
|
2018-11-14 14:49:43 +00:00
|
|
|
|
isRawLevelValid = false;
|
|
|
|
|
|
log.InfoFormat("{0} : State = {1}", Name, state.ToString("X4"));
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public void RunDeviceAfter()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nivotrackCfg.DebugLevel == DebugMode.Simulate) return;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if ((StateMachine.Time % 2) == (nivotrackCfg.Address % 2))
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
RequestProcessValue();
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public void StopDevice() { }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
public void StopDevice2() { }
|
2018-10-24 14:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-12 17:26:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the most recent raw level measurement and applies correction table
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public double ReadLevel()
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
return Config.Entities.MeasurementCorrection.CorrectedValue(rawLevel_mm, Corrections);
|
2018-10-24 14:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <returns>Reference to the operation</returns>
|
|
|
|
|
|
public IOperation ReadLevelOp(ref DoubleBox levelBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
|
|
|
|
this.levelBox = levelBox;
|
|
|
|
|
|
currentOp = CurrentOp.ReadLevel;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-13 12:51:42 +00:00
|
|
|
|
/// <returns>Reference to the operation</returns>
|
2018-11-14 14:49:43 +00:00
|
|
|
|
public IOperation ReadStableLevelOp(ref DoubleBox levelBox, double maxSDev)
|
2018-11-13 12:51:42 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
|
|
|
|
this.levelBox = levelBox;
|
2018-11-14 14:49:43 +00:00
|
|
|
|
this.maxSDev = maxSDev;
|
2018-11-13 12:51:42 +00:00
|
|
|
|
currentOp = CurrentOp.ReadStableLevel;
|
2018-11-14 14:49:43 +00:00
|
|
|
|
|
2018-11-13 12:51:42 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
/// <summary>Start this operation</summary>
|
|
|
|
|
|
public void Start()
|
|
|
|
|
|
{
|
2018-11-14 14:49:43 +00:00
|
|
|
|
if (currentOp == CurrentOp.ReadLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isRawLevelValid && (levelBox != null)) levelBox.Val = ReadLevel();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (currentOp == CurrentOp.ReadStableLevel)
|
2018-11-12 17:26:23 +00:00
|
|
|
|
{
|
2018-11-14 14:49:43 +00:00
|
|
|
|
currentReadingsCount = 0;
|
|
|
|
|
|
measurementCompleted = false;
|
|
|
|
|
|
if (isRawLevelValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
levelReadings[currentReadingsCount++] = ReadLevel();
|
|
|
|
|
|
}
|
2018-11-12 17:26:23 +00:00
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this operation</summary>
|
|
|
|
|
|
/// <returns>Event.ResultsPrinted</returns>
|
|
|
|
|
|
public Event Run()
|
|
|
|
|
|
{
|
2018-11-14 14:49:43 +00:00
|
|
|
|
if (measurementCompleted)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((currentOp == CurrentOp.ReadLevel) && (levelBox != null)) levelBox.Val = ReadLevel();
|
|
|
|
|
|
return Event.LevelDone;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((currentOp == CurrentOp.ReadLevel) && isRawLevelValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (levelBox != null) levelBox.Val = ReadLevel();
|
|
|
|
|
|
measurementCompleted = true;
|
|
|
|
|
|
return Event.LevelDone;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if ((currentOp == CurrentOp.ReadStableLevel) && isRawLevelValid)
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Read new level and put it into a FIFO buffer
|
|
|
|
|
|
///
|
|
|
|
|
|
if (currentReadingsCount < StableReadingsCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
levelReadings[currentReadingsCount++] = ReadLevel();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/// Shift data in the (already full) buffer, save the mass into the last buffer item
|
|
|
|
|
|
for (int i = 1; i < StableReadingsCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
levelReadings[i - 1] = levelReadings[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
levelReadings[StableReadingsCount - 1] = ReadLevel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Evaluate reading stability
|
|
|
|
|
|
///
|
|
|
|
|
|
if (currentReadingsCount >= StableReadingsCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
Array.Copy(levelReadings, sortedLevelReadings, StableReadingsCount);
|
|
|
|
|
|
Array.Sort(sortedLevelReadings);
|
|
|
|
|
|
|
|
|
|
|
|
if (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
/// Spread of values (except of the largest and the smalles value) must be <= maxSDev
|
|
|
|
|
|
|
|
|
|
|
|
if ((sortedLevelReadings[StableReadingsCount - 2] - sortedLevelReadings[1]) <= maxSDev)
|
|
|
|
|
|
{
|
|
|
|
|
|
double massSum = 0;
|
|
|
|
|
|
for (int i = 1; i < StableReadingsCount - 1; i++) massSum += sortedLevelReadings[i];
|
|
|
|
|
|
double ave = massSum / (StableReadingsCount - 2);
|
|
|
|
|
|
if (levelBox != null) levelBox.Val = ave;
|
|
|
|
|
|
measurementCompleted = true;
|
|
|
|
|
|
log.InfoFormat("ReadStableLevelOp.Run() ... valid level={0} ... returning LevelDone", ave);
|
|
|
|
|
|
return Event.LevelDone;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
/// Standard deviation (except of the largest and the smalles value) must be <= maxSDev
|
|
|
|
|
|
double massSum = 0;
|
|
|
|
|
|
for (int i = 1; i < StableReadingsCount - 1; i++) massSum += sortedLevelReadings[i];
|
|
|
|
|
|
double ave = massSum / (StableReadingsCount - 2);
|
|
|
|
|
|
double sdev = 0;
|
|
|
|
|
|
for (int i = 1; i < StableReadingsCount - 1; i++) sdev += ((sortedLevelReadings[i] - ave) * (sortedLevelReadings[i] - ave));
|
|
|
|
|
|
|
|
|
|
|
|
if (Math.Sqrt(sdev / (StableReadingsCount - 2)) <= maxSDev)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (levelBox != null) levelBox.Val = ave;
|
|
|
|
|
|
measurementCompleted = true;
|
|
|
|
|
|
log.InfoFormat("ReadStableMassOp.Run() ... valid mass={0} ... returning Event.BalanceDone", ave);
|
|
|
|
|
|
return Event.LevelDone;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Event.Busy;
|
|
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
currentOp = CurrentOp.None;
|
|
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|