Update prog e test lua x allarmi (refactor)
This commit is contained in:
@@ -1,38 +1,40 @@
|
||||
|
||||
-- This is a one-line comment that ends at the end of the line
|
||||
--[[ This a multi-line (long) [comment](http://www.lua.org/manual/5.1/manual.html#2.1)
|
||||
that ends with this closing bracket --> ]]
|
||||
--[=[ This is also a long comment ]=]
|
||||
|
||||
|
||||
|
||||
--[[---------------------------------------------------
|
||||
--[[---------------------------------------------------
|
||||
Procedura decodifica Allarmi:
|
||||
|
||||
- ricezione tab elenco allarmi gestiti a bitmap, se ## = silenziati
|
||||
- produzione var string con status finale risultati
|
||||
- ricezione obj stato corrente x i vari allarmi
|
||||
- decodifica x ogni info ricevuta dello stato allarmi relativo
|
||||
- invio (salvataggio) stato allarmi tramite funzione
|
||||
Variabili IN:
|
||||
- alarmList(array/tabella) (elenco allarmi gestiti a bitmap, se ## = silenziati)
|
||||
- currVal(INT) stato corrente
|
||||
- lastVal(INT) ultimo stato (oppure 0) - opzionale
|
||||
- muteMap(INT) valore BITMASK x silenziamento allarmi (xFF default = tutto attivo, ogni bit a 0 è silenziato) - opzionale
|
||||
|
||||
Variabili OUT:
|
||||
- alarmStatus (stirng con status finale risultato allarmi attivi)
|
||||
|
||||
Viene calcolato internamente la bitmask dall'elenco allarmi
|
||||
-----------------------------------------------------]]
|
||||
|
||||
---------------------------------------------------]]--
|
||||
-- ricezione da codice tab status allarmi, attenzione da C# è 0 based, qui è 1 based --> fix costruzione tabella
|
||||
|
||||
-- variabile semaforo callMode x chiamate locali o remote da NLua
|
||||
callMode = callMode or ''
|
||||
-- variabile semaforo callMode (locali o remote da NLua)
|
||||
callMode = callMode or ''
|
||||
currVal = currVal or 0
|
||||
lastVal = lastVal or 0
|
||||
muteMap = muteMap or 0Xff
|
||||
bitMask = bitMask or 0x00
|
||||
valueChanged = false
|
||||
|
||||
-- se non è da NLua inizializzo variabili accessorie
|
||||
if(callMode ~= 'NLua')
|
||||
then
|
||||
alarmList = {"##Allarme01", "Allarme02", "AllarmeSilenziato03", "##Allarme04", "Allarme05", "Allarme06", "Allarme07", "##Allarme08"}
|
||||
numAlarm = #alarmList
|
||||
-- valori status da testare
|
||||
bitMask = 0
|
||||
lastVal = 0
|
||||
currVal = 3
|
||||
local function checkInit()
|
||||
if(callMode ~= 'NLua') then
|
||||
alarmList = {"##Allarme01", "Allarme02", "AllarmeSilenziato03", "##Allarme04", "Allarme05", "Allarme06", "Allarme07", "##Allarme08"}
|
||||
numAlarm = #alarmList
|
||||
-- valori status da testare
|
||||
lastVal = 0
|
||||
currVal = 3
|
||||
end
|
||||
end
|
||||
|
||||
function testBit(testVal, i)
|
||||
local function testBit(testVal, i)
|
||||
local answ=0
|
||||
local idx=2^(i-1)
|
||||
-- verifico se sia bit diverso...
|
||||
@@ -40,86 +42,89 @@ function testBit(testVal, i)
|
||||
return answ
|
||||
end
|
||||
|
||||
function valMask(testVal)
|
||||
local function valMask(testVal)
|
||||
return testVal & bitMask
|
||||
end
|
||||
|
||||
-- init tabella
|
||||
local function setupTable()
|
||||
if numAlarm > 0 and alarmList[0] ~= nil then
|
||||
bitMask = 0
|
||||
for i = numAlarm,1, -1 do
|
||||
alarmListTable[i] = alarmList[i-1]
|
||||
end
|
||||
else
|
||||
alarmListTable = alarmList
|
||||
end
|
||||
end
|
||||
-- calcolo maschera...
|
||||
local function setupBitMask()
|
||||
for i = 1, numAlarm,1 do
|
||||
if(string.sub(alarmListTable[i],0,2)~='##') then
|
||||
bitMask = math.floor(bitMask + 2^(i-1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- confronto lastVal e currVal
|
||||
local function checkVariation()
|
||||
valueChanged = valMask(lastVal) ~= valMask(currVal)
|
||||
if(callMode ~= 'NLua') then
|
||||
if(valueChanged) then
|
||||
print('LastVal: '.. lastVal .. ' | CurrVal: '.. currVal .. ' || LastValMask: '.. valMask(lastVal) .. ' | CurrValMask: '.. valMask(currVal))
|
||||
else
|
||||
print('UNCHANGED | LastValMask: '.. valMask(lastVal) .. ' | CurrValMask: '.. valMask(currVal))
|
||||
end
|
||||
end
|
||||
|
||||
-- salva nuovo valore corrente (post mask)
|
||||
lastVal = valMask(currVal)
|
||||
end
|
||||
|
||||
|
||||
local function checkActiveAlarms()
|
||||
for i = 1, numAlarm do
|
||||
bTest = testBit(valMask(currVal),i)
|
||||
if(bTest) then
|
||||
alarmListActive[i]=alarmListTable[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function calcStatusVar()
|
||||
for k, v in pairs(alarmListActive) do
|
||||
alarmStatus = alarmStatus .. k .. ' - ' .. v .. ' | '
|
||||
end
|
||||
|
||||
if(#alarmStatus == 0) then
|
||||
alarmStatus = 'All OK'
|
||||
else
|
||||
if(#alarmStatus > 3) then
|
||||
alarmStatus = string.sub(alarmStatus, 0, #alarmStatus -3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function displayTestInfo()
|
||||
if(callMode ~= 'NLua') then
|
||||
print('muteMap: '.. muteMap .. ' | bitMask: '.. bitMask)
|
||||
print(alarmStatus)
|
||||
--print('------------------------------')
|
||||
--for i,val in pairs(alarmListTable) do
|
||||
-- print("AL"..i.." | "..val)
|
||||
--end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- MAIN
|
||||
alarmListTable = {}
|
||||
alarmListActive = {}
|
||||
alarmStatus = ""
|
||||
|
||||
if numAlarm > 0 and alarmList[0] ~= nil
|
||||
then
|
||||
bitMask = 0
|
||||
for i = numAlarm,1, -1
|
||||
do
|
||||
alarmListTable[i] = alarmList[i-1]
|
||||
end
|
||||
else
|
||||
alarmListTable = alarmList
|
||||
end
|
||||
|
||||
-- calcolo maschera...
|
||||
for i = 1, numAlarm,1
|
||||
do
|
||||
if(string.sub(alarmListTable[i],0,2)~='##')
|
||||
then
|
||||
bitMask = bitMask + 2^(i-1)
|
||||
end
|
||||
end
|
||||
|
||||
--print('BitMask: '..bitMask)
|
||||
|
||||
for i = 1, numAlarm do
|
||||
bTest = testBit(valMask(currVal),i)
|
||||
if(bTest)
|
||||
then
|
||||
-- print("AL"..i.." | "..alarmListTable[i])
|
||||
alarmListActive[i]=alarmListTable[i]
|
||||
end
|
||||
end
|
||||
|
||||
-- confronto lastVal e currVal
|
||||
if(valMask(lastVal) ~= valMask(currVal))
|
||||
then
|
||||
print('LastVal: '.. lastVal .. ' | CurrVal: '.. currVal .. ' || LastValMask: '.. valMask(lastVal) .. ' | CurrValMask: '.. valMask(currVal))
|
||||
--for i = 1, numAlarm
|
||||
--do
|
||||
-- bTest = testBit(valMask(currVal),i)
|
||||
-- if(bTest)
|
||||
-- then
|
||||
-- -- print("AL"..i.." | "..alarmListTable[i])
|
||||
-- alarmListActive[i]=alarmListTable[i]
|
||||
-- end
|
||||
--end
|
||||
-- salva nuovo valore corrente (post mask)
|
||||
lastVal = valMask(currVal)
|
||||
else
|
||||
print('UNCHANGED | LastValMask: '.. valMask(lastVal) .. ' | CurrValMask: '.. valMask(currVal))
|
||||
end
|
||||
|
||||
for k, v in pairs(alarmListActive) do
|
||||
alarmStatus = alarmStatus .. k .. ' - ' .. v .. ' | '
|
||||
end
|
||||
|
||||
if(#alarmStatus == 0) then
|
||||
alarmStatus = 'All OK'
|
||||
else
|
||||
if(#alarmStatus > 3) then
|
||||
alarmStatus = string.sub(alarmStatus, 0, #alarmStatus -3)
|
||||
end
|
||||
end
|
||||
|
||||
--print(alarmStatus)
|
||||
|
||||
--print('------------------------------')
|
||||
--
|
||||
--for i,val in pairs(alarmListTable) do
|
||||
-- print("AL"..i.." | "..val)
|
||||
--end
|
||||
--print('------------------------------')
|
||||
--
|
||||
--for i,val in ipairs(alarmListTable) do
|
||||
-- print("AL"..i.." | "..val)
|
||||
--end
|
||||
checkInit()
|
||||
setupTable()
|
||||
setupBitMask()
|
||||
checkVariation()
|
||||
checkActiveAlarms()
|
||||
calcStatusVar()
|
||||
displayTestInfo()
|
||||
|
||||
Reference in New Issue
Block a user