Freifunk: Add page to show services announced by olsrd
[project/luci.git] / modules / 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 module("luci.controller.freifunk.freifunk", package.seeall)
15
16 function index()
17 local i18n = luci.i18n.translate
18
19 local page = node()
20 page.lock = true
21 page.target = alias("freifunk")
22 page.subindex = true
23 page.index = false
24
25 local page = node("freifunk")
26 page.title = "Freifunk"
27 page.target = alias("freifunk", "index")
28 page.order = 5
29 page.setuser = "nobody"
30 page.setgroup = "nogroup"
31 page.i18n = "freifunk"
32 page.index = true
33
34 local page = node("freifunk", "index")
35 page.target = template("freifunk/index")
36 page.title = "Übersicht"
37 page.order = 10
38 page.indexignore = true
39
40 local page = node("freifunk", "index", "contact")
41 page.target = template("freifunk/contact")
42 page.title = "Kontakt"
43
44 entry({"freifunk", "status"}, alias("freifunk", "status", "status"), "Status", 20)
45
46 local page = node("freifunk", "status", "status")
47 page.target = form("freifunk/public_status")
48 page.title = i18n("Overview")
49 page.order = 20
50 page.i18n = "base"
51 page.setuser = false
52 page.setgroup = false
53
54 entry({"freifunk", "status.json"}, call("jsonstatus"))
55 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
56
57 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
58
59 if nixio.fs.access("/etc/config/luci_statistics") then
60 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("Statistics"), 40)
61 end
62
63 assign({"mini", "freifunk"}, {"admin", "freifunk"}, "Freifunk", 15)
64 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 15)
65 local page = node("admin", "freifunk", "index")
66 page.target = cbi("freifunk/freifunk")
67 page.title = "Freifunk"
68 page.order = 30
69
70 local page = node("admin", "freifunk", "contact")
71 page.target = cbi("freifunk/contact")
72 page.title = "Kontakt"
73 page.order = 40
74
75 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Karte"), 50)
76 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
77
78 entry({"freifunk", "services"}, template("freifunk-services/services"), i18n("Services"), 60)
79 entry({"freifunk", "services", "content"}, template("freifunk-services/services"), nil, 61)
80
81 end
82
83 local function fetch_olsrd()
84 local sys = require "luci.sys"
85 local util = require "luci.util"
86 local table = require "table"
87 local rawdata = sys.httpget("http://127.0.0.1:2006/")
88
89 if #rawdata == 0 then
90 if nixio.fs.access("/proc/net/ipv6_route", "r") then
91 rawdata = sys.httpget("http://[::1]:2006/")
92 if #rawdata == 0 then
93 return nil
94 end
95 else
96 return nil
97 end
98 end
99
100 local data = {}
101
102 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
103
104
105 for i, tbl in ipairs(tables) do
106 local lines = util.split(tbl, "\r?\n", nil, true)
107 local name = table.remove(lines, 1):sub(8)
108 local keys = util.split(table.remove(lines, 1), "\t")
109 local split = #keys - 1
110
111 data[name] = {}
112
113 for j, line in ipairs(lines) do
114 local fields = util.split(line, "\t", split)
115 data[name][j] = {}
116 for k, key in pairs(keys) do
117 data[name][j][key] = fields[k]
118 end
119
120 if data[name][j].Linkcost then
121 data[name][j].LinkQuality,
122 data[name][j].NLQ,
123 data[name][j].ETX =
124 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
125 end
126 end
127 end
128
129 return data
130 end
131
132 function zeroes()
133 local string = require "string"
134 local http = require "luci.http"
135 local zeroes = string.rep(string.char(0), 8192)
136 local cnt = 0
137 local lim = 1024 * 1024 * 1024
138
139 http.prepare_content("application/x-many-zeroes")
140
141 while cnt < lim do
142 http.write(zeroes)
143 cnt = cnt + #zeroes
144 end
145 end
146
147 function jsonstatus()
148 local root = {}
149 local sys = require "luci.sys"
150 local uci = require "luci.model.uci"
151 local util = require "luci.util"
152 local http = require "luci.http"
153 local json = require "luci.json"
154 local ltn12 = require "luci.ltn12"
155 local version = require "luci.version"
156 local webadmin = require "luci.tools.webadmin"
157
158 local cursor = uci.cursor_state()
159
160 local ffzone = webadmin.firewall_find_zone("freifunk")
161 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
162 local ffwifs = ffznet and util.split(ffznet, " ") or {}
163
164
165 root.protocol = 1
166
167 root.system = {
168 uptime = {sys.uptime()},
169 loadavg = {sys.loadavg()},
170 sysinfo = {sys.sysinfo()},
171 hostname = sys.hostname()
172 }
173
174 root.firmware = {
175 luciname=version.luciname,
176 luciversion=version.luciversion,
177 distname=version.distname,
178 distversion=version.distversion
179 }
180
181 root.freifunk = {}
182 cursor:foreach("freifunk", "public", function(s)
183 root.freifunk[s[".name"]] = s
184 end)
185
186 cursor:foreach("system", "system", function(s)
187 root.geo = {
188 latitude = s.latitude,
189 longitude = s.longitude
190 }
191 end)
192
193 root.network = {}
194 root.wireless = {devices = {}, interfaces = {}, status = {}}
195 local wifs = root.wireless.interfaces
196 local wifidata = luci.sys.wifi.getiwconfig() or {}
197 local netdata = luci.sys.net.deviceinfo() or {}
198
199 for _, vif in ipairs(ffwifs) do
200 root.network[vif] = cursor:get_all("network", vif)
201 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
202 cursor:foreach("wireless", "wifi-iface", function(s)
203 if s.device == vif and s.network == vif then
204 wifs[#wifs+1] = s
205 if s.ifname then
206 root.wireless.status[s.ifname] = wifidata[s.ifname]
207 end
208 end
209 end)
210 end
211
212 root.olsrd = fetch_olsrd()
213
214 http.prepare_content("application/json")
215 ltn12.pump.all(json.Encoder(root):source(), http.write)
216 end