Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
///
|
||||
/// Copyright (c) 2016-2020 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using log4net;
|
||||
using SchematicDrawing;
|
||||
using TBF.Rig.Generic;
|
||||
using TBF.Boxes;
|
||||
|
||||
namespace TBF.Rig.Modbus.PressureMeter.Meret
|
||||
{
|
||||
public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, IDevice, 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; } }
|
||||
|
||||
readonly GenericDevices.IModbus modbus;
|
||||
double receivedPressure;
|
||||
|
||||
public bool MsrmntAvailable { get { return true; } }
|
||||
public double MeasuredVal { get { return receivedPressure; } }
|
||||
public string AltString { get { return string.Empty; } }
|
||||
|
||||
|
||||
public PressureMeter() { }
|
||||
|
||||
public PressureMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
||||
: base(cfg)
|
||||
{
|
||||
pressureMtrCfg = cfg as PressureMeterCfg;
|
||||
|
||||
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
|
||||
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
||||
|
||||
log.Warn(this.ToString());
|
||||
}
|
||||
|
||||
///
|
||||
/// IDevice interface
|
||||
///
|
||||
public void Initialize() { }
|
||||
|
||||
public void RunDeviceBefore()
|
||||
{
|
||||
if (pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
|
||||
pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (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 = Config.Units.ConvertFrom(Config.Unit.kPa, System.BitConverter.ToSingle(telegram, 3));
|
||||
log.WarnFormat("Pressure: {0}={1}bar", Name, receivedPressure.ToString("F3"));
|
||||
}
|
||||
}
|
||||
|
||||
if ((StateMachine.Time % Constants.ModbusPollPeriod) == (pressureMtrCfg.ModbusAddress % Constants.ModbusPollPeriod)) /// Each 'ModbusDevices' seconds
|
||||
{
|
||||
const byte Function = 4; /// Read input registers
|
||||
const ushort Address = 0; /// Pressure
|
||||
modbus.SendMessage((byte)pressureMtrCfg.ModbusAddress, Function, Address, 2);
|
||||
}
|
||||
}
|
||||
|
||||
public void RunDeviceAfter() { }
|
||||
public void StopDevice() { }
|
||||
public void StopDevice2() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the water pressure
|
||||
/// </summary>
|
||||
/// <returns>Pressure in mBar</returns>
|
||||
public double ReadPressure()
|
||||
{
|
||||
if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal)
|
||||
{
|
||||
return Config.Formulas.CorrectedValue(receivedPressure, Corrections);
|
||||
}
|
||||
else if (Cfg.DebugLevel == Config.Entities.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user