Ambient clean-up.

This commit is contained in:
Milan Hanajik
2021-04-02 15:12:22 +02:00
parent 4e9d1c8281
commit 51aa0f4e43
6 changed files with 80 additions and 124 deletions
+11 -33
View File
@@ -19,7 +19,7 @@ namespace TBF.Rig.Modbus.Ambient.Comet
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)); }
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
///
/// Configuration and wrappers
@@ -50,18 +50,15 @@ namespace TBF.Rig.Modbus.Ambient.Comet
public double MeasuredVal { get { return temperature; } }
public string AltString
{
get {
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;
readonly Common.Modbus modbus;
///
/// Measured values when MsrmntState == MsrmntState.Valid
@@ -87,41 +84,25 @@ namespace TBF.Rig.Modbus.Ambient.Comet
{
ambientCfg = cfg as AmbientCfg;
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
modbus = TbfComponents.FindComponent(cfg.ParentName, components) as Common.Modbus;
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;
temperature = DefaultTemperature;
pressure = DefaultPressure;
humidity = DefaultHumidity;
msrmntTimeStamp = 0;
ticketNumber = modbus.RegisterForPolling();
log.FatalFormat("Successfully initialized device {0}", ToString());
log.FatalFormat("{0} - Device successfully initialized", Name);
}
/// <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();
@@ -138,7 +119,6 @@ namespace TBF.Rig.Modbus.Ambient.Comet
pressure = value / 10000.0;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
UpdateProcessData(temperature, pressure, humidity);
@@ -149,9 +129,7 @@ namespace TBF.Rig.Modbus.Ambient.Comet
public void RunDeviceAfter()
{
if (ambientCfg.DebugLevel != Config.Entities.DebugMode.Simulate &&
ambientCfg.DebugLevel != Config.Entities.DebugMode.FailureDuringOperation &&
modbus.IsMyTurn(ticketNumber))
if (modbus.IsMyTurn(ticketNumber))
{
/// Read four registers: 0x31, 0x32, 0x33, 0x34
UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!)