Files
webwindowconfigurator/Test.UI/Components/Pages/TestComponent.razor.cs
T
2025-08-07 14:35:19 +02:00

173 lines
5.2 KiB
C#

using EgwCoreLib.Lux.Core;
using EgwCoreLib.Lux.Data.Services;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
using NLog;
using WebWindowConfigurator;
using WebWindowConfigurator.DTO;
namespace Test.UI.Components.Pages
{
public partial class TestComponent : IDisposable
{
#region Public Properties
public Template SelTemplate { get; set; } = new Template(0, "---SELECT---", "", "");
#endregion Public Properties
#region Public Methods
public void Dispose()
{
DLService.CalcDonePipe.EA_NewMessage -= CalcDonePipe_EA_NewMessage;
}
#endregion Public Methods
#region Protected Fields
protected string currJwd = "...";
#endregion Protected Fields
#region Protected Properties
protected List<TemplateSelectDTO> AvailTemplateList { get; set; } = new List<TemplateSelectDTO>();
[Inject]
protected IConfiguration Config { get; set; } = null!;
[Inject]
protected DataLayerServices DLService { get; set; } = null!;
[Inject]
protected ImageCacheService ICService { get; set; } = null!;
protected MarkupString JsonSer
{
get => (MarkupString)currJwd.Replace(Environment.NewLine, "<br/>").Replace(" ", "&nbsp;");
}
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
ConfInit();
DLService.CalcDonePipe.EA_NewMessage += CalcDonePipe_EA_NewMessage;
await ReloadData();
}
#endregion Protected Methods
#region Private Fields
private string apiUrl = "";
private string calcTag = "";
private string cFile = "Data/Setup.json";
private TemplateSelectDTO? currSel = null;
private string currSvg = "";
private string imgBasePath = "";
private string imgTag = "";
private string subChannel = "";
#endregion Private Fields
#region Private Properties
private string windowUid = "CurrWindow";
//{
// get
// {
// string answ = "";
// if (currSel != null)
// {
// answ = currSel.SVGFileName.Replace(".svg", "");
// }
// return answ;
// }
//}
#endregion Private Properties
#region Private Methods
private async void CalcDonePipe_EA_NewMessage(object? sender, EventArgs e)
{
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (currArgs.msgUid.Equals($"{subChannel}:{windowUid}"))
{
currSvg = currArgs.newMessage;
}
await InvokeAsync(StateHasChanged);
}
await Task.Delay(1);
}
private void ConfInit()
{
apiUrl = Config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "";
imgBasePath = Config.GetValue<string>("ServerConf:ImageBaseUrl") ?? "";
imgTag = Config.GetValue<string>("ServerConf:ImageFileTag") ?? "";
calcTag = Config.GetValue<string>("ServerConf:ImageCalcTag") ?? "";
subChannel = Config.GetValue<string>("ServerConf:SvgChannel") ?? "";
}
/// <summary>
/// Rilettura dati
/// </summary>
/// <returns></returns>
private async Task ReloadData()
{
//return base.OnInitializedAsync();
AvailTemplateList = new List<TemplateSelectDTO>();
await Task.Delay(100);
// brutale, da rivedere...
if (File.Exists(cFile))
{
string rawVal = File.ReadAllText(cFile);
var rawList = JsonConvert.DeserializeObject<List<TemplateSelectDTO>>(rawVal) ?? new List<TemplateSelectDTO>();
// calcolo URL immagini... DTO interno da rivedere?
foreach (var item in rawList)
{
item.ImageUrl = ICService.ImageUrl($"{apiUrl}/{imgBasePath}", false, item.SVGFileName);
AvailTemplateList.Add(item);
}
}
}
private async Task SaveJWD(string newSerial)
{
currJwd = newSerial;
// chiamo la chiamata POST alla API, che manda la richiesta via REDIS
if (currSel != null)
{
await ICService.CallRestPost($"{apiUrl}/{imgBasePath}", $"{calcTag}/{windowUid}", currJwd);
}
}
private void SetTemplate(TemplateSelectDTO selTemp)
{
string rawVal = "";
currSel = selTemp;
// brutale,d a rivedere...
if (File.Exists(selTemp.JwdFileName))
{
rawVal = File.ReadAllText(selTemp.JwdFileName);
}
SelTemplate = new Template(selTemp.Index, selTemp.Description, selTemp.SVGFileName, rawVal);
}
#endregion Private Methods
}
}