luci/applications/luci-olsr/luasrc/controller/olsr.lua

170 lines
3.4 KiB
Lua
Raw Normal View History

module("luci.controller.olsr", package.seeall)
2008-05-08 16:14:49 +00:00
2008-05-22 14:04:03 +00:00
function index()
if not luci.fs.isfile("/etc/config/olsr") then
return
end
local page = node("admin", "status", "olsr")
2008-05-27 20:39:48 +00:00
page.target = call("action_index")
2008-05-22 14:04:03 +00:00
page.title = "OLSR"
local page = node("admin", "status", "olsr", "routes")
2008-05-27 20:39:48 +00:00
page.target = call("action_routes")
2008-05-22 14:04:03 +00:00
page.title = "Routen"
page.order = 10
local page = node("admin", "status", "olsr", "topology")
2008-05-27 20:39:48 +00:00
page.target = call("action_topology")
2008-05-22 14:04:03 +00:00
page.title = "Topologie"
page.order = 20
local page = node("admin", "status", "olsr", "hna")
2008-05-27 20:39:48 +00:00
page.target = call("action_hna")
2008-05-22 14:04:03 +00:00
page.title = "HNA"
page.order = 30
local page = node("admin", "status", "olsr", "mid")
2008-05-27 20:39:48 +00:00
page.target = call("action_mid")
2008-05-22 14:04:03 +00:00
page.title = "MID"
page.order = 50
2008-06-06 22:27:03 +00:00
entry({"admin", "services", "olsrd"}, cbi("olsr/olsrd"), "OLSR")
2008-05-22 14:04:03 +00:00
end
2008-05-08 16:14:49 +00:00
function action_index()
local data = fetch_txtinfo("links")
if not data or not data.Links then
luci.template.render("status-olsr/error_olsr")
2008-05-08 16:14:49 +00:00
return nil
end
local function compare(a, b)
if tonumber(a.ETX) == 0 then
return false
end
if tonumber(b.ETX) == 0 then
return true
end
return tonumber(a.ETX) < tonumber(b.ETX)
end
table.sort(data.Links, compare)
luci.template.render("status-olsr/index", {links=data.Links})
2008-05-08 16:14:49 +00:00
end
function action_routes()
local data = fetch_txtinfo("routes")
if not data or not data.Routes then
luci.template.render("status-olsr/error_olsr")
2008-05-08 16:14:49 +00:00
return nil
end
local function compare(a, b)
if tonumber(a.ETX) == 0 then
return false
end
if tonumber(b.ETX) == 0 then
return true
end
return tonumber(a.ETX) < tonumber(b.ETX)
end
table.sort(data.Routes, compare)
luci.template.render("status-olsr/routes", {routes=data.Routes})
2008-05-08 16:14:49 +00:00
end
function action_topology()
local data = fetch_txtinfo("topology")
if not data or not data.Topology then
luci.template.render("status-olsr/error_olsr")
2008-05-08 16:14:49 +00:00
return nil
end
local function compare(a, b)
return a["Destination IP"] < b["Destination IP"]
end
table.sort(data.Topology, compare)
luci.template.render("status-olsr/topology", {routes=data.Topology})
2008-05-08 16:14:49 +00:00
end
function action_hna()
local data = fetch_txtinfo("hna")
if not data or not data.HNA then
luci.template.render("status-olsr/error_olsr")
2008-05-08 16:14:49 +00:00
return nil
end
local function compare(a, b)
return a.Network < b.Network
end
table.sort(data.HNA, compare)
luci.template.render("status-olsr/hna", {routes=data.HNA})
2008-05-08 16:14:49 +00:00
end
function action_mid()
local data = fetch_txtinfo("mid")
if not data or not data.MID then
luci.template.render("status-olsr/error_olsr")
2008-05-08 16:14:49 +00:00
return nil
end
local function compare(a, b)
return a.IP < b.IP
end
table.sort(data.MID, compare)
luci.template.render("status-olsr/mid", {mids=data.MID})
2008-05-08 16:14:49 +00:00
end
-- Internal
function fetch_txtinfo(otable)
require("luci.sys")
2008-05-08 16:14:49 +00:00
otable = otable or ""
local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
2008-05-08 16:14:49 +00:00
if #rawdata == 0 then
return nil
end
local data = {}
local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
2008-05-08 16:14:49 +00:00
for i, tbl in ipairs(tables) do
local lines = luci.util.split(tbl, "\n")
2008-05-08 16:14:49 +00:00
local name = table.remove(lines, 1):sub(8)
local keys = luci.util.split(table.remove(lines, 1), "\t")
2008-05-08 16:14:49 +00:00
data[name] = {}
for j, line in ipairs(lines) do
local fields = luci.util.split(line, "\t")
2008-05-08 16:14:49 +00:00
data[name][j] = {}
for k, key in pairs(keys) do
data[name][j][key] = fields[k]
end
end
end
return data
end