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