Files
lux/Lux.API/Controllers/WindowController.cs
T
2025-07-18 18:07:42 +02:00

159 lines
5.2 KiB
C#

using EgwMultiEngineManager;
using Lux.Data;
using Lux.Data.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace Lux.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class WindowController : ControllerBase
{
#region Public Constructors
public WindowController(IConfiguration config, ILogger<WindowController> logger, ImageCacheService imgServ, ExecProcessManager? EgwProcManager = null)
{
_logger = logger;
_config = config;
// verifico se usare engine
enableEgwEng = _config.GetValue<bool>("ServerConf:EgwEngineEnab");
if (enableEgwEng && EgwProcManager != null)
{
ProcessMan = EgwProcManager;
ProcessMan.m_AnswerReceived += ProcessMan_m_AnswerReceived;
}
ImgService = imgServ;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta
/// PUT: api/calc/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpPost("calc/{id}")]
public async Task<ActionResult<string>> calc(string id, [FromBody] string currJwd)
{
Stopwatch sw = new Stopwatch();
sw.Start();
lastSvg = "";
string svgContent = "";
// ...se ricevo percorso --> leggo jwd/svg cablato
if (!string.IsNullOrEmpty(currJwd))
{
if (enableEgwEng && ProcessMan != null)
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
DictExec.Add("Mode", "1");
DictExec.Add("Jwd", currJwd);
int nId = 1;
ProcessArgs currArgs = new ProcessArgs(nId, DictExec);
bool done = ProcessMan.ArgumentsEnqueue(currArgs);
waitResult = true;
int numWait = 200;
while (numWait > 0 && waitResult)
{
numWait--;
await Task.Delay(waitDelay);
}
}
// se ho risultato mostro...
if (!string.IsNullOrEmpty(lastSvg))
{
svgContent = lastSvg;
// salvo!
ImgService.SaveSvg(id, svgContent);
}
else
{
svgContent = "EMPTY";
}
}
sw.Stop();
_logger.LogInformation($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return Ok(svgContent);
}
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta
/// PUT: api/calc/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpGet("svg/{id}")]
public async Task<ActionResult<string>> svg(string id)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string svgContent = "";
// ...se ricevo percorso --> leggo jwd/svg cablato
if (!string.IsNullOrEmpty(id))
{
svgContent = ImgService.LoadSvg(id);
}
sw.Stop();
_logger.LogInformation($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return Ok(svgContent);
}
#endregion Public Methods
#region Private Fields
private readonly ILogger<WindowController> _logger;
private IConfiguration _config;
private bool enableEgwEng = true;
//private Dictionary<int, string> listSvg = new Dictionary<int, string>();
private string lastSvg = "";
private int waitDelay = 10;
private bool waitResult = false;
#endregion Private Fields
#region Private Properties
private ImageCacheService ImgService { get; set; }
/// <summary>
/// Esecutore di processi generico
/// </summary>
private ExecProcessManager? ProcessMan { get; set; }
#endregion Private Properties
#region Private Methods
/// <summary>
/// Restituisce una risposta all'esecuzione
/// </summary>
/// <param name="ProcessArgsResult"></param>
private void ProcessMan_m_AnswerReceived(ProcessArgsResult result)
{
// verifico che sia la mia richeista con id immagine... FARE!!!
// salvo il risultato...
if (result.Args != null && result.Args.Count > 0)
{
if (result.Args.ContainsKey("Svg"))
{
// salvo SVG
string newSvg = result.Args["Svg"];
// salvo nel dizionario
lastSvg = newSvg;
waitResult = false;
}
}
}
#endregion Private Methods
}
}