- Exclude `IHasFromUNIDisablePossibility`, `SerialPortDevice`, and `SerialPortDeviceFake`. - Remove unused `SharedComponents` namespace in `PressureMeter.cs`.
272 lines
10 KiB
C#
272 lines
10 KiB
C#
using AppDiagnostic;
|
|
using Common;
|
|
using Config.Entities;
|
|
using log4net;
|
|
using SchematicDrawing;
|
|
///
|
|
/// Copyright (c) 2016-2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TBF.Boxes;
|
|
using TBF.Resources;
|
|
using TBF.Rig.Generic;
|
|
using TBF.Rig.GenericDevices;
|
|
|
|
namespace TBF.Rig.Modbus.PressureMeter.Meret
|
|
{
|
|
public class PressureMeter : ComponentBase, IPressureMeter, IDevice, ISequenceCondition, IDrawingItCmpntWithMeasuredVal
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(PressureMeter));
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
|
|
|
readonly PressureMeterCfg pressureMtrCfg;
|
|
public IDrawingItem DrawingItem { get { return pressureMtrCfg as IDrawingItem; } }
|
|
|
|
Common.Modbus modbus;
|
|
|
|
double receivedPressure;
|
|
|
|
public bool MsrmntAvailable { get { return true; } }
|
|
public double MeasuredVal { get { return receivedPressure; } }
|
|
public double MsrdValLimLo { get { return Units.ConvertFrom(MsrdUnit, pressureMtrCfg.MsrdValLimLo); } }
|
|
public double MsrdValLimHi { get { return Units.ConvertFrom(MsrdUnit, pressureMtrCfg.MsrdValLimHi); } }
|
|
public Unit MsrdUnit { get { return pressureMtrCfg.MsrdUnit; } }
|
|
public string MsrdFormat { get { return pressureMtrCfg.MsrdFormat; } }
|
|
public string AltString { get { return string.Empty; } }
|
|
|
|
int ticketNumber; /// 0 .. number of devices registered for regular polling - 1
|
|
public bool IsOffline
|
|
{
|
|
get { return pressureMtrCfg.IsOffline; }
|
|
set { pressureMtrCfg.IsOffline = value; }
|
|
}
|
|
|
|
int silentCycles = 0;
|
|
const int OfflineAfterSilentCycles = 30;
|
|
|
|
|
|
public PressureMeter() { }
|
|
|
|
public PressureMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
pressureMtrCfg = cfg as PressureMeterCfg;
|
|
CreateConditions();
|
|
}
|
|
|
|
///
|
|
/// IDevice interface
|
|
///
|
|
public override void Initialize()
|
|
{
|
|
if (DebugLevel == DebugMode.Normal)
|
|
{
|
|
modbus = TbfComponents.FindComponent(pressureMtrCfg.ParentName) as Common.Modbus;
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
modbus.ComponentNames[pressureMtrCfg.ModbusAddress] = Name;
|
|
ticketNumber = modbus.RegisterForPolling();
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
|
|
|
IsOffline = true;
|
|
silentCycles = OfflineAfterSilentCycles;
|
|
}
|
|
else
|
|
{
|
|
log.FatalFormat("{0} simulated: {1}", Name, this);
|
|
}
|
|
}
|
|
|
|
public void RunDeviceBefore()
|
|
{
|
|
if (DebugLevel != DebugMode.Normal)
|
|
return;
|
|
|
|
bool gotValidResponse = false;
|
|
|
|
if (DebugLevel == DebugMode.Normal && modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Count > 0)
|
|
{
|
|
byte[] telegram = modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Dequeue();
|
|
if (telegram.Length == 9 && telegram[1] == 4 && telegram[2] == 4)
|
|
{
|
|
/// Swap byte order
|
|
byte t1 = telegram[3];
|
|
byte t2 = telegram[4];
|
|
byte t3 = telegram[5];
|
|
byte t4 = telegram[6];
|
|
telegram[3] = t4;
|
|
telegram[4] = t3;
|
|
telegram[5] = t2;
|
|
telegram[6] = t1;
|
|
|
|
receivedPressure = Units.ConvertFrom(Unit.kPa, System.BitConverter.ToSingle(telegram, 3));
|
|
log.WarnFormat("Pressure: {0}={1}bar", Name, receivedPressure.ToString("F3"));
|
|
|
|
gotValidResponse = true;
|
|
}
|
|
else
|
|
{
|
|
string telHex = telegram == null ? "<null>" : BitConverter.ToString(telegram);
|
|
|
|
string reason;
|
|
if (telegram == null) reason = "null telegram";
|
|
else if (telegram.Length != 9) reason = $"unexpected length (expected 9, got {telegram.Length})";
|
|
else if (telegram.Length > 1 && telegram[1] != 4) reason = $"unexpected function (expected 0x04, got 0x{telegram[1]:X2})";
|
|
else if (telegram.Length > 2 && telegram[2] != 4) reason = $"unexpected bytecount (expected 0x04, got 0x{telegram[2]:X2})";
|
|
else reason = "unknown mismatch";
|
|
|
|
//LiveLogCache.Instance.AddLog(string.Format(
|
|
// ">> RunDeviceBefore() >> component: {0} bad received telegram: {1} data={2}",
|
|
// Name,
|
|
// reason,
|
|
// telHex));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
bool debugOk = (DebugLevel == DebugMode.Normal);
|
|
int addr = pressureMtrCfg.ModbusAddress;
|
|
|
|
try
|
|
{
|
|
var q = modbus.ReceivedTelegrams[addr];
|
|
int qCount = (q == null) ? -1 : q.Count;
|
|
|
|
//LiveLogCache.Instance.AddLog(string.Format(
|
|
// ">> RunDeviceBefore() >> component: {0} condition failed | DebugOk={1} | Addr={2} | QueueNull={3} | QueueCount={4} | silentCycles={5}",
|
|
// Name, debugOk, addr, (q == null), qCount, silentCycles));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LiveLogCache.Instance.AddLog(string.Format(
|
|
// ">> RunDeviceBefore() >> component: {0} condition failed | DebugOk={1} | Addr={2} | silentCycles={3} | EXCEPTION={4}",
|
|
// Name, debugOk, addr, silentCycles, ex.Message));
|
|
}
|
|
}
|
|
|
|
if (gotValidResponse)
|
|
{
|
|
silentCycles = 0;
|
|
IsOffline = false;
|
|
}
|
|
else
|
|
{
|
|
silentCycles++;
|
|
if (silentCycles >= OfflineAfterSilentCycles)
|
|
{
|
|
IsOffline = true;
|
|
//LiveLogCache.Instance.AddLog(string.Format(">> RunDeviceBefore() >> component: {0} IsOffline", Name));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RunDeviceAfter()
|
|
{
|
|
if (DebugLevel == DebugMode.Normal && modbus.IsMyTurn(ticketNumber))
|
|
{
|
|
const byte Function = 4; /// Read input registers
|
|
const ushort Address = 0; /// Pressure
|
|
modbus.SendMessage((byte)pressureMtrCfg.ModbusAddress, Function, Address, 2, Name);
|
|
}
|
|
}
|
|
|
|
public void StopDevice() { }
|
|
public void StopDevice2() { }
|
|
|
|
|
|
/// <summary>
|
|
/// Returns the water pressure
|
|
/// </summary>
|
|
/// <returns>Pressure in mBar</returns>
|
|
public double ReadPressure()
|
|
{
|
|
if (DebugLevel == DebugMode.Normal)
|
|
{
|
|
return MeasurementCorrection.CorrectedValue(receivedPressure, Corrections);
|
|
}
|
|
else if (DebugLevel == DebugMode.Simulate)
|
|
{
|
|
return 1.0;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: PressureDone, Error
|
|
/// </summary>
|
|
/// <param name="pressureBox">Reference to a variable for the pressure in Bar</param>
|
|
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadPressureOp(ref DoubleBox pressureBox)
|
|
{
|
|
return new ReadPressureOp(this, ref pressureBox);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: pressureDone, Error
|
|
/// </summary>
|
|
/// <param name="pressureBox">Reference to a variable for the pressure in Bar</param>
|
|
/// <param name="pressureDone">Event returned when measurement done</param>
|
|
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadPressureOp(ref DoubleBox pressureBox, Event pressureDone)
|
|
{
|
|
return new ReadPressureOp(this, ref pressureBox, pressureDone);
|
|
}
|
|
|
|
|
|
///
|
|
/// ISequenceCondition interface implementation (conditions in transition sequences)
|
|
///
|
|
IList<string> sequenceConditionNames;
|
|
IList<IOperation> sequenceConditions;
|
|
|
|
public int ConditionsCount { get { return sequenceConditions != null ? sequenceConditions.Count : 0; } }
|
|
|
|
/// Strings are added to the combo-box for transition sequence condition selection
|
|
public string ConditionName(int i)
|
|
{
|
|
if (sequenceConditionNames != null && i < sequenceConditionNames.Count && i >= 0)
|
|
return sequenceConditionNames[i];
|
|
else
|
|
return string.Empty;
|
|
}
|
|
|
|
/// Operations are executed as a part of a transition sequence
|
|
public IOperation ConditionOp(int i)
|
|
{
|
|
if (sequenceConditions != null && i < sequenceConditions.Count && i >= 0)
|
|
return sequenceConditions[i];
|
|
else
|
|
return null;
|
|
}
|
|
|
|
void ClearConditions()
|
|
{
|
|
sequenceConditionNames = new List<string>();
|
|
sequenceConditions = new List<IOperation>();
|
|
}
|
|
|
|
void AddCondition(string conditionName, IOperation conditionOperation)
|
|
{
|
|
sequenceConditionNames.Add(conditionName);
|
|
sequenceConditions.Add(conditionOperation);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a list of conditions
|
|
/// </summary>
|
|
void CreateConditions()
|
|
{
|
|
ClearConditions();
|
|
foreach (var pressLimit in new double[] { 2.18, 2.2, 2.22, 2.24, 2.26, 2.27 })
|
|
{
|
|
AddCondition(string.Format("{0} {1} > {2} bar", Strings.Wait_until, Name, pressLimit), new WaitUntilPressureIsOp(this, Pr.IsGT, pressLimit));
|
|
AddCondition(string.Format("{0} {1} < {2} bar", Strings.Wait_until, Name, pressLimit), new WaitUntilPressureIsOp(this, Pr.IsLT, pressLimit));
|
|
}
|
|
}
|
|
}
|
|
}
|