using EgwCoreLib.Lux.Data.Services; using EgwCoreLib.Lux.Data; using Newtonsoft.Json; using NLog; using StackExchange.Redis; using System.Diagnostics; using EgwMultiEngineManager.Data; namespace Lux.API.Services { public class ExternalMessageProcessor { #region Public Constructors /// /// Init classe message processor con accesso REDIS e DB /// /// /// public ExternalMessageProcessor(ImageCacheService imgService, DataLayerServices dlService) { cacheService = imgService; dbService = dlService; } #endregion Public Constructors private string redisBaseKey = "Lux:Cache"; #region Public Methods public async Task HandleResultMessageAsync(string channel, string message) { Log.Info($"Processing message from {channel} | {message.Length}"); Stopwatch sw = new Stopwatch(); sw.Start(); string rawData = $"{message}"; if (!string.IsNullOrEmpty(rawData) && rawData.Length > 2) { try { var retData = JsonConvert.DeserializeObject(rawData); if (retData != null) { if (retData.Args != null && retData.Args.Count > 0) { /*------------------------------------------ * Richieste "continue" di stato/update * * ----------------------------------------*/ // gestione ritorno preview SVG if (retData.Args.ContainsKey("Svg")) { // recupero UID ed SVG string UID = retData.Args["UID"]; string newSvg = retData.Args["Svg"]; // reinvio in redis (cache + channel) await cacheService.SaveSvgAsync(UID, retData.ExecEnvironment, newSvg); } // gestione ritorno preview PNG if (retData.Args.ContainsKey("Png")) { // recupero UID ed SVG string UID = retData.Args["UID"]; string newPng = retData.Args["Png"]; // reinvio in redis (cache + channel) await cacheService.SavePngAsync(UID, retData.ExecEnvironment, newPng); } // gesitone ritorno BOM if (retData.Args.ContainsKey("BOM")) { // recupero UID e BOM string UID = retData.Args["UID"]; string newBom = retData.Args["BOM"]; // salvo BOM ricevuta RAW in redis (cache) await cacheService.SaveBomAsync(UID, retData.ExecEnvironment, newBom); // salvo la BOM completa con i dati recuperati dal DB await dbService.SaveBomAsync(UID, retData.ExecEnvironment, newBom); // reset cache redis... await cacheService.ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:Offers:*"); await cacheService.ExecFlushRedisPatternAsync((RedisValue)$"{redisBaseKey}:OfferRows:*"); // pubblico refresh info così da segnalare richiesta di update conseguente await cacheService.PublishUpdateAsync(UID, retData.ExecEnvironment); } // gestione ritorno tipo Shape if (retData.Args.ContainsKey("SashShape")) { // recupero UID ed SVG string UID = retData.Args["UID"]; string newShape = retData.Args["SashShape"]; // reinvio in redis (cache + channel) await cacheService.SaveShapeAsync(UID, retData.ExecEnvironment, newShape); } // gestione ritorno opzioni HW if (retData.Args.ContainsKey("HardwareOptions")) { // recupero UID ed SVG string UID = retData.Args["UID"]; string newHwOpt = retData.Args["HardwareOptions"]; // reinvio in redis (cache + channel) await cacheService.SaveHwOptAsync(UID, retData.ExecEnvironment, newHwOpt); } /*------------------------------------------ * Richieste "one shot" di configurazione * - fare 1/gg o su richiesta utente * ----------------------------------------*/ // Gestione ritorno lista preferiti HW (ammissibili) if (retData.Args.ContainsKey("HardwareModelList")) { // recupero UID e lista preferiti HW string UID = retData.Args["UID"]; string newHml = retData.Args["HardwareModelList"]; // salvo HardwareList ricevuta await cacheService.SaveHmlAsync(UID, retData.ExecEnvironment, newHml); await dbService.SaveHmlAsync(UID, retData.ExecEnvironment, newHml); } // gestione ritorno lista profili VALIDI per JWD if (retData.Args.ContainsKey("ProfileList")) { // recupero UID e elenco profili validi string UID = retData.Args["UID"]; string profList = retData.Args["ProfileList"]; // salvo ProfileList ricevuta await cacheService.SaveProfileListAsync(UID, retData.ExecEnvironment, profList); await dbService.SaveProfileListAsync(UID, retData.ExecEnvironment, profList); } } } } catch (Exception exc) { Log.Error($"Errore in fase decodifica messaggio da REDIS Channel{Environment.NewLine}{exc}"); } } sw.Stop(); Log.Info($"HandleResultMessageAsync | {sw.Elapsed.TotalMilliseconds:N3} ms"); #if false // Process message (parse, validate, act) var payload = message; // or JSON-deserialize, etc. await _redisService.SetAsync("last:result", payload); #endif } #endregion Public Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private readonly ImageCacheService cacheService; private readonly DataLayerServices dbService; #endregion Private Fields } }