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
14 module("luci.controller.freifunk.freifunk", package.seeall)
17 local i18n = luci.i18n.translate
18 local uci = require "luci.model.uci".cursor()
23 page.target = alias("freifunk")
27 local page = node("freifunk")
28 page.title = i18n("Freifunk")
29 page.target = alias("freifunk", "index")
31 page.setuser = "nobody"
32 page.setgroup = "nogroup"
33 page.i18n = "freifunk"
36 local page = node("freifunk", "index")
37 page.target = template("freifunk/index")
38 page.title = i18n("Overview")
40 page.indexignore = true
42 local page = node("freifunk", "index", "contact")
43 page.target = template("freifunk/contact")
44 page.title = i18n("Contact")
47 local page = node("freifunk", "status")
48 page.target = template("freifunk/public_status")
49 page.title = i18n("Status")
55 entry({"freifunk", "status.json"}, call("jsonstatus"))
56 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
57 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
59 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, i18n("OLSR"), 30)
61 if nixio.fs.access("/etc/config/luci_statistics") then
62 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("Statistics"), 40)
66 assign({"mini", "freifunk"}, {"admin", "freifunk"}, i18n("Freifunk"), 5)
67 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), i18n("Freifunk"), 5)
69 local page = node("admin", "freifunk")
70 page.target = template("freifunk/adminindex")
71 page.title = i18n("Freifunk")
74 local page = node("admin", "freifunk", "basics")
75 page.target = cbi("freifunk/basics")
76 page.title = i18n("Basic Settings")
79 local page = node("admin", "freifunk", "basics", "profile")
80 page.target = cbi("freifunk/profile")
81 page.title = i18n("Profile")
84 local page = node("admin", "freifunk", "basics", "profile_expert")
85 page.target = cbi("freifunk/profile_expert")
86 page.title = i18n("Profile (Expert)")
89 local page = node("admin", "freifunk", "Index-Page")
90 page.target = cbi("freifunk/user_index")
91 page.title = i18n("Index Page")
94 local page = node("admin", "freifunk", "contact")
95 page.target = cbi("freifunk/contact")
96 page.title = i18n("Contact")
99 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Map"), 50)
100 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
103 local function fetch_olsrd()
104 local sys = require "luci.sys"
105 local util = require "luci.util"
106 local table = require "table"
107 local rawdata = sys.httpget("http://127.0.0.1:2006/")
109 if #rawdata == 0 then
110 if nixio.fs.access("/proc/net/ipv6_route", "r") then
111 rawdata = sys.httpget("http://[::1]:2006/")
112 if #rawdata == 0 then
122 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
125 for i, tbl in ipairs(tables) do
126 local lines = util.split(tbl, "\r?\n", nil, true)
127 local name = table.remove(lines, 1):sub(8)
128 local keys = util.split(table.remove(lines, 1), "\t")
129 local split = #keys - 1
133 for j, line in ipairs(lines) do
134 local fields = util.split(line, "\t", split)
136 for k, key in pairs(keys) do
137 data[name][j][key] = fields[k]
140 if data[name][j].Linkcost then
141 data[name][j].LinkQuality,
144 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
153 local string = require "string"
154 local http = require "luci.http"
155 local zeroes = string.rep(string.char(0), 8192)
157 local lim = 1024 * 1024 * 1024
159 http.prepare_content("application/x-many-zeroes")
167 function jsonstatus()
169 local sys = require "luci.sys"
170 local uci = require "luci.model.uci"
171 local util = require "luci.util"
172 local http = require "luci.http"
173 local json = require "luci.json"
174 local ltn12 = require "luci.ltn12"
175 local version = require "luci.version"
176 local webadmin = require "luci.tools.webadmin"
178 local cursor = uci.cursor_state()
180 local ffzone = webadmin.firewall_find_zone("freifunk")
181 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
182 local ffwifs = ffznet and util.split(ffznet, " ") or {}
188 uptime = {sys.uptime()},
189 loadavg = {sys.loadavg()},
190 sysinfo = {sys.sysinfo()},
191 hostname = sys.hostname()
195 luciname=version.luciname,
196 luciversion=version.luciversion,
197 distname=version.distname,
198 distversion=version.distversion
202 cursor:foreach("freifunk", "public", function(s)
203 root.freifunk[s[".name"]] = s
206 cursor:foreach("system", "system", function(s)
208 latitude = s.latitude,
209 longitude = s.longitude
214 root.wireless = {devices = {}, interfaces = {}, status = {}}
215 local wifs = root.wireless.interfaces
216 local wifidata = luci.sys.wifi.getiwconfig() or {}
217 local netdata = luci.sys.net.deviceinfo() or {}
219 for _, vif in ipairs(ffwifs) do
220 root.network[vif] = cursor:get_all("network", vif)
221 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
222 cursor:foreach("wireless", "wifi-iface", function(s)
223 if s.device == vif and s.network == vif then
226 root.wireless.status[s.ifname] = wifidata[s.ifname]
232 root.olsrd = fetch_olsrd()
234 http.prepare_content("application/json")
235 ltn12.pump.all(json.Encoder(root):source(), http.write)
238 function public_status_json()
239 local twa = require "luci.tools.webadmin"
240 local sys = require "luci.sys"
241 local i18n = require "luci.i18n"
242 local path = luci.dispatcher.context.requestpath
246 for dev in path[#path]:gmatch("[%w%.%-]+") do
247 local j = { id = dev }
248 local iw = luci.sys.wifi.getiwinfo(dev)
252 "channel", "txpower", "bitrate", "signal", "noise",
253 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
261 local load1, load5, load15 = sys.loadavg()
263 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
264 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
265 tonumber(memtotal) / 1024,
266 tonumber(memtotal - memfree) / 1024,
267 tostring(i18n.translate("used")),
269 tostring(i18n.translate("free")),
271 tostring(i18n.translate("cached")),
273 tostring(i18n.translate("buffered"))
276 local dr4 = sys.net.defaultroute()
277 local dr6 = sys.net.defaultroute6()
281 gateway = dr6.nexthop:string(),
282 dest = dr6.dest:string(),
289 gateway = dr4.gateway:string(),
290 dest = dr4.dest:string(),
296 time = os.date("%c"),
297 uptime = twa.date_format(tonumber(sys.uptime())),
298 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
304 luci.http.prepare_content("application/json")
305 luci.http.write_json(rv)