From 9afffb28e068d7b52fd85f725eff625369a7649d Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Wed, 12 Feb 2025 14:58:27 +0100 Subject: [PATCH] stichproben nach linie --- .../CordonelAssemblyLine/Model/ExeConfig.cs | 11 +- .../Model/LeakViewModel.cs | 7 +- Common/Stichproben/Cordonel/Stichprobe.cs | 6 + .../Stichproben/Cordonel/StichprobeResult.cs | 3 + .../Cordonel/StichprobenDbContext.cs | 165 ++++++++++++------ .../{Stichproben.cs => StichprobenManager.cs} | 28 ++- .../Stichproben/Cordonel/StichprobenRule.cs | 4 + .../Stichproben/Cordonel/StichprobenRules.cs | 112 ------------ Common/Stichproben/Stichproben.csproj | 3 +- 9 files changed, 147 insertions(+), 192 deletions(-) rename Common/Stichproben/Cordonel/{Stichproben.cs => StichprobenManager.cs} (58%) delete mode 100644 Common/Stichproben/Cordonel/StichprobenRules.cs diff --git a/Common/CordonelAssemblyLine/Model/ExeConfig.cs b/Common/CordonelAssemblyLine/Model/ExeConfig.cs index 691817c0..a0c68f86 100644 --- a/Common/CordonelAssemblyLine/Model/ExeConfig.cs +++ b/Common/CordonelAssemblyLine/Model/ExeConfig.cs @@ -6,6 +6,10 @@ public class ExeConfig { + public static readonly string DllFilePath + = typeof(ExeConfig).Assembly.Location; + public static readonly ExeConfig Current = new ExeConfig(); + public int PickingSlot { get @@ -74,9 +78,8 @@ { return default(string); } - - var dll = typeof(ExeConfig).Assembly.Location; - var configuration = ConfigurationManager.OpenExeConfiguration(dll); + + var configuration = ConfigurationManager.OpenExeConfiguration(DllFilePath); var settings = configuration?.AppSettings?.Settings ?? new KeyValueConfigurationCollection(); if (!settings.AllKeys.Contains(key)) @@ -95,7 +98,7 @@ } var dll = typeof(ExeConfig).Assembly.Location; - var configuration = ConfigurationManager.OpenExeConfiguration(dll); + var configuration = ConfigurationManager.OpenExeConfiguration(DllFilePath); var settings = configuration?.AppSettings?.Settings ?? new KeyValueConfigurationCollection(); if (!settings.AllKeys.Contains(key)) diff --git a/Common/CordonelAssemblyLine/Model/LeakViewModel.cs b/Common/CordonelAssemblyLine/Model/LeakViewModel.cs index abdfe364..8ae6f8dd 100644 --- a/Common/CordonelAssemblyLine/Model/LeakViewModel.cs +++ b/Common/CordonelAssemblyLine/Model/LeakViewModel.cs @@ -144,7 +144,6 @@ namespace CordonelAssemblyLine.Model private Timer tmrAutoDetect; - private readonly Stichproben.Cordonel.Stichproben stichproben; public LeakViewModel(Int32 slotNR, Int32? stationID = null) { @@ -161,8 +160,6 @@ namespace CordonelAssemblyLine.Model $"2x10-7 { System.Environment.NewLine} " + $"2*10^-7 { System.Environment.NewLine} " + "0,0000002"; - - this.stichproben = new Stichproben.Cordonel.Stichproben(typeof(LeakViewModel).Assembly.Location); } public Boolean isHydraulicStaion() @@ -392,7 +389,9 @@ namespace CordonelAssemblyLine.Model if (int.TryParse($"{this.CurrentPcbID}", out var pcbId)) { CurrentMeter.Login(); - var result = this.stichproben.ShouldBePressureTested(pcbId); + + // jedes mal neu wegen die ExeConfig.Current.LeakStationId + var result = new StichprobenManager(ExeConfig.Current.LeakStationId).ShouldBePressureTested(pcbId); if (result.ShouldBePressureTested) { diff --git a/Common/Stichproben/Cordonel/Stichprobe.cs b/Common/Stichproben/Cordonel/Stichprobe.cs index 00457669..adfb005b 100644 --- a/Common/Stichproben/Cordonel/Stichprobe.cs +++ b/Common/Stichproben/Cordonel/Stichprobe.cs @@ -1,9 +1,13 @@ namespace Stichproben.Cordonel { + using System; + internal class Stichprobe { public int Id { get; set; } + public int LineId { get; set; } + public int PcbId { get; set; } public int Nummer { get; set; } @@ -13,5 +17,7 @@ public bool IstStichprobe { get; set; } public bool? IstDicht { get; set; } + + public DateTime Datum { get; set; } } } diff --git a/Common/Stichproben/Cordonel/StichprobeResult.cs b/Common/Stichproben/Cordonel/StichprobeResult.cs index 7869550e..ff4ab113 100644 --- a/Common/Stichproben/Cordonel/StichprobeResult.cs +++ b/Common/Stichproben/Cordonel/StichprobeResult.cs @@ -17,5 +17,8 @@ public int Number { get; } public bool ShouldBePressureTested { get; } + + public override string ToString() + => $"Test: {this.Number,5} Rule: {this.Rule,3}%/{this.Count} ShouldBePressureTested: {this.ShouldBePressureTested}"; } } \ No newline at end of file diff --git a/Common/Stichproben/Cordonel/StichprobenDbContext.cs b/Common/Stichproben/Cordonel/StichprobenDbContext.cs index e80d1924..85d507b6 100644 --- a/Common/Stichproben/Cordonel/StichprobenDbContext.cs +++ b/Common/Stichproben/Cordonel/StichprobenDbContext.cs @@ -2,73 +2,152 @@ { using LaaPackages.SqlClient; - using System; using System.Linq; internal class StichprobenDbContext { + private readonly int leakStationId; private readonly SqlConnection sqlConnection; - public StichprobenDbContext(string connectionString) + public StichprobenDbContext(int leakStationId) { - this.sqlConnection = SqlConnection.CreateSqlConnection(connectionString, error => - { - Console.WriteLine(error); - }); + this.leakStationId = leakStationId; + this.sqlConnection = SqlConnection.CreateSqlConnection("Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;"); } - public void AddNew(Stichprobe stichprobe) + public void AddStichprobe(Stichprobe stichprobe) => this.sqlConnection .CreateCommand($@" INSERT INTO Stichproben ( PcbId + , LineId , Nummer , Prozentsatz , IstStichprobe) VALUES (@{nameof(stichprobe.PcbId)} + , @{nameof(this.leakStationId)} , @{nameof(stichprobe.Nummer)} , @{nameof(stichprobe.Prozentsatz)} , @{nameof(stichprobe.IstStichprobe)})") .SetParameter(nameof(stichprobe.PcbId), stichprobe.PcbId) + .SetParameter(nameof(this.leakStationId), this.leakStationId) .SetParameter(nameof(stichprobe.Nummer), stichprobe.Nummer) .SetParameter(nameof(stichprobe.Prozentsatz), stichprobe.Prozentsatz) .SetParameter(nameof(stichprobe.IstStichprobe), stichprobe.IstStichprobe) .ExecuteNonQuery(); - public Stichprobe GetLastOrDefault() + public StichprobenRule FindRuleOrMin(int prozentsatz) => this.sqlConnection - .CreateCommand(@" + .CreateCommand($@" + SELECT TOP 1 + Id + , LineId + , [%] + , Count + FROM StichprobenRegeln + WHERE LineId = @{nameof(this.leakStationId)} + AND ([%] = @{nameof(prozentsatz)} OR 1 = 1) + ORDER BY [%]") + .SetParameter(nameof(this.leakStationId), this.leakStationId) + .SetParameter(nameof(prozentsatz), prozentsatz) + .FirstOrDefault(x => new StichprobenRule + { + Id = x.GetValue(0), + LineId = x.GetValue(1), + Percent = x.GetValue(2), + Count = x.GetValue(3), + }); + + public StichprobenRule GetMaxRule() + => this.sqlConnection + .CreateCommand($@" + SELECT TOP 1 + Id + , LineId + , [%] + , Count + FROM StichprobenRegeln + WHERE LineId = @{nameof(this.leakStationId)} + ORDER BY [%] DESC") + .SetParameter(nameof(this.leakStationId), this.leakStationId) + .FirstOrDefault(x => new StichprobenRule + { + Id = x.GetValue(0), + LineId = x.GetValue(1), + Percent = x.GetValue(2), + Count = x.GetValue(3), + }); + + public StichprobenRule GetMinRule() + => this.sqlConnection + .CreateCommand($@" + SELECT TOP 1 + Id + , LineId + , [%] + , Count + FROM StichprobenRegeln + WHERE LineId = @{nameof(this.leakStationId)} + ORDER BY [%]") + .SetParameter(nameof(this.leakStationId), this.leakStationId) + .FirstOrDefault(x => new StichprobenRule + { + Id = x.GetValue(0), + LineId = x.GetValue(1), + Percent = x.GetValue(2), + Count = x.GetValue(3), + }); + + public StichprobenRule GetNextRuleOrMin(int prozentsatz) + => this.sqlConnection + .CreateCommand($@" + SELECT TOP 1 + Id + , LineId + , [%] + , Count + FROM StichprobenRegeln + WHERE LineId = @{nameof(this.leakStationId)} + AND ([%] < @{nameof(prozentsatz)} OR 1 = 1) + ORDER BY [%]") + .SetParameter(nameof(this.leakStationId), this.leakStationId) + .SetParameter(nameof(prozentsatz), prozentsatz) + .FirstOrDefault(x => new StichprobenRule + { + Id = x.GetValue(0), + LineId = x.GetValue(1), + Percent = x.GetValue(2), + Count = x.GetValue(3), + }); + + public Stichprobe GetLastStichprobeOrDefault() + => this.sqlConnection + .CreateCommand($@" SELECT Id , PcbId + , LineId , Nummer , Prozentsatz , IstStichprobe , IstDicht + , Datum FROM Stichproben + WHERE LineId = @{nameof(this.leakStationId)} ORDER BY Id DESC") + .SetParameter(nameof(this.leakStationId), this.leakStationId) .FirstOrDefault(x => new Stichprobe { Id = x.GetValue(0), - PcbId = x.GetValue(1), - Nummer = x.GetValue(2), - Prozentsatz = x.GetValue(3), - IstStichprobe = x.GetValue(4), - IstDicht = x.GetValue(5), + LineId = x.GetValue(1), + PcbId = x.GetValue(2), + Nummer = x.GetValue(3), + Prozentsatz = x.GetValue(4), + IstStichprobe = x.GetValue(5), + IstDicht = x.GetValue(6), }); - public void SetPressureTestPassed(int id, bool? pressureTestPassed) + public bool IsAnyStichprobeUndicht() => this.sqlConnection - .CreateCommand($@" - UPDATE Stichproben - SET IstDicht = @{nameof(pressureTestPassed)} - WHERE Id = @{nameof(id)}") - .SetParameter(nameof(pressureTestPassed), pressureTestPassed) - .SetParameter(nameof(id), id) - .ExecuteNonQuery(); - - public bool IsAnyUndicht() - { - var missingResults = this.sqlConnection .CreateCommand($@" UPDATE Stichproben SET IstDicht = d.Dicht @@ -76,34 +155,10 @@ FROM Stichproben AS s LEFT JOIN Druckpruefung AS d ON s.PcbId = d.FabNr - WHERE s.IstDicht IS NULL") - .ExecuteReader(x => x.GetValue(0)); - - return missingResults?.Any(x => x == false) ?? false; - } - - //public bool? PressureTestPassed(int pcbId) - //{ - // var missingResults = this.sqlConnection - // .CreateCommand($@" - // UPDATE Stichproben - // SET IstDicht = d.Dicht - // OUTPUT inserted.IstDicht - // FROM Stichproben AS s - // LEFT JOIN Druckpruefung AS d - // ON s.PcbId = d.FabNr - // WHERE s.IstDicht IS NULL") - // .SetParameter(nameof(pcbId), pcbId) - // .ExecuteReader(x => x.GetValue(0)); - - // return this.sqlConnection - // .CreateCommand($@" - // SELECT TOP 1 Dicht - // FROM Druckpruefung - // WHERE FabNr = @{nameof(pcbId)} - // ORDER BY Pruefgang DESC") - // .SetParameter(nameof(pcbId), pcbId) - // .FirstOrDefault(x => x.GetValue(0)); - //} + WHERE s.LineId = @{nameof(this.leakStationId)} + AND s.IstDicht IS NULL") + .SetParameter(nameof(this.leakStationId), this.leakStationId) + .ExecuteReader(x => x.GetValue(0)) + .Any(x => x == false); } } diff --git a/Common/Stichproben/Cordonel/Stichproben.cs b/Common/Stichproben/Cordonel/StichprobenManager.cs similarity index 58% rename from Common/Stichproben/Cordonel/Stichproben.cs rename to Common/Stichproben/Cordonel/StichprobenManager.cs index 555f0eeb..a936cddf 100644 --- a/Common/Stichproben/Cordonel/Stichproben.cs +++ b/Common/Stichproben/Cordonel/StichprobenManager.cs @@ -1,27 +1,23 @@ namespace Stichproben.Cordonel { - public class Stichproben + public class StichprobenManager { - private readonly StichprobenRules stichprobenRules; private readonly StichprobenDbContext stichprobenDb; - public Stichproben(string assemblyLocation) - { - this.stichprobenRules = new StichprobenRules(assemblyLocation); - this.stichprobenDb = new StichprobenDbContext("Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;"); - } + public StichprobenManager(int leakStationId) + => this.stichprobenDb = new StichprobenDbContext(leakStationId); public StichprobeResult ShouldBePressureTested(int pcbId) { - var isAnyUndicht = this.stichprobenDb.IsAnyUndicht(); - var previousStichprobe = this.stichprobenDb.GetLastOrDefault(); + var isAnyUndicht = this.stichprobenDb.IsAnyStichprobeUndicht(); + var previousStichprobe = this.stichprobenDb.GetLastStichprobeOrDefault(); if (previousStichprobe is null) { previousStichprobe = new Stichprobe(); } - var stichprobenRule = this.stichprobenRules.Min(); + var stichprobenRule = this.stichprobenDb.GetMinRule(); var currentStichprobe = new Stichprobe { PcbId = pcbId @@ -29,24 +25,26 @@ if (isAnyUndicht == false) { - stichprobenRule = this.stichprobenRules.FindRuleOrMin(previousStichprobe.Prozentsatz); + stichprobenRule = this.stichprobenDb.FindRuleOrMin(previousStichprobe.Prozentsatz); if (previousStichprobe.Nummer >= stichprobenRule.Count) { - stichprobenRule = this.stichprobenRules.GetNext(previousStichprobe.Prozentsatz); + stichprobenRule = this.stichprobenDb.GetNextRuleOrMin(previousStichprobe.Prozentsatz); } - else if (previousStichprobe.Nummer != 0) + + if (previousStichprobe.Nummer != 0) { currentStichprobe.Nummer = previousStichprobe.Nummer + 1; } } else { - stichprobenRule = this.stichprobenRules.Max(); + stichprobenRule = this.stichprobenDb.GetMaxRule(); } var percent = stichprobenRule.Percent; var moduloDevider = 100 / percent; + currentStichprobe.Prozentsatz = percent; currentStichprobe.IstStichprobe = currentStichprobe.Nummer % moduloDevider == 0; @@ -57,7 +55,7 @@ if (previousStichprobe.PcbId != currentStichprobe.PcbId) { - this.stichprobenDb.AddNew(currentStichprobe); + this.stichprobenDb.AddStichprobe(currentStichprobe); } return new StichprobeResult(currentStichprobe.IstStichprobe, currentStichprobe.Nummer, stichprobenRule.Percent, stichprobenRule.Count); diff --git a/Common/Stichproben/Cordonel/StichprobenRule.cs b/Common/Stichproben/Cordonel/StichprobenRule.cs index b15ee02d..a4e63977 100644 --- a/Common/Stichproben/Cordonel/StichprobenRule.cs +++ b/Common/Stichproben/Cordonel/StichprobenRule.cs @@ -2,6 +2,10 @@ { internal class StichprobenRule { + public int Id { get; set; } + + public int LineId { get; set; } + public int Percent { get; set; } public int Count { get; set; } diff --git a/Common/Stichproben/Cordonel/StichprobenRules.cs b/Common/Stichproben/Cordonel/StichprobenRules.cs deleted file mode 100644 index 42256769..00000000 --- a/Common/Stichproben/Cordonel/StichprobenRules.cs +++ /dev/null @@ -1,112 +0,0 @@ -namespace Stichproben.Cordonel -{ - using System; - using System.Collections.Generic; - using System.Configuration; - using System.Linq; - using System.Text.RegularExpressions; - - internal class StichprobenRules - { - private readonly List rules; - - public StichprobenRules(string assemblyLocation) - { - this.rules = new List(); - - this.LoadOrCreateRules(assemblyLocation); - } - - public void Add(int percent, int count) - { - var rule = new StichprobenRule - { - Percent = percent, - Count = count - }; - - var rulesCount = this.rules.Count; - var insertIndex = default(int); - - for (; insertIndex < rulesCount; insertIndex++) - { - var current = this.rules[insertIndex]; - - if (percent >= current.Percent) - { - break; - } - } - - this.rules.Insert(insertIndex, rule); - } - - public StichprobenRule FindRuleOrMin(int percent) - => this.rules.FirstOrDefault(x => x.Percent == percent) - ?? this.rules.LastOrDefault(); - - public StichprobenRule GetNext(int percent) - { - var stichprobenRule = this.Min(); - - foreach (var rule in this.rules) - { - if (rule.Percent < percent) - { - stichprobenRule = rule; - - break; - } - } - - return stichprobenRule; - } - - public StichprobenRule Min() - => this.rules.LastOrDefault(); - - public StichprobenRule Max() - => this.rules.FirstOrDefault(); - - private void LoadOrCreateRules(string assemblyLocation) - { - var configuration = ConfigurationManager.OpenExeConfiguration(assemblyLocation); - var settings = configuration.AppSettings.Settings; - var stichprobenRulesKeys = settings - .AllKeys - .Where(x => Regex.IsMatch($"{x}", $@"{nameof(Stichprobe)}\.(?\d+)")); - - if (stichprobenRulesKeys.Any()) - { - foreach (var key in stichprobenRulesKeys) - { - var tokens = key.Split(new [] { '.' }, StringSplitOptions.RemoveEmptyEntries); - - if (!int.TryParse($"{tokens[1]}", out var percent)) - { - percent = 5; - } - - if (!int.TryParse($"{settings[key].Value}", out var count)) - { - count = 100; - } - - this.Add(percent, count); - } - } - else - { - settings.Add($"{nameof(Stichprobe)}.100", "25"); - settings.Add($"{nameof(Stichprobe)}.50", "50"); - settings.Add($"{nameof(Stichprobe)}.10", "100"); - settings.Add($"{nameof(Stichprobe)}.5", "100"); - - configuration.Save(ConfigurationSaveMode.Modified); - ConfigurationManager.RefreshSection("appSettings"); - - this.LoadOrCreateRules(assemblyLocation); - } - } - } -} diff --git a/Common/Stichproben/Stichproben.csproj b/Common/Stichproben/Stichproben.csproj index fcf6e6cf..448656e8 100644 --- a/Common/Stichproben/Stichproben.csproj +++ b/Common/Stichproben/Stichproben.csproj @@ -45,10 +45,9 @@ - + -