236 lines
8.6 KiB
C#
236 lines
8.6 KiB
C#
///
|
|
/// Copyright (c) 2016-2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using Common;
|
|
using Config.Entities;
|
|
using SchematicDrawing;
|
|
using TBF.Rig.Generic;
|
|
using TBF.Boxes;
|
|
using TBF.Resources;
|
|
using TBF.Rig.GenericDevices;
|
|
|
|
namespace TBF.Rig.Modbus.Meret.AdjustableScale
|
|
{
|
|
public class AdjustableMeter : ComponentBase, IAdjustableMeter, IDevice, ISequenceCondition, IDrawingItCmpntWithMeasuredVal
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(AdjustableMeter));
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
|
|
|
readonly AdjustableMeterCfg _adjustableMtrCfg;
|
|
public IDrawingItem DrawingItem { get { return _adjustableMtrCfg as IDrawingItem; } }
|
|
|
|
Common.Modbus modbus;
|
|
|
|
double receivedValue;
|
|
double receivedAmpers;
|
|
|
|
public bool MsrmntAvailable { get { return true; } }
|
|
public double MeasuredVal { get { return receivedValue; } }
|
|
public double MsrdValLimLo { get { return Units.ConvertFrom(MsrdUnit, _adjustableMtrCfg.MsrdValLimLo); } }
|
|
public double MsrdValLimHi { get { return Units.ConvertFrom(MsrdUnit, _adjustableMtrCfg.MsrdValLimHi); } }
|
|
public Unit MsrdUnit { get { return _adjustableMtrCfg.MsrdUnit; } }
|
|
public string MsrdFormat { get { return _adjustableMtrCfg.MsrdFormat; } }
|
|
public string AltString { get { return string.Empty; } }
|
|
|
|
int ticketNumber; /// 0 .. number of devices registered for regular polling - 1
|
|
|
|
public double CorrectionValLo { get { return Units.ConvertFrom(MsrdUnit, _adjustableMtrCfg.CorrectionValLo); } }
|
|
public double CorrectionValHi { get { return Units.ConvertFrom(MsrdUnit, _adjustableMtrCfg.CorrectionValHi); } }
|
|
|
|
IList<MeasurementCorrection> CorrectionsLocal;
|
|
|
|
|
|
public AdjustableMeter() { }
|
|
|
|
public AdjustableMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
_adjustableMtrCfg = cfg as AdjustableMeterCfg;
|
|
CreateConditions();
|
|
}
|
|
|
|
///
|
|
/// IDevice interface
|
|
///
|
|
public override void Initialize()
|
|
{
|
|
if (DebugLevel == DebugMode.Normal)
|
|
{
|
|
InitModbus();
|
|
|
|
if (CorrectionValLo != 0 || CorrectionValHi != 0 ) {
|
|
CorrectionsLocal = new List<MeasurementCorrection>();
|
|
MeasurementCorrection Lo, Hi;
|
|
Lo = new MeasurementCorrection(1);
|
|
Lo.Measurement = CorrectionValLo;
|
|
Lo.Correction = MsrdValLimLo;
|
|
CorrectionsLocal.Add(Lo);
|
|
Hi = new MeasurementCorrection(2);
|
|
Hi.Measurement = CorrectionValHi;
|
|
Hi.Correction = MsrdValLimHi;
|
|
CorrectionsLocal.Add(Hi);
|
|
|
|
log.FatalFormat("{0} Correction initialised - CorrectionValLo: {1} CorrectionValHi: {2}", Name, CorrectionValLo, CorrectionValHi);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
log.FatalFormat("{0} simulated: {1}", Name, this);
|
|
}
|
|
}
|
|
|
|
private void InitModbus()
|
|
{
|
|
modbus = TbfComponents.FindComponent(_adjustableMtrCfg.ParentName) as Common.Modbus;
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
modbus.ComponentNames[_adjustableMtrCfg.ModbusAddress] = Name;
|
|
ticketNumber = modbus.RegisterForPolling();
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
|
}
|
|
|
|
public void RunDeviceBefore()
|
|
{
|
|
if (DebugLevel == DebugMode.Normal && modbus.ReceivedTelegrams[_adjustableMtrCfg.ModbusAddress].Count > 0)
|
|
{
|
|
byte[] telegram = modbus.ReceivedTelegrams[_adjustableMtrCfg.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;
|
|
|
|
receivedAmpers = Units.ConvertFrom(Unit.A, System.BitConverter.ToSingle(telegram, 3));
|
|
if (CorrectionsLocal != null)
|
|
{
|
|
receivedValue = MeasurementCorrection.GetCorrection(receivedAmpers, CorrectionsLocal);
|
|
}
|
|
else
|
|
{
|
|
receivedValue = receivedAmpers;
|
|
}
|
|
|
|
log.WarnFormat("Adjustable meter: {0}={1} Unit interpolated from Ampers: {2}", Name, receivedValue.ToString("F3"), receivedAmpers.ToString("F3"));
|
|
}
|
|
}
|
|
}
|
|
|
|
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)_adjustableMtrCfg.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(receivedValue, 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 ReadAdjustableOp(ref DoubleBox value)
|
|
{
|
|
return new ReadValueOp(this, ref value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: valueDone, 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 ReadAdjustableOp(ref DoubleBox value, Event valueDone)
|
|
{
|
|
return new ReadValueOp(this, ref value, valueDone);
|
|
}
|
|
|
|
|
|
///
|
|
/// 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 WaitUntilValueIsOp(this, Pr.IsGT, pressLimit));
|
|
AddCondition(string.Format("{0} {1} < {2} bar", Strings.Wait_until, Name, pressLimit), new WaitUntilValueIsOp(this, Pr.IsLT, pressLimit));
|
|
}
|
|
}
|
|
}
|
|
}
|