2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 module("luci.controller.freifunk.freifunk", package.seeall)
18 local uci = require "luci.model.uci".cursor()
24 page.target = alias("freifunk")
28 page = node("freifunk")
29 page.title = _("Freifunk")
30 page.target = alias("freifunk", "index")
32 page.setuser = "nobody"
33 page.setgroup = "nogroup"
34 page.i18n = "freifunk"
37 page = node("freifunk", "index")
38 page.target = template("freifunk/index")
39 page.title = _("Overview")
41 page.indexignore = true
43 page = node("freifunk", "contact")
44 page.target = template("freifunk/contact")
45 page.title = _("Contact")
48 page = node("freifunk", "status")
49 page.target = template("freifunk/public_status")
50 page.title = _("Status")
56 entry({"freifunk", "status.json"}, call("jsonstatus"))
57 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
58 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
60 if nixio.fs.access("/usr/sbin/luci-splash") then
61 assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
64 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
66 if nixio.fs.access("/etc/config/luci_statistics") then
67 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
71 assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
72 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
74 page = node("admin", "freifunk")
75 page.target = template("freifunk/adminindex")
76 page.title = _("Freifunk")
79 page = node("admin", "freifunk", "basics")
80 page.target = cbi("freifunk/basics")
81 page.title = _("Basic Settings")
84 page = node("admin", "freifunk", "basics", "profile")
85 page.target = cbi("freifunk/profile")
86 page.title = _("Profile")
89 page = node("admin", "freifunk", "basics", "profile_expert")
90 page.target = cbi("freifunk/profile_expert")
91 page.title = _("Profile (Expert)")
94 page = node("admin", "freifunk", "Index-Page")
95 page.target = cbi("freifunk/user_index")
96 page.title = _("Index Page")
99 page = node("admin", "freifunk", "contact")
100 page.target = cbi("freifunk/contact")
101 page.title = _("Contact")
104 entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
105 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
106 entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
109 local function fetch_olsrd()
110 local sys = require "luci.sys"
111 local util = require "luci.util"
112 local table = require "table"
113 local rawdata = sys.httpget("http://127.0.0.1:2006/")
115 if #rawdata == 0 then
116 if nixio.fs.access("/proc/net/ipv6_route", "r") then
117 rawdata = sys.httpget("http://[::1]:2006/")
118 if #rawdata == 0 then
128 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
131 for i, tbl in ipairs(tables) do
132 local lines = util.split(tbl, "\r?\n", nil, true)
133 local name = table.remove(lines, 1):sub(8)
134 local keys = util.split(table.remove(lines, 1), "\t")
135 local split = #keys - 1
139 for j, line in ipairs(lines) do
140 local fields = util.split(line, "\t", split)
142 for k, key in pairs(keys) do
143 data[name][j][key] = fields[k]
146 if data[name][j].Linkcost then
147 data[name][j].LinkQuality,
150 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
159 local string = require "string"
160 local http = require "luci.http"
161 local zeroes = string.rep(string.char(0), 8192)
163 local lim = 1024 * 1024 * 1024
165 http.prepare_content("application/x-many-zeroes")
173 function jsonstatus()
175 local sys = require "luci.sys"
176 local uci = require "luci.model.uci"
177 local util = require "luci.util"
178 local http = require "luci.http"
179 local json = require "luci.json"
180 local ltn12 = require "luci.ltn12"
181 local version = require "luci.version"
182 local webadmin = require "luci.tools.webadmin"
184 local cursor = uci.cursor_state()
186 local ffzone = webadmin.firewall_find_zone("freifunk")
187 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
188 local ffwifs = ffznet and util.split(ffznet, " ") or {}
194 uptime = {sys.uptime()},
195 loadavg = {sys.loadavg()},
196 sysinfo = {sys.sysinfo()},
197 hostname = sys.hostname()
201 luciname=version.luciname,
202 luciversion=version.luciversion,
203 distname=version.distname,
204 distversion=version.distversion
208 cursor:foreach("freifunk", "public", function(s)
209 root.freifunk[s[".name"]] = s
212 cursor:foreach("system", "system", function(s)
214 latitude = s.latitude,
215 longitude = s.longitude
220 root.wireless = {devices = {}, interfaces = {}, status = {}}
221 local wifs = root.wireless.interfaces
222 local netdata = luci.sys.net.deviceinfo() or {}
224 for _, vif in ipairs(ffwifs) do
225 root.network[vif] = cursor:get_all("network", vif)
226 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
227 cursor:foreach("wireless", "wifi-iface", function(s)
228 if s.device == vif and s.network == vif then
231 local iwinfo = luci.sys.wifi.getiwinfo(s.ifname)
233 root.wireless.status[s.ifname] = { }
237 "channel", "txpower", "bitrate", "signal", "noise",
238 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
240 root.wireless.status[s.ifname][f] = iwinfo[f]
248 root.olsrd = fetch_olsrd()
250 http.prepare_content("application/json")
251 ltn12.pump.all(json.Encoder(root):source(), http.write)
254 function public_status_json()
255 local twa = require "luci.tools.webadmin"
256 local sys = require "luci.sys"
257 local i18n = require "luci.i18n"
258 local path = luci.dispatcher.context.requestpath
262 for dev in path[#path]:gmatch("[%w%.%-]+") do
263 local j = { id = dev }
264 local iw = luci.sys.wifi.getiwinfo(dev)
268 "channel", "txpower", "bitrate", "signal", "noise",
269 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
277 local load1, load5, load15 = sys.loadavg()
279 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
280 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
281 tonumber(memtotal) / 1024,
282 tonumber(memtotal - memfree) / 1024,
283 tostring(i18n.translate("used")),
285 tostring(i18n.translate("free")),
287 tostring(i18n.translate("cached")),
289 tostring(i18n.translate("buffered"))
292 local dr4 = sys.net.defaultroute()
293 local dr6 = sys.net.defaultroute6()
297 gateway = dr6.nexthop:string(),
298 dest = dr6.dest:string(),
305 gateway = dr4.gateway:string(),
306 dest = dr4.dest:string(),
310 local dr = sys.exec("ip r s t olsr-default")
312 local dest, gateway, dev, metr = dr:match("^(%w+) via (%d+.%d+.%d+.%d+) dev (%w+) +metric (%d+)")
323 time = os.date("%a, %d %b %Y, %H:%M:%S"),
324 uptime = twa.date_format(tonumber(sys.uptime())),
325 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
331 luci.http.prepare_content("application/json")
332 luci.http.write_json(rv)