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