Aggiunta refresh x update proj da REST a UI

This commit is contained in:
Samuele Locatelli
2024-07-01 15:21:53 +02:00
parent 1851a06041
commit f39e5802dd
12 changed files with 392 additions and 153 deletions
+7
View File
@@ -20,5 +20,12 @@ namespace MagMan.Core
public const string redisBaseAddr = "MagManUi";
public const string rKeyConfig = $"{redisBaseAddr}:Cache";
// REDIS Channels messaggi x QueueMan
public static readonly string Q_MMAN_GEN = $"MagManGeneral";
public static readonly string Q_MMAN_ALIAS_MAT = $"MagManAliasMaterial";
public static readonly string Q_MMAN_PROJ = $"MagManProj";
public static readonly string Q_MMAN_RES = $"MagManRes";
public static readonly string Q_MMAN_LOG = $"MagManLog";
}
}
+152
View File
@@ -0,0 +1,152 @@
using NLog;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagMan.Core
{
public class MessagePipe
{
#region Public Constructors
public MessagePipe(IConnectionMultiplexer redisConn, string channelName, bool enableLog = false)
{
_channel = new RedisChannel(channelName, RedisChannel.PatternMode.Literal); ;
redis = redisConn;
redisDb = redis.GetDatabase();
this.enableLog = enableLog;
// aggiungo sottoscrittore
setupSubscriber();
}
#endregion Public Constructors
#region Public Events
public event EventHandler EA_NewMessage = delegate { };
#endregion Public Events
#region Public Methods
/// <summary>
/// Invio messaggio sul canale + salvataggio in cache REDIS
/// </summary>
/// <param name="memKey">Chiave REDIS x salvare valore</param>
/// <param name="message"></param>
public bool saveAndSendMessage(string memKey, string message)
{
bool answ = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// invio notifica tramite il canale richiesto
answ = sendMessage(message);
if (redisDb != null)
{
redisDb.StringSetAsync(memKey, message);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
if (numSent.ContainsKey(memKey))
{
numSent[memKey]++;
}
else
{
numSent.Add(memKey, 1);
}
if (enableLog || numSent[memKey] > 30)
{
Log.Info($"saveAndSendMessage| mKey {memKey} x {numSent[memKey]} | {message.Length} size | {ts.TotalMilliseconds} ms");
numSent[memKey] = 0;
}
return answ;
}
/// <summary>
/// Invio messaggio sul canale
/// </summary>
/// <param name="newMess"></param>
/// <returns></returns>
public bool sendMessage(string newMess)
{
bool answ = false;
ISubscriber sub = redis.GetSubscriber();
sub.Publish(_channel, newMess);
return answ;
}
#endregion Public Methods
#region Protected Fields
protected static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Protected Fields
#region Private Fields
private bool enableLog = false;
private Dictionary<string, int> numSent = new Dictionary<string, int>();
private IConnectionMultiplexer redis;
private IDatabase? redisDb;
#endregion Private Fields
#region Private Properties
/// <summary>
/// Canale associato al gestore pipeline messaggi
/// </summary>
private RedisChannel _channel { get; set; } = new RedisChannel("Default", RedisChannel.PatternMode.Literal);
#endregion Private Properties
#region Private Methods
private void setupSubscriber()
{
ISubscriber sub = redis.GetSubscriber();
//Subscribe to the channel named messages
sub.Subscribe(_channel, (channel, message) =>
{
Log.Trace($"ch {channel} | {message}");
// messaggio
PubSubEventArgs mea = new PubSubEventArgs($"{message}");
// se qualcuno ascolta sollevo evento nuovo valore...
if (EA_NewMessage != null)
{
EA_NewMessage(this, mea);
}
});
Log.Info($"Subscribed {_channel}");
}
#endregion Private Methods
}
public class PubSubEventArgs : EventArgs
{
#region Public Constructors
public PubSubEventArgs(string messaggio)
{
this.newMessage = messaggio;
}
#endregion Public Constructors
#region Public Properties
public string newMessage { get; set; } = "";
#endregion Public Properties
}
}
+115 -75
View File
@@ -1,6 +1,7 @@
using Blazored.LocalStorage;
using Blazored.SessionStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.VisualBasic;
using NLog;
using StackExchange.Redis;
using System.Diagnostics;
@@ -19,27 +20,70 @@ namespace MagMan.Core.Services
// setup componenti REDIS
redisConn = RedConn;
redisDb = redisConn.GetDatabase();
// setup canali pub/sub
QueUpdGen = new MessagePipe(redisConn, Const.Q_MMAN_GEN);
QueUpdAliasMat = new MessagePipe(redisConn, Const.Q_MMAN_ALIAS_MAT);
QueUpdProj = new MessagePipe(redisConn, Const.Q_MMAN_PROJ);
QueUpdRes = new MessagePipe(redisConn, Const.Q_MMAN_RES);
QueUpdLog = new MessagePipe(redisConn, Const.Q_MMAN_LOG);
}
#endregion Public Constructors
#region Public Events
public event Action EA_FilterUpdated = null!;
public event Action EA_HideSearch = null!;
public event Action EA_PageUpdated = null!;
public event Action EA_SearchUpdated = null!;
public event Action EA_ShowSearch = null!;
public event Action EA_CustomerSel = null!;
public event Action EA_FilterUpdated = null!;
public event Action EA_HideSearch = null!;
public event Action EA_KeySel = null!;
public event Action EA_PageUpdated = null!;
public event Action EA_SearchUpdated = null!;
public event Action<bool> EA_ShowCustomers = null!;
public event Action EA_ShowSearch = null!;
#endregion Public Events
#region Public Properties
public string UserName { get; set; } = "NA";
public int CustomerID
{
get => _customerID;
set
{
if (_customerID != value)
{
_customerID = value;
if (EA_CustomerSel != null)
{
EA_CustomerSel?.Invoke();
}
}
}
}
public int KeyNum
{
get => _keyNum;
set
{
if (_keyNum != value)
{
_keyNum = value;
if (EA_KeySel != null)
{
EA_KeySel?.Invoke();
}
}
}
}
public string PageIcon
{
@@ -82,41 +126,24 @@ namespace MagMan.Core.Services
}
}
}
public int CustomerID
{
get => _customerID;
set
{
if (_customerID != value)
{
_customerID = value;
if (EA_CustomerSel != null)
{
EA_CustomerSel?.Invoke();
}
}
}
}
public int KeyNum
{
get => _keyNum;
set
{
if (_keyNum != value)
{
_keyNum = value;
if (EA_KeySel != null)
{
EA_KeySel?.Invoke();
}
}
}
}
public string SelOrderCode { get; set; } = "";
public string SelPlantId { get; set; } = "0";
public bool ShowCustomers
{
get => _showCustomers;
set
{
_showCustomers = value;
if (EA_ShowCustomers != null)
{
EA_ShowCustomers?.Invoke(value);
}
}
}
public bool ShowSearch
{
get => _showSearch;
@@ -143,18 +170,36 @@ namespace MagMan.Core.Services
}
}
public bool ShowCustomers
{
get => _showCustomers;
set
{
_showCustomers = value;
if (EA_ShowCustomers != null)
{
EA_ShowCustomers?.Invoke(value);
}
}
}
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Alias/Materials
/// </summary>
public MessagePipe QueUpdAliasMat { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW (General/Generics)
/// </summary>
public MessagePipe QueUpdGen { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info LogMachine
/// </summary>
public MessagePipe QueUpdLog { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Projects
/// </summary>
public MessagePipe QueUpdProj { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Resources
/// </summary>
public MessagePipe QueUpdRes { get; set; } = null!;
public string UserName { get; set; } = "NA";
#endregion Public Properties
#region Public Methods
/// <summary>
/// Cliente selezionato (da browser data cache)
@@ -173,28 +218,6 @@ namespace MagMan.Core.Services
await localStore.SetItemAsync("ClientID", newVal);
}
/// <summary>
/// KeyNum da cliente selezionato (da browser data cache)
/// </summary>
public async Task<int> KeyNumGet()
{
var answ = await localStore.GetItemAsync<int>("KeyNum");
return answ;
}
/// <summary>
/// Imposta KeyNum da cliente selezionato (browser data cache)
/// </summary>
public async Task KeyNumSet(int newVal)
{
await localStore.SetItemAsync("KeyNum", newVal);
}
#endregion Public Properties
#region Public Methods
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
@@ -220,6 +243,23 @@ namespace MagMan.Core.Services
return fatto;
}
/// <summary>
/// KeyNum da cliente selezionato (da browser data cache)
/// </summary>
public async Task<int> KeyNumGet()
{
var answ = await localStore.GetItemAsync<int>("KeyNum");
return answ;
}
/// <summary>
/// Imposta KeyNum da cliente selezionato (browser data cache)
/// </summary>
public async Task KeyNumSet(int newVal)
{
await localStore.SetItemAsync("KeyNum", newVal);
}
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
@@ -528,13 +568,13 @@ namespace MagMan.Core.Services
#region Private Fields
private int _customerID = -1;
private int _keyNum = -1;
private string _pageIcon = "";
private string _pageName = "";
private string _searchVal = "";
private int _customerID = -1;
private int _keyNum = -1;
private bool _showSearch = false;
private bool _showCustomers = true;
private bool _showSearch = false;
private Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
+28
View File
@@ -1,4 +1,6 @@
using EgwCoreLib.Razor;
using k8s.Models;
using MagMan.Core;
using MagMan.Core.Services;
using MagMan.Data.Admin.DbModels;
using MagMan.Data.Admin.Services;
@@ -29,6 +31,31 @@ namespace MagMan.UI.Components
public void Dispose()
{
AppMService.EA_SearchUpdated -= AppMService_EA_SearchUpdated;
AppMService.QueUpdProj.EA_NewMessage -= QueUpdProj_EA_NewMessage;
}
/// <summary>
/// Gestione messaggio proj
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
private async void QueUpdProj_EA_NewMessage(object? sender, EventArgs e)
{
// verifico se il messaggio sia per la mia KEY e nel caso --> refresh!
// aggiorno visualizzazione
PubSubEventArgs currArgs = (PubSubEventArgs)e;
// conversione on-the-fly SVG da mostrare
if (!string.IsNullOrEmpty(currArgs.newMessage))
{
if (KeyNum > 0 && currArgs.newMessage == $"{KeyNum}")
{
await ReloadData();
//await Task.Run(async () => await InvokeAsync(StateHasChanged));
}
}
}
#endregion Public Methods
@@ -101,6 +128,7 @@ namespace MagMan.UI.Components
{
currSearch = "";
AppMService.EA_SearchUpdated += AppMService_EA_SearchUpdated;
AppMService.QueUpdProj.EA_NewMessage += QueUpdProj_EA_NewMessage;
yLim = Configuration.GetValue<double>("OptConf:projProgYLim");
rLim = Configuration.GetValue<double>("OptConf:projProgRLim");
}
+8 -3
View File
@@ -1,6 +1,7 @@
using k8s.Models;
using MagMan.Core;
using MagMan.Core.DTO;
using MagMan.Core.Services;
using MagMan.Data.Admin.DbModels;
using MagMan.Data.Admin.Services;
using MagMan.Data.Tenant.DbModels;
@@ -20,10 +21,11 @@ namespace MagMan.UI.Controllers
{
#region Public Constructors
public ProjectsController(MTAdminService MTDataService, TenantService TDataService)
public ProjectsController(MTAdminService MTDataService, TenantService TDataService, MessageService messageService)
{
MTAdmService = MTDataService;
TService = TDataService;
MService = messageService;
// json serializer... FIX errore loop circolare https://www.ryadel.com/en/jsonserializationexception-self-referencing-loop-detected-error-fix-entity-framework-asp-net-core/
JSSettings = new JsonSerializerSettings()
{
@@ -162,9 +164,11 @@ namespace MagMan.UI.Controllers
{
Log.Error($"ProjectsController.upsert | Errore in fase salvataggio ProjectDTO{Environment.NewLine}{exc}");
}
// resetto cache redis
await MTAdmService.FlushRedisCache();
// broadcast msg update via redis, indico solo nKey x aggiornare tab principale dato KEY rif
MService.QueUpdProj.sendMessage($"{nKey}");
}
// resetto cache redis
await MTAdmService.FlushRedisCache();
}
}
return answ;
@@ -187,6 +191,7 @@ namespace MagMan.UI.Controllers
private MTAdminService MTAdmService { get; set; } = null!;
private TenantService TService { get; set; } = null!;
private MessageService MService { get; set; } = null!;
#endregion Private Properties
}
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>1.0.2406.2913</Version>
<Version>1.0.2407.0115</Version>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
+4
View File
@@ -1,3 +1,5 @@
using k8s.Models;
using MagMan.Core;
using MagMan.Core.Services;
using MagMan.Data.Admin.Services;
using MagMan.Data.Tenant.DbModels;
@@ -51,6 +53,8 @@ namespace MagMan.UI.Pages
await ReloadData();
}
protected void SaveProj(ProjModel? newRec)
{
ProjSel = newRec;
+1 -1
View File
@@ -32,7 +32,7 @@
"MultiRoleEnab": false,
"MultiClaimEnab": true,
"ScanOpDelay": 3000,
"projProgRLim": 0.4,
"projProgRLim": 0.2,
"projProgYLim": 0.8
},
"AlarmDest": "samuele.locatelli@egalware.com, ceo@steamware.net",
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>MagMan - Wood Warehouse Management System</i>
<h4>Versione: 1.0.2406.2913</h4>
<h4>Versione: 1.0.2407.0115</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.0.2406.2913
1.0.2407.0115
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.0.2406.2913</version>
<version>1.0.2407.0115</version>
<url>http://nexus.steamware.net/repository/SWS/MagMan/stable/0/MagMan.UI.zip</url>
<changelog>http://nexus.steamware.net/repository/SWS/MagMan/stable/0/ChangeLog.html</changelog>
<mandatory>false</mandatory>
+73 -70
View File
@@ -100,7 +100,6 @@ namespace DemoApp
// controllo ci sia qulcosa da inviare...
if (recList.Count > 0)
{
// fintanto che ce ne sono procedo...
while (numSent < num2send)
{
@@ -189,6 +188,79 @@ namespace DemoApp
return answ;
}
/// <summary>
/// Esegue invio info avanzamento di un SINGOLO PROD dato il suo Id + info avanzamento
/// </summary>
/// <param name="prodId">ID del progetto (locale)</param>
/// <param name="procTime">Tempo complessivo di lavorazione</param>
/// <param name="valAct">
/// Valore attuale a completamento (tipicamente numero barre/num pezzi DA FARE)
/// </param>
/// <param name="valMax">
/// Valore massimo a completamento (tipicamente numero barre/num pezzi DA FARE)
/// </param>
/// <returns>Risultato sincronizzazione</returns>
public SyncResult ProjSendProg(int prodId, double procTime, double valAct, double valMax)
{
SyncResult answ = SyncResult.ERR_ND;
// verifico server ok
bool servOk = commLib.CheckRemote();
if (!servOk)
{
answ = SyncResult.ERR_ServerKo;
}
else
{
using (ProdController dbContr = new ProdController())
{
// in primis recupero item
var currRec = dbContr.FindByProdId(prodId);
if (currRec == null)
{
answ = SyncResult.ERR_ProjIdNotFound;
}
else
{
// verifico ci sia cloud ID...
if (currRec.ProjCloudId == 0)
{
answ = SyncResult.ERR_ProjCloudIdNotFound;
}
else
{
try
{
// preparo obj da inviare...
ProjectProgrDTO projProgrDTO = new ProjectProgrDTO()
{
ProjCloudId = currRec.ProjCloudId,
ValAct = valAct,
ValMax = valMax,
ProcTimeReal = procTime
};
// invio e recupero ID...
bool fatto = commLib.ProjectProgrSend(projProgrDTO);
// se inviato, faccio upgrade locale...
if (!fatto)
{
answ = SyncResult.ERR_ProjNotSync;
}
else
{
answ = SyncResult.ALL_OK;
}
}
catch
{
answ = SyncResult.ERR_ServerKo;
}
}
}
}
}
return answ;
}
/// <summary>
/// Esegue sync di un SINGOLO PROD (cloud proj):
/// - legge il progetto locale + upload x sync (verso cloud)
@@ -265,75 +337,6 @@ namespace DemoApp
return answ;
}
/// <summary>
/// Esegue invio info avanzamento di un SINGOLO PROD dato il suo Id + info avanzamento
/// </summary>
/// <param name="prodId">ID del progetto (locale)</param>
/// <param name="procTime">Tempo complessivo di lavorazione</param>
/// <param name="valAct">Valore attuale a completamento (tipicamente numero barre/num pezzi DA FARE)</param>
/// <param name="valMax">Valore massimo a completamento (tipicamente numero barre/num pezzi DA FARE)</param>
/// <returns>Risultato sincronizzazione</returns>
public SyncResult ProjSendProg(int prodId, double procTime, double valAct, double valMax)
{
SyncResult answ = SyncResult.ERR_ND;
// verifico server ok
bool servOk = commLib.CheckRemote();
if (!servOk)
{
answ = SyncResult.ERR_ServerKo;
}
else
{
using (ProdController dbContr = new ProdController())
{
// in primis recupero item
var currRec = dbContr.FindByProdId(prodId);
if (currRec == null)
{
answ = SyncResult.ERR_ProjIdNotFound;
}
else
{
// verifico ci sia cloud ID...
if (currRec.ProjCloudId == 0)
{
answ = SyncResult.ERR_ProjCloudIdNotFound;
}
else
{
try
{
// preparo obj da inviare...
ProjectProgrDTO projProgrDTO = new ProjectProgrDTO()
{
ProjCloudId = currRec.ProjCloudId,
ValAct = valAct,
ValMax = valMax,
ProcTimeReal = procTime
};
// invio e recupero ID...
bool fatto = commLib.ProjectProgrSend(projProgrDTO);
// se inviato, faccio upgrade locale...
if (!fatto)
{
answ = SyncResult.ERR_ProjNotSync;
}
else
{
answ = SyncResult.ALL_OK;
}
}
catch
{
answ = SyncResult.ERR_ServerKo;
}
}
}
}
}
return answ;
}
#endregion Public Methods
#region Private Fields