Refactorization : BenchControl -> Rig
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
///
|
||||
/// Copyright (c) 2016-2020 Sensus Slovensko a.s.
|
||||
///
|
||||
using System.Collections.Generic;
|
||||
using TBF.Rig.Generic;
|
||||
|
||||
namespace TBF.Rig.Modbus.PressureMeter.Meret
|
||||
{
|
||||
public class Factory : IComponentFactory
|
||||
{
|
||||
public string ClassName { get { return GetType().Namespace.Substring(8); } }
|
||||
public override string ToString() { return ClassName; }
|
||||
|
||||
public void ResetStaticProperties() { PressureMeter.ResetStaticProperties(); }
|
||||
|
||||
public IComponent DummyComponent() { return new PressureMeter(); }
|
||||
|
||||
public IComponent GetComponent(IComponentCfg cfg, IList<IComponent> components) { return new PressureMeter(cfg, components); }
|
||||
|
||||
public IComponentCfg DefaultConfig() { return new PressureMeterCfg(this); }
|
||||
|
||||
public IComponentCfg CmpntCfgFromCmpntEntity(Config.Entities.Component component)
|
||||
{
|
||||
return ComponentCfgBase.CreateFromDbEntity(PressureMeterCfg.Serializer, component, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
///
|
||||
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Serialization;
|
||||
using Config;
|
||||
using Config.Entities;
|
||||
using SchematicDrawing;
|
||||
using TBF.Rig.Generic;
|
||||
|
||||
namespace TBF.Rig.Modbus.PressureMeter.Meret
|
||||
{
|
||||
public class PressureMeterCfg : ComponentCfgBase, IChildComponentCfg, GenericDevices.ICalibInfoCfg, IParamsProvider,
|
||||
IDrawingItemWithMeasuredVal
|
||||
{
|
||||
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(PressureMeterCfg) })[0];
|
||||
public override XmlSerializer GetSerializer() { return Serializer; }
|
||||
|
||||
public IComponentCfgCtrl GetControl(IList<Config.Entities.Component> cmpntEntities)
|
||||
{
|
||||
var parents = cmpntEntities.Where(x => x.ClassName == "Modbus.Common");
|
||||
return new Configs.ParamsProvider.ComponentCfgCtrl(this, parents);
|
||||
}
|
||||
|
||||
///
|
||||
/// Serialized parameters
|
||||
///
|
||||
public int ModbusAddress; /// 0 (1..254)
|
||||
public double DefaultPressure; /// 1
|
||||
public string MsrdFormat { get; set; } /// 2
|
||||
public Config.Unit MsrdUnit { get; set; } /// 3
|
||||
public double MsrdValLimLo { get; set; } /// 4
|
||||
public double MsrdValLimHi { get; set; } /// 5
|
||||
|
||||
/// Calibration info serialized parameters displayed in Metrology tab page
|
||||
public string CalibCertificateNr { get; set; }
|
||||
public DateTime CalibDate { get; set; }
|
||||
public DateTime CalibValidDate { get; set; }
|
||||
|
||||
/// Schematic drawing info
|
||||
public Shape Shape { get; set; }
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
public Sz Sz { get; set; }
|
||||
public Orient Orient { get; set; }
|
||||
public bool Flip { get; set; }
|
||||
public int LblX { get; set; }
|
||||
public int LblY { get; set; }
|
||||
public Orient LblOrient { get; set; }
|
||||
public int MsrdX { get; set; }
|
||||
public int MsrdY { get; set; }
|
||||
public Orient MsrdOrient { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
public IList<GNode> GNodes { get; set; }
|
||||
|
||||
/// Private parameterless constructor invoked by all other (public) constructors
|
||||
PressureMeterCfg()
|
||||
{
|
||||
GNodes = new List<GNode>();
|
||||
}
|
||||
|
||||
public PressureMeterCfg(IComponentFactory factory)
|
||||
: this()
|
||||
{
|
||||
Shape = Shape.PressM;
|
||||
Sz = Sz.M;
|
||||
Factory = factory;
|
||||
Name = "Pr";
|
||||
ParentName = "Modbus";
|
||||
InitializeAll();
|
||||
}
|
||||
|
||||
public string ComponentName { get { return Name; } }
|
||||
|
||||
public void InitializeAll()
|
||||
{
|
||||
ModbusAddress = 49;
|
||||
DefaultPressure = 2.34;
|
||||
MsrdFormat = "{0:F2} bar";
|
||||
MsrdUnit = Config.Unit.bar;
|
||||
MsrdValLimLo = -0.1;
|
||||
MsrdValLimHi = 25.0;
|
||||
|
||||
MsrdY = 22;
|
||||
}
|
||||
|
||||
string[] paramNames = new string[]
|
||||
{
|
||||
"Modbus address", /// 0
|
||||
"Default pressure", /// 1
|
||||
"Display format", /// 2
|
||||
"Unit of pressure", /// 3
|
||||
"Pressure limit Lo", /// 4
|
||||
"Pressure limit Hi", /// 5
|
||||
};
|
||||
public string ParamName(int i) { return paramNames[i]; }
|
||||
public int ParamsCount() { return paramNames.Length; }
|
||||
|
||||
public ICollection<string> ParamValues(int i)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 3:
|
||||
return new string[] { "Pa", "hPa", "mbar", "kPa", "inHg", "psi", "bar", "MPa" };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string ToString(int i)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0: return ModbusAddress.ToString();
|
||||
case 1: return DefaultPressure.ToString();
|
||||
case 2: return MsrdFormat;
|
||||
case 3: return MsrdUnit.ToString();
|
||||
case 4: return MsrdValLimLo.ToString();
|
||||
case 5: return MsrdValLimHi.ToString();
|
||||
default:
|
||||
return string.Format("{0}(1) {2}[{3},{4}] addr={5}", Name,
|
||||
(string.IsNullOrEmpty(ParentName) ? "-" : ParentName), Shape, X, Y, ModbusAddress);
|
||||
}
|
||||
}
|
||||
|
||||
public CfgUpdateFlags UpdateParam(int i, string str)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0: ModbusAddress = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
|
||||
case 1: DefaultPressure = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
|
||||
case 2: MsrdFormat = str; return CfgUpdateFlags.RestartRqrd;
|
||||
case 3:
|
||||
foreach (var u in new Unit[] { Unit.Pa, Unit.hPa, Unit.mbar, Unit.kPa, Unit.inHg, Unit.psi, Unit.bar, Unit.MPa })
|
||||
{
|
||||
if (str == u.ToString())
|
||||
{
|
||||
MsrdUnit = u;
|
||||
return CfgUpdateFlags.RestartRqrd;
|
||||
}
|
||||
}
|
||||
return CfgUpdateFlags.None;
|
||||
case 4: MsrdValLimLo = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
|
||||
case 5: MsrdValLimHi = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
|
||||
default: return CfgUpdateFlags.None;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ValidateParam(int i, string str, out string message)
|
||||
{
|
||||
message = string.Empty;
|
||||
int idummy;
|
||||
double dummy;
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
if (int.TryParse(str, out idummy) && idummy >= 1 && idummy <= 254) return true;
|
||||
break;
|
||||
case 1:
|
||||
case 4:
|
||||
case 5:
|
||||
if (TBF.Utils.TryParseSDouble(str, out dummy)) return true;
|
||||
break;
|
||||
case 2:
|
||||
return true;
|
||||
case 3:
|
||||
if (ParamValues(3).Contains(str)) return true;
|
||||
break;
|
||||
default:
|
||||
message = "Invalid index";
|
||||
return false;
|
||||
}
|
||||
|
||||
message = ParamName(i) + " is invalid";
|
||||
return false;
|
||||
}
|
||||
|
||||
void CopyContentTo(PressureMeterCfg prms)
|
||||
{
|
||||
prms.ParentName = this.ParentName;
|
||||
|
||||
prms.CalibCertificateNr = this.CalibCertificateNr;
|
||||
prms.CalibDate = this.CalibDate;
|
||||
prms.CalibValidDate = this.CalibValidDate;
|
||||
prms.Shape = this.Shape;
|
||||
prms.Sz = this.Sz;
|
||||
prms.Orient = this.Orient;
|
||||
prms.Flip = this.Flip;
|
||||
prms.LblX = this.LblX;
|
||||
prms.LblY = this.LblY;
|
||||
prms.LblOrient = this.LblOrient;
|
||||
prms.MsrdX = this.MsrdX;
|
||||
prms.MsrdY = this.MsrdY;
|
||||
prms.MsrdOrient = this.MsrdOrient;
|
||||
|
||||
prms.ModbusAddress = this.ModbusAddress;
|
||||
prms.DefaultPressure = this.DefaultPressure;
|
||||
prms.MsrdFormat = this.MsrdFormat;
|
||||
prms.MsrdUnit = this.MsrdUnit;
|
||||
prms.MsrdValLimLo = this.MsrdValLimLo;
|
||||
prms.MsrdValLimHi = this.MsrdValLimHi;
|
||||
}
|
||||
|
||||
public Config.Entities.IParamsProvider Clone()
|
||||
{
|
||||
PressureMeterCfg pars = new PressureMeterCfg();
|
||||
CopyContentTo(pars);
|
||||
return pars;
|
||||
}
|
||||
|
||||
public bool UpdateEmbeddedDbEntity()
|
||||
{
|
||||
return true; /// =OK, do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
///
|
||||
/// Copyright (c) 2016-2021 Sensus Slovensko a.s.
|
||||
///
|
||||
using System;
|
||||
using log4net;
|
||||
using TBF.Boxes;
|
||||
|
||||
namespace TBF.Rig.Modbus.PressureMeter.Meret
|
||||
{
|
||||
public class ReadPressureOp : IOperation
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(ReadPressureOp));
|
||||
public override string ToString() { return string.Format("ReadPressureOp(.,{0},.)", eventDone); }
|
||||
|
||||
/// Set by the constructor
|
||||
readonly PressureMeter pressureMeter;
|
||||
readonly DoubleBox pressure; /// Box for the measured value
|
||||
readonly Event eventDone;
|
||||
|
||||
/// <summary>
|
||||
/// Events: PressureDone or Error
|
||||
/// </summary>
|
||||
/// <param name="pressureMeter">Pressure meter reference</param>
|
||||
/// <param name="pressure">Reference to the measured pressure variable, value is in bar</param>
|
||||
/// <param name="eventDone">Event to be returned by Run() when completed OK</param>
|
||||
public ReadPressureOp(PressureMeter pressureMeter, ref DoubleBox pressure, Event eventDone)
|
||||
{
|
||||
if (pressureMeter == null) throw new ArgumentNullException("pressureMeter");
|
||||
this.pressureMeter = pressureMeter;
|
||||
this.pressure = pressure;
|
||||
this.eventDone = eventDone;
|
||||
|
||||
log.Debug(this.ToString());
|
||||
}
|
||||
public ReadPressureOp(PressureMeter pressureMeter, ref DoubleBox pressure)
|
||||
: this(pressureMeter, ref pressure, Event.PressureDone)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>Start this operation</summary>
|
||||
public void Start()
|
||||
{
|
||||
if (pressure != null) pressure.Val = pressureMeter.ReadPressure();
|
||||
}
|
||||
|
||||
/// <summary>Run this operation</summary>
|
||||
/// <returns>
|
||||
/// Event.PressureInDone or Event.PressureOutDone
|
||||
/// </returns>
|
||||
public Event Run()
|
||||
{
|
||||
if (pressure != null) pressure.Val = pressureMeter.ReadPressure();
|
||||
return eventDone;
|
||||
}
|
||||
|
||||
/// <summary>Stop this operation</summary>
|
||||
public void Stop() { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user