applications/luci-olsr: rework ui
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4         if not nixio.fs.access("/etc/config/olsrd") then
5                 return
6         end
7
8         require("luci.i18n").loadc("olsr")
9         local i18n = luci.i18n.translate
10
11         local page  = node("admin", "status", "olsr")
12         page.target = call("action_index")
13         page.title  = "OLSR"
14         page.i18n   = "olsr"
15         page.subindex = true
16
17         local page  = node("admin", "status", "olsr", "routes")
18         page.target = call("action_routes")
19         page.title  = i18n("Routen")
20         page.order  = 10
21
22         local page  = node("admin", "status", "olsr", "topology")
23         page.target = call("action_topology")
24         page.title  = i18n("Topologie")
25         page.order  = 20
26
27         local page  = node("admin", "status", "olsr", "hna")
28         page.target = call("action_hna")
29         page.title  = "HNA"
30         page.order  = 30
31
32         local page  = node("admin", "status", "olsr", "mid")
33         page.target = call("action_mid")
34         page.title  = "MID"
35         page.order  = 50
36
37         local ol = entry(
38                 {"admin", "services", "olsrd"},
39                 cbi("olsr/olsrd"), "OLSR"
40         )
41         ol.i18n = "olsr"
42         ol.subindex = true
43
44         entry(
45                 {"admin", "services", "olsrd", "iface"},
46                 cbi("olsr/olsrdiface")
47         ).leaf = true
48
49         entry(
50                 {"admin", "services", "olsrd", "hna"},
51                 cbi("olsr/olsrdhna"), "HNA Announcements"
52         )
53
54         oplg = entry(
55                 {"admin", "services", "olsrd", "plugins"},
56                 cbi("olsr/olsrdplugins"), "Plugins"
57         )
58         oplg.i18n = "olsr"
59         oplg.leaf = true
60         oplg.subindex = true
61
62         local uci = require("luci.model.uci").cursor()
63         uci:foreach("olsrd", "LoadPlugin",
64                 function (section)
65                         local lib = section.library
66                         entry(
67                                 {"admin", "services", "olsrd", "plugins", lib },
68                                 cbi("olsr/olsrdplugins"),
69                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
70                         )
71                 end
72         )
73 end
74
75 function action_index()
76         local data = fetch_txtinfo("links")
77
78         if not data or not data.Links then
79                 luci.template.render("status-olsr/error_olsr")
80                 return nil
81         end
82
83         local function compare(a, b)
84                 local c = tonumber(a.Cost)
85                 local d = tonumber(b.Cost)
86
87                 if not c or c == 0 then
88                         return false
89                 end
90
91                 if not d or d == 0 then
92                         return true
93                 end
94
95                 return c < d
96         end
97
98         table.sort(data.Links, compare)
99
100         luci.template.render("status-olsr/index", {links=data.Links})
101 end
102
103 function action_routes()
104         local data = fetch_txtinfo("routes")
105
106         if not data or not data.Routes then
107                 luci.template.render("status-olsr/error_olsr")
108                 return nil
109         end
110
111         local function compare(a, b)
112                 local c = tonumber(a.ETX)
113                 local d = tonumber(b.ETX)
114
115                 if not c or c == 0 then
116                         return false
117                 end
118
119                 if not d or d == 0 then
120                         return true
121                 end
122
123                 return c < d
124         end
125
126         table.sort(data.Routes, compare)
127
128         luci.template.render("status-olsr/routes", {routes=data.Routes})
129 end
130
131 function action_topology()
132         local data = fetch_txtinfo("topology")
133
134         if not data or not data.Topology then
135                 luci.template.render("status-olsr/error_olsr")
136                 return nil
137         end
138
139         local function compare(a, b)
140                 return a["Dest. IP"] < b["Dest. IP"]
141         end
142
143         table.sort(data.Topology, compare)
144
145         luci.template.render("status-olsr/topology", {routes=data.Topology})
146 end
147
148 function action_hna()
149         local data = fetch_txtinfo("hna")
150
151         if not data or not data.HNA then
152                 luci.template.render("status-olsr/error_olsr")
153                 return nil
154         end
155
156         local function compare(a, b)
157                 return a.Destination < b.Destination
158         end
159
160         table.sort(data.HNA, compare)
161
162         luci.template.render("status-olsr/hna", {routes=data.HNA})
163 end
164
165 function action_mid()
166         local data = fetch_txtinfo("mid")
167
168         if not data or not data.MID then
169                 luci.template.render("status-olsr/error_olsr")
170                 return nil
171         end
172
173         local function compare(a, b)
174                 return a["IP address"] < b["IP address"]
175         end
176
177         table.sort(data.MID, compare)
178
179         luci.template.render("status-olsr/mid", {mids=data.MID})
180 end
181
182
183 -- Internal
184 function fetch_txtinfo(otable)
185         require("luci.sys")
186         otable = otable or ""
187         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
188
189         if #rawdata == 0 then
190                 if nixio.fs.access("/proc/net/ipv6_route", "r") then
191                         rawdata = luci.sys.httpget("http://[::1]:2006/"..otable)
192                         if #rawdata == 0 then
193                                 return nil
194                         end
195                 else
196                         return nil
197                 end
198         end
199
200         local data = {}
201
202         local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
203
204
205         for i, tbl in ipairs(tables) do
206                 local lines = luci.util.split(tbl, "\r?\n", nil, true)
207                 local name  = table.remove(lines, 1):sub(8)
208                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
209                 local split = #keys - 1
210
211                 data[name] = {}
212
213                 for j, line in ipairs(lines) do
214                         local fields = luci.util.split(line, "\t", split)
215                         data[name][j] = {}
216                         for k, key in pairs(keys) do
217                                 data[name][j][key] = fields[k]
218                         end
219
220                         if data[name][j].Linkcost then
221                                 data[name][j].LinkQuality,
222                                 data[name][j].NLQ,
223                                 data[name][j].ETX =
224                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
225                         end
226                 end
227         end
228
229         return data
230 end