using NLog; using NLua; using System.Diagnostics; namespace MP.MONO.DECODER { public class CounterManager { #region Public Constructors /// /// Getione contatori x check variazioni e accumulazione conteggi /// public CounterManager() { // preparo variabile x avvisare script che il modo è da NLua state["callMode"] = "NLua"; // carico il file luaPath = Path.Combine(Directory.GetCurrentDirectory(), "lua", "CountersDecoder.lua"); state.DoFile(luaPath); Log.Info("CounterManager OK"); Console.WriteLine("CounterManager OK"); } #endregion Public Constructors #region Public Methods /// /// Funzione chiamata (LUA) x controllo contatori + update in oggetto counter da persistere /// /// Situazione valori attuale /// Dizionario valori da salvare... public Dictionary processData(Dictionary newStatusList) { DateTime adesso = DateTime.Now; double delta = 0; // accumulo frazioni di minuto solo se ho un dato temporale precedente if (dtLastCheck != null) { delta = adesso.Subtract((DateTime)dtLastCheck).TotalMinutes; } // preparo esecuzione bool calcOk = false; if (lastStatusList != null && lastStatusList.Count > 0) { state["delta"] = delta; // tab dataList state.NewTable("dataList"); var currTabData = state.GetTable("dataList"); foreach (var item in lastStatusList) { currTabData[item.Key] = item.Value; } state["dataList"] = currTabData; // tab countAcc state.NewTable("countAcc"); var currTabCount = state.GetTable("countAcc"); foreach (var item in countAccum) { currTabCount[item.Key] = item.Value; } state["countAcc"] = currTabCount; // call! try { // effettuo calcolo state.DoString("doProcess()"); } catch (Exception exc) { Log.Error($"exception during doProcess{Environment.NewLine}{exc}"); } // recupero valore calcolato bool.TryParse(state.GetString("calcOk"), out calcOk); if (calcOk) { try { // leggo nuovi valori accumulati.. var tabAccum = state.GetTable("countAcc"); //var dictAccum = state.GetTableDict(tabAccum); var dictAccum = state["countAcc"]; // vado a fare upgrade... string sKey = ""; string sVal = ""; double dVal = 0; foreach (var item in tabAccum.Keys) { sKey = $"{item}"; sVal = $"{tabAccum[item]}"; dVal = 0; double.TryParse(sVal, out dVal); if (countAccum.ContainsKey(sKey)) { countAccum[sKey] = dVal; } else { countAccum.Add(sKey, dVal); } } // trace... stampo counters... foreach (var cItem in countAccum.OrderBy(x => x.Key)) { Log.Trace($"COUNTERS | {cItem.Key} | {cItem.Value}"); } } catch (Exception exc) { Log.Error($"exception during LUA processData{Environment.NewLine}{exc}"); } } } // salvo lastCheck dtLastCheck = adesso; // salvo ultimo stato da merge con precedente... foreach (var item in newStatusList) { if (lastStatusList == null) { lastStatusList = new Dictionary(); } if (lastStatusList.ContainsKey(item.Key)) { lastStatusList[item.Key] = item.Value; } else { lastStatusList.Add(item.Key, item.Value); } } // ritorno return countAccum; } /// /// Resetta l'accumulatore indicato /// /// /// public bool resetAccum(string accKey) { bool fatto = false; if (countAccum.ContainsKey(accKey)) { countAccum[accKey] = 0; } return fatto; } #endregion Public Methods #region Protected Fields /// /// Accumulatore valori counter /// protected static Dictionary countAccum = new Dictionary(); /// /// Elenco ultimi valori ricevuti x confronto /// protected static Dictionary lastStatusList = new Dictionary(); /// /// Log instance /// protected static Logger Log = LogManager.GetCurrentClassLogger(); /// /// Path script LUA /// protected static string luaPath = ""; /// /// OBJ state da scambiare /// protected static Lua state = new Lua(); /// /// DataOra ultimo check x confronto /// protected DateTime? dtLastCheck = null; #endregion Protected Fields } }