diff --git a/MP.MONO.DECODER/ParamsManager.cs b/MP.MONO.DECODER/ParamsManager.cs index e304619..f12fd17 100644 --- a/MP.MONO.DECODER/ParamsManager.cs +++ b/MP.MONO.DECODER/ParamsManager.cs @@ -41,8 +41,10 @@ namespace MP.MONO.DECODER luaPath = Path.Combine(Directory.GetCurrentDirectory(), "lua", "ParamsDecoder.lua"); state["callMode"] = "NLua"; - state["lastVal"] = 0; - state["currVal"] = 0; + state["vcFunct"] = "AVG"; + + // carico il file + state.DoFile(luaPath); Log.Info("ParamsManager OK"); Console.WriteLine("ParamsManager OK"); @@ -92,27 +94,34 @@ namespace MP.MONO.DECODER } // effettuo verifiche scadenza + bool calcOk = false; + double calcVal = 0; if (ParamsAccumulator.ContainsKey(item.Key)) { if (ParamsAccumulator[item.Key].isElapsed) { + string funcMode = $"{ParamsAccumulator[item.Key].Funzione}"; // se scaduto --> mando a LUA x calcolo + state["valList"] = ParamsAccumulator[item.Key].dataArray; + state["numRec"] = ParamsAccumulator[item.Key].dataArray.Count; + state["vcFunct"] = funcMode; - // FIXME TODO - var calcVal = ParamsAccumulator[item.Key].dataArray.FirstOrDefault(); + //// effettuo calcolo + //state.DoFile(luaPath); + state.DoString("doProcess()"); + + // recupero valore calcolato + bool.TryParse(state.GetString("calcOk"), out calcOk); + if (calcOk) + { + calcVal = state.GetNumber("calcVal"); + Log.Trace($"calcVal: {calcVal}"); + } + + //// FIXME TODO + //var calcVal = ParamsAccumulator[item.Key].dataArray.FirstOrDefault(); #if false - // processing LUA del calcolo dei dati "scaduti" - //state["prefix"] = prefix; - //state["alarmList"] = alarmList; - //state["numAlarm"] = alarmList.Count; - //state["muteMask"] = muteMask; - //state["disableMask"] = disableMask; - // invio dati aggiornati a LUA - state["lastVal"] = lastVal; - state["currVal"] = currVal; - - state.DoFile(luaPath); alarmStatus = state.GetString("alarmStatus"); // recupero NUOVO valore (filtrato) attuale da salvare currVal = (uint)state.GetNumber("lastVal"); diff --git a/MP.MONO.DECODER/lua/ParamsDecoder.lua b/MP.MONO.DECODER/lua/ParamsDecoder.lua index c9470cf..b56e76c 100644 --- a/MP.MONO.DECODER/lua/ParamsDecoder.lua +++ b/MP.MONO.DECODER/lua/ParamsDecoder.lua @@ -1,9 +1,10 @@ ---[[--------------------------------------------------- +--[[--------------------------------------------------- Procedura calcolo statistiche parametri: Variabili IN: - vcFunct(string): tipo di processing da effettuare tra [POINT/AVG/MEDIAN/MIN/MAX] - valList(): elenco VALORI double da processare +- numRec(int): conteggio numero recorda da processare Variabili OUT: - calcVal(double): valore calcolato finale @@ -17,60 +18,65 @@ if not package.path:find(ZBS,1,true) then package.cpath = ZBS .. "/bin/?.dll;" .. ZBS .. "/bin/clibs53/?.dll;" .. package.cpath end --- variabile semaforo callMode (locali o remote da NLua) -callMode = callMode or '' -vcFunct = vcFunct or '' -valList = valList or {} -calcVal = 0 -calcOk = false - -- se non è da NLua inizializzo variabili accessorie local function checkInit() if (callMode ~= 'NLua') then -- imposto valori test - valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 } - vcFunct = 'MIN' + valList = { 4467, 4468, 4467, 4467, 4467, 4467 } + numRec = #valList + --valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 } + vcFunct = 'AVG' --POINT AVG MEDIAN MIN MAX end end +local function setupTable() + if numRec > 0 and valList[0] ~= nil then + for i = numRec, 1, -1 do + valListTable[i] = valList[i-1] + end + else + valListTable = valList + end +end + local function doCalc() - if(#valList>0) then + if(numRec > 0) then -- verifica il tipo di richiesta if(vcFunct == 'AVG') then s = 0 - for i,v in ipairs(valList) do + for i,v in ipairs(valListTable) do s = s + v end - calcVal = s / #valList + calcVal = s / numRec elseif(vcFunct == 'POINT') then - calcVal = valList[#valList] + calcVal = valListTable[numRec] elseif(vcFunct == 'MEDIAN') then - table.sort(valList) - calcVal = valList[#valList/2] + table.sort(valListTable) + calcVal = valListTable[numRec/2] elseif(vcFunct == 'MIN') then - table.sort(valList) - calcVal = valList[1] + table.sort(valListTable) + calcVal = valListTable[1] elseif(vcFunct == 'MAX') then - table.sort(valList) - calcVal = valList[#valList] + table.sort(valListTable) + calcVal = valListTable[numRec] end calcOk = true else - calcVal = 0 - calcOk = false + calcVal = -999999 + numRec = 1 + calcOk = false end end local function displayTestInfo() if (callMode ~= 'NLua') then print('------------------------------') - print('calcOk: ' .. tostring(calcOk)) - print('vcFunct: ' .. vcFunct) - for i,val in pairs(valList) do + print('calcOk: ' .. tostring(calcOk) .. ' | vcFunct: ' .. vcFunct) + for i,val in pairs(valList) do print("v_"..i.." | "..val) - end + end print('calcVal: ' .. calcVal) print('------------------------------') end @@ -78,7 +84,22 @@ end -- MAIN +function doProcess() + -- variabile semaforo callMode (locali o remote da NLua) + callMode = callMode or '' + calcVal = -999 + vcFunct = vcFunct or '' + valList = valList or {} + numRec = numRec or 1 + calcOk = false + valListTable = {} -checkInit() -doCalc() -displayTestInfo() + checkInit() + setupTable() + doCalc() + displayTestInfo() +end + +if (callMode ~= 'NLua') then + doProcess() +end diff --git a/MP.MONO.DECODER/lua/ParamsDecoder2.lua b/MP.MONO.DECODER/lua/ParamsDecoder2.lua new file mode 100644 index 0000000..5e2fbdf --- /dev/null +++ b/MP.MONO.DECODER/lua/ParamsDecoder2.lua @@ -0,0 +1,98 @@ +--[[--------------------------------------------------- +Procedura calcolo statistiche parametri: + +Variabili IN: +- vcFunct(string): tipo di processing da effettuare tra [POINT/AVG/MEDIAN/MIN/MAX] +- valList(): elenco VALORI double da processare +- numRec(int): conteggio numero recorda da processare + +Variabili OUT: +- calcVal(double): valore calcolato finale +-----------------------------------------------------]] + + +-- Per eventuale debug +local ZBS = "c:/ZeroBraneStudio" +if not package.path:find(ZBS,1,true) then + package.path = ZBS .. "/lualibs/?/?.lua;" .. ZBS .. "/lualibs/?.lua;" .. package.path + package.cpath = ZBS .. "/bin/?.dll;" .. ZBS .. "/bin/clibs53/?.dll;" .. package.cpath +end + +-- se non è da NLua inizializzo variabili accessorie +local function checkInit() + if (callMode ~= 'NLua') then + -- imposto valori test + valList = { 4467, 4468, 4467, 4467, 4467, 4467 } + --valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 } + vcFunct = 'AVG' + --POINT AVG MEDIAN MIN MAX + end +end + +local function setupTable() + if numRec > 0 and valList[0] ~= nil then + for i = numRec, 1, -1 do + valListTable[i] = valList[i-1] + end + else + valListTable = valList + end +end + + +local function doCalc() + if(numRec > 0) then + -- verifica il tipo di richiesta + if(vcFunct == 'AVG') then + s = 0 + for i,v in ipairs(valListTable) do + s = s + v + end + calcVal = s / numRec + elseif(vcFunct == 'POINT') then + calcVal = valListTable[numRec] + elseif(vcFunct == 'MEDIAN') then + table.sort(valListTable) + calcVal = valListTable[numRec/2] + elseif(vcFunct == 'MIN') then + table.sort(valListTable) + calcVal = valListTable[1] + elseif(vcFunct == 'MAX') then + table.sort(valListTable) + calcVal = valListTable[numRec] + end + calcOk = true + else + calcVal = -999999 + numRec = 1 + calcOk = false + end +end + +local function displayTestInfo() + if (callMode ~= 'NLua') then + print('------------------------------') + print('calcOk: ' .. tostring(calcOk) .. ' | vcFunct: ' .. vcFunct) + for i,val in pairs(valList) do + print("v_"..i.." | "..val) + end + print('calcVal: ' .. calcVal) + print('------------------------------') + end +end + + +-- MAIN +-- variabile semaforo callMode (locali o remote da NLua) +callMode = callMode or '' +calcVal = -999 +vcFunct = vcFunct or '' +valList = valList or {} +numRec = numRec or 1 +calcOk = false +valListTable = {} + +checkInit() +setupTable() +doCalc() +displayTestInfo() diff --git a/MP.MONO.DECODER/lua/test/ScratchPad.lua b/MP.MONO.DECODER/lua/test/ScratchPad.lua index b714306..81bf250 100644 --- a/MP.MONO.DECODER/lua/test/ScratchPad.lua +++ b/MP.MONO.DECODER/lua/test/ScratchPad.lua @@ -4,6 +4,7 @@ Procedura calcolo statistiche parametri: Variabili IN: - vcFunct(string): tipo di processing da effettuare tra [POINT/AVG/MEDIAN/MIN/MAX] - valList(): elenco VALORI double da processare +- numRec(int): conteggio numero recorda da processare Variabili OUT: - calcVal(double): valore calcolato finale @@ -17,58 +18,64 @@ if not package.path:find(ZBS,1,true) then package.cpath = ZBS .. "/bin/?.dll;" .. ZBS .. "/bin/clibs53/?.dll;" .. package.cpath end --- variabile semaforo callMode (locali o remote da NLua) -callMode = callMode or '' -vcFunct = vcFunct or '' -valList = valList or {} -calcVal = 0 -calcOk = false - -- se non è da NLua inizializzo variabili accessorie local function checkInit() if (callMode ~= 'NLua') then -- imposto valori test - valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 } - vcFunct = 'MAX' + valList = { 4467, 4468, 4467, 4467, 4467, 4467 } + --valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 } + vcFunct = 'AVG' --POINT AVG MEDIAN MIN MAX end end +local function setupTable() + if numRec > 0 and valList[0] ~= nil then + for i = numRec, 1, -1 do + valListTable[i] = valList[i-1] + end + else + valListTable = valList + end +end + local function doCalc() - if(#valList>0) then + if(numRec > 0) then -- verifica il tipo di richiesta if(vcFunct == 'AVG') then s = 0 - for i,v in ipairs(valList) do + for i,v in ipairs(valListTable) do s = s + v end - calcVal = s / #valList + calcVal = s / numRec elseif(vcFunct == 'POINT') then - calcVal = valList[#valList] + calcVal = valListTable[numRec] elseif(vcFunct == 'MEDIAN') then - table.sort(valList) - calcVal = valList[#valList/2] + table.sort(valListTable) + calcVal = valListTable[numRec/2] elseif(vcFunct == 'MIN') then - calcVal = math.min(valList) + table.sort(valListTable) + calcVal = valListTable[1] elseif(vcFunct == 'MAX') then - calcVal = math.max(valList) + table.sort(valListTable) + calcVal = valListTable[numRec] end calcOk = true else - calcVal = 0 - calcOk = false + calcVal = -999999 + numRec = 1 + calcOk = false end end local function displayTestInfo() if (callMode ~= 'NLua') then print('------------------------------') - print('calcOk: ' .. tostring(calcOk)) - print('vcFunct: ' .. vcFunct) - for i,val in pairs(valList) do + print('calcOk: ' .. tostring(calcOk) .. ' | vcFunct: ' .. vcFunct) + for i,val in pairs(valList) do print("v_"..i.." | "..val) - end + end print('calcVal: ' .. calcVal) print('------------------------------') end @@ -76,7 +83,16 @@ end -- MAIN +-- variabile semaforo callMode (locali o remote da NLua) +callMode = callMode or '' +calcVal = -999 +vcFunct = vcFunct or '' +valList = valList or {} +numRec = numRec or 1 +calcOk = false +valListTable = {} checkInit() +setupTable() doCalc() displayTestInfo()