MagFlux: - Request protocol implementation start

This commit is contained in:
Thomas Wiedebusch
2023-05-04 19:02:20 +02:00
parent 27308461be
commit 23c9a61370
8 changed files with 527 additions and 139 deletions
@@ -69,6 +69,42 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
/// </summary>
public Boolean QualityWatchMode;
/// <summary>
/// Represent FW version
/// </summary>
public String FwVersion
{
get;
private set;
} = "?";
/// <summary>
/// Represent meter size in mm
/// </summary>
public Int32 MeterSize_mm
{
get;
private set;
}
/// <summary>
/// Represent FW version
/// </summary>
public String FwBuildDate
{
get;
private set;
} = "?";
/// <summary>
/// Represent FW GIT hash
/// </summary>
public String FwGitHash
{
get;
private set;
} = "?";
/// <summary>
/// Represent pcb serial number
/// </summary>
@@ -189,6 +225,12 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
}
}
public Boolean IsLoggedOn
{
private set;
get;
}
/// <inheritdoc />
public ErrorState ErrorStatus
{
@@ -221,11 +263,9 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
//initially do not signal event
private readonly AutoResetEvent _onSyncKeepSessionThread = new AutoResetEvent(false);
private Thread _keepSessionThread;
private Timer _keepSessionTimer;
private readonly CancellationTokenSource _keepSessionToken = new CancellationTokenSource();
private ILogger _logger;
private ILogger _loggerRawData;
#endregion
@@ -359,7 +399,7 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
return;
}
_streamingDecoder = new StreamingDecoder(false);
if (StreamingPort == null || !(StreamingPort is LedSerialPort))
if (!(StreamingPort is LedSerialPort))
{
throw new ApplicationException("No valid streaming port");
}
@@ -415,7 +455,6 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
/// </remarks>
public MagFluxMeter()
{
SetLogger();
}
/// <inheritdoc />
@@ -423,6 +462,9 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
{
_logger.Trace($"Slot:{Slot} - Start to dispose");
StreamingProtocol.OnRecordIsDecoded -= StreamingProtocol_ApplyDecodedRecord;
RequestProtocol.OnRecordIsDecoded -= RequestProtocol_ApplyDecodedRecord;
LogRawData(false);
LogManager.Flush();
//isPart of Logout
@@ -512,22 +554,24 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
Boolean ignoreCorruptedData = true, String password = "")
{
Slot = slot;
SetLogger();
//SetLogger();
_password = password;
if (requestPort != null)
{
//if (requestPort.Value.Type == "Xylem.Common.Hardware.Interfaces.Ports.SerialPorts.UartSerialPort")
//{
// var requestIdent = $"Slot:{Slot}, Port:{requestPort.Value.PortName}, Protocol:Request, Type:UART -";
if (requestPort.Value.Type == "Xylem.Common.Hardware.Interfaces.Ports.SerialPorts.UartSerialPort")
{
var requestIdent = $"Slot:{Slot}, Port:{requestPort.Value.PortName}, Protocol:Request, Type:UART -";
// RequestProtocol = new RequestProtocol(requestIdent);
// TransmitProtocol = new UartTransmitProtocol(requestPort.Value.PortName);
// RequestProtocol.SetTransmitProtocol(TransmitProtocol);
RequestProtocol = new RequestProtocol(requestIdent);
TransmitProtocol = new UartTransmitProtocol(requestPort.Value.PortName);
//RequestProtocol.SetTransmitProtocol(TransmitProtocol);
// RequestPort = new UartSerialPort(requestIdent, requestPort.Value.GetSerialPort(),
// TransmitProtocol.GetTransmitPortSettings());
//}
// request port is needed to assign the port name from file, else it is not used
// as the IMagFluxRequestProtocol implements the port handling and communication
RequestPort = new UartSerialPort(requestIdent, requestPort.Value.GetSerialPort(),
TransmitProtocol.GetTransmitPortSettings());
}
}
if (streamingPort != null)
@@ -541,6 +585,8 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
StreamingProtocol = new StreamingProtocol(streamingIdent, ignoreCorruptedData);
}
Configuration = new ProcessConfig(ProgramConfig.ConfigFileName);
SetLogger();
_logger.Debug($"Slot:{Slot} - Setup done");
}
@@ -552,9 +598,16 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
{
if (RequestPort != null && RequestPort.GetPortName() != BaseSerialPort.PortNotAssigned)
{
LinkPortToProtocol(RequestPort, RequestProtocol);
_logger.Trace($"Slot:{Slot} - RequestPort and RequestProtocol linked");
RequestProtocol.OnRecordIsDecoded += RequestProtocol_ApplyDecodedRecord;
if (RequestProtocol.Connect(RequestPort.GetPortName()))
{
_logger.Trace($"Slot:{Slot} - RequestPort and RequestProtocol linked");
RequestProtocol.OnRecordIsDecoded += RequestProtocol_ApplyDecodedRecord;
RequestProtocol.CloseConnection();
}
else
{
_logger.Trace($"Slot:{Slot} - RequestPort Connection failed");
}
}
if (StreamingPort != null && StreamingPort.GetPortName() != BaseSerialPort.PortNotAssigned)
{
@@ -571,7 +624,7 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
/// </summary>
/// <param name="port">On <see cref="IPort" /> should one once added in runtime</param>
/// <param name="protocol"></param>
public void LinkPortToProtocol(IPort port, IProtocol protocol)
private void LinkPortToProtocol(IPort port, IProtocol protocol)
{
//for streaming mode only
if (port == null)
@@ -607,7 +660,7 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
/// </summary>
/// <param name="markRecord"></param>
/// <exception cref="ApplicationException"></exception>
public void SyncStreamingBuffer(SyncMarkRecord markRecord)
private void SyncStreamingBuffer(SyncMarkRecord markRecord)
{
if (StreamingPort == null)
{
@@ -641,13 +694,9 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
/// <param name="useRequest"></param>
/// <param name="useStreaming"></param>
/// <exception cref="ApplicationException"></exception>
public void SetupFromConfigFile(Int32 slot, Boolean ignoreCorruptedData, Boolean useRequest, Boolean useStreaming)
private void SetupFromConfigFile(Int32 slot, Boolean ignoreCorruptedData, Boolean useRequest, Boolean useStreaming)
{
Slot = slot;
SetLogger();
var configFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(MagFlux), ProgramConfig.SerialConfigFileName);
if (!File.Exists(configFile))
@@ -668,7 +717,6 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
throw new ApplicationException($"Slot:{slot} - File " +
$"{ProgramConfig.SerialConfigFileName} does not contain valid configuration");
}
_logger.Debug($"Slot:{slot} - Configuration loaded");
if (!useRequest)
{
SetupMagFluxMeter(slot, null, slotConfig.Streaming, ignoreCorruptedData);
@@ -681,18 +729,49 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
{
SetupMagFluxMeter(slot, slotConfig.Request, slotConfig.Streaming, ignoreCorruptedData);
}
SetLogger();
_logger.Debug($"Slot:{slot} - Configuration loaded");
}
/// <inheritdoc />
public Boolean Logout()
{
RequestProtocol.CloseConnection();
IsLoggedOn = false;
return true;
}
/// <inheritdoc />
public Boolean Login()
{
return true;
if (!RequestProtocol.Connect(RequestPort.GetPortName())) return IsLoggedOn;
IsLoggedOn = true;
if (RequestProtocol.GetFwVersion(out var fwVersion))
{
FwVersion = fwVersion;
}
if (RequestProtocol.GetFwBuildDate(out var fwBuildDate))
{
FwBuildDate = fwBuildDate;
}
if (RequestProtocol.GetMeterSize_mm(out var meterSize_mm))
{
MeterSize_mm = meterSize_mm;
}
if (RequestProtocol.GetFwGitHash(out var gitHash))
{
FwGitHash = gitHash;
}
if (RequestProtocol.GetSensorSerialNo(out var serialNumber))
{
SerialNumber = serialNumber;
}
if (RequestProtocol.GetUniqueId(out var pcbId))
{
PcbId = pcbId;
}
return IsLoggedOn;
}
/// <inheritdoc />
@@ -716,7 +795,7 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
}
else
{
WriteLog($"wait for measurement is starting");
WriteLog("Wait on measurement start");
}
ProcessStatus = ProcessState.WaitForMeasurement;
}
@@ -994,13 +1073,23 @@ namespace Xylem.Common.Hardware.WaterMeter.MagFlux.MagFluxCore
if (string.IsNullOrEmpty(CurrentActionText))
{
_logger = NLogHelper.CreateOrGetMultiLogger($"Slot:{Slot}", $"", "Meter", "MeterBase", "MeterBase");
if (RequestProtocol != null)
{
var path = NLogHelper.GetPath(_logger);
RequestProtocol.SetLogLocation(path);
}
}
else
{
var id = !string.IsNullOrEmpty(SerialNumber) ? $"SN{SerialNumber}" : $"PCB{PcbId}";
_logger = NLogHelper.CreateOrGetMultiLogger($"Slot:{Slot}_{id}_{CurrentActionText}", $"Slot", "Meter", "MeterBase", "MeterBase");
if (RequestProtocol != null)
{
var path = NLogHelper.GetPath(_logger);
RequestProtocol.SetLogLocation(path);
}
if (StreamingPort != null && StreamingPort is LedSerialPort)
{
var streamingIdent = $"LedRawData_Slot_{Slot}_{id}_{CurrentActionText}";