Prepared for LUT downloading, IrDASerialPort changed

This commit is contained in:
Thomas Wiedebusch
2022-03-02 14:24:47 +01:00
parent 835f242d95
commit 14eda41bdd
24 changed files with 2086 additions and 358 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,53 @@
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile.Consts
{
/// <summary>
/// Genesis meter lookup table update states
/// </summary>
public enum LutUpdateState
{
/// <summary>
/// Metrology lookup table update state Idle
/// </summary>
Idle,
/// <summary>
/// Single step failed
/// </summary>
StepFailed,
/// <summary>
/// Entire process failed
/// </summary>
UpdateFailed,
/// <summary>
/// Start initial FW update
/// </summary>
StartInitial,
/// <summary>
/// Repeat update
/// </summary>
RepeatLutUpdate,
/// <summary>
/// Upload lookup table file
/// </summary>
UploadLutFile,
/// <summary>
/// Download lookup table file
/// </summary>
DownloadLutFile,
/// <summary>
/// Compare lookup table file
/// </summary>
CompareLutFile,
/// <summary>
/// Download the CRC, the meter size and store calibration as preparation for the lookup table
/// </summary>
PrepareLutDownload,
/// <summary>
/// Erase lookup table file
/// </summary>
EraseLutFile,
/// <summary>
/// Delimiter for list
/// </summary>
LutUpdateStateListDelimiter
}
}
@@ -66,11 +66,13 @@
<Compile Include="..\..\..\..\.shared\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Consts\LutUpdateState.cs" />
<Compile Include="Consts\FwUpdateState.cs" />
<Compile Include="Consts\FwUpdateStateInfo.cs" />
<Compile Include="Consts\FwUpdateStateText.cs" />
<Compile Include="MeterFileRestore.cs" />
<Compile Include="MeterFilesEraseRestore.cs" />
<Compile Include="MeterLutUpdate.cs" />
<Compile Include="MeterFwUpdate.cs" />
<Compile Include="MeterFwUpdateCapability.cs" />
<Compile Include="MeterFwUpdateRuler.cs" />
@@ -0,0 +1,553 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xylem.Common.Hardware.WaterMeter.Genesis.Applications;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile.Consts;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile.Properties;
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore.Consts;
using Xylem.Common.Utils.ProcessExec;
using Xylem.Common.Utils.ProcessExec.EventArguments;
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
{
/// <summary>
/// Meter lookup table for metrology update procedure
/// </summary>
public class MeterLutUpdate : IProcessState
{
#region Variables
/// <summary>
/// Port scan result event for message dispatcher to caller
/// </summary>
public event EventHandler<ProcessExecEventArgs> OnProcessUpdate;
//disk and name of lookup file
private const String StrLutFileName = "1\\mettbl";
//disk and name of lookup file
private const String StrLutBackupFileName = "1\\mettbl_b";
// string as identifier for Genesis lookup table
private const String TablesFileMagic = "GEN_LUT";
private const String StrProcessLut = "Processing metrology lookup table";
//trigger update retries
private const Int32 RegisterWriteRetries = 2;
//update reties for files
private const Int32 FileWriteRetries = 0;
//retry of entire update procedure
private const Int32 UpdateProcedureRetries = 5;
//threshold to increase the file write timing
private const Int32 IncreaseTimeoutUpdateProcedureRetries = 4;
private Int32 _updateProcedureRetryCtr;
private GenesisMeter _genesisMeter;
private MeterFile _meterFile;
// Text for caller to inform about actual process being carried out
private String _actualOperation;
private LutUpdateState _lutUpdateState;
// Process counter for caller to monitor actual progress
private Int32 _processedBytesCtr;
private Int32 _overallBytesCtr;
// Look up table file
private Int32 _lutCrc;
private Byte _meterSize;
public FileApplications LutFile;
#endregion
#region Events
/// <summary>
/// Process update event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2020-Dec-10" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public virtual void ProcessUpdate_Event(Object sender, ProcessExecEventArgs e)
{
// copy text from overall messages to actual, because this is from sub-routine
OnProcessUpdate?.Invoke(this, new ProcessExecEventArgs(StrProcessLut,
OverallProcessCtrPercent, _actualOperation, e.OverallProcessPercent));
}
#endregion
#region Information
/// <summary>
/// Process counter in percent
/// </summary>
/// <remarks date="2019-Jun-20" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Double OverallProcessCtrPercent
{
get
{
var processFileBytes = _meterFile?.ProcessedBytesCtr ?? 0;
return 100.0 * (processFileBytes + _processedBytesCtr) /
(_overallBytesCtr > 0 ? _overallBytesCtr : 1);
}
}
/// <summary>
/// Single file process counter in percent
/// </summary>
/// <remarks date="2019-Jun-24" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Double SingleFileProcessCtrPercent => _meterFile?.ProcessCtrPercent ?? 0;
#endregion
#region CtorAssignment
/// <summary>
/// Ctor
/// </summary>
/// <param name="genesisMeter"></param>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public MeterLutUpdate(GenesisMeter genesisMeter)
{
if (genesisMeter == null) return;
AssignGenesis(genesisMeter);
}
/// <summary>
/// Assign new genesis
/// </summary>
/// <param name="genesisMeter"></param>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public void AssignGenesis(GenesisMeter genesisMeter)
{
_genesisMeter = genesisMeter;
_actualOperation = "";
LutFile = null;
_lutUpdateState = LutUpdateState.Idle;
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
if (_meterFile == null) return;
_meterFile.OnProcessUpdate -= ProcessUpdate_Event;
_meterFile = null;
}
#endregion
#region ProcessInformation
/// <summary>
/// Returns the actual file in process for update
/// </summary>
/// <returns></returns>
/// <remarks date="2019-Jun-20" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public String GetActualOperation()
{
return _actualOperation;
}
/// <summary>
/// Returns the LUT Update state
/// </summary>
/// <returns></returns>
/// <remarks date="2022-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public String GetLutUpdateStateOperation()
{
return _lutUpdateState == LutUpdateState.Idle ? "" : "Downloading...";
}
#endregion
#region Actions
/// <summary>
/// Try to re-establish the file system, therefor logout to close all open files
/// and login again.
/// </summary>
/// <remarks date="2019-Jun-21" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
/// <remarks date="2019-Jun-28" author="Thomas Wiedebusch">
/// - Enable auto-logon added
/// </remarks>
/// <remarks date="2019-Jul-03" author="Thomas Wiedebusch">
/// - Removed auto-logon
/// </remarks>
public void ReestablishMeterFileSystem()
{
_genesisMeter?.Logout();
_genesisMeter?.ReLogin();
}
private Boolean _stopUpdateProcess;
/// <summary>
/// Stop the update process
/// </summary>
public Boolean StopUpdateProcess
{
get => _stopUpdateProcess;
set
{
_stopUpdateProcess = value;
if (_meterFile != null)
{
_meterFile.StopProcess = value;
}
_lutUpdateState = LutUpdateState.UpdateFailed;
}
}
/// <summary>
/// Update meter lookup table
/// </summary>
/// <returns></returns>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Boolean UpdateMeterLut()
{
if (!UpdatePreparation())
{
return false;
}
_lutUpdateState = LutUpdateState.StartInitial;
while (_lutUpdateState != LutUpdateState.Idle && !StopUpdateProcess)
{
LutUpdateStateMachine();
}
return ExitPreparation(true);
}
/// <summary>
/// Upload lookup table file and compare with the loaded from file system
/// </summary>
/// <remarks date="2019-Jun-29" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Boolean VerifyLutFile()
{
if (!UpdatePreparation())
{
return false;
}
return ExitPreparation(true);
}
/// <summary>
/// Reset lookup table update process and all lists and counters
/// </summary>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public void ResetAll()
{
LutFile = null;
//remove the update stop action
StopUpdateProcess = false;
//reset all counters
_processedBytesCtr = 0;
_overallBytesCtr = 0;
_updateProcedureRetryCtr = 0;
//set state machine for automatic FW update
_lutUpdateState = LutUpdateState.Idle;
_actualOperation = "";
//logout/login/auto-login
ReestablishMeterFileSystem();
_genesisMeter?.Logout();
}
/// <summary>
/// Common update preparation and check
/// </summary>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Boolean UpdatePreparation()
{
_actualOperation = "";
_lutUpdateState = LutUpdateState.Idle;
_meterFile = new MeterFile(_genesisMeter);
if (_meterFile != null) _meterFile.OnProcessUpdate += ProcessUpdate_Event;
_genesisMeter?.ReLogin();
_genesisMeter?.SetProcessState(DisplayCodes.FwUpdateActive, false);
if (_genesisMeter == null || !_genesisMeter.IsLoggedOn)
{
return false;
}
//set default timeout for file write operation
_meterFile.SetDefaultFileWriteTimeout();
//remove the update stop action
StopUpdateProcess = false;
return true;
}
/// <summary>
/// Common exit routine
/// </summary>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Boolean ExitPreparation(Boolean returnValue)
{
if (_meterFile != null) _meterFile.OnProcessUpdate -= ProcessUpdate_Event;
_meterFile = null;
ReestablishMeterFileSystem();
_genesisMeter?.Logout();
if (!StopUpdateProcess)
{
return returnValue;
}
_actualOperation = Resources.StrWaiting;
return false;
}
#endregion
#region StateMachine
/// <summary>
/// State machine for metrology lookup table update process
/// </summary>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
private void LutUpdateStateMachine()
{
switch (_lutUpdateState)
{
case LutUpdateState.Idle:
break;
case LutUpdateState.StartInitial:
_updateProcedureRetryCtr = 0;
_lutUpdateState = LutUpdateState.PrepareLutDownload;
break;
case LutUpdateState.PrepareLutDownload:
_lutUpdateState = PrepareLutDownload() ? LutUpdateState.DownloadLutFile : LutUpdateState.StepFailed;
break;
case LutUpdateState.DownloadLutFile:
_lutUpdateState = DownloadLutFile() ? LutUpdateState.Idle : LutUpdateState.StepFailed;
break;
case LutUpdateState.StepFailed:
_processedBytesCtr = 0;
_overallBytesCtr = 0;
_lutUpdateState = _updateProcedureRetryCtr++ < UpdateProcedureRetries
? LutUpdateState.RepeatLutUpdate
: LutUpdateState.UpdateFailed;
break;
case LutUpdateState.RepeatLutUpdate:
_processedBytesCtr = 0;
_overallBytesCtr = 0;
_lutUpdateState = LutUpdateState.DownloadLutFile;
if (_updateProcedureRetryCtr >= IncreaseTimeoutUpdateProcedureRetries)
{
_meterFile?.SetExtremeFileWriteTimeout();
}
break;
case LutUpdateState.UpdateFailed:
_processedBytesCtr = 0;
_overallBytesCtr = 0;
StopUpdateProcess = true;
_lutUpdateState = LutUpdateState.Idle;
break;
case LutUpdateState.UploadLutFile:
break;
case LutUpdateState.CompareLutFile:
break;
case LutUpdateState.EraseLutFile:
break;
default:
_lutUpdateState = LutUpdateState.Idle;
break;
}
}
#endregion
#region LutFile
/// <summary>
/// Check the content of the metrology lookup table file:
/// - begins with "APP>",
/// - Application ID at position 0x28,
/// - CRC at position 0x04 LSB, 0x05 MSB
/// - Version at position 0x06 and 0x07 decimal
/// - Build upgrade meter file name
/// </summary>
/// <returns></returns>
/// <remarks date="2022-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
private static Boolean ValidateLutFile()
{
//var fileApp = new FileApplications();
//fileApp.AppId = fileApp.BinData[FileAppAppIdIndex];
//fileApp.Crc = (UInt16)(fileApp.BinData[FileAppCrcIndex] + (fileApp.BinData[FileAppCrcIndex + 1] << 8));
//fileApp.Version = (UInt32)((fileApp.BinData[FileAppVersionIndex] & 0x0F)
// + ((fileApp.BinData[FileAppVersionIndex] & 0xF0) >> 4) * 10
// + (fileApp.BinData[FileAppVersionIndex + 1] & 0x0F) * 100
// + ((fileApp.BinData[FileAppVersionIndex + 1] & 0xF0) >> 4) * 1000);
//fileApp.StrVersion = GenesisMeter.BuildFwVersionString(fileApp.BinData[FileAppVersionIndex + 1],
// fileApp.BinData[FileAppVersionIndex]);
//var fileAppStartId = Encoding.UTF8.GetString(fileApp.BinData.ToArray(), 0, 4);
////check update file start identifier
//if (fileAppStartId != StrFileAppStartId)
//{
// return false;
//}
////TODO THW check CRC
////TODO THW check upgrade file name against content to select a valid package
////the meter file name is needed for writing the file to the meter or reading it back
//fileApp.MeterFilename = StrUpgradeAppPartialFileName + $"{fileApp.AppId:X2}";
return true;
}
/// <summary>
/// Download the CRC, the meter size and store calibration as preparation for the lookup table
/// </summary>
/// <returns></returns>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
private Boolean PrepareLutDownload()
{
Boolean returnValue;
var retryCtr = 0;
//set extended timeout for trigger upgrade response delay
_genesisMeter.TransmitProtocol.SetResponseTimeout(5000);
do
{
returnValue = _genesisMeter.WriteRegister<Byte>(Register.Genesisflow.MeterSize, _meterSize);
returnValue &= _genesisMeter.WriteRegister<Int32>(Register.Genesisflow.LookupFileCrc, _lutCrc);
returnValue &= _genesisMeter.WriteRegister<Byte>(Register.Genesisflow.StoreCalibration, 1);
if (!returnValue)
{
ReestablishMeterFileSystem();
}
} while (!returnValue && retryCtr++ < RegisterWriteRetries && !StopUpdateProcess);
//set timeout back to default value
_genesisMeter.TransmitProtocol.SetDefaultResponseTimeout();
return returnValue && !StopUpdateProcess;
}
/// <summary>
/// Download the metrology lookup table files
/// </summary>
/// <returns></returns>
/// <remarks date="2022-Feb-16" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
private Boolean DownloadLutFile()
{
//counters for actual process information
_processedBytesCtr = 0;
_overallBytesCtr = 0;
var retryCtr = 0;
Boolean returnValue;
//write file portion and retry if not successful
do
{
returnValue = _meterFile.WriteMeterFile(StrLutFileName, LutFile.BinData.ToArray());
returnValue &= _meterFile.WriteMeterFile(StrLutBackupFileName, LutFile.BinData.ToArray());
if (!returnValue)
{
ReestablishMeterFileSystem();
}
} while (!returnValue && retryCtr++ < FileWriteRetries && !StopUpdateProcess);
returnValue &= retryCtr <= FileWriteRetries;
//update information for process bar
_processedBytesCtr += LutFile.BinData.Count;
return !StopUpdateProcess && returnValue;
}
/// <summary>
/// Load file from memory
/// </summary>
/// <param name="filePathName">to binary files</param>
/// <returns></returns>
/// <remarks date="2022-Feb-15" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Boolean LoadLutFile(String filePathName)
{
if (!File.Exists(filePathName))
{
return false;
}
LutFile = null;
LutFile = new FileApplications(filePathName)
{
BinData = new List<Byte>(File.ReadAllBytes(filePathName))
};
// validate the file
return ValidateLutFile();
}
#endregion
}
}
@@ -81,6 +81,7 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.Registers
public static readonly String StartHit = "GENESISFLOW_StartHit";
public static readonly String NumFirePulses = "GENESISFLOW_NumFirePulses";
public static readonly String LookupFileCrc = "GENESISFLOW_LookupFileCrc";
};