diff --git a/Common/CommonConsole/Program.cs b/Common/CommonConsole/Program.cs index d458b207..0dee9096 100644 --- a/Common/CommonConsole/Program.cs +++ b/Common/CommonConsole/Program.cs @@ -1,6 +1,6 @@ namespace CommonConsole { - using Common.Logic.Extensions.SQL; + using Common.Hardware.WaterMeter.OmniPlus; using System; using System.Collections.Generic; @@ -10,30 +10,7 @@ { public static void Main() { - SQLConnection - .CreateSQLConnection("Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;") - .CreateCommand(@" - SELECT CONVERT(VARBINARY(8), '0x' + REPLACE(x.Address, '-', ''), 1) - , CONVERT(VARBINARY(256), '0x' + REPLACE(x.Value, '-', ''), 1) - FROM (SELECT rh.Address - , rh.Value - , ROW_NUMBER() OVER (PARTITION BY rh.Address ORDER BY rh.date DESC) AS Row - FROM GenesisMeterRegisterHistory AS rh - WHERE rh.PcbId = @pcbId) AS x - WHERE x.Row = 1 - ORDER BY x.Address") - .SetParameter("pcbId", "241330796") - .ExecuteReader(reader => - { - var address = reader.GetValue(0); - if (address.Length == 2) - { - var value = reader.GetValue(1); - - Console.WriteLine($"App: {address[0], 5:X2} Register: {address[1], 5:X2} Data: {BitConverter.ToString(value)}"); - } - }); } public class Cordonel : Dictionary @@ -712,7 +689,21 @@ - +//TX|01-01-01-01 +//TX|9B-33-07-0D-0B-08-FD-03-00-00-E1-84-06-00-02-8B-00-54-25 +//TX|01-01-01-01 +//TX|9B-33-06-0D-0A-08-FD-67-00-E8-03-25-00-02-93-59-12 +//RX|9B-43-07-0D-0B-00-01-00-E8-03-25-00-00-00-01-29-00-33-5A +//TX|01-01-01-01 +//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F +//TX|01-01-01-01 +//TX|9B-33-06-0D-0A-08-FD-67-00-06-00-1E-00-01-A7-0C-65 +//TX|01-01-01-01 +//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F +//TX|01-01-01-01 +//TX|9B-33-06-0D-0A-08-FD-67-00-05-00-25-00-01-AD-CC-4A +//TX|01-01-01-01 +//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs index 7f595268..280881e2 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/IrdaSerialPort.cs @@ -8,27 +8,22 @@ { } - public override Byte[] Write(params Byte[] data) + public override Byte[] Write(params Byte[] bytes) { - var _data = new Byte[0]; + var plainBytes = new Byte[0]; - if (data.TryParseToIrda(out var bytes)) + if (bytes.TryParseToIrda(out var irdaBytes)) { var repeatsCount = 3; for (var i = 0; i < repeatsCount; i++) { - this.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4); - this.Write(bytes, 0, bytes.Length); - - if (this.TXLoggingEnabled) - { - this.LogTRX(bytes); - } + this.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4, 25); + this.Write(irdaBytes, 0, irdaBytes.Length); var answerd = false; var buffer = new Byte[0]; - var readsCount = 3; + var readsCount = 100; while (readsCount-- >= 0) { @@ -39,7 +34,7 @@ Array.Resize(ref buffer, bufferLength + bytesLength); Array.Copy(_bytes, 0, buffer, bufferLength, bytesLength); - if (buffer.TryParseFromIrda(out _data)) + if (buffer.TryParseFromIrda(out plainBytes)) { answerd = true; @@ -59,15 +54,10 @@ } else { - this.LogMessage($"Cannot convert: {BitConverter.ToString(bytes)} - to IRDA message."); + this.LogMessage($"Cannot convert: {BitConverter.ToString(irdaBytes)} - to IRDA message."); } - if (this.RXLoggingEnabled) - { - this.LogTRX(_data); - } - - return _data; + return plainBytes; } } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs index 5da34d03..f1ebd560 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortBase.cs @@ -13,32 +13,23 @@ private readonly SerialPortSettings settings; public SerialPortBase(SerialPortSettings settings) - => this.settings = settings; + { + this.settings = settings; + this.PortName = settings.PortName; + } public event Action Logging; - public event Action TRXLogging; - public String PortName - => this.settings?.PortName; + public String PortName { get; } - public Boolean IsOpen - => serialPorts.TryGetValue(this.PortName, out var serialPort) - ? serialPort?.IsOpen ?? default(Boolean) - : default(Boolean); - - protected Int32 ReadBufferSize - => serialPorts.TryGetValue(this.PortName, out var serialPort) - ? serialPort?.ReadBufferSize ?? default(Int32) - : default(Int32); - - protected Boolean RXLoggingEnabled => this.settings.RXLoggingEnabled; - - protected Boolean TXLoggingEnabled => this.settings.TXLoggingEnabled; + public Int32 ReadSize { get; private set; } public virtual void Close() { try { + this.ReadSize = default(Int32); + if (serialPorts.TryRemove(this.PortName, out var serialPort)) { if (serialPort != null) @@ -47,6 +38,7 @@ serialPort.Dispose(); serialPort = null; + this.LogMessage($"Disconnected: {this}"); } else @@ -54,10 +46,6 @@ this.LogMessage($"{this.PortName} is null!"); } } - else - { - this.LogMessage($"{this.PortName} dose not exists!"); - } } catch (Exception e) { @@ -67,40 +55,38 @@ public virtual void EnsureConnected() { - if (!this.IsOpen) + if (!this.TryGetSerialPort(out var serialPort)) { - this.Close(); this.Open(); } } public virtual void Open() { + this.Close(); + try { - if (serialPorts.TryGetValue(this.PortName, out var serialPort)) - { - serialPort.Close(); - } - else - { - this.LogMessage($"{this.PortName} dose not exists!"); - } + this.ReadSize = default(Int32); - serialPort = new SerialPort + var serialPort = new SerialPort { BaudRate = this.settings.BaudRate, DataBits = this.settings.DataBits, Parity = this.settings.Parity, - PortName = this.PortName, - StopBits = this.settings.StopBits, - ReadTimeout = this.settings.RWTimeout, + PortName = this.settings.PortName, + StopBits = this.settings.StopBits }; - if (serialPorts.TryAdd(this.PortName, serialPort)) + if (serialPorts.TryAdd(serialPort.PortName, serialPort)) { serialPort.Open(); + if (serialPort.IsOpen) + { + this.ReadSize = serialPort.ReadBufferSize; + } + this.LogMessage($"Connected: {this}"); } else @@ -136,41 +122,21 @@ protected void LogMessage(String message) => this.Logging?.Invoke(message); - public void LogTRX(Byte[] data) => this.TRXLogging?.Invoke(BitConverter.ToString(data)); - - public void LogTX(Byte[] data) => this.TRXLogging?.Invoke($"TX|{BitConverter.ToString(data)}"); - - public void LogRX(Byte[] data) => this.TRXLogging?.Invoke($"RX|{BitConverter.ToString(data)}"); - - protected Byte[] Read(Int32 sleep = 100) + protected Byte[] Read(Int32 sleep = 1) { Thread.Sleep(sleep); var bytes = new Byte[0]; - if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen) + if (this.TryGetSerialPort(out var serialPort)) { - var size = this.ReadBufferSize; - var buffer = new Byte[size]; - try { - if (serialPort.IsOpen) - { - var copyLength = serialPort.Read(buffer, 0, size); + var buffer = new Byte[this.ReadSize]; + var length = serialPort.Read(buffer, 0, this.ReadSize); - Array.Resize(ref bytes, copyLength); - Array.Copy(buffer, 0, bytes, 0, copyLength); - - if (this.RXLoggingEnabled) - { - this.LogMessage(BitConverter.ToString(bytes)); - } - } - else - { - this.LogMessage($"{this.PortName} is closed!"); - } + Array.Resize(ref bytes, length); + Array.Copy(buffer, 0, bytes, 0, length); } catch (Exception e) { @@ -187,16 +153,11 @@ protected void Write(Byte[] bytes, Int32 start, Int32 length, Int32 sleep = 1) { - if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen) + if (this.TryGetSerialPort(out var serialPort)) { try { serialPort.Write(bytes, start, length); - - if (this.RXLoggingEnabled) - { - this.LogMessage(BitConverter.ToString(bytes)); - } } catch (Exception e) { @@ -210,5 +171,9 @@ Thread.Sleep(sleep); } + + private Boolean TryGetSerialPort(out SerialPort serialPort) + => serialPorts.TryGetValue(this.PortName, out serialPort) + && serialPort?.IsOpen == true; } } diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortExtensions.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortExtensions.cs index 7c274e26..38529c71 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortExtensions.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortExtensions.cs @@ -162,23 +162,43 @@ { return false; } - - var start = Array.IndexOf(bytes, IRDA_START); - - if (start < 0) - { - return false; - } - - var direction = Array.IndexOf(bytes, IRDA_REG2PC, start + 1); - - if (direction < 0) - { - return false; - } var bytesLength = bytes.Length; - var lengthIndex = direction + 1; + var directionIX = -1; + var startIX = 0; + + while (bytesLength > startIX) + { + startIX = Array.IndexOf(bytes, IRDA_START, startIX); + + if (startIX < 0) + { + break; + } + + directionIX = Array.IndexOf(bytes, IRDA_REG2PC, ++startIX); + + if (directionIX > 0) + { + break; + } + else + { + directionIX = Array.IndexOf(bytes, (Byte)0x82, startIX++); + + if (directionIX >= 0) + { + directionIX = -1; + } + } + } + + if (startIX < 0) + { + return false; + } + + var lengthIndex = directionIX + 1; if (lengthIndex >= bytesLength) { diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortSettings.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortSettings.cs index d23fe8af..a2690357 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortSettings.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/SerialPortSettings.cs @@ -13,11 +13,5 @@ public String PortName { get; set; } public SerialPortStopBits StopBits { get; set; } - - public Int32 RWTimeout { get; set; } = 3000; - - public Boolean RXLoggingEnabled { get; set; } - - public Boolean TXLoggingEnabled { get; set; } } } \ No newline at end of file diff --git a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/UniProSerialPort.cs b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/UniProSerialPort.cs index 0d77321b..bc48f7c1 100644 --- a/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/UniProSerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports/UniProSerialPort.cs @@ -20,11 +20,6 @@ { this.Write(bytes, 0, bytes.Length, 50); - if (this.TXLoggingEnabled) - { - this.LogTRX(bytes); - } - var answerd = false; var buffer = new Byte[0]; var readsCount = 3; @@ -61,11 +56,6 @@ this.LogMessage($"Cannot convert: {BitConverter.ToString(bytes)} - to UniPro message."); } - if (this.RXLoggingEnabled) - { - this.LogTRX(_data); - } - return _data; } } diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs index b4a83692..6b75f02b 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/IrdaConnection.cs @@ -17,26 +17,26 @@ StopBits = SerialPortStopBits.One, }; this.serialPortBase = new IrdaSerialPort(settings); + this.serialPortBase.Logging += this.LogText; } public override Byte[] Send(params Byte[] bytes) { this.serialPortBase.EnsureConnected(); - if (bytes.TryParseToUI1236(out var _bytes)) + var plainBytes = new Byte[] { }; + + if (bytes.TryParseToUI1236(out var ui1236Request)) { - var __bytes = this.serialPortBase.Write(_bytes); - - if (__bytes.TryParseFromUI1236(out var data)) - { - this.LogTX(bytes); - this.LogRX(data); - - return data; - } + this.serialPortBase + .Write(ui1236Request) + .TryParseFromUI1236(out plainBytes); } - return _bytes; + this.LogText($"TX|{BitConverter.ToString(bytes)}"); + this.LogText($"RX|{BitConverter.ToString(plainBytes)}"); + + return plainBytes; } } } diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/OmniConnection.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/OmniConnection.cs index 1cb17966..13aa03c8 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/OmniConnection.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/OmniConnection.cs @@ -11,13 +11,17 @@ { private SerialPortConnection connection; + public StringBuilder Telegrams { get; private set; } + public void Disconnect() => this.connection.Disconnect(); public void UseIrda(String portName) { this.connection?.Disconnect(); + this.Telegrams = new StringBuilder(); this.connection = new IrdaConnection(portName); + this.connection.TRXLogging += this.Logging; this.connection.Connect(); } @@ -32,10 +36,15 @@ { this.connection?.Disconnect(); + this.Telegrams = new StringBuilder(); this.connection = new UniProConnection(portName); + this.connection.TRXLogging += this.Logging; this.connection.Connect(); } + private void Logging(String text) + => this.Telegrams?.AppendLine(text); + public Byte[] Send(params Byte[] bytes) => this.connection.Send(bytes); @@ -81,8 +90,6 @@ return new ReadingDigits(response); } - public String GetRecords() => this.connection.GetRecords(); - public ReadingUnits ViewReadingUnits() { var response = this.connection.Send(VIEW_READING_UNITS); @@ -412,8 +419,6 @@ return new FeatureResponse(response); } - public void StartRecording() => this.connection.StartRecording(); - public FactorySeal FactorySeal(Boolean? seal = null) { var request = default(Byte[]); diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Programming/ProgrammingState.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Programming/ProgrammingState.cs index 3e710175..a9c7f1a3 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Programming/ProgrammingState.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/Programming/ProgrammingState.cs @@ -13,17 +13,9 @@ public OMNI OMNI { get; set; } - public void ConnectToIrda() - { - this.Connection.UseIrda(this.PortName); - this.Connection.StartRecording(); - } + public void ConnectToIrda() => this.Connection.UseIrda(this.PortName); - public void ConnectToUniPro() - { - this.Connection.UseUniPro(this.PortName); - this.Connection.StartRecording(); - } + public void ConnectToUniPro() => this.Connection.UseUniPro(this.PortName); public void ViewSystemParameters() { diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/SerialPortConnection.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/SerialPortConnection.cs index 9a18d1e7..44c61a9c 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/SerialPortConnection.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/SerialPortConnection.cs @@ -3,38 +3,21 @@ using Common.Hardware.Interfaces.Ports; using System; - using System.Text; internal abstract class SerialPortConnection { - private readonly StringBuilder records = new StringBuilder(); - protected SerialPortBase serialPortBase; + public event Action TRXLogging; + public void Connect() => this.serialPortBase.Open(); public void Disconnect() => this.serialPortBase.Close(); - public String GetRecords() - { - var records = this.records.ToString(); - - this.records.Clear(); - - return records; - } - public abstract Byte[] Send(params Byte[] request); - public void StartRecording() - { - this.records.Clear(); + protected void LogTRX(params Byte[] bytes) => this.LogText(BitConverter.ToString(bytes)); - this.serialPortBase.TRXLogging += text => this.records.AppendLine(text); - } - - protected void LogTX(Byte[] bytes) => this.serialPortBase.LogTX(bytes); - - protected void LogRX(Byte[] bytes) => this.serialPortBase.LogRX(bytes); + protected void LogText(String text) => this.TRXLogging?.Invoke(text); } } diff --git a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/UniProConnection.cs b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/UniProConnection.cs index da13523f..86773b14 100644 --- a/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/UniProConnection.cs +++ b/Common/Hardware/WaterMeter/OmniPlus/Common.Hardware.WaterMeter.OmniPlus/UniProConnection.cs @@ -14,11 +14,10 @@ DataBits = SerialPortDataBits.X8, Parity = SerialPortParity.None, PortName = portName, - RXLoggingEnabled = true, StopBits = SerialPortStopBits.One, - TXLoggingEnabled = true, }; this.serialPortBase = new UniProSerialPort(settings); + this.serialPortBase.Logging += this.LogText; } public override Byte[] Send(params Byte[] bytes) @@ -27,8 +26,8 @@ var response = this.serialPortBase.Write(bytes); - this.LogTX(bytes); - this.LogRX(response); + this.LogText($"TX|{BitConverter.ToString(bytes)}"); + this.LogText($"RX|{BitConverter.ToString(response)}"); return response; } diff --git a/Common/OmniPlus/OmniPlus/Inspection/OmniMeter.cs b/Common/OmniPlus/OmniPlus/Inspection/OmniMeter.cs index 9201c4e6..5ee9fdc9 100644 --- a/Common/OmniPlus/OmniPlus/Inspection/OmniMeter.cs +++ b/Common/OmniPlus/OmniPlus/Inspection/OmniMeter.cs @@ -2,9 +2,8 @@ { using Common.Hardware.WaterMeter.OmniPlus; using Common.Hardware.WaterMeter.OmniPlus.Features; - using NLog; - // using OmniPlus.Features; + using NLog; using System; @@ -24,8 +23,6 @@ this.logger = logger; this.connection = new OmniConnection(); this.connection.UseIrda(portName); - this.connection.StartRecording(); - // this.connection.StateChenged += this.NotifyStateChanged; } public int Slot { get; } @@ -76,8 +73,6 @@ { if (this.connection != null) { - // this.connection.StateChenged -= this.NotifyStateChanged; - this.logger.Trace(this.connection.GetRecords()); this.connection.Disconnect(); } @@ -146,7 +141,7 @@ DataLogIntervalMinutes = configuration.DataLogIntervalMins, ManifacturingTime = system.ManifacturingTime.SecondsSince, NumberOfReadingDigits = configuration.ReadingDigits, - // OptionalUniDirFields = configuration.OptionalUniDirFields, + OptionalUniDirFields = 0x00C0, ReadingPreset = configuration.Readings.Value, RebootCount = 0, SystemTime = Epoch20000101.UTCNow, diff --git a/Common/Ui/Common.UI.VFM/Models/OmniShipmentModel.cs b/Common/Ui/Common.UI.VFM/Models/OmniShipmentModel.cs index f768c423..fcc55aeb 100644 --- a/Common/Ui/Common.UI.VFM/Models/OmniShipmentModel.cs +++ b/Common/Ui/Common.UI.VFM/Models/OmniShipmentModel.cs @@ -9,10 +9,8 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; - using System.Globalization; using System.Linq; using System.Reflection; - using System.Text; using System.Threading; public class OmniShipmentModel @@ -61,14 +59,13 @@ this.values = new OmniValuesModel(); this.omni.UseUniPro(this.portName); - this.omni.StartRecording(); } internal void Finish() { if (!String.IsNullOrWhiteSpace(this.MeterPcb)) { - var debug = this.omni.GetRecords(); + var debug = this.omni.Telegrams?.ToString(); if (!String.IsNullOrWhiteSpace(debug)) { diff --git a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/CordonelDbContext.cs b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/CordonelDbContext.cs index 800a49a5..1784f7d2 100644 --- a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/CordonelDbContext.cs +++ b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/CordonelDbContext.cs @@ -9,18 +9,18 @@ public class CordonelDbContext { - private readonly SqlConnection sqlConnection; - public CordonelDbContext(String connectionString, Action errorsCallback = null) { - this.sqlConnection = new SqlConnection(connectionString, errorsCallback); - this.EOLProgress = new EOLProgressRepository(this.sqlConnection); - this.ProductionOrders = new ProductionOrdersRepository(this.sqlConnection); - this.Requirements = new RequirementsRepository(this.sqlConnection); - this.SpecialRequirements = new SpecialRequirementsRepository(this.sqlConnection, this); - this.StandardRequirements = new StandardRequirementsRepository(this.sqlConnection); + this.SqlConnection = new SqlConnection(connectionString, errorsCallback); + this.EOLProgress = new EOLProgressRepository(this); + this.ProductionOrders = new ProductionOrdersRepository(this); + this.Requirements = new RequirementsRepository(this); + this.SpecialRequirements = new SpecialRequirementsRepository(this); + this.StandardRequirements = new StandardRequirementsRepository(this); } + internal SqlConnection SqlConnection { get; } + public IEOLProgressRepository EOLProgress { get; } public IPORepository ProductionOrders { get; } diff --git a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Interfaces/IEOLProgressRepository[T].cs b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Interfaces/IEOLProgressRepository[T].cs index ba360972..317ce670 100644 --- a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Interfaces/IEOLProgressRepository[T].cs +++ b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Interfaces/IEOLProgressRepository[T].cs @@ -1,9 +1,10 @@ namespace LaaProduction.Data.Cordonel.Interfaces { using System; + using System.Collections.Generic; public interface IEOLProgressRepository : IRepositoryCreate, IRepositoryRead, IRepositoryUpdate, IRepositoryDelete { - void RunSentinel(Int32 pcbId); + IEnumerable> RunSentinel(T pcbId); } } diff --git a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Repositories/EOLProgressRepository.cs b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Repositories/EOLProgressRepository.cs index 8ed24fcb..de63445b 100644 --- a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Repositories/EOLProgressRepository.cs +++ b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Repositories/EOLProgressRepository.cs @@ -5,14 +5,19 @@ using LaaProduction.Data.Cordonel.Models; using System; + using System.Collections.Generic; using System.Linq; public class EOLProgressRepository : IEOLProgressRepository { + private readonly CordonelDbContext dbContext; private readonly SqlConnection sqlConnection; - public EOLProgressRepository(SqlConnection sqlConnection) - => this.sqlConnection = sqlConnection; + public EOLProgressRepository(CordonelDbContext dbContext) + { + this.dbContext = dbContext; + this.sqlConnection = dbContext.SqlConnection; + } public EOLProgressModel Create(EOLProgressModel model) { @@ -191,8 +196,15 @@ } // Test pcb: 233410052 - public void RunSentinel(Int32 pcbId) + public IEnumerable> RunSentinel(EOLProgressModel model) { + if (model is null) + { + yield return new KeyValuePair("EOLProgressModel", "Empty model is not allowed."); + } + + var pcbid = model.PcbId; + var cordonelInfoList = this.sqlConnection .CreateCommand($@" SELECT DISTINCT @@ -232,8 +244,8 @@ ON unn.SerialNo = gm.Seriennummer LEFT JOIN Genesis_PcbPassword AS pwd ON CAST(unn.PcbId AS NVARCHAR(16)) = pwd.PcbId - WHERE unn.PcbId = 233410052") - .SetParameter(nameof(pcbId), pcbId) + WHERE unn.PcbId = @{nameof(pcbid)}") + .SetParameter(nameof(pcbid), pcbid) .ExecuteReader(reader => new CordonelInfo { Pono = reader.GetValue(0), @@ -248,36 +260,25 @@ if (cordonelInfoList.Length != 1) { - // TODO: error handling - - return; + yield return new KeyValuePair($"{pcbid}", "More than 1 entry in the database is not allowed."); } var cordonelInfo = cordonelInfoList[0]; if (cordonelInfo.TestStatus != 30) { - // TODO: error handling - - return; + yield return new KeyValuePair($"{pcbid}", $"Is not correctly calibrated, status: {cordonelInfo.TestStatus}."); } else if (cordonelInfo.Sealed == false) { - // TODO: error handling - - return; + yield return new KeyValuePair($"{pcbid}", $"Is not correctly sealed."); } else if (cordonelInfo.Pending == false) { - // TODO: error handling - - return; + yield return new KeyValuePair($"{pcbid}", $"Was allready finisched."); } - var eol = this.Read(new EOLProgressModel - { - PcbId = pcbId - }); + var requirement = this.sqlConnection .CreateCommand(@" @@ -290,7 +291,7 @@ WHERE rh.PcbId = @pcbId) AS x WHERE x.Row = 1 ORDER BY x.Address") - .SetParameter(nameof(pcbId), $"{pcbId}") + .SetParameter(nameof(pcbid), $"{pcbid}") .ExecuteReader(reader => { var addressBytes = reader.GetValue(0); @@ -304,8 +305,8 @@ SET SAPExportDone = 'OK' , SentinelCompleted = 'OK' , SentinelCompletedDate = GETDATE() - WHERE PcbId = @{nameof(eol.PcbId)}") - .SetParameter(nameof(eol.PcbId), eol.PcbId) + WHERE PcbId = @{nameof(pcbid)}") + .SetParameter(nameof(pcbid), pcbid) .ExecuteNonQuery(); } } diff --git a/LaaProductionWeb/LaaProductionWeb/API/APIExtensions.cs b/LaaProductionWeb/LaaProductionWeb/API/APIExtensions.cs index 17d79abe..3df29556 100644 --- a/LaaProductionWeb/LaaProductionWeb/API/APIExtensions.cs +++ b/LaaProductionWeb/LaaProductionWeb/API/APIExtensions.cs @@ -50,9 +50,7 @@ return errors; } - public static String ErrorsJson(this ModelStateDictionary modelStateDictionary) - { - return JsonConvert.SerializeObject(modelStateDictionary.Errors(), Formatting.Indented); - } + public static String ErrorsJson(this ModelStateDictionary modelStateDictionary) + => JsonConvert.SerializeObject(modelStateDictionary.Errors(), Formatting.Indented); } } \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb/API/Cordonel/EOLProgressController.cs b/LaaProductionWeb/LaaProductionWeb/API/Cordonel/EOLProgressController.cs index e11342e7..2c6b34a3 100644 --- a/LaaProductionWeb/LaaProductionWeb/API/Cordonel/EOLProgressController.cs +++ b/LaaProductionWeb/LaaProductionWeb/API/Cordonel/EOLProgressController.cs @@ -5,6 +5,7 @@ using LaaProductionWeb.App_Infrastructure; using System; + using System.Linq; using System.Web.Http; [RoutePrefix(RoutePrefixes.API_CORDONEL_EOLPROGRESS)] @@ -52,8 +53,18 @@ { return this.BadRequest(); } + + var errors = this.dbcontext.EOLProgress.RunSentinel(actualEOLProgressModel); - this.dbcontext.EOLProgress.RunSentinel(actualEOLProgressModel.PcbId); + if (errors?.Count() > 0) + { + foreach (var error in errors) + { + this.ModelState.AddModelError(error.Key, error.Value); + } + + return this.BadRequest(); + } } else {