Hi all
I am trying to reduce overhead in my plugin and one of the things that I have done is remove polling.
Below is based on guessed "state machine"IORelay plugin. Can any Lua experts tell me if there is a better way of doing this? I have tried to cut it down th the bare minimum to ensure that it is understandable.
[code]–Global values
writeQueue = {}
TIMEOUT = 1
local handler = nil
local handlerDevice
local handlerData
local handlerTime
DEBUG_MODE = true
local function log(text, level)
luup.log("Plugin: " .. text, (level or 1))
end
local function debug(text)
if (DEBUG_MODE == true) then
log("debug: " .. text, 50)
end
end
local function processQueue()
--Process first command in queue.
local data = writeQueue[1]
if (data == nil) then
return true
end
buffer = ""
handlerTime = os.time()
handler = data[2]
handlerDevice = data[3]
handlerData = data[4]
table.remove(writeQueue,1)
write(data[1])
return true
end
local function processData()
local t = os.time()
if (handler ~= nil) then
if (t >= handlerTime + TIMEOUT) then
log("poll: stale handler found, queue length:"..#writeQueue)
return processQueue()
end
debug(string.format("processData: exiting, busy (entries=%i)", #writeQueue))
return true
end
debug(string.format("processData: entering, free (entries=%i)", #writeQueue))
return processQueue()
end
local function queueData(data, handler, device, handlerData)
–insert into command queue
table.insert(writeQueue, {data, handler, device, handlerData})
while #writeQueue > 0 do
processData()
end
end
function incomingData(lul_data)
–Incoming data handler.
local data = tostring(lul_data)
debug(string.format(“incoming: data=0x%02X”, data:byte(1)))
if (handler ~= nil) then
if (handler(data, handlerDevice, handlerData) == false) then
handler = nil
end
else
debug(string.format("incoming: No handler set to take data=0x%x", data:byte(1)))
end
end
[/code]
Thanks
Zoot