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