d795dc4ed69fc8fac345da1239db4b82699bc934
[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         local page  = node("admin", "status", "olsr")
9         page.target = call("action_index")
10         page.title  = "OLSR"
11         
12         local page  = node("admin", "status", "olsr", "routes")
13         page.target = call("action_routes")
14         page.title  = "Routen"
15         page.order  = 10
16         
17         local page  = node("admin", "status", "olsr", "topology")
18         page.target = call("action_topology")
19         page.title  = "Topologie"
20         page.order  = 20
21         
22         local page  = node("admin", "status", "olsr", "hna")
23         page.target = call("action_hna")
24         page.title  = "HNA"
25         page.order  = 30
26         
27         local page  = node("admin", "status", "olsr", "mid")
28         page.target = call("action_mid")
29         page.title  = "MID"
30         page.order  = 50
31
32         entry({"admin", "services", "olsrd"}, cbi("olsr/olsrd"), "OLSR")
33 end
34
35 function action_index()
36         local data = fetch_txtinfo("links")
37         
38         if not data or not data.Links then
39                 luci.template.render("status-olsr/error_olsr")
40                 return nil
41         end
42         
43         local function compare(a, b)
44                 if tonumber(a.ETX) == 0 then
45                         return false
46                 end
47                 
48                 if tonumber(b.ETX) == 0 then
49                         return true
50                 end
51                 
52                 return tonumber(a.ETX) < tonumber(b.ETX)
53         end
54         
55         table.sort(data.Links, compare)
56         
57         luci.template.render("status-olsr/index", {links=data.Links})
58 end
59
60 function action_routes()
61         local data = fetch_txtinfo("routes")
62         
63         if not data or not data.Routes then
64                 luci.template.render("status-olsr/error_olsr")
65                 return nil
66         end
67         
68         local function compare(a, b)
69                 if tonumber(a.ETX) == 0 then
70                         return false
71                 end
72                 
73                 if tonumber(b.ETX) == 0 then
74                         return true
75                 end
76                 
77                 return tonumber(a.ETX) < tonumber(b.ETX)
78         end
79         
80         table.sort(data.Routes, compare)
81         
82         luci.template.render("status-olsr/routes", {routes=data.Routes})
83 end
84
85 function action_topology()
86         local data = fetch_txtinfo("topology")
87         
88         if not data or not data.Topology then
89                 luci.template.render("status-olsr/error_olsr")
90                 return nil
91         end
92         
93         local function compare(a, b)
94                 return a["Destination IP"] < b["Destination IP"]
95         end
96         
97         table.sort(data.Topology, compare)
98         
99         luci.template.render("status-olsr/topology", {routes=data.Topology})
100 end
101
102 function action_hna()
103         local data = fetch_txtinfo("hna")
104         
105         if not data or not data.HNA then
106                 luci.template.render("status-olsr/error_olsr")
107                 return nil
108         end
109         
110         local function compare(a, b)
111                 return a.Network < b.Network
112         end
113         
114         table.sort(data.HNA, compare)
115         
116         luci.template.render("status-olsr/hna", {routes=data.HNA})
117 end
118
119 function action_mid()
120         local data = fetch_txtinfo("mid")
121         
122         if not data or not data.MID then
123                 luci.template.render("status-olsr/error_olsr")
124                 return nil
125         end
126         
127         local function compare(a, b)
128                 return a.IP < b.IP
129         end
130         
131         table.sort(data.MID, compare)
132         
133         luci.template.render("status-olsr/mid", {mids=data.MID})
134 end
135
136
137 -- Internal
138 function fetch_txtinfo(otable)
139         require("luci.sys")
140         otable = otable or ""
141         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
142         
143         if #rawdata == 0 then
144                 return nil
145         end
146         
147         local data = {}
148         
149         local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
150         
151
152         for i, tbl in ipairs(tables) do
153                 local lines = luci.util.split(tbl, "\n")
154                 local name  = table.remove(lines, 1):sub(8)
155                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
156                 
157                 data[name] = {}
158                 
159                 for j, line in ipairs(lines) do
160                         local fields = luci.util.split(line, "\t")
161                         data[name][j] = {}
162                         for k, key in pairs(keys) do
163                                 data[name][j][key] = fields[k] 
164                         end
165                 end
166         end
167         
168         return data
169 end