Refactorization : BenchControl -> Rig

This commit is contained in:
Milan Hanajik
2021-03-05 16:12:24 +01:00
parent 84c8ec26b6
commit 6cf3bd6160
1461 changed files with 6330 additions and 6330 deletions
+216
View File
@@ -0,0 +1,216 @@
///
/// Copyright (c) 2017-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Config;
using Config.Entities;
using SchematicDrawing;
using TBF.Rig.Generic;
using TBF.Boxes;
namespace TBF.Rig.Modbus.Ambient.Comet
{
/// <summary>
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
/// Connection settings: 9600 Bd 8-bits No-parity 2-stop-bits Flow control: none.
/// </summary>
public class Ambient : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient, IDrawingItCmpntWithMeasuredVal
{
private static readonly ILog log = LogManager.GetLogger(typeof(Ambient));
public override string ToString() { return string.Format("Ambient({0})", Cfg.ToString(1)); }
///
/// Configuration and wrappers
///
readonly AmbientCfg ambientCfg;
///
Unit TempUnit { get { return ambientCfg.TempUnit; } }
double TempLimLo { get { return Units.ConvertFrom(TempUnit, ambientCfg.TempLimLo); } }
double TempLimHi { get { return Units.ConvertFrom(TempUnit, ambientCfg.TempLimHi); } }
double DefaultTemperature { get { return Units.ConvertFrom(TempUnit, ambientCfg.DefaultTemp); } }
///
Unit PressureUnit { get { return ambientCfg.PressureUnit; } }
double PressureLimLo { get { return Units.ConvertFrom(PressureUnit, ambientCfg.PressureLimLo); } }
double PressureLimHi { get { return Units.ConvertFrom(PressureUnit, ambientCfg.PressureLimHi); } }
double DefaultPressure { get { return Units.ConvertFrom(PressureUnit, ambientCfg.DefaultPressure); } }
///
Unit HumiUnit { get { return ambientCfg.HumiUnit; } }
double HumiLimLo { get { return Units.ConvertFrom(HumiUnit, ambientCfg.HumiLimLo); } }
double HumiLimHi { get { return Units.ConvertFrom(HumiUnit, ambientCfg.HumiLimHi); } }
double DefaultHumidity { get { return Units.ConvertFrom(HumiUnit, ambientCfg.DefaultHumi); } }
///
/// IDrawingItCmpntWithMeasuredVal interface
///
public IDrawingItem DrawingItem { get { return ambientCfg as IDrawingItem; } }
///
public bool MsrmntAvailable { get { return true; } }
public double MeasuredVal { get { return temperature; } }
public string AltString
{
get {
return string.Format(ambientCfg.Format, Units.ConvertTo(TempUnit, temperature),
Units.ConvertTo(PressureUnit, pressure),
Units.ConvertTo(HumiUnit, humidity));
}
}
readonly GenericDevices.IModbus modbus;
/// <summary>The state of the measurement</summary>
MsrmntState msrmntState;
///
/// Measured values when MsrmntState == MsrmntState.Valid
///
double temperature; /// [°C]
double pressure; /// [bar]
double humidity; /// [R%]
/// <summary>Measurement time stamp when MsrmntState == MsrmntState.Valid</summary>
int msrmntTimeStamp;
public Ambient()
{
}
/// <summary>
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
/// </summary>
public Ambient(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
ambientCfg = cfg as AmbientCfg;
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
temperature = DefaultTemperature;
pressure = DefaultPressure;
humidity = DefaultHumidity;
log.Warn(this.ToString());
}
public void Initialize()
{
if (ambientCfg.DebugLevel == DebugMode.Simulate)
{
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
return;
}
msrmntState = MsrmntState.Busy;
log.FatalFormat("Successfully initialized device {0}", ToString());
}
/// <summary>Run this device</summary>
public void RunDeviceBefore()
{
if (ambientCfg.DebugLevel == DebugMode.Simulate ||
ambientCfg.DebugLevel == DebugMode.FailureDuringOperation)
{
msrmntTimeStamp = StateMachine.Time;
return;
}
if (modbus.ReceivedTelegrams[ambientCfg.ModbusAddress].Count > 0)
{
byte[] telegram = modbus.ReceivedTelegrams[ambientCfg.ModbusAddress].Dequeue();
if (telegram.Length == 13 && telegram[1] == 3 && telegram[2] == 8)
{
int value = (int)telegram[3] * 256 + (int)telegram[4];
temperature = value / 10.0;
value = (int)telegram[5] * 256 + (int)telegram[6];
humidity = value / 10.0;
value = (int)telegram[9] * 256 + (int)telegram[10];
pressure = value / 10000.0;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
UpdateProcessData(temperature, pressure, humidity);
log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure);
}
}
if ((StateMachine.Time % 10) == (ambientCfg.ModbusAddress % 10)) /// Each 10 seconds
{
/// Read four registers: 0x31, 0x32, 0x33, 0x34
UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!)
UInt16 count = 4;
/// Prepare data to be sent
byte[] msg = new byte[8];
msg[0] = (byte)ambientCfg.ModbusAddress;
msg[1] = 3; /// CMD = Read registers
msg[2] = (byte)(regAddr >> 8); /// Control Word Hi
msg[3] = (byte)(regAddr & 0xFF); /// Control Word Lo
msg[4] = (byte)(count >> 8); /// Serial Com Reference Hi
msg[5] = (byte)(count & 0xFF); /// Serial Com Reference Lo
modbus.SendMessage(msg);
}
}
public void RunDeviceAfter() { }
public void StopDevice() { }
public void StopDevice2() { }
void UpdateProcessData(double temperature, double pressure, double humidity)
{
TBF.Rig.Sequences.ProcessData.AmbTemp.Val = temperature;
TBF.Rig.Sequences.ProcessData.AmbPress.Val = pressure;
TBF.Rig.Sequences.ProcessData.AmbHumi.Val = humidity;
}
///
/// Boxes for the operation result
///
DoubleBox tempBox;
DoubleBox pressureBox;
DoubleBox humiBox;
public IOperation ReadAmbientOp(DoubleBox temperature, DoubleBox pressure, DoubleBox humidity)
{
this.tempBox = temperature;
this.pressureBox = pressure;
this.humiBox = humidity;
return this;
}
/// <summary>Start this operation</summary>
public void Start()
{
if (tempBox != null) tempBox.Val = temperature;
if (pressureBox != null) pressureBox.Val = pressure;
if (humiBox != null) humiBox.Val = humidity;
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.FlowInDone or Event.FlowOutDone
/// </returns>
public Event Run()
{
if (tempBox != null) tempBox.Val = temperature;
if (pressureBox != null) pressureBox.Val = pressure;
if (humiBox != null) humiBox.Val = humidity;
return Event.AmbientDone;
}
/// <summary>Stop this operation</summary>
public void Stop() { }
}
}
+290
View File
@@ -0,0 +1,290 @@
///
/// 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.Ambient.Comet
{
public class AmbientCfg : ComponentCfgBase, IChildComponentCfg, IParamsProvider, IDrawingItemWithMeasuredVal
{
public static XmlSerializer Serializer = XmlSerializer.FromTypes(new[] { typeof(AmbientCfg) })[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 Unit TempUnit; /// 1
public double TempLimLo; /// 2
public double TempLimHi; /// 3
public double DefaultTemp; /// 4
public Unit PressureUnit; /// 5
public double PressureLimLo; /// 6
public double PressureLimHi; /// 7
public double DefaultPressure; /// 8
public Unit HumiUnit; /// 9
public double HumiLimLo; /// 10
public double HumiLimHi; /// 11
public double DefaultHumi; /// 12
public string Format; /// 13
/// 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 string MsrdFormat { get { return string.Empty; } }
[XmlIgnore]
public Config.Unit MsrdUnit { get { return TempUnit; } }
[XmlIgnore]
public double MsrdValLimLo { get { return TempLimLo; } }
[XmlIgnore]
public double MsrdValLimHi { get { return TempLimHi; } }
[XmlIgnore]
public IList<GNode> GNodes { get; set; }
/// Private parameterless constructor invoked by all other (public) constructors
AmbientCfg()
{
GNodes = new List<GNode>();
Format = string.Empty;
}
public AmbientCfg(IComponentFactory factory)
: this()
{
Shape = Shape.PressM;
Sz = Sz.M;
Factory = factory;
Name = "Ambient";
ParentName = "Modbus";
InitializeAll();
}
public string ComponentName { get { return Name; } }
public void InitializeAll()
{
ModbusAddress = 1;
TempUnit = Unit.C;
TempLimLo = 15;
TempLimHi = 30;
DefaultTemp = 25;
PressureUnit = Unit.hPa;
PressureLimLo = 960;
PressureLimHi = 1060;
DefaultPressure = 1013;
HumiUnit = Unit.RPct;
HumiLimLo = 20;
HumiLimHi = 80;
DefaultHumi = 50;
Format = "{0:F1} °C\n{1:F0} hPa\n{2:F0} R%";
MsrdY = 22;
}
string[] paramNames = new string[]
{
"Modbus address", /// 0
"Unit of temperature", /// 1
"Temperature limit Lo", /// 2
"Temperature limit Hi", /// 3
"Default temperature", /// 4
"Unit of pressure", /// 5
"Pressure limit Lo", /// 6
"Pressure limit Hi", /// 7
"Default pressure", /// 8
"Unit of rel. humidity", /// 9
"Rel. humidity limit Lo", /// 10
"Rel. humidity limit Hi", /// 11
"Default retl. humidity", /// 12
"Display format", /// 13
};
public string ParamName(int i) { return paramNames[i]; }
public int ParamsCount() { return paramNames.Length; }
public ICollection<string> ParamValues(int i)
{
switch (i)
{
case 1:
return new string[] { "°C", "°F", "K" };
case 5:
return new string[] { "Pa", "hPa", "mbar", "kPa", "inHg", "psi", "bar", "MPa" };
case 9:
return new string[] { "R%" };
default:
return null;
}
}
public string ToString(int i)
{
switch (i)
{
case 0: return ModbusAddress.ToString();
case 1: return TempUnit.ToDescription();
case 2: return TempLimLo.ToString();
case 3: return TempLimHi.ToString();
case 4: return DefaultTemp.ToString();
case 5: return PressureUnit.ToDescription();
case 6: return PressureLimLo.ToString();
case 7: return PressureLimHi.ToString();
case 8: return DefaultPressure.ToString();
case 9: return HumiUnit.ToDescription();
case 10: return HumiLimLo.ToString();
case 11: return HumiLimHi.ToString();
case 12: return DefaultHumi.ToString();
case 13: return Format.Replace("\n", "<LF>");
default:
return string.Format("Name={0}, Parent={1}, ModbusAddr={2}", Name,
(string.IsNullOrEmpty(ParentName) ? "-" : ParentName), ModbusAddress);
}
}
public CfgUpdateFlags UpdateParam(int i, string str)
{
switch (i)
{
case 0: ModbusAddress = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 1:
foreach (var u in new Unit[] { Unit.C, Unit.F, Unit.K })
{
if (str == u.ToDescription()) { TempUnit = u; return CfgUpdateFlags.RestartRqrd; }
}
return CfgUpdateFlags.None;
case 2: TempLimLo = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
case 3: TempLimHi = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
case 4: DefaultTemp = Utils.ParseSDouble(str); return CfgUpdateFlags.RestartRqrd;
case 5:
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.ToDescription()) { PressureUnit = u; return CfgUpdateFlags.RestartRqrd; }
}
return CfgUpdateFlags.None;
case 6: PressureLimLo = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 7: PressureLimHi = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 8: DefaultPressure = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 9:
foreach (var u in new Unit[] { Unit.RPct })
{
if (str == u.ToDescription()) { HumiUnit = u; return CfgUpdateFlags.RestartRqrd; }
}
return CfgUpdateFlags.None;
case 10: HumiLimLo = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 11: HumiLimHi = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 12: DefaultHumi = Utils.ParseUDouble(str); return CfgUpdateFlags.RestartRqrd;
case 13: Format = str.Replace("<LF>", "\n"); 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 5:
case 9:
if (ParamValues(i).Contains(str)) return true;
break;
case 2:
case 3:
case 4:
if (TBF.Utils.TryParseSDouble(str, out dummy)) return true;
break;
case 6:
case 7:
case 8:
case 10:
case 11:
case 12:
if (TBF.Utils.TryParseUDouble(str, out dummy)) return true;
break;
case 13:
return true;
default:
message = "Invalid index";
return false;
}
message = ParamName(i) + " is invalid";
return false;
}
void CopyContentTo(AmbientCfg prms)
{
prms.ParentName = this.ParentName;
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.TempUnit = this.TempUnit;
prms.TempLimLo = this.TempLimLo;
prms.TempLimHi = this.TempLimHi;
prms.DefaultTemp = this.DefaultTemp;
prms.PressureUnit = this.PressureUnit;
prms.PressureLimLo = this.PressureLimLo;
prms.PressureLimHi = this.PressureLimHi;
prms.DefaultPressure = this.DefaultPressure;
prms.HumiUnit = this.HumiUnit;
prms.HumiLimLo = this.HumiLimLo;
prms.HumiLimHi = this.HumiLimHi;
prms.DefaultHumi = this.DefaultHumi;
prms.Format = this.Format;
}
public Config.Entities.IParamsProvider Clone()
{
AmbientCfg pars = new AmbientCfg();
CopyContentTo(pars);
return pars;
}
public bool UpdateEmbeddedDbEntity()
{
return true; /// =OK, do nothing
}
}
}
+27
View File
@@ -0,0 +1,27 @@
///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System.Collections.Generic;
using TBF.Rig.Generic;
namespace TBF.Rig.Modbus.Ambient.Comet
{
public class Factory : IComponentFactory
{
public string ClassName { get { return GetType().Namespace.Substring(8); } }
public override string ToString() { return ClassName; }
public void ResetStaticProperties() { Ambient.ResetStaticProperties(); }
public IComponent DummyComponent() { return new Ambient(); }
public IComponent GetComponent(IComponentCfg cfg, IList<IComponent> components) { return new Ambient(cfg, components); }
public IComponentCfg DefaultConfig() { return new AmbientCfg(this); }
public IComponentCfg CmpntCfgFromCmpntEntity(Config.Entities.Component component)
{
return ComponentCfgBase.CreateFromDbEntity(AmbientCfg.Serializer, component, this);
}
}
}