using Logic.ProductionToProductMapper.Cordonel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web.Http; using Xylem.Common.Logic.ServiceCore; namespace Service.MeterProcessState.Controllers { [RoutePrefix("api/GenesisMeter")] public class GenesisMeterController : ApiController { public static class GlobalConfig { public static Lazy connectionString = new Lazy(() => System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString ); } [Route("PostErrorMessage"), HttpPost] public async Task PostErrorMessage(String PcbId, String ErrorMessage) { try { using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) { var dt = dataacces.ExecuteQuery($"Insert into dbo.GenesisMeterErrorMessages ([PcbId], [Message],[date] ) values ('{PcbId}', '{ErrorMessage}', GETUTCDATE())"); } return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => { return "store"; })); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } } [Route("PostCalibrationResult"), HttpPost] public async Task PostCalibrationResult(String PcbId, [FromBody]CalibrationResult results) { try { var sb = new StringBuilder(); sb.AppendLine( $"Insert into dbo.CordonelMeterCalibrationResults " + $"([PcbId], " + $"[calFactor1]," + $"[calFactor2]," + $"[calFactor3]," + $"[zeroOffset1]," + $"[zeroOffset2]," + $"[zeroOffset3]," + $"[firstHitUpdatePeriod]," + $"[firstHitShift]," + $"[firstHitPercent1]," + $"[firstHitPercent2]," + $"[firstHitPercent3]," + $"[toFTempOffset1]," + $"[toFTempOffset2]," + $"[toFTempOffset3]," + $"[meterSize]," + $"[date] ) " + $"values " + $"('{PcbId}'," + $" {results.CalFactor1}," + $" {results.CalFactor2}," + $" {results.CalFactor3}," + $" {results.ZeroOffset1}," + $" {results.ZeroOffset2}," + $" {results.ZeroOffset3}," + $" {results.FirstHitUpdatePeriod}," + $" { results.FirstHitShift}," + $" {results.FirstHitPercent1}," + $" {results.FirstHitPercent2}," + $" {results.FirstHitPercent3}," + $" {results.ToFTempOffset1}," + $" {results.ToFTempOffset2}," + $" {results.ToFTempOffset3}," + $" {results.MeterSize},"); sb.AppendLine($" GETUTCDATE());"); sb.AppendLine($"declare @Ident as int;"); sb.AppendLine($"Select @Ident = @@IDENTITY;"); foreach (var logInfo in results.AdditionalLogInfos) { sb.AppendLine($"insert into CordonelMeterCalibrationLog"); sb.AppendLine($" select '{PcbId}', @Ident, '{logInfo.Key}', '{logInfo.Value}'; "); } using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) { var dt = dataacces.ExecuteQuery(sb.ToString()); } return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => { return true; })); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } } [Route("GetCalibrationResults"), HttpGet] public async Task GetCalibrationResults(String PcbId) { try { var ret = new List(); var sb = new StringBuilder(); return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => { sb.AppendLine("declare @pcbid as nvarchar(50) "); sb.AppendLine($"set @pcbid = '{PcbId}' "); sb.AppendLine("select "); sb.AppendLine("r.ID, "); sb.AppendLine("r.[PcbId], "); sb.AppendLine("r.[calFactor1], "); sb.AppendLine("r.[calFactor2], "); sb.AppendLine("r.[calFactor3], "); sb.AppendLine("r.[zeroOffset1], "); sb.AppendLine("r.[zeroOffset2], "); sb.AppendLine("r.[zeroOffset3], "); sb.AppendLine("r.[firstHitUpdatePeriod], "); sb.AppendLine("r.[firstHitShift], "); sb.AppendLine("r.[firstHitPercent1], "); sb.AppendLine("r.[firstHitPercent2], "); sb.AppendLine("r.[firstHitPercent3], "); sb.AppendLine("r.[toFTempOffset1], "); sb.AppendLine("r.[toFTempOffset2], "); sb.AppendLine("r.[toFTempOffset3], "); sb.AppendLine("r.[meterSize], "); sb.AppendLine("r.[date] "); sb.AppendLine("from CordonelMeterCalibrationResults r "); sb.AppendLine("WHERE r.PcbId =@pcbid "); sb.AppendLine("order by r.[date] DESC "); using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) { var dtCalibrationResults = dataacces.ExecuteQuery(sb.ToString()); foreach (var item in dtCalibrationResults.Select()) { var calibrationResult = new CalibrationResult(); calibrationResult.DbId = (Int32)item["ID"]; calibrationResult.CalFactor1 = (UInt32)(Int32)item["calFactor1"]; calibrationResult.CalFactor2 = (UInt32)(Int32)item["calFactor2"]; calibrationResult.CalFactor3 = (UInt32)(Int32)item["calFactor3"]; calibrationResult.ZeroOffset1 = (Int32)item["zeroOffset1"]; calibrationResult.ZeroOffset2 = (Int32)item["zeroOffset2"]; calibrationResult.ZeroOffset3 = (Int32)item["zeroOffset3"]; calibrationResult.FirstHitUpdatePeriod = (UInt16)(Int32)item["firstHitUpdatePeriod"]; calibrationResult.FirstHitShift = (UInt16)(Int32)item["firstHitShift"]; calibrationResult.FirstHitPercent1 = (UInt16)(Int32)item["firstHitPercent1"]; calibrationResult.FirstHitPercent2 = (UInt16)(Int32)item["firstHitPercent2"]; calibrationResult.FirstHitPercent3 = (UInt16)(Int32)item["firstHitPercent3"]; calibrationResult.ToFTempOffset1 = (Int32)item["toFTempOffset1"]; calibrationResult.ToFTempOffset2 = (Int32)item["toFTempOffset2"]; calibrationResult.ToFTempOffset3 = (Int32)item["toFTempOffset3"]; calibrationResult.MeterSize = (UInt16)(Int32)item["meterSize"]; calibrationResult.Date = (DateTime)item["date"]; ret.Add(calibrationResult); } if (ret.Any()) { sb = new StringBuilder(); sb.AppendLine("declare @pcbid as nvarchar(50) "); sb.AppendLine($"set @pcbid = '{PcbId}' "); sb.AppendLine("select ID, PcbId,CalibrationResultsID, LogKey,LogText from CordonelMeterCalibrationLog"); sb.AppendLine(" where PcbId = @pcbid "); sb.AppendLine(" Order By CalibrationResultsID DESC, ID DESC "); var dtCalibrationResultsLogInfo = dataacces.ExecuteQuery(sb.ToString()); foreach (var item in dtCalibrationResultsLogInfo.Select()) { ret.First(f => f.DbId == (Int32)item["CalibrationResultsID"])?.AddAdditionalLog((String)item["LogKey"], (String)item["LogText"]); } } return ret; } })); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } } [Route("GetCalibrationResultsForAll"), HttpGet] public async Task GetCalibrationResultsForAll() { try { StringBuilder sbRet = new StringBuilder(); var ret = new List(); var sb = new StringBuilder(); //return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => //{ sb.AppendLine("select "); sb.AppendLine("res.[ID], "); sb.AppendLine(" res.[PcbId] "); sb.AppendLine(" ,[ZeroOffset1] "); sb.AppendLine(" ,[ZeroOffset2] "); sb.AppendLine(" ,[ZeroOffset3] "); sb.AppendLine(" ,[FirstHitPercent1] "); sb.AppendLine(" ,[FirstHitPercent2] "); sb.AppendLine(" ,[FirstHitPercent3] "); sb.AppendLine(" ,[ToFTempOffset1] "); sb.AppendLine(" ,[ToFTempOffset2] "); sb.AppendLine(" ,[ToFTempOffset3] "); sb.AppendLine(" ,[MeterSize] "); sb.AppendLine(" ,[date] "); sb.AppendLine(" , clogP1.LogText as [Amp.Test Mean value PATH 1] "); sb.AppendLine(" , clogP2.LogText as [Amp.Test Mean value PATH 2] "); sb.AppendLine(" , clogP3.LogText as [Amp.Test Mean value PATH 3] "); sb.AppendLine("from[CordonelMeterCalibrationResults] res "); sb.AppendLine("inner join[CordonelMeterCalibrationLog] clogP1 on res.ID = clogP1.CalibrationResultsID and clogP1.LogKey = 'Amp. Test Mean value PATH 1' "); sb.AppendLine("inner join[CordonelMeterCalibrationLog] clogP2 on res.ID = clogP2.CalibrationResultsID and clogP2.LogKey = 'Amp. Test Mean value PATH 2' "); sb.AppendLine("inner join[CordonelMeterCalibrationLog] clogP3 on res.ID = clogP3.CalibrationResultsID and clogP3.LogKey = 'Amp. Test Mean value PATH 3' "); sb.AppendLine("where res.id in ( "); sb.AppendLine("SELECT "); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" max([CalibrationResultsID]) as ResultsID "); sb.AppendLine(" "); sb.AppendLine(" FROM[Auftrag].[dbo].[CordonelMeterCalibrationLog] "); sb.AppendLine(" group by[PcbId] "); sb.AppendLine(" ) "); sb.AppendLine("order by res.[date] DESC "); sbRet.Append("ID;PcbId;Path;ZeroOffset;FirstHitPercent;ToFTempOffset1;MeterSize;date"); for (Int32 i = 5; i <= 35; i++) { sbRet.Append($";Amp_Mean_on_{i}%"); } sbRet.AppendLine(";"); using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value)) { var dtCalibrationResults = dataacces.ExecuteQuery(sb.ToString()); foreach (var item in dtCalibrationResults.Rows) { if (item is DataRow dr) { for (Int32 i = 1; i <= 3; i++) { sbRet.Append(dr["ID"].ToString()).Append(";"); sbRet.Append(dr["PcbId"].ToString()).Append(";"); sbRet.Append(i).Append(";"); sbRet.Append(dr[$"ZeroOffset{i}"].ToString()).Append(";"); sbRet.Append(dr[$"FirstHitPercent{i}"].ToString()).Append(";"); sbRet.Append(dr[$"ToFTempOffset{i}"].ToString()).Append(";"); sbRet.Append(dr["MeterSize"].ToString()).Append(";"); sbRet.Append(dr["date"].ToString()).Append(";"); var ampMenPercText = dr[$"Amp.Test Mean value PATH {i}"].ToString(); Char[] delims = new[] { '\r', '\n' }; var lines = ampMenPercText.Split(delims, StringSplitOptions.RemoveEmptyEntries); var c = 5; if (lines.Count() > 30 && lines.Count() < 41) { foreach (var perString in lines) { sbRet.Append(perString.Trim().Replace($"at {c}=", "")).Append(";"); c = c + 1; if (c == 36) { break; } } } else { sbRet.Append("ERROR;"); } sbRet.AppendLine(""); } } } return Request.CreateResponse(HttpStatusCode.OK, sbRet.ToString()); } //})); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex); } } } }