ac143277dbbab896f31bd13d2071e36ba891e9bd
[project/luci.git] / modules / freifunk / luasrc / controller / freifunk / olsr.lua
1 module("luci.controller.freifunk.olsr", package.seeall)
2 require("luci.sys")
3
4 function index()
5         local page  = node("freifunk", "olsr")
6         page.target = call("action_index")
7         page.title  = "OLSR"
8         page.order  = 30
9         
10         local page  = node("freifunk", "olsr", "routes")
11         page.target = call("action_routes")
12         page.title  = "Routen"
13         page.order  = 10
14         
15         local page  = node("freifunk", "olsr", "topology")
16         page.target = call("action_topology")
17         page.title  = "Topologie"
18         page.order  = 20
19         
20         local page  = node("freifunk", "olsr", "hna")
21         page.target = call("action_hna")
22         page.title  = "HNA"
23         page.order  = 30
24         
25         local page  = node("freifunk", "olsr", "mid")
26         page.target = call("action_mid")
27         page.title  = "MID"
28         page.order  = 50
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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-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("freifunk-olsr/mid", {mids=data.MID})
130 end
131
132
133 -- Internal
134 function fetch_txtinfo(otable)
135         otable = otable or ""
136         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
137         
138         if #rawdata == 0 then
139                 return nil
140         end
141         
142         local data = {}
143         
144         local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
145         
146
147         for i, tbl in ipairs(tables) do
148                 local lines = luci.util.split(tbl, "\n")
149                 local name  = table.remove(lines, 1):sub(8)
150                 local keys  = luci.util.split(table.remove(lines, 1), "\t")
151                 
152                 data[name] = {}
153                 
154                 for j, line in ipairs(lines) do
155                         local fields = luci.util.split(line, "\t")
156                         data[name][j] = {}
157                         for k, key in pairs(keys) do
158                                 data[name][j][key] = fields[k] 
159                         end
160                 end
161         end
162         
163         return data
164 end