using APCViewer.Models; using APCViewer.Singleton; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Shared; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using System.Web; namespace APCViewer.Controllers { public class HomeController : Controller, IHomeController { private readonly Log _Log; private readonly Singleton.IBackground _Background; private readonly IHttpContextAccessor _HttpContextAccessor; public HomeController(ILogger logger, Singleton.IBackground background, IHttpContextAccessor httpContextAccessor) { _Log = new Log(logger); _Background = background; _HttpContextAccessor = httpContextAccessor; } public ActionResult Index() { return View(); } public ActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public ActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } public ActionResult Encode(string value = null) { string result = string.Empty; if (!string.IsNullOrEmpty(value)) result = HttpUtility.UrlEncode(value); return Content(result, "text/plain"); } public ActionResult Background(bool? message_clear = null, bool? exceptions_clear = null, bool? set_is_primary_instance = null, bool? logistics_clear = null) { _Background.SendStatusOk(); if (message_clear.HasValue && message_clear.Value) _Background.ClearMessage(); if (exceptions_clear.HasValue && exceptions_clear.Value) _Background.Exceptions.Clear(); if (set_is_primary_instance.HasValue) { if (set_is_primary_instance.Value) _Background.SetIsPrimaryInstance(); else _Background.ClearIsPrimaryInstance(); } if (logistics_clear.HasValue && logistics_clear.Value) _Background.LogisticsClear(); string message; if (string.IsNullOrWhiteSpace(_Background.Message)) message = "N/A"; else message = _Background.Message; //Response.AppendToLog(_Background.Message); List exceptions = new(); foreach (Exception exception in _Background.Exceptions) exceptions.Add(exception); ViewBag.Message = message; ViewBag.Exceptions = exceptions; ViewBag.URLs = _Background.AppSettings.URLs; ViewBag.Profile = _Background.IsEnvironment.Profile; ViewBag.WorkingDirectory = _Background.WorkingDirectory; ViewBag.IsPrimaryInstance = _Background.IsPrimaryInstance(); ViewBag.ExceptionsCount = string.Concat("Exception(s) - ", exceptions.Count); return View(); } public ActionResult PDSF(string directory = null, string filter = null, bool is_gaN = false, bool is_Si = false) { Tuple tuple = _Background.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forPDSF: true); ViewBag.Files = tuple.Item1; ViewBag.Grouped = tuple.Item2; ViewBag.Sorted = tuple.Item3; ViewBag.Directory = tuple.Item4; return View(); } private StringBuilder GetPDSFHtml(string pdsfFile) { StringBuilder result = new(); result.AppendLine(""); if (string.IsNullOrEmpty(pdsfFile)) throw new Exception(""); else if (!System.IO.File.Exists(pdsfFile)) result.AppendLine(""); else { bool header = true; bool body = false; bool footer = false; string logisticsSegment; List logistics = new(); string[] pdsfLines = System.IO.File.ReadAllLines(pdsfFile); foreach (string pdsfLine in pdsfLines) { if (pdsfLine.StartsWith("LOGISTICS_")) { logisticsSegment = pdsfLine.Split('\t')[0]; if (!logistics.Contains(logisticsSegment)) { logistics.Add(logisticsSegment); result.AppendLine("
Invalid input
File doesn't exist!

"); } } if (pdsfLine.StartsWith("NUM_DATA_ROWS") || pdsfLine.StartsWith("END_HEADER")) { body = false; footer = true; result.AppendLine("

"); } if (header) result.Append(""); else if (body) result.Append(""); else if (footer) { if (pdsfLine.StartsWith("DELIMITER")) result.Append(""); else result.Append(""); } else throw new Exception(); if (pdsfLine.StartsWith("END_OFFSET")) { header = false; body = true; result.AppendLine("
").Append(pdsfLine.Replace("\t", "")).AppendLine(" 
").Append(pdsfLine.Replace("\t", "")).AppendLine(" 
").Append(pdsfLine.Replace(";", "")).AppendLine(" 
").Append(pdsfLine.Replace("\t", "").Replace("=", "").Replace(";", "")).AppendLine(" 

"); } } } result.AppendLine("
"); return result; } public ContentResult ViewPDSF(string id = null) { string pdsfFile; StringBuilder result = new(); if (!id.Contains('_')) result.AppendLine("
A) Error: Invalid input
"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) result.AppendLine("
B) Error: Invalid input
"); else { pdsfFile = _Background.GetPDSF(sequence); result = GetPDSFHtml(pdsfFile); } } return Content(result.ToString(), "text/html"); } public ActionResult DownloadPDSF(string id = null) { string pdsfFile; if (!id.Contains('_')) throw new Exception("A) Error: Invalid input"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) throw new Exception("B) Error: Invalid input"); else pdsfFile = _Background.GetPDSF(sequence); } return File(pdsfFile, "text/plain", Path.GetFileName(pdsfFile)); } public ContentResult ViewCustomPDSF(string pdsf_file = null) { StringBuilder result = GetPDSFHtml(pdsf_file); return Content(result.ToString(), "text/html"); } public ActionResult DownloadCustomPDSF(string pdsf_file = null) { if (string.IsNullOrEmpty(pdsf_file)) throw new Exception("Error: Invalid input"); else if (!System.IO.File.Exists(pdsf_file)) throw new Exception("Error: file does not exist"); return File(pdsf_file, "text/plain", Path.GetFileName(pdsf_file)); } public ActionResult IPDSF(string directory = null, string filter = null, bool is_gaN = false, bool is_Si = false) { Tuple tuple = _Background.SetViewBag(directory, filter, isGaN: is_gaN, isSi: is_Si, forIPDSF: true); ViewBag.Files = tuple.Item1; ViewBag.Grouped = tuple.Item2; ViewBag.Sorted = tuple.Item3; ViewBag.Directory = tuple.Item4; return View(); } private StringBuilder GetIPDSFHtml(string ipdsfFile) { StringBuilder result = new(); result.AppendLine(""); if (string.IsNullOrEmpty(ipdsfFile)) throw new Exception(""); else if (!System.IO.File.Exists(ipdsfFile)) result.AppendLine(""); else { bool header = true; bool body = false; bool footer = false; string logisticsSegment; List logistics = new(); string[] ipdsfLines = System.IO.File.ReadAllLines(ipdsfFile); foreach (string ipdsfLine in ipdsfLines) { if (ipdsfLine.StartsWith("LOGISTICS_")) { logisticsSegment = ipdsfLine.Split('\t')[0]; if (!logistics.Contains(logisticsSegment)) { logistics.Add(logisticsSegment); result.AppendLine("
Invalid input
File doesn't exist!

"); } } if (ipdsfLine.StartsWith("NUM_DATA_ROWS") || ipdsfLine.StartsWith("END_HEADER")) { body = false; footer = true; result.AppendLine("

"); } if (header) result.Append(""); else if (body) result.Append(""); else if (footer) { if (ipdsfLine.StartsWith("DELIMITER")) result.Append(""); else result.Append(""); } else throw new Exception(); if (ipdsfLine.StartsWith("END_OFFSET")) { header = false; body = true; result.AppendLine("
").Append(ipdsfLine.Replace("\t", "")).AppendLine(" 
").Append(ipdsfLine.Replace("\t", "")).AppendLine(" 
").Append(ipdsfLine.Replace(";", "")).AppendLine(" 
").Append(ipdsfLine.Replace("\t", "").Replace("=", "").Replace(";", "")).AppendLine(" 

"); } } } result.AppendLine("
"); return result; } public ContentResult ViewIPDSF(string id = null) { string ipdsfFile; StringBuilder result = new(); if (!id.Contains('_')) result.AppendLine("
A) Error: Invalid input
"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) result.AppendLine("
B) Error: Invalid input
"); else { ipdsfFile = _Background.GetIPDSF(sequence); result = GetIPDSFHtml(ipdsfFile); } } return Content(result.ToString(), "text/html"); } public ActionResult DownloadIPDSF(string id = null) { string ipdsfFile; if (!id.Contains('_')) throw new Exception("A) Error: Invalid input"); else { if (!long.TryParse(id.Split('_')[1], out long sequence)) throw new Exception("B) Error: Invalid input"); else ipdsfFile = _Background.GetIPDSF(sequence); } return File(ipdsfFile, "text/plain", Path.GetFileName(ipdsfFile)); } public ContentResult ViewCustomIPDSF(string ipdsf_file = null) { StringBuilder result = GetIPDSFHtml(ipdsf_file); return Content(result.ToString(), "text/html"); } public ActionResult DownloadCustomIPDSF(string ipdsf_file = null) { if (string.IsNullOrEmpty(ipdsf_file)) throw new Exception("Error: Invalid input"); else if (!System.IO.File.Exists(ipdsf_file)) throw new Exception("Error: file does not exist"); return File(ipdsf_file, "text/plain", Path.GetFileName(ipdsf_file)); } public ActionResult TimePivot(bool is_gaN = false, bool is_Si = false) { Tuple, List> tuple = _Background.GetTimePivot(isGaN: is_gaN, isSi: is_Si); ViewBag.forIPDSF = tuple.Item1; ViewBag.forPDSF = tuple.Item2; return View(); } IActionResult IHomeController.Background(bool? message_clear, bool? exceptions_clear, bool? set_is_primary_instance, bool? logistics_clear) => throw new NotImplementedException(); IActionResult IHomeController.DownloadCustomIPDSF(string ipdsf_file) => throw new NotImplementedException(); IActionResult IHomeController.DownloadCustomPDSF(string pdsf_file) => throw new NotImplementedException(); IActionResult IHomeController.DownloadIPDSF(string id) => throw new NotImplementedException(); IActionResult IHomeController.DownloadPDSF(string id) => throw new NotImplementedException(); IActionResult IHomeController.Encode(string value) => throw new NotImplementedException(); IActionResult IHomeController.Error() => throw new NotImplementedException(); IActionResult IHomeController.Index() => throw new NotImplementedException(); IActionResult IHomeController.IPDSF(string directory, string filter, bool is_gaN, bool is_Si) => throw new NotImplementedException(); IActionResult IHomeController.PDSF(string directory, string filter, bool is_gaN, bool is_Si) => throw new NotImplementedException(); IActionResult IHomeController.Privacy() => throw new NotImplementedException(); IActionResult IHomeController.TimePivot(bool is_gaN, bool is_Si) => throw new NotImplementedException(); } } // dotnet publish --configuration Release --runtime win-x64 --verbosity normal --self-contained true -o "L:\net5.0\APCViewer" // dotnet publish --configuration Release --runtime win-x64 --verbosity normal --self-contained true -o "D:\net5.0\APCViewer" // dotnet publish --configuration Release --runtime win-x64 --verbosity normal --self-contained true -o "D:\.jenkins\publish\manual-Mesa-0\APCViewer" //http://mestsa005.infineon.com:8080/job/Mesa/buildWithParameters?token=DotnetRules&projectName=APC%20Viewer // http://mestsa02ec.ec.local:8080/job/Mesa/buildWithParameters?token=DotnetRules&projectName=APC%20Viewer //sc create APCViewer_5003 binPath="D:\.jenkins\publish\manual-Mesa-0\APCViewer\APC Viewer.exe"