e30cdb10f8fe0a8c06a9c9f079711441d7eeb850
[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 version = require "luci.version"
132         local webadmin = require "luci.tools.webadmin"
133
134         local cursor = uci.cursor_state()
135
136         local ffzone = webadmin.firewall_find_zone("freifunk")
137         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
138         local ffwifs = ffznet and util.split(ffznet, " ") or {}
139
140
141         root.protocol = 1
142
143         root.system = {
144                 uptime = {sys.uptime()},
145                 loadavg = {sys.loadavg()},
146                 sysinfo = {sys.sysinfo()},
147                 hostname = sys.hostname()
148         }
149
150         root.firmware = {
151                 luciname=version.luciname,
152                 luciversion=version.luciversion,
153                 distname=version.distname,
154                 distversion=version.distversion
155         }
156
157         root.freifunk = {}
158         cursor:foreach("freifunk", "public", function(s)
159                 root.freifunk[s[".name"]] = s
160         end)
161
162         cursor:foreach("system", "system", function(s)
163                 root.geo = {
164                         latitude = s.latitude,
165                         longitude = s.longitude
166                 }
167         end)
168
169         root.network = {}
170         root.wireless = {devices = {}, interfaces = {}, status = {}}
171         local wifs = root.wireless.interfaces
172         local wifidata = luci.sys.wifi.getiwconfig() or {}
173         local netdata = luci.sys.net.deviceinfo() or {}
174
175         for _, vif in ipairs(ffwifs) do
176                 root.network[vif] = cursor:get_all("network", vif)
177                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
178                 cursor:foreach("wireless", "wifi-iface", function(s)
179                         if s.device == vif and s.network == vif then
180                                 wifs[#wifs+1] = s
181                                 if s.ifname then
182                                         root.wireless.status[s.ifname] = wifidata[s.ifname]
183                                 end
184                         end
185                 end)
186         end
187
188         root.olsrd = fetch_olsrd()
189
190         http.prepare_content("application/json")
191         ltn12.pump.all(json.Encoder(root):source(), http.write)
192 end