Files
lux/Lux.API/Controllers/WindowController.cs
T
2025-08-04 11:05:53 +02:00

197 lines
8.1 KiB
C#

using EgwCoreLib.Lux.Data.Services;
using EgwMultiEngineManager;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System.Diagnostics;
namespace Lux.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class WindowController : ControllerBase
{
#region Public Constructors
public WindowController(IConfiguration config, ImageCacheService imgServ, IRedisService redisService)
{
_config = config;
_redisService = redisService;
_imgService = imgServ;
pubChannel = _config.GetValue<string>("ServerConf:PubChannel") ?? "";
}
#endregion Public Constructors
#region Public Methods
[Obsolete("Please use svg-preview")]
[HttpPost("calc/{id}")]
public async Task<ActionResult<string>> calc(string id, [FromBody] string currJwd)
{
return await svgPreview(id, currJwd);
}
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, invia richeista calcolo modo 2 (BOM)
/// PUT: api/svgPreview/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpPost("bom/{id}")]
public async Task<ActionResult<string>> 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))
{
if (true)
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
DictExec.Add("Mode", "2");
// UID cablato x ora...
DictExec.Add("UID", id);
DictExec.Add("Jwd", currJwd);
int nId = 1;
ProcessArgs currArgs = new ProcessArgs(nId, DictExec);
await _redisService.PublishAsync(pubChannel, currArgs.sProcessArgs);
svgContent = "DONE";
}
}
sw.Stop();
Log.Info($"getBom | {sw.Elapsed.TotalMilliseconds:N3} ms");
return Ok(svgContent);
}
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta
/// PUT: api/svgPreview/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();
Log.Info($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return Ok(svgContent);
}
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta
/// PUT: api/svgPreview/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpGet("svgfile/{id}")]
[HttpGet("svg-file/{id}")]
public async Task<ActionResult<string>> svgFile(string id)
{
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", "");
}
string svgContent = await _imgService.LoadSvgAsync(id);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(svgContent);
sw.Stop();
Log.Info($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return File(bytes, "image/svg+xml");
}
/// <summary>
/// Chiamata POST: riceve Json in formato JWD serializzato, lo ripete come risposta
/// PUT: api/svg-preview/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpPost("svg-preview/{id}")]
public async Task<ActionResult<string>> 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))
{
if (true)
{
Dictionary<string, string> DictExec = new Dictionary<string, string>();
DictExec.Add("Mode", "1");
// UID cablato x ora...
DictExec.Add("UID", id);
DictExec.Add("Jwd", currJwd);
int nId = 1;
ProcessArgs currArgs = new ProcessArgs(nId, DictExec);
await _redisService.PublishAsync(pubChannel, currArgs.sProcessArgs);
svgContent = "DONE";
}
}
sw.Stop();
Log.Info($"calcSvg | {sw.Elapsed.TotalMilliseconds:N3} ms");
return Ok(svgContent);
}
#endregion Public Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private readonly IRedisService _redisService;
private readonly string pubChannel = "";
private IConfiguration _config;
/// <summary>
/// Demorichiesta jwd x fare test richiesta calcolo
/// </summary>
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 ImageCacheService _imgService { get; set; }
#endregion Private Properties
}
}