using Microsoft.AspNetCore.Mvc; using NLog; using SkiaSharp; using Svg; using Svg.Skia; using System.Drawing; using WebDoorCreator.Data.Services; #if false using SkiaSharp; using Svg.Skia; #endif namespace WebDoorCreator.API.Controllers { [Route("api/[controller]")] [ApiController] public class DoorImageController : ControllerBase { #region Public Constructors public DoorImageController(IConfiguration configuration, QueueDataService cQDService, WebDoorCreatorService cWDCService) { Log.Info("Starting DoorImageController"); _configuration = configuration; WaitReloadSvg = _configuration.GetValue("RuntimeOpt:WaitReloadSvg"); QDService = cQDService; WDService = cWDCService; Log.Info("Avviato DoorImageController"); } #endregion Public Constructors #region Public Methods // GET: api/Alive [HttpGet] public string Get() { return "OK"; } [HttpGet("GetImage.png")] public async Task GetImagePng(int DoorId) { byte[] result = new byte[0]; string svgContent = await QDService.DoorGetLastSvg(DoorId); // se fosse vuoto... if (string.IsNullOrEmpty(svgContent)) { // richiede ricalcolo img svgContent = await SendRecalcReq(DoorId); } // se fosse vuoto... if (string.IsNullOrEmpty(svgContent)) { // legge img vuota svgContent = QDService.DoorGetMissingSvg(); } // ora prosegue con conversione... if (!string.IsNullOrEmpty(svgContent)) { using (var svg = new SKSvg()) { if (svg.FromSvg(svgContent) is { }) { using (var stream = new MemoryStream()) { svg.Picture?.ToImage(stream, SKColors.Empty, SKEncodedImageFormat.Png, 100, 1f, 1f, SKColorType.Rgba8888, SKAlphaType.Unpremul, SKColorSpace.CreateSrgb()); result = stream.ToArray(); } } } #if false var mySvg = SvgDocument.FromSvg(svgContent); //result = System.Text.Encoding.UTF8.GetBytes(svgContent); var myBmp = mySvg.Draw(); using (var stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); result= stream.ToArray(); } #endif } return File(result, "image/png"); } [HttpGet("GetImage.svg")] public async Task GetImageSvg(int DoorId) { string svgContent = await QDService.DoorGetLastSvg(DoorId); // se fosse vuoto... if (string.IsNullOrEmpty(svgContent)) { // richiede ricalcolo img await SendRecalcReq(DoorId); // 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 IConfiguration _configuration = null!; private static Logger Log = LogManager.GetCurrentClassLogger(); 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) { string answ = ""; // richiede ricalcolo img List doorIdList = new List() { $"{DoorId}" }; // chiamo reset richieste var list2Proc = await QDService.ResetQueueByDoorList(doorIdList); // recupero DDF ed invio x processing if (DoorId > 0) { string currDDF = await WDService.DoorOpGetDDF(DoorId); // versione corrente del DDF generato int currVers = await QDService.SendCalcReq(DoorId, currDDF); } // attende ... await Task.Delay(WaitReloadSvg); // riprova lettura answ = await QDService.DoorGetLastSvg(DoorId); return answ; } #endregion Private Methods } }