Files
mapo-mono/MP.MONO.DECODER/lua/AlarmDecoder.lua
T
2022-03-16 10:43:30 +01:00

131 lines
3.3 KiB
Lua

--[[---------------------------------------------------
Procedura decodifica Allarmi:
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 (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
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
local function testBit(testVal, i)
local answ=0
local idx=2^(i-1)
-- verifico se sia bit diverso...
answ = (testVal&idx == idx)
return answ
end
local function valMask(testVal)
return testVal & bitMask
end
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 = ""
checkInit()
setupTable()
setupBitMask()
checkVariation()
checkActiveAlarms()
calcStatusVar()
displayTestInfo()