84669dce847e67992770a0fb5db5098a88b8662b
[project/luci.git] / modules / luci-mod-freifunk / luasrc / controller / freifunk / freifunk.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.freifunk.freifunk", package.seeall)
5
6 function index()
7         local uci = require "luci.model.uci".cursor()
8         local page
9
10         -- Frontend
11         page          = node()
12         page.lock     = true
13         page.target   = alias("freifunk")
14         page.subindex = true
15         page.index    = false
16
17         page          = node("freifunk")
18         page.title    = _("Freifunk")
19         page.target   = alias("freifunk", "index")
20         page.order    = 5
21         page.setuser  = "nobody"
22         page.setgroup = "nogroup"
23         page.i18n     = "freifunk"
24         page.index    = true
25
26         page          = node("freifunk", "index")
27         page.target   = template("freifunk/index")
28         page.title    = _("Overview")
29         page.order    = 10
30         page.indexignore = true
31
32         page          = node("freifunk", "contact")
33         page.target   = template("freifunk/contact")
34         page.title    = _("Contact")
35         page.order    = 15
36
37         page          = node("freifunk", "status")
38         page.target   = template("freifunk/public_status")
39         page.title    = _("Status")
40         page.order    = 20
41         page.i18n     = "base"
42         page.setuser  = false
43         page.setgroup = false
44
45         entry({"freifunk", "status.json"}, call("jsonstatus"))
46         entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
47
48         if nixio.fs.access("/usr/sbin/luci-splash") then
49                 assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
50         end
51
52         page = assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
53         page.setuser = false
54         page.setgroup = false
55
56         if nixio.fs.access("/etc/config/luci_statistics") then
57                 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
58         end
59
60         -- backend
61         assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
62         entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
63
64         page        = node("admin", "freifunk")
65         page.target = template("freifunk/adminindex")
66         page.title  = _("Freifunk")
67         page.order  = 5
68
69         page        = node("admin", "freifunk", "basics")
70         page.target = cbi("freifunk/basics")
71         page.title  = _("Basic Settings")
72         page.order  = 5
73         
74         page        = node("admin", "freifunk", "basics", "profile")
75         page.target = cbi("freifunk/profile")
76         page.title  = _("Profile")
77         page.order  = 10
78
79         page        = node("admin", "freifunk", "basics", "profile_expert")
80         page.target = cbi("freifunk/profile_expert")
81         page.title  = _("Profile (Expert)")
82         page.order  = 20
83
84         page        = node("admin", "freifunk", "Index-Page")
85         page.target = cbi("freifunk/user_index")
86         page.title  = _("Index Page")
87         page.order  = 50
88
89         page        = node("admin", "freifunk", "contact")
90         page.target = cbi("freifunk/contact")
91         page.title  = _("Contact")
92         page.order  = 15
93
94         entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
95         entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
96         entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
97 end
98
99 function zeroes()
100         local string = require "string"
101         local http = require "luci.http"
102         local zeroes = string.rep(string.char(0), 8192)
103         local cnt = 0
104         local lim = 1024 * 1024 * 1024
105         
106         http.prepare_content("application/x-many-zeroes")
107
108         while cnt < lim do
109                 http.write(zeroes)
110                 cnt = cnt + #zeroes
111         end
112 end
113
114 function jsonstatus()
115         local root = {}
116         local sys = require "luci.sys"
117         local uci = require "luci.model.uci"
118         local util = require "luci.util"
119         local http = require "luci.http"
120         local json = require "luci.json"
121         local ltn12 = require "luci.ltn12"
122         local version = require "luci.version"
123         local webadmin = require "luci.tools.webadmin"
124
125         local cursor = uci.cursor_state()
126
127         local ffzone = webadmin.firewall_find_zone("freifunk")
128         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
129         local ffwifs = ffznet and util.split(ffznet, " ") or {}
130
131         local sysinfo = util.ubus("system", "info") or { }
132         local boardinfo = util.ubus("system", "board") or { }
133
134         local loads = sysinfo.load or { 0, 0, 0 }
135
136         local memory = sysinfo.memory or {
137                 total = 0,
138                 free = 0,
139                 shared = 0,
140                 buffered = 0
141         }
142
143         local swap = sysinfo.swap or {
144                 total = 0,
145                 free = 0
146         }
147
148
149         root.protocol = 1
150
151         root.system = {
152                 uptime = { sysinfo.uptime or 0 },
153                 loadavg = { loads[1] / 65535.0, loads[2] / 65535.0, loads[3] / 65535.0 },
154                 sysinfo = {
155                         boardinfo.system or "?",
156                         boardinfo.model or "?",
157                         memory.total,
158                         0, -- former cached memory
159                         memory.buffered,
160                         memory.free,
161                         0, -- former bogomips
162                         swap.total,
163                         0, -- former cached swap
164                         swap.free
165                 },
166                 hostname = boardinfo.hostname
167         }
168
169         root.firmware = {
170                 luciname=version.luciname,
171                 luciversion=version.luciversion,
172                 distname=version.distname,
173                 distversion=version.distversion
174         }
175
176         root.freifunk = {}
177         cursor:foreach("freifunk", "public", function(s)
178                 root.freifunk[s[".name"]] = s
179         end)
180
181         cursor:foreach("system", "system", function(s)
182                 root.geo = {
183                         latitude = s.latitude,
184                         longitude = s.longitude
185                 }
186         end)
187
188         root.network = {}
189         root.wireless = {devices = {}, interfaces = {}, status = {}}
190         local wifs = root.wireless.interfaces
191         local netdata = luci.sys.net.deviceinfo() or {}
192
193         for _, vif in ipairs(ffwifs) do
194                 root.network[vif] = cursor:get_all("network", vif)
195                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
196                 cursor:foreach("wireless", "wifi-iface", function(s)
197                         if s.device == vif and s.network == vif then
198                                 wifs[#wifs+1] = s
199                                 if s.ifname then
200                                         local iwinfo = luci.sys.wifi.getiwinfo(s.ifname)
201                                         if iwinfo then
202                                                 root.wireless.status[s.ifname] = { }
203
204                                                 local _, f
205                                                 for _, f in ipairs({
206                                                         "channel", "txpower", "bitrate", "signal", "noise",
207                                                         "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
208                                                 }) do
209                                                         root.wireless.status[s.ifname][f] = iwinfo[f]
210                                                 end
211                                         end
212                                 end
213                         end
214                 end)
215         end
216
217         http.prepare_content("application/json")
218         ltn12.pump.all(json.Encoder(root):source(), http.write)
219 end