--[[ log RF States before a reboot is done and set them again after reboot --]] module(...,package.seeall) require "xap" info={ version="1.0", description="log RF States" } --[[ This function opens a file and reads it line by line ]] function readINIfile(filename) local err local i = 1 fh,err = io.open(filename) if err then print("OOps"); return; end while true do lines[i] = fh:read() if lines[i] == nil then break end i = i + 1 end fh:close() return i - 1 end --[[ This function reads a file and writes it back modified line by line ]] function writeINIfile(filename, rf, state) local linecount = readINIfile(filename) local j = 1 local err fho,err = io.open(filename,"w") if err then print("OOps2"); return; end for j = 1, linecount do if string.find(lines[j],"rf" .. rf) then fho:write("rf" .. rf .. "=",state) fho:write("\n") else fho:write(lines[j]) fho:write("\n") end end fho:close() end function setrfs(source) local i local state local j local linecount = readINIfile(filename) for j = 1, linecount do if string.find(lines[j],"rf%d=") then i,state = string.match(lines[j], "rf(%d+)=(%a+)") xap.sendShort(string.format([[xap-header { target=%s%s class=xAPBSC.cmd } output.state.1 { id=* state=%s }]], source, tostring(i), tostring(state))) end end end function logstate() -- Ignore the xAPBSC.info class messages. if xap.getValue("xap-header","class") == "xAPBSC.event" then -- Get the relays STATE either on or off state = xap.getValue("output.state","state") -- This LUA pattern will parse the source address ie. 'dbzoo.livebox.Controller:rf.1' -- From this string extract the digit 1. rf = string.gsub(xap.getValue("xap-header","source"),".*(%d)","%1") -- Print out this relays current state writeINIfile(filename, rf, state) end end function init() -- Watch for a directed message to rf lines = {} filename = '/etc/wm-settings.ini' source = 'dbzoo.livebox.Controller:rf.' setrfs(source) f = xap.Filter() f:add("xap-header","source",source .. '*') f:callback(logstate) end