155 lines
5.4 KiB
C#
155 lines
5.4 KiB
C#
using MP.MONO.Core;
|
|
using MP.MONO.Core.DTO;
|
|
using MP.MONO.Data.DbModels;
|
|
using NLog;
|
|
using NLua;
|
|
|
|
namespace MP.MONO.DECODER
|
|
{
|
|
public class ToolsManager
|
|
{
|
|
#region Private Fields
|
|
|
|
/// <summary>
|
|
/// Dizionario dei valori accumulati sulle variabili
|
|
/// </summary>
|
|
private Dictionary<string, VCData> VarAccumulator = new Dictionary<string, VCData>();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
protected static Logger Log = LogManager.GetCurrentClassLogger();
|
|
protected static string luaPath = "";
|
|
protected static Lua state = new Lua();
|
|
protected List<string> AlarmList = new List<string>();
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Fields
|
|
|
|
public static string alarmStatus = "";
|
|
public static bool valueChanged = false;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public ToolsManager()
|
|
{
|
|
// preparo variabile x avvisare script che il modo è da NLua
|
|
state["callMode"] = "NLua";
|
|
state["vcFunct"] = "AVG";
|
|
|
|
// carico il file (derivato da quello dei parametri)
|
|
luaPath = Path.Combine(Directory.GetCurrentDirectory(), "lua", "ToolsDecoder.lua");
|
|
state.DoFile(luaPath);
|
|
|
|
Log.Info("ToolsManager OK");
|
|
Console.WriteLine("ToolsManager OK");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Funzione chiamata LUA x calcolo degli eventuali tools val con periodi "scaduti"
|
|
/// </summary>
|
|
/// <param name="paramsList"></param>
|
|
/// <param name="redisToolsConf"></param>
|
|
/// <returns></returns>
|
|
public List<DataLogModel> processData(Dictionary<string, double> paramsList, List<DisplayDataDTO> redisToolsConf)
|
|
{
|
|
List<DataLogModel> answ = new List<DataLogModel>();
|
|
DateTime adesso = DateTime.Now;
|
|
|
|
// vado ad "accumulare i dati" a quelli presenti...
|
|
foreach (var item in paramsList)
|
|
{
|
|
var currParam = redisToolsConf.FirstOrDefault(x => x.Title == item.Key);
|
|
// cerco nelle variabili accomulatori...
|
|
if (!VarAccumulator.ContainsKey(item.Key))
|
|
{
|
|
if (currParam != null)
|
|
{
|
|
var dataList = new List<double>();
|
|
dataList.Add(item.Value);
|
|
VCData newSet = new VCData()
|
|
{
|
|
dataArray = dataList,
|
|
DTStart = adesso,
|
|
Funzione = currParam.VcFunc,
|
|
Period = currParam.SamplePeriod
|
|
};
|
|
// se non ci fosse creo
|
|
VarAccumulator.Add(item.Key, newSet);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// altrimenti aggiorno
|
|
VarAccumulator[item.Key].dataArray.Add(item.Value);
|
|
}
|
|
|
|
// effettuo verifiche scadenza
|
|
bool calcOk = false;
|
|
double calcVal = 0;
|
|
if (VarAccumulator.ContainsKey(item.Key))
|
|
{
|
|
if (VarAccumulator[item.Key].isElapsed)
|
|
{
|
|
string funcMode = $"{VarAccumulator[item.Key].Funzione}";
|
|
// se scaduto --> mando a LUA x calcolo
|
|
state["valList"] = VarAccumulator[item.Key].dataArray;
|
|
state["numRec"] = VarAccumulator[item.Key].dataArray.Count;
|
|
state["vcFunct"] = funcMode;
|
|
|
|
// effettuo calcolo
|
|
state.DoString("doProcess()");
|
|
|
|
// recupero valore calcolato
|
|
bool.TryParse(state.GetString("calcOk"), out calcOk);
|
|
if (calcOk)
|
|
{
|
|
try
|
|
{
|
|
var stringVal = state.GetString("calcVal");
|
|
if (!string.IsNullOrEmpty(stringVal))
|
|
{
|
|
calcVal = state.GetNumber("calcVal");
|
|
Log.Trace($"calcVal: {calcVal}");
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"exception during processData for {item.Key}{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
|
|
// aggiungo alla lista finale...
|
|
var dbRecord = new DataLogModel()
|
|
{
|
|
DtRif = adesso,
|
|
FluxType = item.Key,
|
|
MachineId = 1,
|
|
DataType = Enums.DataLogType.Tools,
|
|
ValNum = calcVal,
|
|
ValStr = $"{calcVal:N3}"
|
|
|
|
};
|
|
answ.Add(dbRecord);
|
|
|
|
// elimino dai valori accumulati...
|
|
VarAccumulator.Remove(item.Key);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |