diff --git a/MP.MONO.DECODER/lua/ParamsDecoder.lua b/MP.MONO.DECODER/lua/ParamsDecoder.lua index c0fe7af..2b76d18 100644 --- a/MP.MONO.DECODER/lua/ParamsDecoder.lua +++ b/MP.MONO.DECODER/lua/ParamsDecoder.lua @@ -1,10 +1,10 @@ --[[--------------------------------------------------- -Procedura calcolo STATUS: +Procedura calcolo statistiche parametri: Variabili IN: -- lastMode +- 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 +- numRec(int): conteggio numero record da processare Variabili OUT: - calcVal(double): valore calcolato finale @@ -22,104 +22,50 @@ end local function checkInit() if (callMode ~= 'NLua') then -- imposto valori test - dataList = {} - dataList["MACHINE STATUS"] = 1 - dataList["PROCESS STATUS"] = 2 - dataList["PROCESS MODE"] = 2 + 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 - numRec = tablelength(dataList) end -function tablelength(T) - local count = 0 - for _ in pairs(T) do count = count + 1 end - return count -end - --- Main function: effettua calcolo stato secondo i parametri ricevuti -local function doCalc() - - -- se ho dati processo - if(numRec > 0) then - - -- /************************************************************************ - -- * - -- * - -- * - -- * - -- * - -- * - -- * - -- * - -- * - -- * - -- * - -- *************************************************************************/ - - -- calcolo dello statusId - if(dataList["MACHINE STATUS"] == 0) then - statusId = 1 - else - if(dataList["PROCESS MODE"] == 1) then - statusId = 8 - elseif(dataList["PROCESS MODE"] == 2) then - statusId = 2 - elseif(dataList["PROCESS MODE"] == 3) then - statusId = 9 - elseif(dataList["PROCESS MODE"] == 4) then - statusId = 5 - elseif(dataList["PROCESS MODE"] == 5) then - statusId = 10 - elseif(dataList["PROCESS MODE"] == 6) then - statusId = 11 - elseif(dataList["PROCESS MODE"] == 7) then - statusId = 12 - elseif(dataList["PROCESS MODE"] == 8) then - statusId = 13 - end +local function setupTable() + if numRec > 0 and valList[0] ~= nil then + for i = numRec, 1, -1 do + valListTable[i] = valList[i-1] end - -- calcolo del processId - if(dataList["PROCESS STATUS"] == 1) then - modeId = 2 - elseif(dataList["PROCESS STATUS"] == 2) then - modeId = 1 - elseif(dataList["PROCESS STATUS"] == 3) then - modeId = 3 - elseif(dataList["PROCESS STATUS"] == 6) then - modeId = 8 - elseif(dataList["PROCESS STATUS"] == 8) then - modeId = 9 - elseif(dataList["PROCESS STATUS"] == 11) then - modeId = 10 - end - - calcOk = true - - -- altrimenti errore else - numRec = 1 + 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 @@ -127,12 +73,11 @@ end local function displayTestInfo() if (callMode ~= 'NLua') then print('------------------------------') - print('calcOk: ' .. tostring(calcOk)) - for i,val in pairs(dataList) do - print(""..i.." | "..val) + print('calcOk: ' .. tostring(calcOk) .. ' | vcFunct: ' .. vcFunct) + for i,val in pairs(valList) do + print("v_"..i.." | "..val) end - print('statusId: ' .. statusId) - print('modeId: ' .. modeId) + print('calcVal: ' .. calcVal) print('------------------------------') end end @@ -142,14 +87,15 @@ end function doProcess() -- variabile semaforo callMode (locali o remote da NLua) callMode = callMode or '' - numRec = 0 - statusId = 0 - modeId = 0 - dataList = dataList or {} + calcVal = -999 + vcFunct = vcFunct or '' + valList = valList or {} + numRec = numRec or 1 calcOk = false - - --vero task da eseguire + valListTable = {} + checkInit() + setupTable() doCalc() displayTestInfo() end