diff --git a/Common/.vs/config/applicationhost.config b/Common/.vs/config/applicationhost.config index 883679a3..a894c3cf 100644 --- a/Common/.vs/config/applicationhost.config +++ b/Common/.vs/config/applicationhost.config @@ -155,7 +155,7 @@ - + diff --git a/Common/CommonConsole/CommonConsole.csproj b/Common/CommonConsole/CommonConsole.csproj index 4e1011f6..833b6e4f 100644 --- a/Common/CommonConsole/CommonConsole.csproj +++ b/Common/CommonConsole/CommonConsole.csproj @@ -81,5 +81,6 @@ + \ No newline at end of file diff --git a/Common/CommonConsole/Program.cs b/Common/CommonConsole/Program.cs index d5d39fa9..d62da667 100644 --- a/Common/CommonConsole/Program.cs +++ b/Common/CommonConsole/Program.cs @@ -1,24 +1,9 @@ -using OmniPlus; -using OmniPlus.Features; -using System; - -namespace CommonConsole +namespace CommonConsole { public static class Program { public static void Main() { - var connection = new OmniIrdaConnection("COM36"); - connection.StateChenged += Console.WriteLine; - - connection.Open(); - - for (int i = 0; i < 1000; i++) - { - var readings = connection.ViewReadings(); - } - - connection.Close(); } } } \ No newline at end of file diff --git a/Common/OmniPlus/InspectionUI/App.config b/Common/OmniPlus/InspectionUI/App.config index d9c69576..aaaf148c 100644 --- a/Common/OmniPlus/InspectionUI/App.config +++ b/Common/OmniPlus/InspectionUI/App.config @@ -10,5 +10,6 @@ + diff --git a/Common/OmniPlus/InspectionUI/Models/Appsettings.cs b/Common/OmniPlus/InspectionUI/Models/Appsettings.cs index 880c182d..ccbf35e8 100644 --- a/Common/OmniPlus/InspectionUI/Models/Appsettings.cs +++ b/Common/OmniPlus/InspectionUI/Models/Appsettings.cs @@ -45,13 +45,18 @@ get => GetSetting(nameof(PSNR)); } + public static int FlushMinutes + { + get => int.TryParse(GetSetting(nameof(FlushMinutes)), out var flush) ? flush : 0; + } + private static string GetSetting(string setting) { var appsettingsSection = ExeConfiguration.AppSettings.SectionInformation.Name; ConfigurationManager.RefreshSection(appsettingsSection); - return Settings[setting].Value; + return Settings[setting].Value ?? string.Empty; } } } diff --git a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs index dc3154f7..9bbca7b2 100644 --- a/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs +++ b/Common/OmniPlus/InspectionUI/ViewModels/MetersPoolVM.cs @@ -122,6 +122,10 @@ var pruefpunkte = this.omniContext .Auftrag .Pruefpunkte; + var isQmax = true; + var awaiter = new ManualResetEventSlim(); + + cancellationToken.Register(() => awaiter.Set()); foreach (var pp in pruefpunkte) { @@ -132,6 +136,12 @@ this.referenceMeter.Start(pp); + if (isQmax) + { + isQmax = false; + awaiter.Wait(Appsettings.FlushMinutes * 60 * 1000); + } + if (cancellationToken.IsCancellationRequested) { break; diff --git a/Common/OmniPlus/OmniPlus/OmniMeter.cs b/Common/OmniPlus/OmniPlus/OmniMeter.cs index 0c8a11a8..9b702000 100644 --- a/Common/OmniPlus/OmniPlus/OmniMeter.cs +++ b/Common/OmniPlus/OmniPlus/OmniMeter.cs @@ -370,7 +370,14 @@ public void NotifyStateChanged(object state) { - this.logger.Trace(state); + try + { + this.logger.Trace(state); + } + catch (Exception e) + { + + } } private ILogger CreateNLogger(string name) diff --git a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/LaaProduction.Data.Cordonel.csproj b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/LaaProduction.Data.Cordonel.csproj new file mode 100644 index 00000000..e8097ed6 --- /dev/null +++ b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/LaaProduction.Data.Cordonel.csproj @@ -0,0 +1,46 @@ + + + + + Debug + AnyCPU + {03784923-1FD3-4535-80F5-286728DF62AB} + Library + Properties + LaaProduction.Data.Cordonel + LaaProduction.Data.Cordonel + v4.0 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Properties/AssemblyInfo.cs b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..60664a85 --- /dev/null +++ b/LaaProductionWeb/Data/LaaProduction.Data.Cordonel/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("LaaProduction.Data.Cordonel")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LaaProduction.Data.Cordonel")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("03784923-1fd3-4535-80f5-286728df62ab")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLCommand.cs b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLCommand.cs new file mode 100644 index 00000000..3e997dd7 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLCommand.cs @@ -0,0 +1,18 @@ +namespace LaaProduction.Data.SQL.Interfaces +{ + using System; + using System.Collections.Generic; + + public interface ISQLCommand + { + int ExecuteNonQuery(); + + IEnumerable ExecuteReader(Func expression); + + T ExecuteScalar(Func expression); + + T FirstOrDefault(Func expression); + + ISQLCommand SetParameter(string name, object value); + } +} diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLConnection.cs b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLConnection.cs new file mode 100644 index 00000000..c4e7d72c --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLConnection.cs @@ -0,0 +1,15 @@ +namespace LaaProduction.Data.SQL.Interfaces +{ + /// + /// simply exposes the method. + /// + public interface ISQLConnection + { + /// + /// Creates a command with given command text. + /// + /// - SQL query string. + /// + ISQLCommand CreateCommand(string commandText); + } +} diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLReader.cs b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLReader.cs new file mode 100644 index 00000000..2f155280 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/Interfaces/ISQLReader.cs @@ -0,0 +1,11 @@ +namespace LaaProduction.Data.SQL.Interfaces +{ + using System; + + public interface ISQLReader + { + T GetValue(); + + object GetValue(string column); + } +} \ No newline at end of file diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/LaaProduction.Data.SQL.csproj b/LaaProductionWeb/LaaProduction.Data.SQL/LaaProduction.Data.SQL.csproj new file mode 100644 index 00000000..53eb5ee7 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/LaaProduction.Data.SQL.csproj @@ -0,0 +1,52 @@ + + + + + Debug + AnyCPU + {82D34580-23FE-4315-BC51-51D565ABAE23} + Library + Properties + LaaProduction.Data.SQL + LaaProduction.Data.SQL + v4.0 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/Properties/AssemblyInfo.cs b/LaaProductionWeb/LaaProduction.Data.SQL/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..aa0ea89c --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("LaaProduction.Data.SQL")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("LaaProduction.Data.SQL")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("82d34580-23fe-4315-bc51-51d565abae23")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/SQLCommand.cs b/LaaProductionWeb/LaaProduction.Data.SQL/SQLCommand.cs new file mode 100644 index 00000000..46e42d06 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/SQLCommand.cs @@ -0,0 +1,60 @@ +namespace LaaProduction.Data.SQL +{ + using LaaProduction.Data.SQL.Interfaces; + + using System; + using System.Collections.Generic; + using System.Data.SqlClient; + using System.Linq; + + public class SQLCommand : ISQLCommand + { + private readonly SQLConnection sqlConnection; + private readonly IDictionary parameters; + + public SQLCommand(SQLConnection sqlConnection, string commandText) + { + this.sqlConnection = sqlConnection.EnsureNotNull(nameof(sqlConnection)); + this.CommandText = commandText.EnsureNotNullOrWhiteSpace(nameof(commandText)); + this.parameters = new Dictionary(); + } + + internal string CommandText { get; } + + internal SqlParameter[] Parameters + => this.parameters.Values.ToArray(); + + public int ExecuteNonQuery() + => this.sqlConnection.ExecuteNonQuery(this); + + public IEnumerable ExecuteReader(Func expression) + { + foreach (var result in this.sqlConnection.ExecuteReader(this, expression)) + { + yield return result; + } + } + + public T ExecuteScalar(Func expression) + => this.sqlConnection.ExecuteScalar(this, expression); + + public T FirstOrDefault(Func expression) + => this.sqlConnection.FirstOrDefault(this, expression); + + public ISQLCommand SetParameter(string name, object value) + { + name.ThrowIfNullOrWhiteSpace(nameof(name)); + + if (value is null) + { + value = DBNull.Value; + } + + var parameter = new SqlParameter(name, value); + + this.parameters[name] = parameter; + + return this; + } + } +} diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/SQLConnection.cs b/LaaProductionWeb/LaaProduction.Data.SQL/SQLConnection.cs new file mode 100644 index 00000000..a82faa97 --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/SQLConnection.cs @@ -0,0 +1,192 @@ +namespace LaaProduction.Data.SQL +{ + using LaaProduction.Data.SQL.Interfaces; + + using System; + using System.Collections.Generic; + using System.Data.SqlClient; + using System.Linq; + using System.Text; + + /// + /// A wrapper class around the . + /// Exposes , and classes + /// in a fluent manner, all exposed methods can be chained in a single C# query. + /// + public class SQLConnection : ISQLConnection + { + private readonly string connectionString; + private readonly Action errorCallback; + + /// + /// When is null, white space or is not a valid connection string exception is thrown. + /// + /// - SQL connection string. + public SQLConnection(string connectionString) + { + var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); + + this.connectionString = connectionStringBuilder.ToString(); + } + + /// + /// When is null, white space or is not a valid connection string exception is thrown. + /// Additionaly accepts for error handling. + /// Whenever the sql query throws error on execution, this will be passesd within the callback. + /// + /// - SQL connection string. + /// - error handler for SQL execution errors. + public SQLConnection(string connectionString, Action errorCallback = null) + : this(connectionString) + => this.errorCallback = errorCallback; + + /// + /// Creates a wrapper that accepts a SQL query string. + /// + /// - SQL query string + /// - that exposes basic functionalities from the . + public ISQLCommand CreateCommand(string commandText) + => new SQLCommand(this, commandText); + + /// + /// Static method that creates a client with given connection string. + /// + /// - SQL connection string. + /// + public static ISQLConnection CreateSQLConnection(string connectionString) + => new SQLConnection(connectionString); + + /// + /// Static method that creates a client with given connection string and error callback that receives all error messages. + /// + /// - SQL connection string. + /// - error handler for SQL execution errors. + /// + public static ISQLConnection CreateSQLConnection(string connectionString, Action errorCallback = null) + => new SQLConnection(connectionString, errorCallback); + + /// + /// Internal method called from . + /// Like in a performs INSERT, UPDATE, DELETE queries and returns the number of rows affected. + /// + /// - SQL query string + /// - the number of rows affected. + internal int ExecuteNonQuery(SQLCommand command) + { + var rowsAffected = 0; + + using (var sqlConnection = new SqlConnection(this.connectionString)) + { + sqlConnection.OpenWithErrorHandling(this.errorCallback); + + using (var sqlCommand = sqlConnection.CreateCommand()) + { + sqlCommand.CommandText = command.CommandText; + + sqlCommand.Parameters.AddRange(command.Parameters); + + rowsAffected = sqlCommand.ExecuteNonQuery(); + + sqlCommand.Parameters.Clear(); + } + } + + return rowsAffected; + } + + /// + /// Internal method called from . + /// Like in a performs SELECT queries and returns a enumerable of the results readed. + /// + /// Is the type that returns the + /// - SQL query string + /// Expression that reads specified data from the . + /// + internal IEnumerable ExecuteReader(SQLCommand command, Func expression) + { + using (var sqlConnection = new SqlConnection(this.connectionString)) + { + sqlConnection.OpenWithErrorHandling(this.errorCallback); + + using (var sqlCommand = sqlConnection.CreateCommand()) + { + sqlCommand.CommandText = command.CommandText; + + sqlCommand.Parameters.AddRange(command.Parameters); + + using (SQLReader sqlReader = sqlCommand.ExecuteReader()) + { + sqlCommand.Parameters.Clear(); + + while (sqlReader.Read()) + { + yield return expression(sqlReader); + } + } + } + } + } + + /// + /// Internal method called from . + /// Like in a performs SELECT, READ, UPDATE, DELETE queries and returns first column from the first row from the result. + /// + /// From user specified. + /// - SQL query string + /// Expression that reads specified data from the . + /// + internal T ExecuteScalar(SQLCommand command, Func expression) + { + var scalarResult = default(T); + + using (var sqlConnection = new SqlConnection(this.connectionString)) + { + this.OpenWithErrorHandling(sqlConnection, this.errorCallback); + + using (var sqlCommand = sqlConnection.CreateCommand()) + { + sqlCommand.CommandText = command.CommandText; + + sqlCommand.Parameters.AddRange(command.Parameters); + + var scalarObject = sqlCommand.ExecuteScalar(); + + sqlCommand.Parameters.Clear(); + + if (scalarObject is T scalarValue) + { + scalarResult = scalarValue; + } + } + } + + return scalarResult; + } + + /// + /// Is same like but returns only the first data row. + /// + /// From user specified. + /// - SQL query string + /// Expression that reads specified data from the . + /// + internal T FirstOrDefault(SQLCommand command, Func expression) + => this + .ExecuteReader(command, expression) + .FirstOrDefault(); + + private void OpenWithErrorHandling(SqlConnection connection, Action errorCallback) + { + connection.FireInfoMessageEventOnUserErrors = true; + connection.InfoMessage += (sender, args) => + { + var errors = new StringBuilder(); + + foreach (SqlError e in args.Errors) + { + errors.Append($"{e.Server}|{e.Source}|{e.S}|{e.Class}|{e.Number}"); + } + }; + } + } +} diff --git a/LaaProductionWeb/LaaProduction.Data.SQL/SQLReader.cs b/LaaProductionWeb/LaaProduction.Data.SQL/SQLReader.cs new file mode 100644 index 00000000..3fa4f92a --- /dev/null +++ b/LaaProductionWeb/LaaProduction.Data.SQL/SQLReader.cs @@ -0,0 +1,80 @@ +namespace LaaProduction.Data.SQL +{ + using LaaProduction.Data.SQL.Interfaces; + + using System; + using System.Data.SqlClient; + + public class SQLReader : ISQLReader, IDisposable + { + private readonly SqlDataReader sqlDataReader; + + private int index; + + private SQLReader(SqlDataReader sqlDataReader) + => this.sqlDataReader = sqlDataReader; + + public void Dispose() + { + this.sqlDataReader.Close(); + this.sqlDataReader.Dispose(); + } + + public T GetValue() + { + this.index++; + + if (!this.sqlDataReader.IsDBNull(this.index)) + { + var value = this.sqlDataReader.GetValue(this.index); + var type = Nullable.GetUnderlyingType(typeof(T)); + + if (type is null) + { + type = typeof(T); + } + + try + { + value = Convert.ChangeType(value, type); + } + catch (Exception) + { + // TODO: + } + + if (value is T _value) + { + return _value; + } + } + + return default(T); + } + + public object GetValue(string column) + { + var colIndex = this.sqlDataReader.GetOrdinal(column); + + if (0 <= colIndex && colIndex < this.sqlDataReader.FieldCount) + { + if (!this.sqlDataReader.IsDBNull(colIndex)) + { + return this.sqlDataReader.GetValue(colIndex); + } + } + + return default(object); + } + + internal bool Read() + { + this.index = -1; + + return this.sqlDataReader.Read(); + } + + public static implicit operator SQLReader(SqlDataReader sqlDataReader) + => new SQLReader(sqlDataReader); + } +} \ No newline at end of file diff --git a/LaaProductionWeb/LaaProductionWeb.sln b/LaaProductionWeb/LaaProductionWeb.sln index d6cdfec5..07152590 100644 --- a/LaaProductionWeb/LaaProductionWeb.sln +++ b/LaaProductionWeb/LaaProductionWeb.sln @@ -44,6 +44,12 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "LaaProductionVFM", "LaaProd EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PendingEmails", "PendingEmails\PendingEmails.csproj", "{8C2A940A-173C-421D-9FE1-80CC01C8C5F9}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Data", "Data", "{90A75AEB-70C5-42E5-A802-6B744280B8F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaaProduction.Data.Cordonel", "Data\LaaProduction.Data.Cordonel\LaaProduction.Data.Cordonel.csproj", "{03784923-1FD3-4535-80F5-286728DF62AB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LaaProduction.Data.SQL", "LaaProduction.Data.SQL\LaaProduction.Data.SQL.csproj", "{82D34580-23FE-4315-BC51-51D565ABAE23}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -90,10 +96,22 @@ Global {8C2A940A-173C-421D-9FE1-80CC01C8C5F9}.Debug|Any CPU.Build.0 = Debug|Any CPU {8C2A940A-173C-421D-9FE1-80CC01C8C5F9}.Release|Any CPU.ActiveCfg = Release|Any CPU {8C2A940A-173C-421D-9FE1-80CC01C8C5F9}.Release|Any CPU.Build.0 = Release|Any CPU + {03784923-1FD3-4535-80F5-286728DF62AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03784923-1FD3-4535-80F5-286728DF62AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03784923-1FD3-4535-80F5-286728DF62AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03784923-1FD3-4535-80F5-286728DF62AB}.Release|Any CPU.Build.0 = Release|Any CPU + {82D34580-23FE-4315-BC51-51D565ABAE23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82D34580-23FE-4315-BC51-51D565ABAE23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82D34580-23FE-4315-BC51-51D565ABAE23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82D34580-23FE-4315-BC51-51D565ABAE23}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {03784923-1FD3-4535-80F5-286728DF62AB} = {90A75AEB-70C5-42E5-A802-6B744280B8F8} + {82D34580-23FE-4315-BC51-51D565ABAE23} = {90A75AEB-70C5-42E5-A802-6B744280B8F8} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9D3C9EBA-FB4B-4BF4-8158-4A9FE47D3E88} EndGlobalSection