Added computer-readable Freifunk status page
[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
45         local page  = node("freifunk", "status")
46         page.target = form("freifunk/public_status")
47         page.title  = "Status"
48         page.order  = 20
49         page.i18n   = "admin-core"
50         page.setuser  = false
51         page.setgroup = false
52
53         entry({"freifunk", "status.json"}, call("jsonstatus"))
54
55         assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
56
57         if luci.fs.access("/etc/config/luci_statistics") then
58                 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("stat_statistics", "Statistiken"), 40)
59         end
60
61         assign({"mini", "freifunk"}, {"admin", "freifunk"}, "Freifunk", 15)
62         entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 15)
63         local page  = node("admin", "freifunk", "index")
64         page.target = cbi("freifunk/freifunk")
65         page.title  = "Freifunk"
66         page.order  = 30
67
68         local page  = node("admin", "freifunk", "contact")
69         page.target = cbi("freifunk/contact")
70         page.title  = "Kontakt"
71         page.order  = 40
72 end
73
74 local function fetch_olsrd()
75         local sys = require "luci.sys"
76         local util = require "luci.util"
77         local table = require "table"
78         local rawdata = sys.httpget("http://127.0.0.1:2006/")
79
80         if #rawdata == 0 then
81                 if luci.fs.access("/proc/net/ipv6_route", "r") then
82                         rawdata = sys.httpget("http://[::1]:2006/")
83                         if #rawdata == 0 then
84                                 return nil
85                         end
86                 else
87                         return nil
88                 end
89         end
90
91         local data = {}
92
93         local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
94
95
96         for i, tbl in ipairs(tables) do
97                 local lines = util.split(tbl, "\r?\n", nil, true)
98                 local name  = table.remove(lines, 1):sub(8)
99                 local keys  = util.split(table.remove(lines, 1), "\t")
100                 local split = #keys - 1
101
102                 data[name] = {}
103
104                 for j, line in ipairs(lines) do
105                         local fields = util.split(line, "\t", split)
106                         data[name][j] = {}
107                         for k, key in pairs(keys) do
108                                 data[name][j][key] = fields[k]
109                         end
110
111                         if data[name][j].Linkcost then
112                                 data[name][j].LinkQuality,
113                                 data[name][j].NLQ,
114                                 data[name][j].ETX =
115                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
116                         end
117                 end
118         end
119
120         return data
121 end
122
123 function jsonstatus()
124         local root = {}
125         local sys = require "luci.sys"
126         local uci = require "luci.model.uci"
127         local util = require "luci.util"
128         local http = require "luci.http"
129         local json = require "luci.json"
130         local ltn12 = require "luci.ltn12"
131         local webadmin = require "luci.tools.webadmin"
132
133         local cursor = uci.cursor_state()
134
135         local ffzone = webadmin.firewall_find_zone("freifunk")
136         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
137         local ffwifs = ffznet and util.split(ffznet, " ") or {}
138
139
140         root.protocol = 1
141
142         root.system = {
143                 uptime = {sys.uptime()},
144                 loadavg = {sys.loadavg()},
145                 sysinfo = {sys.sysinfo()},
146                 hostname = sys.hostname()
147         }
148
149         root.brand = cursor:get_all("luci", "brand")
150
151         root.freifunk = {}
152         cursor:foreach("freifunk", "public", function(s)
153                 root.freifunk[s[".name"]] = s
154         end)
155
156         cursor:foreach("system", "system", function(s)
157                 root.geo = {
158                         latitude = s.latitude,
159                         longitude = s.longitude
160                 }
161         end)
162
163         root.network = {}
164         root.wireless = {devices = {}, interfaces = {}, status = {}}
165         local wifs = root.wireless.interfaces
166         local wifidata = luci.sys.wifi.getiwconfig() or {}
167         local netdata = luci.sys.net.deviceinfo() or {}
168
169         for _, vif in ipairs(ffwifs) do
170                 root.network[vif] = cursor:get_all("network", vif)
171                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
172                 cursor:foreach("wireless", "wifi-iface", function(s)
173                         if s.device == vif and s.network == vif then
174                                 wifs[#wifs+1] = s
175                                 if s.ifname then
176                                         root.wireless.status[s.ifname] = wifidata[s.ifname]
177                                 end
178                         end
179                 end)
180         end
181
182         root.olsrd = fetch_olsrd()
183
184         http.prepare_content("application/json")
185         ltn12.pump.all(json.Encoder(root):source(), http.write)
186 end