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)
101 entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
104 local function fetch_olsrd()
105 local sys = require "luci.sys"
106 local util = require "luci.util"
107 local table = require "table"
108 local rawdata = sys.httpget("http://127.0.0.1:2006/")
110 if #rawdata == 0 then
111 if nixio.fs.access("/proc/net/ipv6_route", "r") then
112 rawdata = sys.httpget("http://[::1]:2006/")
113 if #rawdata == 0 then
123 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
126 for i, tbl in ipairs(tables) do
127 local lines = util.split(tbl, "\r?\n", nil, true)
128 local name = table.remove(lines, 1):sub(8)
129 local keys = util.split(table.remove(lines, 1), "\t")
130 local split = #keys - 1
134 for j, line in ipairs(lines) do
135 local fields = util.split(line, "\t", split)
137 for k, key in pairs(keys) do
138 data[name][j][key] = fields[k]
141 if data[name][j].Linkcost then
142 data[name][j].LinkQuality,
145 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
154 local string = require "string"
155 local http = require "luci.http"
156 local zeroes = string.rep(string.char(0), 8192)
158 local lim = 1024 * 1024 * 1024
160 http.prepare_content("application/x-many-zeroes")
168 function jsonstatus()
170 local sys = require "luci.sys"
171 local uci = require "luci.model.uci"
172 local util = require "luci.util"
173 local http = require "luci.http"
174 local json = require "luci.json"
175 local ltn12 = require "luci.ltn12"
176 local version = require "luci.version"
177 local webadmin = require "luci.tools.webadmin"
179 local cursor = uci.cursor_state()
181 local ffzone = webadmin.firewall_find_zone("freifunk")
182 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
183 local ffwifs = ffznet and util.split(ffznet, " ") or {}
189 uptime = {sys.uptime()},
190 loadavg = {sys.loadavg()},
191 sysinfo = {sys.sysinfo()},
192 hostname = sys.hostname()
196 luciname=version.luciname,
197 luciversion=version.luciversion,
198 distname=version.distname,
199 distversion=version.distversion
203 cursor:foreach("freifunk", "public", function(s)
204 root.freifunk[s[".name"]] = s
207 cursor:foreach("system", "system", function(s)
209 latitude = s.latitude,
210 longitude = s.longitude
215 root.wireless = {devices = {}, interfaces = {}, status = {}}
216 local wifs = root.wireless.interfaces
217 local wifidata = luci.sys.wifi.getiwconfig() or {}
218 local netdata = luci.sys.net.deviceinfo() or {}
220 for _, vif in ipairs(ffwifs) do
221 root.network[vif] = cursor:get_all("network", vif)
222 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
223 cursor:foreach("wireless", "wifi-iface", function(s)
224 if s.device == vif and s.network == vif then
227 root.wireless.status[s.ifname] = wifidata[s.ifname]
233 root.olsrd = fetch_olsrd()
235 http.prepare_content("application/json")
236 ltn12.pump.all(json.Encoder(root):source(), http.write)
239 function public_status_json()
240 local twa = require "luci.tools.webadmin"
241 local sys = require "luci.sys"
242 local i18n = require "luci.i18n"
243 local path = luci.dispatcher.context.requestpath
247 for dev in path[#path]:gmatch("[%w%.%-]+") do
248 local j = { id = dev }
249 local iw = luci.sys.wifi.getiwinfo(dev)
253 "channel", "txpower", "bitrate", "signal", "noise",
254 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
262 local load1, load5, load15 = sys.loadavg()
264 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
265 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
266 tonumber(memtotal) / 1024,
267 tonumber(memtotal - memfree) / 1024,
268 tostring(i18n.translate("used")),
270 tostring(i18n.translate("free")),
272 tostring(i18n.translate("cached")),
274 tostring(i18n.translate("buffered"))
277 local dr4 = sys.net.defaultroute()
278 local dr6 = sys.net.defaultroute6()
282 gateway = dr6.nexthop:string(),
283 dest = dr6.dest:string(),
290 gateway = dr4.gateway:string(),
291 dest = dr4.dest:string(),
297 time = os.date("%a, %d %b %Y, %H:%M:%S"),
298 uptime = twa.date_format(tonumber(sys.uptime())),
299 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
305 luci.http.prepare_content("application/json")
306 luci.http.write_json(rv)