diff --git a/Common/Service/MeterProcessState/Controllers/TestBenchController.cs b/Common/Service/MeterProcessState/Controllers/TestBenchController.cs index c7a668af..f696719a 100644 --- a/Common/Service/MeterProcessState/Controllers/TestBenchController.cs +++ b/Common/Service/MeterProcessState/Controllers/TestBenchController.cs @@ -1,4 +1,7 @@ -using System; +using LaaProductionSQL; +using System; +using System.ComponentModel.DataAnnotations; +using System.Configuration; using System.Net; using System.Net.Http; using System.Text; @@ -8,16 +11,13 @@ using Xylem.Common.Logic.ServiceCore; namespace Xylem.Common.Service.MeterProcessState.Controllers { - [RoutePrefix("api/TestBench")] public class TestBenchController : ApiController { public static class GlobalConfig { - public static Lazy connectionString = new Lazy(() - => - System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString - ); + public static Lazy ConnectionString = new Lazy(() + => ConfigurationManager.ConnectionStrings["default"].ConnectionString); } /// @@ -34,7 +34,7 @@ namespace Xylem.Common.Service.MeterProcessState.Controllers { return await Task.Run(() => { - using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) + using (var dataacces = new SqlDataAccess(GlobalConfig.ConnectionString.Value)) { StringBuilder sb = new StringBuilder(); @@ -82,7 +82,7 @@ namespace Xylem.Common.Service.MeterProcessState.Controllers { return await Task.Run(() => { - using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) + using (var dataacces = new SqlDataAccess(GlobalConfig.ConnectionString.Value)) { StringBuilder sb = new StringBuilder(); @@ -122,5 +122,87 @@ namespace Xylem.Common.Service.MeterProcessState.Controllers return Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } } + + [HttpGet] + [Route("GetOffsetCalibration/{pcbId}")] + public IHttpActionResult GetOffsetCalibration(int pcbId) + { + var offset = SQLConnection + .CreateSQLConnection(GlobalConfig.ConnectionString.Value) + .CreateCommand($@" + SELECT [PcbId] + , [TestRunNr] + , [Offset] + FROM [Cordonel_Calibrations] + WHERE [PcbId] = @{nameof(pcbId)}") + .SetParameter(nameof(pcbId), pcbId) + .FirstOrDefault(x => new CordonelCalibration + { + PcbId = x.GetInt(), + TestRunNr = x.GetInt(), + Offset = x.GetValue() + }); + + return this.Json(offset); + } + + [HttpPost] + public IHttpActionResult AddOffsetCalibration([FromBody] CordonelCalibration model) + { + var rowsAffected = SQLConnection + .CreateSQLConnection(GlobalConfig.ConnectionString.Value) + .CreateCommand($@" + INSERT + INTO [Cordonel_Calibrations] + ( [PcbId] + , [TestRunNr] + , [Offset]) + VALUES (@{nameof(CordonelCalibration.PcbId)} + , @{nameof(CordonelCalibration.TestRunNr)} + , @{nameof(CordonelCalibration.Offset)})") + .SetParameter(nameof(CordonelCalibration.PcbId), model.PcbId) + .SetParameter(nameof(CordonelCalibration.TestRunNr), model.TestRunNr) + .SetParameter(nameof(CordonelCalibration.Offset), model.Offset) + .ExecuteNonQuery(); + + if (rowsAffected == 1) + { + return this.Ok(); + } + + return this.BadRequest("Invalid data."); + } + + [HttpPost] + public IHttpActionResult SetOffsetCalibration([FromBody] CordonelCalibration model) + { + var rowsAffected = SQLConnection + .CreateSQLConnection(GlobalConfig.ConnectionString.Value) + .CreateCommand($@" + UPDATE [Cordonel_Calibrations] + SET [TestRunNr] = @{nameof(CordonelCalibration.TestRunNr)} + , [Offset] = @{nameof(CordonelCalibration.Offset)} + WHERE [PcbId] = @{nameof(CordonelCalibration.PcbId)}") + .SetParameter(nameof(CordonelCalibration.TestRunNr), model.TestRunNr) + .SetParameter(nameof(CordonelCalibration.Offset), model.Offset) + .SetParameter(nameof(CordonelCalibration.PcbId), model.PcbId) + .ExecuteNonQuery(); + + if (rowsAffected == 1) + { + return this.Ok(); + } + + return this.BadRequest("Invalid data."); + } + + public class CordonelCalibration + { + public int PcbId { get; set; } + + public int TestRunNr { get; set; } + + public double Offset { get; set; } + } } } diff --git a/Common/Service/MeterProcessState/MeterProcessState.csproj b/Common/Service/MeterProcessState/MeterProcessState.csproj index 3a13d526..2a5ef8e3 100644 --- a/Common/Service/MeterProcessState/MeterProcessState.csproj +++ b/Common/Service/MeterProcessState/MeterProcessState.csproj @@ -49,6 +49,9 @@ 7 + + ..\..\packages\LaaProductionSQL.1.0.4\lib\netstandard2.0\LaaProductionSQL.dll + ..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.4.1.0\lib\net472\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll @@ -65,6 +68,9 @@ + + ..\..\packages\System.Data.SqlClient.4.8.5\lib\net461\System.Data.SqlClient.dll + @@ -320,6 +326,7 @@ PreserveNewest + diff --git a/Common/Service/MeterProcessState/README.md b/Common/Service/MeterProcessState/README.md new file mode 100644 index 00000000..1a10229a --- /dev/null +++ b/Common/Service/MeterProcessState/README.md @@ -0,0 +1,6 @@ +CREATE TABLE [Cordonel_Calibrations] ( + [PcbId] INT NOT NULL, + [TestRunNr] INT NOT NULL, + [Offset] FLOAT NOT NULL, + CONSTRAINT [PK_Cordonel_Calibrations_PcbId] PRIMARY KEY ([PcbId]) +); \ No newline at end of file diff --git a/Common/Service/MeterProcessState/packages.config b/Common/Service/MeterProcessState/packages.config index 1479196c..a0588213 100644 --- a/Common/Service/MeterProcessState/packages.config +++ b/Common/Service/MeterProcessState/packages.config @@ -6,6 +6,7 @@ + @@ -39,6 +40,7 @@ + \ No newline at end of file