summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-mod-freifunk/luasrc/view/freifunk/public_status.htm
blob: a56e4826a95d3672f1a29e2dbbf157cd6f73b82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<%
local utl = require "luci.util"
local sys = require "luci.sys"
local twa = require "luci.tools.webadmin"
local ip  = require "luci.ip"

-- System

local sysinfo = utl.ubus("system", "info") or { }
local boardinfo = utl.ubus("system", "board") or { }

local loads = sysinfo.load or { 0, 0, 0 }
local meminfo = sysinfo.memory or {
	total = 0,
	free = 0,
	buffered = 0,
	shared = 0
}

local uptime = twa.date_format(sysinfo.uptime or 0)
local time = os.date("%a, %d %b %Y, %H:%M:%S")
local load = string.format("%.2f, %.2f, %.2f", loads[1] / 65535.0, loads[2] / 65535.0, loads[3] / 65535.0)

local mem = string.format(
	"%.2f MB (%.2f %s, %.2f %s, %.2f %s)",
	meminfo.total / 1024 / 1024,
	(meminfo.total - meminfo.free) / 1024 / 1024,
	tostring(i18n.translate("used")),
	meminfo.free / 1024 / 1024,
	tostring(i18n.translate("free")),
	meminfo.buffered / 1024 / 1024,
	tostring(i18n.translate("buffered"))
)

local interval = 5

-- wireless
local ntm = require "luci.model.network".init()
local devices  = ntm:get_wifidevs()
local netlist = { }
local netdevs = { }
local dev
for _, dev in ipairs(devices) do
	local net
	for _, net in ipairs(dev:get_wifinets()) do
		netlist[#netlist+1] = net:ifname()
		netdevs[net:ifname()] = dev:name()
	end
end
local has_iwinfo = pcall(require, "iwinfo")


-- Find default routes

local _, r, def4, def6

for _, r in ipairs(ip.routes({ type = 1, dest_exact = "0.0.0.0/0" })) do
	def4 = {
		gateway = r.gw:string(),
		dest = r.dest:string(),
		dev = r.dev,
		metr = r.metric or 0
	}
	break
end

for _, r in ipairs(ip.routes({ type = 1, dest_exact = "::/0" })) do
	def6 = {
		gateway = r.gw:string(),
		dest = r.dest:string(),
		dev = r.dev,
		metr = r.metric or 0
	}
	break
end


if luci.http.formvalue("status") == "1" then
	local rv = { }
	for dev in pairs(netdevs) do
		local j = { id = dev }
		local iw = luci.sys.wifi.getiwinfo(dev)
		if iw then
			local f
			for _, f in ipairs({
				"channel", "txpower", "bitrate", "signal", "noise",
				"quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
			}) do
				j[f] = iw[f]
			end
		end
		rv[#rv+1] = j
	end


	rv[#rv+1] = {
		time = time,
		uptime = uptime,
		load = load,
		mem = mem,
		defroutev4 = def4,
		defroutev6 = def6
	}

	luci.http.prepare_content("application/json")
	luci.http.write_json(rv)
	return
end
-%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>

<script type="text/javascript">//<![CDATA[
	 XHR.poll(<%=interval%> , '<%=REQUEST_URI%>', { status: 1 },
		function(x, st)
		{
			if (st)
			{
				for( var i = 0; i < st.length; i++ )
				{
					var iw = st[i];
					var is_assoc = (iw.bssid && iw.channel);
					var p = (100 / iw.quality_max * iw.quality);
					var q = is_assoc ? p : -1;

					var icon;
					if (q < 0)
						icon = "<%=resource%>/icons/signal-none.png";
					else if (q == 0)
						icon = "<%=resource%>/icons/signal-0.png";
					else if (q < 25)
						icon = "<%=resource%>/icons/signal-0-25.png";
					else if (q < 50)
						icon = "<%=resource%>/icons/signal-25-50.png";
					else if (q < 75)
						icon = "<%=resource%>/icons/signal-50-75.png";
					else
						icon = "<%=resource%>/icons/signal-75-100.png";

					var power = document.getElementById(iw.id + '-txpower');
					if (power)
						power.innerHTML = String.format('%s dbm', iw.txpower);

					var signal = document.getElementById(iw.id + '-signal');
					if (signal)
						signal.innerHTML = String.format(
							'<img src="%s" title="Signal: %s db / Noise: %s db" alt="Signal Quality" />',
							icon, iw.signal, iw.noise
						);

					var bitrate = document.getElementById(iw.id + '-bitrate');
					if (bitrate)
						bitrate.innerHTML = String.format('%s Mb/s', iw.bitrate ? iw.bitrate / 1000 : '?');

					var ssid = document.getElementById(iw.id + '-ssid');
					if (ssid)
						ssid.innerHTML = iw.ssid;

					var bssid = document.getElementById(iw.id + '-bssid');
					if (bssid)
						bssid.innerHTML = iw.bssid;

					var channel = document.getElementById(iw.id + '-channel');
					if (channel)
						channel.innerHTML = iw.channel;

					var mode = document.getElementById(iw.id + '-mode');
					if (mode)
					mode.innerHTML = iw.mode;
				}

				i = st.length - 1
				var u

				if (u = document.getElementById('dynuptime'))
					u.innerHTML = st[i].uptime;

				if (u = document.getElementById('dynload'))
					u.innerHTML = st[i].load;

				if (u = document.getElementById('dynmem'))
					u.innerHTML = st[i].mem;

				if (u = document.getElementById('dyntime'))
					u.innerHTML = st[i].time;

				if (st[i].defroutev4)
				{
					if (u = document.getElementById('v4dst'))
						u.innerHTML = st[i].defroutev4.dest;

					if (u = document.getElementById('v4gw'))
						u.innerHTML = st[i].defroutev4.gateway;

					if (u = document.getElementById('v4dev'))
						u.innerHTML = st[i].defroutev4.dev;

					if (u = document.getElementById('v4metr'))
						u.innerHTML = st[i].defroutev4.metr;
				}

				if (st[i].defroutev6)
				{
					if (u = document.getElementById('v6dst'))
						u.innerHTML = st[i].defroutev6.dest;

					if (u = document.getElementById('v6gw'))
						u.innerHTML = st[i].defroutev6.gateway;

					if (u = document.getElementById('v6dev'))
						u.innerHTML = st[i].defroutev6.dev;

					if (u = document.getElementById('v6metr'))
						u.innerHTML = st[i].defroutev6.metr;
				}
			}
		}
	);
//]]></script>

<div class="cbi-map">
	<h2><%:System%></h2>
	<div class="cbi-section-node">
		<div class="cbi-value"><label class="cbi-value-title"><%:System%></label><div class="cbi-value-field"><%=boardinfo.system or "?"%></div></div>
		<div class="cbi-value"><label class="cbi-value-title"><%:Model%></label><div class="cbi-value-field"><%=boardinfo.model or "?"%></div></div>
		<div class="cbi-value"><label class="cbi-value-title"><%:Load%></label><div class="cbi-value-field" id="dynload"><%=load%></div></div>
		<div class="cbi-value"><label class="cbi-value-title"><%:Memory%></label><div class="cbi-value-field" id="dynmem"><%=mem%></div></div>
		<div class="cbi-value"><label class="cbi-value-title"><%:Local Time%></label><div class="cbi-value-field" id="dyntime"><%=time%></div></div>
		<div class="cbi-value"><label class="cbi-value-title"><%:Uptime%></label><div class="cbi-value-field" id="dynuptime"><%=uptime%></div></div>
	</div>
</div>

<% if devices[1] then %>

<div class="cbi-map">
	<h2><%:Wireless Overview%></h2>

		<% if not has_iwinfo then %>
			<div class="alert-message warning">
				<h4><%:Package libiwinfo required!%></h4>
				<p><%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%></p>
			</div>
		<% end %>

		<div class="cbi-section">
			<div class="cbi-section-node">
				<div class="table cbi-section-table">
					<div class="tr cbi-section-table-titles">
						<div class="th cbi-section-table-cell"><%:Signal%></div>
						<div class="th cbi-section-table-cell"><%:Bitrate%></div>
						<div class="th cbi-section-table-cell"><%:SSID%></div>
						<div class="th cbi-section-table-cell"><%:BSSID%></div>
						<div class="th cbi-section-table-cell"><%:Channel%></div>
						<div class="th cbi-section-table-cell"><%:Mode%></div>
						<div class="th cbi-section-table-cell"><%:TX%>-<%:Power%></div>
						<div class="th cbi-section-table-cell"><%:Interface%></div>
					</div>
	<%
	for _, dev in ipairs(devices) do
	local net
		for _, net in ipairs(dev:get_wifinets()) do
			netlist[#netlist+1] = net:ifname()
			netdevs[net:ifname()] = dev:name()

			if net.iwinfo.signal and net.iwinfo.bssid then
				local signal = net.iwinfo.signal or "N/A"
				local noise = net.iwinfo.noise or "N/A"
				local q = net.iwinfo.quality or "0"
				local qmax = net.iwinfo.quality_max or "100"
				local qperc = q / qmax * 100

				if qperc == 0 then
					icon = "signal-none.png"
				elseif qperc < 26 then
					icon = "signal-0-25.png"
				elseif qperc < 51 then
					icon = "signal-25-50.png"
				elseif qperc < 76 then
					icon = "signal-50-75.png"
				elseif qperc < 100 then
					icon = "signal-75-100.png"
				else
					icon = "signal-0.png"
				end

				signal_string = "<img src='"..resource.."/icons/"..icon.."' title='Signal: "..signal.." db / Noise: "..noise.." db' alt='Signal Quality'></img>"

				local ssid = net.iwinfo.ssid or "N/A"
				local bssid = net.iwinfo.bssid or "N/A"
				local chan = net.iwinfo.channel or "N/A"
				local mode = net.iwinfo.mode or "N/A"
				local txpwr = net.iwinfo.txpower or "N/A"
				if txpwr ~= "N/A" then
					txpwr = txpwr.." dbm"
				end
				local bitrate = net.iwinfo.bitrate or "N/A"
				if bitrate ~= "N/A" then
					bitrate = ( bitrate / 1000 ).."Mb/s"
				end
				local interface = net.iwinfo.ifname or "N/A"
	%>
						<div class="tr cbi-section-table-row cbi-rowstyle-1">
						<div class="td cbi-value-field" id="<%=net:ifname()%>-signal"><%=signal_string%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-bitrate"><%=bitrate%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-ssid"><%=ssid%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-bssid"><%=bssid%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-channel"><%=chan%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-mode"><%=mode%></div>
						<div class="td cbi-value-field" id="<%=net:ifname()%>-txpower"><%=txpwr%></div>
						<div class="td cbi-value-field"><%=interface%></div>
					</div>
			<% end
		end
	end %>
			</div>
		</div>
	</div>
</div>
<% end %>

<div class="cbi-map">
	<h2><%:Default routes%></h2>
		<div class="cbi-section">
			<div class="cbi-section-node">

<% if not def4 and not def6 then %>
	<%:No default routes known.%>
<%else%>
				<div class="table cbi-section-table">
						<div class="tr cbi-section-table-titles">
							<div class="th cbi-section-table-cell"><%:Network%></div>
							<div class="th cbi-section-table-cell"><%:Interface%></div>
							<div class="th cbi-section-table-cell"><%:Gateway%></div>
							<div class="th cbi-section-table-cell"><%:Metric%></div>
						</div>

	<% if def4 then %>
						<div class="tr cbi-section-table-row cbi-rowstyle-1">
							<div class="td cbi-value-field" id="v4dst"><%=def4.dest%></div>
							<div class="td cbi-value-field" id="v4dev"><%=def4.dev%></div>
							<div class="td cbi-value-field" id="v4gw"><%=def4.gateway%></div>
							<div class="td cbi-value-field" id="v4metr"><%=def4.metr%></div>
						</div>

	<% end
	if def6 then %>

						<div class="tr cbi-section-table-row cbi-rowstyle-2">
							<div class="td cbi-value-field" id="v6dst"><%=def6.dest%></div>
							<div class="td cbi-value-field" id="v6dev"><%=def6.dev%></div>
							<div class="td cbi-value-field" id="v6gw"><%=def6.gateway%></div>
							<div class="td cbi-value-field" id="v6metr"><%=def6.metr%></div>
						</div>

	<% end %>

				</div>
<% end %>
		</div>
	</div>
</div>
<%+footer%>