tbf/Results/Entities/helpers/TestRsltCalibFactorHelper.cs
Michal Buzik 52bd0d01ce Restore Genesis communication and Q3 calibration from special branches
A – Registration and execution
- Register GenesisCommunication Factory in the component list.
- Separate the Genesis form and sequence from iPerl communication.

B – Communication activities
- Restore initialization, connection, PCB reading, password and login.
- Include grouped login, mode switching and disconnection.
- Support processing up to 10 slots.

C1 – Input calibration factors
- Read three factors from Water meters / Text1–Text3.
- Validate integer values in the range 1–65535.
- Preserve the default of 15625 when all three fields are empty.

D – Q3 calibration
- Connect the Prepare Q3 → measurement → Write Q3 workflow.
- Add channel processing to FlyingStart and FlyingStartMassCollection.
- Reset previous measurement data and validate calculated factors.
- Mark factors as stored only after StoreCalibration succeeds.

E – Results and database
- Store calibration factors separately for each meter and channel.
- Add result entities, mappings and Q3 data.
- Extend DB.cs / EnsureSchema to create and update the schema.
- Preserve compatibility with the existing binary format.

Validation:
- Debug build and 18 tests passed.
- Simulated communication runs follow the same activity sequence.
- The complete Q3 workflow has not yet been verified on hardware.

Known limitation:
- An inherited mismatch in simulated responses and error propagation
  can produce an incorrect OK result; this change does not fix it.
2026-09-09 15:04:49 +02:00

118 lines
3.5 KiB
C#

using System.Collections.Generic;
using log4net;
using NHibernate;
namespace Results.Entities.helpers
{
public static class TestRsltCalibFactorHelper
{
private static readonly ILog log = LogManager.GetLogger(typeof(TestRsltCalibFactorHelper));
public static bool TableExists(ISession session)
{
try
{
session.CreateSQLQuery( "SELECT 1 FROM TestRsltCalibFactor LIMIT 1")
.UniqueResult();
return true;
}
catch
{
return false;
}
}
public static void DeleteByTestRsltIdNoTransaction(
ISession session,
int testRsltId)
{
log.DebugFormat("Deleting TestRsltCalibFactor records for TestRsltId: {0}", testRsltId);
session.CreateSQLQuery(@" DELETE FROM TestRsltCalibFactor WHERE TestRsltId = :testRsltId")
.SetParameter("testRsltId", testRsltId)
.ExecuteUpdate();
}
public static IList<TestRsltCalibFactor> GetByTestRsltId(
ISession session,
int testRsltId)
{
return session.QueryOver<TestRsltCalibFactor>()
.Where(x => x.TestRslt.Id == testRsltId)
.OrderBy(x => x.CalibFactorIndex).Asc
.List();
}
public static string Truncate(string value, int maxLength)
{
if (string.IsNullOrEmpty(value))
return string.Empty;
return value.Length <= maxLength
? value
: value.Substring(0, maxLength);
}
public static void CreateTableIfNotExists(ISession session)
{
if (TableExists(session))
return;
string sql;
if (DB.DbType == Common.DBType.MySql)
{
sql = @"
CREATE TABLE IF NOT EXISTS TestRsltCalibFactor (
Id INT NOT NULL AUTO_INCREMENT,
TestRsltId INT NOT NULL,
CalibFactorIndex INT NOT NULL,
BaseCalibFactor INT NOT NULL,
CalculatedCalibFactor INT NOT NULL,
IsCalibFactorValid BIT NOT NULL,
Stored BIT NOT NULL,
ErrorStr VARCHAR(240) NULL,
TimeStart DOUBLE NOT NULL,
TimeEnd DOUBLE NOT NULL,
CalibRawStart DOUBLE NOT NULL,
CalibRawEnd DOUBLE NOT NULL,
Error DOUBLE NOT NULL,
VolumeStart DOUBLE NOT NULL,
VolumeEnd DOUBLE NOT NULL,
PRIMARY KEY (Id),
INDEX IX_TestRsltCalibFactor_TestRsltId (TestRsltId),
CONSTRAINT FK_TestRsltCalibFactor_TestRslt
FOREIGN KEY (TestRsltId) REFERENCES TestRslt(Id)
ON DELETE CASCADE
);";
}
else
{
sql = @"
CREATE TABLE IF NOT EXISTS TestRsltCalibFactor (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
TestRsltId INTEGER NOT NULL,
CalibFactorIndex INTEGER NOT NULL,
BaseCalibFactor INTEGER NOT NULL,
CalculatedCalibFactor INTEGER NOT NULL,
IsCalibFactorValid INTEGER NOT NULL,
Stored INTEGER NOT NULL,
ErrorStr VARCHAR(240) NULL,
TimeStart DOUBLE NOT NULL,
TimeEnd DOUBLE NOT NULL,
CalibRawStart DOUBLE NOT NULL,
CalibRawEnd DOUBLE NOT NULL,
Error DOUBLE NOT NULL,
VolumeStart DOUBLE NOT NULL,
VolumeEnd DOUBLE NOT NULL,
FOREIGN KEY (TestRsltId) REFERENCES TestRslt(Id) ON DELETE CASCADE
);";
}
log.InfoFormat("Creating TestRsltCalibFactor table: {0}", sql);
session.CreateSQLQuery(sql).ExecuteUpdate();
}
}
}