577f596ca6fd07b2b5ca47244db0900edc79ba31
[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/olsr") 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({"admin", "services", "olsrd"}, cbi("olsr/olsrd"), "OLSR").i18n = "olsr"
37 end
38
39 function action_index()
40         local data = fetch_txtinfo("links")
41         
42         if not data or not data.Links then
43                 luci.template.render("status-olsr/error_olsr")
44                 return nil
45         end
46         
47         local function compare(a, b)
48                 local c = tonumber(a.ETX)
49                 local d = tonumber(b.ETX)
50                 
51                 if not c or c == 0 then
52                         return false
53                 end
54                 
55                 if not d or d == 0 then
56                         return true
57                 end
58                 
59                 return c < d
60         end
61         
62         table.sort(data.Links, compare)
63         
64         luci.template.render("status-olsr/index", {links=data.Links})
65 end
66
67 function action_routes()
68         local data = fetch_txtinfo("routes")
69         
70         if not data or not data.Routes then
71                 luci.template.render("status-olsr/error_olsr")
72                 return nil
73         end
74         
75         local function compare(a, b)
76                 local c = tonumber(a.ETX)
77                 local d = tonumber(b.ETX)
78                 
79                 if not c or c == 0 then
80                         return false
81                 end
82                 
83                 if not d or d == 0 then
84                         return true
85                 end
86                 
87                 return c < d
88         end
89         
90         table.sort(data.Routes, compare)
91         
92         luci.template.render("status-olsr/routes", {routes=data.Routes})
93 end
94
95 function action_topology()
96         local data = fetch_txtinfo("topology")
97         
98         if not data or not data.Topology then
99                 luci.template.render("status-olsr/error_olsr")
100                 return nil
101         end
102         
103         local function compare(a, b)
104                 return a["Destination IP"] < b["Destination IP"]
105         end
106         
107         table.sort(data.Topology, compare)
108         
109         luci.template.render("status-olsr/topology", {routes=data.Topology})
110 end
111
112 function action_hna()
113         local data = fetch_txtinfo("hna")
114         
115         if not data or not data.HNA then
116                 luci.template.render("status-olsr/error_olsr")
117                 return nil
118         end
119         
120         local function compare(a, b)
121                 return a.Network < b.Network
122         end
123         
124         table.sort(data.HNA, compare)
125         
126         luci.template.render("status-olsr/hna", {routes=data.HNA})
127 end
128
129 function action_mid()
130         local data = fetch_txtinfo("mid")
131         
132         if not data or not data.MID then
133                 luci.template.render("status-olsr/error_olsr")
134                 return nil
135         end
136         
137         local function compare(a, b)
138                 return a.IP < b.IP
139         end
140         
141         table.sort(data.MID, compare)
142         
143         luci.template.render("status-olsr/mid", {mids=data.MID})
144 end
145
146
147 -- Internal
148 function fetch_txtinfo(otable)
149         require("luci.sys")
150         otable = otable or ""
151         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
152         
153         if #rawdata == 0 then
154                 return nil
155         end
156         
157         local data = {}
158         
159         local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
160         
161
162         for i, tbl in ipairs(tables) do
163                 local lines = luci.util.split(tbl, "\n")
164                 local name  = table.remove(lines, 1):sub(8)
165                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
166                 local split = #keys - 1
167                 
168                 data[name] = {}
169                 
170                 for j, line in ipairs(lines) do
171                         local fields = luci.util.split(line, "\t", split)
172                         data[name][j] = {}
173                         for k, key in pairs(keys) do
174                                 data[name][j][key] = fields[k] 
175                         end
176                         
177                         if data[name][j].Linkcost then
178                                 data[name][j].LinkQuality,
179                                 data[name][j].NLQ,
180                                 data[name][j].ETX =
181                                  data[name][j].Linkcost:match("(.*)/(.*)\t(.*)")
182                         end
183                 end
184         end
185         
186         return data
187 end