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 = "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 = "Übersicht"
40 page.indexignore = true
42 local page = node("freifunk", "index", "contact")
43 page.target = template("freifunk/contact")
44 page.title = "Kontakt"
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"}, "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"}, "Freifunk", 5)
67 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 5)
69 local page = node("admin", "freifunk")
70 page.target = template("freifunk/adminindex")
71 page.title = "Freifunk"
74 local page = node("admin", "freifunk", "basics")
75 page.target = cbi("freifunk/basics")
76 page.title = "Grundeinstellungen"
79 local page = node("admin", "freifunk", "basics", "profile")
80 page.target = cbi("freifunk/profile")
81 page.title = "Profile"
84 local page = node("admin", "freifunk", "basics", "profile_expert")
85 page.target = cbi("freifunk/profile_expert")
86 page.title = "Profile (Expert)"
89 local page = node("admin", "freifunk", "Index-Page")
90 page.target = cbi("freifunk/user_index")
91 page.title = "Index-Page"
94 local page = node("admin", "freifunk", "contact")
95 page.target = cbi("freifunk/contact")
96 page.title = "Kontakt"
99 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Karte"), 50)
100 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
102 uci:foreach("olsrd", "LoadPlugin", function(s)
103 if s.library == "olsrd_nameservice.so.0.3" then
108 entry({"freifunk", "services"}, template("freifunk-services/services"), i18n("Services"), 60)
112 local function fetch_olsrd()
113 local sys = require "luci.sys"
114 local util = require "luci.util"
115 local table = require "table"
116 local rawdata = sys.httpget("http://127.0.0.1:2006/")
118 if #rawdata == 0 then
119 if nixio.fs.access("/proc/net/ipv6_route", "r") then
120 rawdata = sys.httpget("http://[::1]:2006/")
121 if #rawdata == 0 then
131 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
134 for i, tbl in ipairs(tables) do
135 local lines = util.split(tbl, "\r?\n", nil, true)
136 local name = table.remove(lines, 1):sub(8)
137 local keys = util.split(table.remove(lines, 1), "\t")
138 local split = #keys - 1
142 for j, line in ipairs(lines) do
143 local fields = util.split(line, "\t", split)
145 for k, key in pairs(keys) do
146 data[name][j][key] = fields[k]
149 if data[name][j].Linkcost then
150 data[name][j].LinkQuality,
153 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
162 local string = require "string"
163 local http = require "luci.http"
164 local zeroes = string.rep(string.char(0), 8192)
166 local lim = 1024 * 1024 * 1024
168 http.prepare_content("application/x-many-zeroes")
176 function jsonstatus()
178 local sys = require "luci.sys"
179 local uci = require "luci.model.uci"
180 local util = require "luci.util"
181 local http = require "luci.http"
182 local json = require "luci.json"
183 local ltn12 = require "luci.ltn12"
184 local version = require "luci.version"
185 local webadmin = require "luci.tools.webadmin"
187 local cursor = uci.cursor_state()
189 local ffzone = webadmin.firewall_find_zone("freifunk")
190 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
191 local ffwifs = ffznet and util.split(ffznet, " ") or {}
197 uptime = {sys.uptime()},
198 loadavg = {sys.loadavg()},
199 sysinfo = {sys.sysinfo()},
200 hostname = sys.hostname()
204 luciname=version.luciname,
205 luciversion=version.luciversion,
206 distname=version.distname,
207 distversion=version.distversion
211 cursor:foreach("freifunk", "public", function(s)
212 root.freifunk[s[".name"]] = s
215 cursor:foreach("system", "system", function(s)
217 latitude = s.latitude,
218 longitude = s.longitude
223 root.wireless = {devices = {}, interfaces = {}, status = {}}
224 local wifs = root.wireless.interfaces
225 local wifidata = luci.sys.wifi.getiwconfig() or {}
226 local netdata = luci.sys.net.deviceinfo() or {}
228 for _, vif in ipairs(ffwifs) do
229 root.network[vif] = cursor:get_all("network", vif)
230 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
231 cursor:foreach("wireless", "wifi-iface", function(s)
232 if s.device == vif and s.network == vif then
235 root.wireless.status[s.ifname] = wifidata[s.ifname]
241 root.olsrd = fetch_olsrd()
243 http.prepare_content("application/json")
244 ltn12.pump.all(json.Encoder(root):source(), http.write)
247 function public_status_json()
248 local twa = require "luci.tools.webadmin"
249 local sys = require "luci.sys"
250 local i18n = require "luci.i18n"
251 local path = luci.dispatcher.context.requestpath
255 for dev in path[#path]:gmatch("[%w%.%-]+") do
256 local j = { id = dev }
257 local iw = luci.sys.wifi.getiwinfo(dev)
261 "channel", "txpower", "bitrate", "signal", "noise",
262 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
270 local load1, load5, load15 = sys.loadavg()
272 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
273 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
274 tonumber(memtotal) / 1024,
275 tonumber(memtotal - memfree) / 1024,
276 tostring(i18n.translate("used")),
278 tostring(i18n.translate("free")),
280 tostring(i18n.translate("cached")),
282 tostring(i18n.translate("buffered"))
285 local dr4 = sys.net.defaultroute()
286 local dr6 = sys.net.defaultroute6()
290 gateway = dr6.nexthop:string(),
291 dest = dr6.dest:string(),
298 gateway = dr4.gateway:string(),
299 dest = dr4.dest:string(),
305 time = os.date("%c"),
306 uptime = twa.date_format(tonumber(sys.uptime())),
307 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
313 luci.http.prepare_content("application/json")
314 luci.http.write_json(rv)