-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogRFStatesApplet.lua
More file actions
130 lines (116 loc) · 2.88 KB
/
logRFStatesApplet.lua
File metadata and controls
130 lines (116 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--[[
log RF States before a reboot is done and set them again after reboot
--]]
module(...,package.seeall)
require ("xap")
info={
version="1.1", description="log RF States"
}
function createINIfile(filename)
local err
fho, err = io.open(filename, "w")
if err then print("Error opening ini-file " .. filename .. " for writing!"); return; end
fho:write("# personal ini-settings")
fho:write("\n")
fho:write("\n")
fho:write("[rf]")
fho:write("\n")
local rfDevices = 10
for i = 1, rfDevices do
fho:write("rf" .. i .. "=off")
fho:write("\n")
end
fho:close()
end
--[[
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 -- create new ini file
createINIfile(filename)
fh, err = io.open(filename)
end
i = 1
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 including the modified lines
]]
function writeINIfile(filename, rf, state)
local linecount = readINIfile(filename)
local j = 1
local err
local rf_changed = false
fho, err = io.open(filename, "w")
if err then print("Error opening ini-file " .. filename .. " for writing!"); return; end
for j = 1, linecount do
if string.find(lines[j], "rf" .. rf) then
fho:write("rf" .. rf .. "=", state)
fho:write("\n")
rf_changed = true
else
fho:write(lines[j])
fho:write("\n")
end
end
if not rf_changed then -- new rf to be written into the INI-file
fho:write("rf" .. rf .. "=", state)
fho:write("\n")
end
fho:close()
end
--[[
This function reads an INI-file and sets the rf-states as read in the ini file
]]
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
-- print(j)
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(frame)
-- Get the relays STATE either on or off
state = frame:getValue("output.state", "state")
-- This LUA pattern will parse the source address ie. 'dbzoo.livebox.Controller:rf.1'
-- From this string extract the rf device id.
rf = string.match(frame:getValue("xap-header", "source"), "rf%.(%d+)")
-- Print out this device's current state
writeINIfile(filename, rf, state)
-- print(rf)
end
function init()
-- Watch for a directed message to rf
lines = {}
filename = '/etc/xap.d/rf-settings.ini'
source = 'dbzoo.hal.Controller:rf.'
setrfs(source)
f = xap.Filter()
f:add("xap-header", "source", source .. '*')
f:add("xap-header", "class", "xAPBSC.event")
f:callback(logstate)
end