156 lines
4.9 KiB
C#
156 lines
4.9 KiB
C#
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<int>("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<IActionResult> 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<SvgDocument>(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<IActionResult> 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
|
|
|
|
/// <summary>
|
|
/// Invio richiesta ricalcolo porta
|
|
/// </summary>
|
|
/// <param name="DoorId"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> SendRecalcReq(int DoorId)
|
|
{
|
|
string answ = "";
|
|
// richiede ricalcolo img
|
|
List<string> doorIdList = new List<string>() { $"{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
|
|
}
|
|
} |