* luci/app-olsr: first round of olsr improvements:
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4         if not luci.fs.isfile("/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
16         local page  = node("admin", "status", "olsr", "routes")
17         page.target = call("action_routes")
18         page.title  = i18n("olsr_routes", "Routen")
19         page.order  = 10
20
21         local page  = node("admin", "status", "olsr", "topology")
22         page.target = call("action_topology")
23         page.title  = i18n("olsr_topology", "Topologie")
24         page.order  = 20
25
26         local page  = node("admin", "status", "olsr", "hna")
27         page.target = call("action_hna")
28         page.title  = "HNA"
29         page.order  = 30
30
31         local page  = node("admin", "status", "olsr", "mid")
32         page.target = call("action_mid")
33         page.title  = "MID"
34         page.order  = 50
35
36         entry(
37                 {"admin", "services", "olsrd"},
38                 cbi("olsr/olsrd"), "OLSR"
39         ).i18n = "olsr"
40
41         entry(
42                 {"admin", "services", "olsrd", "hna"},
43                 cbi("olsr/olsrdhna"), "HNA Announcements"
44         ).i18n = "olsr"
45
46         entry(
47                 {"admin", "services", "olsrd", "plugins"},
48                 cbi("olsr/olsrdplugins"), "Plugins"
49         ).i18n = "olsr"
50 end
51
52 function action_index()
53         local data = fetch_txtinfo("links")
54
55         if not data or not data.Links then
56                 luci.template.render("status-olsr/error_olsr")
57                 return nil
58         end
59
60         local function compare(a, b)
61                 local c = tonumber(a.ETX)
62                 local d = tonumber(b.ETX)
63
64                 if not c or c == 0 then
65                         return false
66                 end
67
68                 if not d or d == 0 then
69                         return true
70                 end
71
72                 return c < d
73         end
74
75         table.sort(data.Links, compare)
76
77         luci.template.render("status-olsr/index", {links=data.Links})
78 end
79
80 function action_routes()
81         local data = fetch_txtinfo("routes")
82
83         if not data or not data.Routes then
84                 luci.template.render("status-olsr/error_olsr")
85                 return nil
86         end
87
88         local function compare(a, b)
89                 local c = tonumber(a.ETX)
90                 local d = tonumber(b.ETX)
91
92                 if not c or c == 0 then
93                         return false
94                 end
95
96                 if not d or d == 0 then
97                         return true
98                 end
99
100                 return c < d
101         end
102
103         table.sort(data.Routes, compare)
104
105         luci.template.render("status-olsr/routes", {routes=data.Routes})
106 end
107
108 function action_topology()
109         local data = fetch_txtinfo("topology")
110
111         if not data or not data.Topology then
112                 luci.template.render("status-olsr/error_olsr")
113                 return nil
114         end
115
116         local function compare(a, b)
117                 return a["Destination IP"] < b["Destination IP"]
118         end
119
120         table.sort(data.Topology, compare)
121
122         luci.template.render("status-olsr/topology", {routes=data.Topology})
123 end
124
125 function action_hna()
126         local data = fetch_txtinfo("hna")
127
128         if not data or not data.HNA then
129                 luci.template.render("status-olsr/error_olsr")
130                 return nil
131         end
132
133         local function compare(a, b)
134                 return a.Network < b.Network
135         end
136
137         table.sort(data.HNA, compare)
138
139         luci.template.render("status-olsr/hna", {routes=data.HNA})
140 end
141
142 function action_mid()
143         local data = fetch_txtinfo("mid")
144
145         if not data or not data.MID then
146                 luci.template.render("status-olsr/error_olsr")
147                 return nil
148         end
149
150         local function compare(a, b)
151                 return a.IP < b.IP
152         end
153
154         table.sort(data.MID, compare)
155
156         luci.template.render("status-olsr/mid", {mids=data.MID})
157 end
158
159
160 -- Internal
161 function fetch_txtinfo(otable)
162         require("luci.sys")
163         otable = otable or ""
164         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
165
166         if #rawdata == 0 then
167                 return nil
168         end
169
170         local data = {}
171
172         local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
173
174
175         for i, tbl in ipairs(tables) do
176                 local lines = luci.util.split(tbl, "\r?\n", nil, true)
177                 local name  = table.remove(lines, 1):sub(8)
178                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
179                 local split = #keys - 1
180
181                 data[name] = {}
182
183                 for j, line in ipairs(lines) do
184                         local fields = luci.util.split(line, "\t", split)
185                         data[name][j] = {}
186                         for k, key in pairs(keys) do
187                                 data[name][j][key] = fields[k]
188                         end
189
190                         if data[name][j].Linkcost then
191                                 data[name][j].LinkQuality,
192                                 data[name][j].NLQ,
193                                 data[name][j].ETX =
194                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
195                         end
196                 end
197         end
198
199         return data
200 end