196 lines
6.4 KiB
C#
196 lines
6.4 KiB
C#
using NLog;
|
|
using NLua;
|
|
using System.Diagnostics;
|
|
|
|
namespace MP.MONO.DECODER
|
|
{
|
|
public class CounterManager
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Getione contatori x check variazioni e accumulazione conteggi
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Funzione chiamata (LUA) x controllo contatori + update in oggetto counter da persistere
|
|
/// </summary>
|
|
/// <param name="newStatusList">Situazione valori attuale</param>
|
|
/// <returns>Dizionario valori da salvare...</returns>
|
|
public Dictionary<string, double> processData(Dictionary<string, string> 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<string, string>();
|
|
}
|
|
if (lastStatusList.ContainsKey(item.Key))
|
|
{
|
|
lastStatusList[item.Key] = item.Value;
|
|
}
|
|
else
|
|
{
|
|
lastStatusList.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
// ritorno
|
|
return countAccum;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resetta l'accumulatore indicato
|
|
/// </summary>
|
|
/// <param name="accKey"></param>
|
|
/// <returns></returns>
|
|
public bool resetAccum(string accKey)
|
|
{
|
|
bool fatto = false;
|
|
if (countAccum.ContainsKey(accKey))
|
|
{
|
|
countAccum[accKey] = 0;
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Accumulatore valori counter
|
|
/// </summary>
|
|
protected static Dictionary<string, double> countAccum = new Dictionary<string, double>();
|
|
|
|
/// <summary>
|
|
/// Elenco ultimi valori ricevuti x confronto
|
|
/// </summary>
|
|
protected static Dictionary<string, string> lastStatusList = new Dictionary<string, string>();
|
|
|
|
/// <summary>
|
|
/// Log instance
|
|
/// </summary>
|
|
protected static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Path script LUA
|
|
/// </summary>
|
|
protected static string luaPath = "";
|
|
|
|
/// <summary>
|
|
/// OBJ state da scambiare
|
|
/// </summary>
|
|
protected static Lua state = new Lua();
|
|
|
|
/// <summary>
|
|
/// DataOra ultimo check x confronto
|
|
/// </summary>
|
|
protected DateTime? dtLastCheck = null;
|
|
|
|
#endregion Protected Fields
|
|
}
|
|
} |