using EgwCoreLib.Lux.Data.DbModel.Config; namespace Lux.API.Controllers { [Route("api/[controller]")] [ApiController] public class WindowController : ControllerBase { #region Public Constructors public WindowController(IConfiguration config, IRedisService redisService, IImageCacheService imgServ, IConfigDataService confServ, ICalcRuidService crService) { _config = config; _redisService = redisService; _imgService = imgServ; _confService = confServ; chPub = _config.GetValue("ServerConf:ChannelPub") ?? ""; _calcRuidService = crService; } #endregion Public Constructors #region Public Methods [Obsolete("Please use svg-preview")] [HttpPost("calc/{id}")] public async Task> calc(string id, [FromBody] string currJwd) { return await svgPreview(id, currJwd); } /// /// Chiamata POST: riceve Json in formato JWD serializzato, invia richiesta calcolo modo 2 (BOM) /// PUT: api/window/bom/00000000-0000-0000-0000-000000000000 /// /// id oggetto /// [HttpPost("bom/{id}")] public async Task> getBom(string id, [FromBody] string currJwd) { Stopwatch sw = new Stopwatch(); sw.Start(); string svgContent = ""; // se contiene ".svg" lo levo... if (id.EndsWith(".svg")) { id = id.Replace(".svg", ""); } // se messaggio vuoto --> uso default! currJwd = string.IsNullOrEmpty(currJwd) ? demoJwd : currJwd; // ...se ricevo percorso --> leggo jwd/svg cablato if (!string.IsNullOrEmpty(currJwd)) { // init vars Dictionary DictExec = new Dictionary(); var cMode = Egw.Window.Data.Enums.QuestionModes.BOM; DictExec.Add("Mode", $"{(int)cMode}"); // UID cablato x ora... DictExec.Add("UID", id); // creo registrazione richiesta... var ruid = await _calcRuidService.AddRequestAsync($"{cEnvir}", $"{cMode}", id); // aggiungo RUID effettivo DictExec.Add("RUID", ruid); DictExec.Add("SerializedData", currJwd); int nId = 1; // da modificare con tipo richiesta... QuestionDTO currArgs = new QuestionDTO(nId, EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW, DictExec); await _redisService.PublishAsync(chPub, currArgs.sProcessArgs); svgContent = "DONE"; } sw.Stop(); Log.Info($"getBom | {sw.Elapsed.TotalMilliseconds:N3} ms"); return Ok(svgContent); } /// /// Chiamata GET: invia richiesta modo 3 (HardwareModelList) che sarà poi salvata /// GET: api/window/hwlist /// /// id oggetto /// [HttpGet("hwlist")] public async Task> getHardwareList() { Stopwatch sw = new Stopwatch(); sw.Start(); string hwContent = ""; await sendHwReq(); hwContent = "DONE"; sw.Stop(); Log.Info($"getHardwareList | {sw.Elapsed.TotalMilliseconds:N3} ms"); return Ok(hwContent); } /// /// Chiamata GET: /// - se trova in cache risponde con elenco hw già ricevuto /// - se non trova invia richiesta modo 3 (HardwareModelList) che sarà poi salvata /// GET: api/window/hwlist/nome_produttore /// /// nome del produttore, es HW.AGB (default) /// [HttpGet("hwlist/{id}")] public async Task>> getHardwareList(string id) { Stopwatch sw = new Stopwatch(); sw.Start(); string hwReq = id ?? "HW.AGB"; var answ = await _confService.HwModelListAsync(EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW, hwReq); if (answ == null || answ.Count == 0) { await sendHwReq(); answ = new List(); } // filtro quelli default/non significativi (famili=descript) else { answ = answ .Where(x => !x.FamilyName.Equals(x.Description, StringComparison.OrdinalIgnoreCase)) .ToList(); } sw.Stop(); Log.Info($"getHardwareList | {sw.Elapsed.TotalMilliseconds:N3} ms"); return Ok(answ); } /// /// Chiamata GET: restituisce file SVG (da file o da cache) /// PUT: api/window/svg/00000000-0000-0000-0000-000000000000 /// /// id oggetto /// [HttpGet("svg/{id}")] public ActionResult svg(string id) { Stopwatch sw = new Stopwatch(); sw.Start(); string svgContent = ""; // ...se ricevo percorso --> leggo jwd/svg cablato if (!string.IsNullOrEmpty(id)) { // bonifica nome svg da svgContent = _imgService.LoadSvg(id); } sw.Stop(); Log.Info($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms"); return Ok(svgContent); } /// /// Chiamata GET: restituisce file SVG (da file o da cache) /// GET: api/window/svg-file/OFF000000001?env=WINDOW /// /// uid oggetto /// environment oggetto /// [HttpGet("svgfile/{id}")] [HttpGet("svg-file/{id}")] public async Task> svgFile(string id, EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS env = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW) { Stopwatch sw = new Stopwatch(); sw.Start(); if (string.IsNullOrEmpty(id)) return NotFound(); // se contiene ".svg" lo levo... if (id.EndsWith(".svg")) { id = id.Replace(".svg", ""); } // se contiene i caratteri casuali x forzare reload --> li levo if (id.Contains("-")) { id = id.Substring(0, id.IndexOf("-")); } string svgContent = await _imgService.LoadSvgAsync(id, env); // se vuoto --> leggo img logo... if (string.IsNullOrEmpty(svgContent)) { string filePath = Path.Combine("DemoImg", "LogoEgalware.svg"); svgContent = await System.IO.File.ReadAllTextAsync(filePath); } byte[] bytes = System.Text.Encoding.UTF8.GetBytes(svgContent); sw.Stop(); Log.Info($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms"); return File(bytes, "image/svg+xml"); } /// /// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta /// PUT: api/window/svg-preview/00000000-0000-0000-0000-000000000000 /// /// id oggetto /// [HttpPost("svgpreview/{id}")] [HttpPost("svg-preview/{id}")] public async Task> svgPreview(string id, [FromBody] string currJwd) { Stopwatch sw = new Stopwatch(); sw.Start(); string svgContent = ""; // se contiene ".svg" lo levo... if (id.EndsWith(".svg")) { id = id.Replace(".svg", ""); } // se messaggio vuoto --> uso default! currJwd = string.IsNullOrEmpty(currJwd) ? demoJwd : currJwd; // ...se ricevo percorso --> leggo jwd/svg cablato if (!string.IsNullOrEmpty(currJwd)) { // init vars Dictionary DictExec = new Dictionary(); var cMode = Egw.Window.Data.Enums.QuestionModes.PREVIEW; DictExec.Add("Mode", $"{(int)cMode}"); // UID cablato x ora... DictExec.Add("UID", id); // creo registrazione richiesta... var ruid = await _calcRuidService.AddRequestAsync($"{cEnvir}", $"{cMode}", id); // aggiungo RUID effettivo DictExec.Add("RUID", ruid); DictExec.Add("SerializedData", currJwd); int nId = 1; // da modificare con tipo richiesta... QuestionDTO currArgs = new QuestionDTO(nId, cEnvir, DictExec); await _redisService.PublishAsync(chPub, currArgs.sProcessArgs); svgContent = "DONE"; } sw.Stop(); Log.Info($"calcSvg | {sw.Elapsed.TotalMilliseconds:N3} ms"); return Ok(svgContent); } [HttpGet("version")] public string version() { var version = Assembly .GetExecutingAssembly() .GetName() .Version? .ToString() ?? "unknown"; return version; } #endregion Public Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private readonly ICalcRuidService _calcRuidService; private readonly IImageCacheService _imgService; private readonly IRedisService _redisService; private readonly string chPub = ""; private IConfiguration _config; private EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS cEnvir = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW; /// /// Demorichiesta jwd x fare test richiesta calcolo /// private string demoJwd = "{\r\n\"ProfilePath\":\"Profilo78\",\r\n\"AreaList\":[\r\n{\r\n\"Shape\":\"RECTANGLE\",\r\n\"DimensionList\":[\r\n{\r\n\"nIndex\":1,\r\n\"sName\":\"Width\",\r\n\"dValue\":1200.0\r\n},\r\n{\r\n\"nIndex\":2,\r\n\"sName\":\"Height\",\r\n\"dValue\":1500.0\r\n}\r\n],\r\n\"JointList\":[\r\n{\r\n\"nIndex\":1,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":2,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":3,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":4,\r\n\"JointType\":\"FULL_V\"\r\n}\r\n],\r\n\"BottomRail\":false,\r\n\"BottomRailQty\":0,\r\n\"AreaList\":[\r\n{\r\n\"bIsSashVertical\":true,\r\n\"SashList\":[\r\n{\r\n\"OpeningType\":\"TURNONLY_LEFT\",\r\n\"bHasHandle\":false,\r\n\"dDimension\":50.0\r\n},\r\n{\r\n\"OpeningType\":\"TILTTURN_RIGHT\",\r\n\"bHasHandle\":true,\r\n\"dDimension\":50.0\r\n}\r\n],\r\n\"SashType\":\"NULL\",\r\n\"JointList\":[\r\n{\r\n\"nIndex\":1,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":2,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":3,\r\n\"JointType\":\"FULL_V\"\r\n},\r\n{\r\n\"nIndex\":4,\r\n\"JointType\":\"FULL_V\"\r\n}\r\n],\r\n\"Hardware\":\"000000\",\r\n\"AreaList\":[\r\n{\r\n\"AreaList\":[\r\n{\r\n\"FillType\":\"GLASS\",\r\n\"AreaList\":[],\r\n\"AreaType\":\"FILL\"\r\n}\r\n],\r\n\"AreaType\":\"SPLITTED\"\r\n},\r\n{\r\n\"AreaList\":[\r\n{\r\n\"FillType\":\"GLASS\",\r\n\"AreaList\":[],\r\n\"AreaType\":\"FILL\"\r\n}\r\n],\r\n\"AreaType\":\"SPLITTED\"\r\n}\r\n],\r\n\"AreaType\":\"SASH\"\r\n}\r\n],\r\n\"AreaType\":\"FRAME\"\r\n}\r\n]\r\n}"; #endregion Private Fields #region Private Properties private readonly IConfigDataService _confService; #endregion Private Properties #region Private Methods /// /// Esegue invio effettivo richiesta elenco HW list /// /// private async Task sendHwReq() { // init vars Dictionary DictExec = new Dictionary(); var cMode = Egw.Window.Data.Enums.QuestionModes.HARDWARE; var cSubMode = Egw.Window.Data.Enums.QuestionHwSubModes.LIST; var cManufact = Egw.Window.Data.Enums.HardwareManufacturers.AGB; // compongo richiesta DictExec.Add("Mode", $"{(int)cMode}"); // aggiungo dati ID string uid = "HW.AGB"; DictExec.Add("UID", uid); // creo registrazione richiesta... var ruid = await _calcRuidService.AddRequestAsync($"{cEnvir}", $"{cMode}-{cSubMode}", uid); // aggiungo RUID effettivo DictExec.Add("RUID", ruid); DictExec.Add("SubMode", $"{(int)cSubMode}"); DictExec.Add("Manufacturer", $"{(int)cManufact}"); int nId = 1; // da modificare con tipo richiesta... QuestionDTO currArgs = new QuestionDTO(nId, cEnvir, DictExec); await _redisService.PublishAsync(chPub, currArgs.sProcessArgs); } #endregion Private Methods } }