using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using NLog; using NLog.Fluent; using WebDoorCreator.Data.Services; using static System.Net.Mime.MediaTypeNames; namespace WebDoorCreator.UI.Controllers { [Route("api/[controller]")] [ApiController] public class DoorImageController : ControllerBase { #region Public Constructors public DoorImageController(IConfiguration configuration, QueueDataService cQDService, WebDoorCreatorService cWDCService) { Log.Trace("Starting DoorImageController"); _configuration = configuration; WaitReloadSvg = _configuration.GetValue("RuntimeOpt:WaitReloadSvg"); QDService = cQDService; WDService = cWDCService; Log.Trace("Avviato DoorImageController"); } #endregion Public Constructors private Random rnd = new Random(); #region Public Methods // GET: api/DoorImage [HttpGet] public string Get() { return "OK"; } [HttpGet("GetImage.svg")] public async Task GetImage(int DoorId) { string svgContent = await QDService.DoorGetLastSvg(DoorId); // se fosse vuoto... if (string.IsNullOrEmpty(svgContent)) { string vetoReq = await QDService.DoorProcVetoGetAsync(DoorId); if (string.IsNullOrEmpty(vetoReq)) { await QDService.DoorProcVetoSetAsync(DoorId, WaitReloadSvg * 20); // richiede ricalcolo img await SendRecalcReq(DoorId); // attende ... await Task.Delay(WaitReloadSvg); // riprova lettura svgContent = await QDService.DoorGetLastSvg(DoorId); } else { // attesa causale per vedere SE riesce a leggere ugualmente...faccio rand (4..10)*wait reload... await Task.Delay(WaitReloadSvg * rnd.Next(4, 10)); // riprovo lettura svgContent = await QDService.DoorGetLastSvg(DoorId); } } // se fosse vuoto... if (string.IsNullOrEmpty(svgContent)) { // legge img vuota svgContent = QDService.DoorGetMissingSvg(); } var result = System.Text.Encoding.UTF8.GetBytes(svgContent); return File(result, "image/svg+xml"); } #endregion Public Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private IConfiguration _configuration = null!; private int WaitReloadSvg = 100; #endregion Private Fields #region Private Properties private QueueDataService QDService { get; set; } = null!; private WebDoorCreatorService WDService { get; set; } = null!; #endregion Private Properties #region Private Methods /// /// Invio richiesta ricalcolo porta /// /// /// private async Task SendRecalcReq(int DoorId) { if (DoorId > 0) { // richiede ricalcolo img List doorIdList = new List() { $"{DoorId}" }; // chiamo reset richieste var list2Proc = await QDService.ResetQueueByDoorList(doorIdList); await Task.Delay(WaitReloadSvg); // recupero DDF ed invio x processing string currDDF = await WDService.DoorOpGetDDF(DoorId); // versione corrente del DDF generato int currVers = await QDService.SendCalcReq(DoorId, currDDF); await Task.Delay(WaitReloadSvg); } } #endregion Private Methods #if false [HttpGet("GetImage.png")] public async Task GetImagePng(int DoorId) { byte[] result = new byte[0]; string svgContent = await QDataServ.DoorGetLastSvg(DoorId); if (!string.IsNullOrEmpty(svgContent)) { var mySvg = SvgDocument.FromSvg(svgContent); //result = System.Text.Encoding.UTF8.GetBytes(svgContent); var myBmp = mySvg.Draw(); result = Utils.ImageToByte2(myBmp); } return File(result, "image/png"); } #endif } }