diff --git a/Common/Production/ProductionUiCordonel/App.config b/Common/Production/ProductionUiCordonel/App.config index 728a665b..c1e8fe2b 100644 --- a/Common/Production/ProductionUiCordonel/App.config +++ b/Common/Production/ProductionUiCordonel/App.config @@ -35,8 +35,8 @@ - - + + diff --git a/Common/Ui/GenesisToolBox/FrmQsTool.cs b/Common/Ui/GenesisToolBox/FrmQsTool.cs index ed0c2792..ead73fe8 100644 --- a/Common/Ui/GenesisToolBox/FrmQsTool.cs +++ b/Common/Ui/GenesisToolBox/FrmQsTool.cs @@ -119,8 +119,7 @@ namespace Xylem.Common.Ui.GenesisToolBox sbMSG.AppendLine(error); } - var size = RegisterConverter.ByteArrayToValue( - _currentGenesis.ReadRegister(Register.Genesisflow.MeterSize)); + var size = RegisterConverter.ByteArrayToValue(_currentGenesis.ReadRegister(Register.Genesisflow.MeterSize)); if (!GenesisStatusHandler.GetCordonelRequirementInfoFromDbByMeterSize(size, requirements, out error)) { diff --git a/Common/Ui/GenesisToolBox/frmRegisterStore.cs b/Common/Ui/GenesisToolBox/frmRegisterStore.cs index 49141871..87d8e5a2 100644 --- a/Common/Ui/GenesisToolBox/frmRegisterStore.cs +++ b/Common/Ui/GenesisToolBox/frmRegisterStore.cs @@ -1,4 +1,5 @@ -using Logic.ProductionToProductMapper.Cordonel; +using LaaPackages.Features.Cordonel.Models; +using Logic.ProductionToProductMapper.Cordonel; using Newtonsoft.Json; using NLog; using System; @@ -2274,15 +2275,70 @@ namespace Xylem.Common.Ui.GenesisToolBox this.SetBusy(true, "Recalibration is running"); + if (!this.CheckBatteryLifetime()) + { + this.SetBusy(false); + + return; + } + var p1Cal = default(int); var p2Cal = default(int); var p3Cal = default(int); + var logBuilder = new StringBuilder(); try { - p1Cal = RegisterConverter.ByteArrayToValue(this._currentGenesis.ReadRegister(Register.Genesisflow.CalFactor1)); - p2Cal = RegisterConverter.ByteArrayToValue(this._currentGenesis.ReadRegister(Register.Genesisflow.CalFactor2)); - p3Cal = RegisterConverter.ByteArrayToValue(this._currentGenesis.ReadRegister(Register.Genesisflow.CalFactor3)); + var caption = string.Empty; + var buttons = MessageBoxButtons.YesNo; + var icon = MessageBoxIcon.Question; + + int ReadRegister(string name) + { + var value = default(int); + + if (!this._currentGenesis.IsLoggedOn) + { + if (!this._currentGenesis.ReLogin()) + { + var answer = MessageBox.Show("Relogin failed!\n\nWould you try again?", caption, buttons, icon); + + if (answer == DialogResult.Yes) + { + value = ReadRegister(name); + } + } + } + + value = RegisterConverter.ByteArrayToValue(this._currentGenesis.ReadRegister(name)); + + if (value == 0) + { + var answer = MessageBox.Show($"Reading: {name} failed value is - {value}!\n\nWould you try again?", caption, buttons, icon); + + if (answer == DialogResult.Yes) + { + value = ReadRegister(name); + } + } + + return value; + } + + p1Cal = ReadRegister(Register.Genesisflow.CalFactor1); + p2Cal = ReadRegister(Register.Genesisflow.CalFactor2); + p3Cal = ReadRegister(Register.Genesisflow.CalFactor3); + + if (p1Cal == 0 || p2Cal == 0 || p3Cal == 0) + { + return; + } + + logBuilder.AppendLine(); + logBuilder.AppendLine($"Old values:"); + logBuilder.AppendLine($" - P1 CalFactor: {p1Cal}"); + logBuilder.AppendLine($" - P2 CalFactor: {p2Cal}"); + logBuilder.AppendLine($" - P3 CalFactor: {p3Cal}"); } catch (Exception eRead) { @@ -2359,7 +2415,7 @@ namespace Xylem.Common.Ui.GenesisToolBox if (httpResponse.IsSuccessStatusCode && int.TryParse($"{content}", out var rowsAffected)) { - // TODO: ??? + logBuilder.AppendLine($"... old values saved!"); } else { @@ -2383,6 +2439,18 @@ namespace Xylem.Common.Ui.GenesisToolBox if (!values.Any(x => x.P1Off == 0.30001)) { + var offset = values + .OrderByDescending(x => x.Date) + .FirstOrDefault() + .P1Off; + + if (offset > 0.2) + { + MessageBox.Show($"Recalibration is not required!"); + + this.SetBusy(false); + } + try { p1Cal = (int)Math.Round(p1Cal * (1 + 0.6 / 100), 0); @@ -2439,6 +2507,12 @@ namespace Xylem.Common.Ui.GenesisToolBox WriteRegister(Register.Genesisflow.DisplayPow10, -6); WriteRegister(Register.Genesisflow.SealDisplay, 1); WriteRegister(Register.Genesisflow.StoreConfiguration, 1); + + logBuilder.AppendLine(); + logBuilder.AppendLine($"New values:"); + logBuilder.AppendLine($" - P1 CalFactor: {p1Cal}"); + logBuilder.AppendLine($" - P2 CalFactor: {p2Cal}"); + logBuilder.AppendLine($" - P3 CalFactor: {p3Cal}"); } catch (Exception eWrite) { @@ -2495,6 +2569,8 @@ namespace Xylem.Common.Ui.GenesisToolBox if (newValuesWriten) { + logBuilder.AppendLine("... new values saved!"); + foreach (var row in this._dataTable.Rows) { if (row is DataRow dataRow && dataRow["Name"] is string registerName) @@ -2529,6 +2605,138 @@ namespace Xylem.Common.Ui.GenesisToolBox return; } + + MessageBox.Show(logBuilder.ToString(), $"{this._currentPcbId} - successfully recalibrated", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + + private bool CheckBatteryLifetime() + { + var genesisStatus = new GenesisStatus(); + var requirements = new CordonelRequirements(); + var sbMSG = new StringBuilder(); + var hasFehlern = false; + + if (!GenesisStatusHandler.GetCordonelRequirementInfoFromDb($"{_currentGenesis.PcbId}", requirements, out var error)) + { + MessageBox.Show($"Error loading Cordonel requirements: {error}"); + + var size = RegisterConverter.ByteArrayToValue(_currentGenesis.ReadRegister(Register.Genesisflow.MeterSize)); + + if (!GenesisStatusHandler.GetCordonelRequirementInfoFromDbByMeterSize(size, requirements, out error)) + { + MessageBox.Show($"Error loading Cordonel requirements: {error}"); + + requirements = new CordonelRequirements + { + QuiescentCurrent_uA = 119.8, + EstimatedProductionBatteryLoadPercent = 1, + RequiredLifeTimeYearsAssembly = 22, + MaxDrainedBatteryLoadPercentAssembly = 4, + MaxStorageMonths = 5 + }; + } + } + + genesisStatus.QuiescentCurrent_uA = requirements.QuiescentCurrent_uA; + genesisStatus.EstimatedProductionBatteryLoadPercent = requirements.EstimatedProductionBatteryLoadPercent; + + if (GenesisStatusHandler.BuildLifeTimeInformation(_currentGenesis, genesisStatus, out var message)) + { + if (!string.IsNullOrWhiteSpace(message)) + { + MessageBox.Show(message); + } + + sbMSG.AppendLine($"Pcb = {_currentGenesis.PcbId}"); + sbMSG.AppendLine($"POWERMON_TotalUsedSeconds = {genesisStatus.ExceededLifeTime_s}"); + sbMSG.AppendLine($"POWERMON_TotalUsedCharge = {genesisStatus.DrainedBatteryLoad_uAs}"); + sbMSG.AppendLine($"POWERMON_BatteryQuantity = {genesisStatus.BatteryQuantity}"); + sbMSG.AppendLine($"POWERMON_BatteryMilliAHrRating = {genesisStatus.InitialBatteryLoad_mAh}"); + sbMSG.AppendLine(); + sbMSG.AppendLine($"Actual storage months: {genesisStatus.StorageMonths:F1}"); + sbMSG.AppendLine($"Remaining life time in years: {genesisStatus.RemainingLifeTimeYears:F2}"); + sbMSG.AppendLine($"Totally drained battery load in %: {genesisStatus.DrainedBatteryLoadPercent:F2}"); + sbMSG.AppendLine(); + + if (genesisStatus.RemainingLifeTimeYears < requirements.RequiredLifeTimeYearsAssembly) + { + hasFehlern = true; + sbMSG.AppendLine($"{nameof(genesisStatus.RemainingLifeTimeYears)}: {genesisStatus.RemainingLifeTimeYears} < {requirements.RequiredLifeTimeYearsAssembly}"); + } + else + { + sbMSG.AppendLine($"Restlaufzeit i.O."); + } + + if (genesisStatus.AlternativeRequiredBatteryLoadPercent != null + && genesisStatus.AlternativeRequiredBatteryLoadPercent > requirements.MaxDrainedBatteryLoadPercentAssembly) + { + sbMSG.AppendLine($"{nameof(genesisStatus.AlternativeRequiredBatteryLoadPercent)}: {genesisStatus.AlternativeRequiredBatteryLoadPercent} > {requirements.MaxDrainedBatteryLoadPercentAssembly}"); + hasFehlern = true; + } + else + { + sbMSG.AppendLine($"Alternative Batterieverbrauch Montage i.O."); + } + + if (genesisStatus.DrainedBatteryLoadPercent > requirements.MaxDrainedBatteryLoadPercentAssembly) + { + sbMSG.AppendLine($"{nameof(genesisStatus.DrainedBatteryLoadPercent)}: {genesisStatus.DrainedBatteryLoadPercent} > {requirements.MaxDrainedBatteryLoadPercentAssembly}"); + hasFehlern = true; + } + else + { + sbMSG.AppendLine($"Maximaler Batterieverbrauch Montage i.O."); + } + + if (genesisStatus.StorageMonths > requirements.MaxStorageMonths) + { + hasFehlern = true; + sbMSG.AppendLine($"{nameof(genesisStatus.StorageMonths)}: {genesisStatus.StorageMonths} > {requirements.MaxStorageMonths}"); + } + else + { + sbMSG.AppendLine($"Maximallagerzeit i.O."); + } + + if (hasFehlern) + { + sbMSG.AppendLine($"Battery lifetime limits sind Übreschritten"); + } + else + { + sbMSG.AppendLine($"Battery lifetime limits sind OK"); + } + + sbMSG.AppendLine(); + sbMSG.AppendLine($"Preproduction max. drained battery load: {genesisStatus.AlternativeRequiredBatteryLoadPercent} %"); + sbMSG.AppendLine($"Meter reports drained battery load: {genesisStatus.DrainedBatteryLoadPercent} %"); + + if (hasFehlern) + { + sbMSG.AppendLine(); + sbMSG.AppendLine("YES - to ignore the error and continue, NO to cancel!"); + + hasFehlern = MessageBox.Show(sbMSG.ToString() + , $"Battery for {_currentGenesis.PcbId} has remaining life time of {genesisStatus.RemainingLifeTimeYears:F2} years" + , buttons: MessageBoxButtons.YesNo + , icon: MessageBoxIcon.Error) == DialogResult.No; + } + else + { + MessageBox.Show(sbMSG.ToString() + , $"Battery for {_currentGenesis.PcbId} has remaining life time of {genesisStatus.RemainingLifeTimeYears:F2} years" + , buttons: MessageBoxButtons.OK + , icon: MessageBoxIcon.Information); + } + + } + else + { + MessageBox.Show("Failed to get life time information", caption: null, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); + } + + return hasFehlern == false; } }