using MP.Data.Conf; using MP.Data.DatabaseModels; using Newtonsoft.Json; using NLog; using StackExchange.Redis; using System.Text; namespace MP.WASM.Mon.Data { public class MpDataService : IDisposable { #region Public Fields public static MP.Data.Controllers.MpMonController dbController { get; set; } = null!; #endregion Public Fields #region Private Fields private static IConfiguration _configuration = null!; private static ILogger _logger = null!; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); /// /// Oggetto per connessione a REDIS /// private ConnectionMultiplexer redisConn = null!; //ISubscriber sub = redis.GetSubscriber(); /// /// Oggetto DB redis da impiegare x chiamate R/W /// private IDatabase redisDb = null!; #endregion Private Fields #region Public Constructors public MpDataService(IConfiguration configuration, ILogger logger) { _logger = logger; _configuration = configuration; // setup compoenti REDIS this.redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis")); this.redisDb = this.redisConn.GetDatabase(); //// setup canali pub/sub //actLogPipe = new MessagePipe(redisConn, Constants.ACT_LOG_M_QUEUE); // conf DB string connStr = _configuration.GetConnectionString("Mp.Mon"); if (string.IsNullOrEmpty(connStr)) { _logger.LogError("ConnString empty!"); } else { dbController = new MP.Data.Controllers.MpMonController(configuration); StringBuilder sb = new StringBuilder(); sb.AppendLine($"DbController OK"); //sb.AppendLine($"CST: {dbController.CustomersCount()} | CNT: {dbController.CountersCount()} | BSK: {dbController.BasketsCount()} | NGT: {dbController.NegotiationsCount()} | DOC: {dbController.DocsCount()} | ITM: {dbController.ItemsCount()} | RES: {dbController.ResourcesCount()}"); _logger.LogInformation(sb.ToString()); } // setup conf IOB da dizionario tryLoadIobTags(); } #endregion Public Constructors #region Public Properties /// /// Dizionario dei tag configurati per IOB /// public Dictionary> currTagConf { get; set; } = new Dictionary>(); #endregion Public Properties #region Public Methods public Task> ConfigGetAll() { return Task.FromResult(dbController.ConfigGetAll().ToList()); } public void Dispose() { // Clear database controller dbController.Dispose(); } /// /// restituisce il valore da REDIS associato al tag richeisto /// /// Chiave in cui cercare il valore /// public string getTagConf(string redKey) { string outVal = ""; // cerco in REDIS la conf x l'IOB var rawData = redisDb.StringGet(redKey); if (!string.IsNullOrEmpty(rawData)) { outVal = $"{rawData}"; } return outVal; } public Task> MacchineGetAll() { return Task.FromResult(dbController.MacchineGetAll().ToList()); } public Task> MseGetAll() { var dbResult = dbController.MseGetAll(); if (dbResult == null) { dbResult = new List(); } return Task.FromResult(dbResult); } #endregion Public Methods #region Private Methods /// /// Prova a caricare da file la conf degli IOB se presente /// private void tryLoadIobTags() { Dictionary> currConf = new Dictionary>(); string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location; if (!string.IsNullOrEmpty(strExeFilePath)) { string? strWorkPath = Path.GetDirectoryName(strExeFilePath); if (!string.IsNullOrEmpty(strWorkPath)) { string filePath = $"{strWorkPath}/Conf/iobTagsConf.json"; if (File.Exists(filePath)) { string rawData = File.ReadAllText(filePath); if (!string.IsNullOrEmpty(rawData)) { var fileConfData = JsonConvert.DeserializeObject(rawData); if (fileConfData != null) { // effettuo esplosione conf SE contenesse il valore "***" = tutti gli IOB if (fileConfData.IobSetup.ContainsKey("***")) { // recupero elenco macchine... var elencoMacc = dbController.MacchineGetAll(); // x ogni macchina creo le righe standard da conf... var baseConf = fileConfData.IobSetup.Where(x => x.Key == "***").FirstOrDefault(); foreach (var item in elencoMacc) { if (!string.IsNullOrEmpty(item.IdxMacchina)) { // converto i valori x la macchina corrente... // clono in nuovo oggetto var specVal = baseConf.Value.Select(i => i.Clone()).ToList(); // sostituisco segnaposto foreach (var singleVal in specVal) { singleVal.TagLocation = singleVal.TagLocation.Replace("***", item.IdxMacchina); } // ora aggiungo eventuali valori in override... if (fileConfData.IobSetup.ContainsKey(item.IdxMacchina)) { var otConf = fileConfData.IobSetup.Where(x => x.Key == item.IdxMacchina).FirstOrDefault(); //verifico x ogni valore other... foreach (var otTag in otConf.Value) { var ovrTag = specVal.Where(x => x.ColNum == otTag.ColNum && x.RowNum == otTag.RowNum).FirstOrDefault(); // se contiene --> sovrascrivo if (ovrTag != null) { //ovrTag = otTag.Clone(); specVal.Remove(ovrTag); specVal.Add(otTag.Clone()); } // se non contiene --> aggiungo else { specVal.Add(otTag); } } } currConf.Add(item.IdxMacchina, specVal); } } } // altrimenti copio ed ho finito else { currConf = fileConfData.IobSetup; } } } } if (currConf != null) { currTagConf = currConf; } } } } #endregion Private Methods } }