modules/freifunk: convert public status template to XHR.poll()
[project/luci.git] / modules / freifunk / luasrc / view / freifunk / public_status.htm
1 <%
2
3 local sys = require "luci.sys"
4 local twa = require "luci.tools.webadmin"
5
6 -- System
7 local system, model, memtotal, memcached, membuffers, memfree, bogomips = sys.sysinfo()
8 local uptime = twa.date_format(tonumber(sys.uptime()))
9 local_time = os.date("%a, %d %b %Y, %H:%M:%S")
10 local load1, load5, load15 = sys.loadavg()
11 local load = string.format("%.2f, %.2f, %.2f", load1, load5, load15)
12
13 local mem = string.format(
14         "%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
15         tonumber(memtotal) / 1024,
16         tonumber(memtotal - memfree) / 1024,
17         tostring(i18n.translate("used")),
18         memfree / 1024,
19         tostring(i18n.translate("free")),
20         memcached / 1024,
21         tostring(i18n.translate("cached")),
22         membuffers / 1024,
23         tostring(i18n.translate("buffered"))
24 )
25
26 -- update interval
27 local bogomips = bogomips or 100
28 local interval
29 if bogomips > 350 then
30         interval = "5000"
31 else
32         interval = "10000"
33 end
34
35 -- wireless
36 local ntm = require "luci.model.network".init()
37 local devices  = ntm:get_wifidevs()
38 local netlist = { }
39 local netdevs = { }
40 local dev
41 for _, dev in ipairs(devices) do
42         local net
43         for _, net in ipairs(dev:get_wifinets()) do
44                 netlist[#netlist+1] = net:ifname()
45                 netdevs[net:ifname()] = dev:name()
46         end
47 end
48 local has_iwinfo = pcall(require, "iwinfo")
49
50 -- Routes
51 local defroutev4 = sys.net.defaultroute()
52 local defroutev6 = sys.net.defaultroute6()
53
54 -%>
55
56 <%+header%>
57
58
59 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
60
61 <script type="text/javascript">//<![CDATA[
62         XHR.poll(<%=interval%>, '<%=luci.dispatcher.build_url("freifunk", "status", "public_status_json", table.concat(netlist, ","))%>', null,
63                 function(x, st)
64                 {
65                         if (st)
66                         {
67                                 for( var i = 0; i < st.length; i++ )
68                                 {
69                                         var iw = st[i];
70                                         var is_assoc = (iw.bssid && iw.channel);
71                                         var p = (100 / iw.quality_max * iw.quality);
72                                         var q = is_assoc ? p : -1;
73
74                                         var icon;
75                                         if (q < 0)
76                                                 icon = "<%=resource%>/icons/signal-none.png";
77                                         else if (q == 0)
78                                                 icon = "<%=resource%>/icons/signal-0.png";
79                                         else if (q < 25)
80                                                 icon = "<%=resource%>/icons/signal-0-25.png";
81                                         else if (q < 50)
82                                                 icon = "<%=resource%>/icons/signal-25-50.png";
83                                         else if (q < 75)
84                                                 icon = "<%=resource%>/icons/signal-50-75.png";
85                                         else
86                                                 icon = "<%=resource%>/icons/signal-75-100.png";
87
88                                         var power = document.getElementById(iw.id + '-txpower');
89                                         if (power)
90                                                 power.innerHTML = String.format('%s dbm', iw.txpower);
91
92                                         var signal = document.getElementById(iw.id + '-signal');
93                                         if (signal)
94                                                 signal.innerHTML = String.format(
95                                                         '<img src="%s" title="Signal: %s db / Noise: %s db" alt="Signal Quality" />',
96                                                         icon, iw.signal, iw.noise
97                                                 );
98
99                                         var bitrate = document.getElementById(iw.id + '-bitrate');
100                                         if (bitrate)
101                                                 bitrate.innerHTML = String.format('%s Mb/s', iw.bitrate ? iw.bitrate / 1000 : '?');
102
103                                         var ssid = document.getElementById(iw.id + '-ssid');
104                                         if (ssid)
105                                                 ssid.innerHTML = iw.ssid;
106
107                                         var bssid = document.getElementById(iw.id + '-bssid');
108                                         if (bssid)
109                                                 bssid.innerHTML = iw.bssid;
110
111                                         var channel = document.getElementById(iw.id + '-channel');
112                                         if (channel)
113                                                 channel.innerHTML = iw.channel;
114
115                                         var mode = document.getElementById(iw.id + '-mode');
116                                         if (mode)
117                                         mode.innerHTML = iw.mode;
118                                 }
119
120                                 i = st.length - 1
121                                 var u
122
123                                 if (u = document.getElementById('dynuptime'))
124                                         u.innerHTML = st[i].uptime;
125
126                                 if (u = document.getElementById('dynload'))
127                                         u.innerHTML = st[i].load;
128
129                                 if (u = document.getElementById('dynmem'))
130                                         u.innerHTML = st[i].mem;
131
132                                 if (u = document.getElementById('dyntime'))
133                                         u.innerHTML = st[i].time;
134
135                                 if (st[i].defroutev4)
136                                 {
137                                         if (u = document.getElementById('v4dst'))
138                                                 u.innerHTML = st[i].defroutev4.dest;
139
140                                         if (u = document.getElementById('v4gw'))
141                                                 u.innerHTML = st[i].defroutev4.gateway;
142
143                                         if (u = document.getElementById('v4dev'))
144                                                 u.innerHTML = st[i].defroutev4.dev;
145
146                                         if (u = document.getElementById('v4metr'))
147                                                 u.innerHTML = st[i].defroutev4.metr;
148                                 }
149
150                                 if (st[i].defroutev6)
151                                 {
152                                         if (u = document.getElementById('v6dst'))
153                                                 u.innerHTML = st[i].defroutev6.dest;
154
155                                         if (u = document.getElementById('v6gw'))
156                                                 u.innerHTML = st[i].defroutev6.gateway;
157
158                                         if (u = document.getElementById('v6dev'))
159                                                 u.innerHTML = st[i].defroutev6.dev;
160
161                                         if (u = document.getElementById('v6metr'))
162                                                 u.innerHTML = st[i].defroutev6.metr;
163                                 }
164                         }
165                 }
166         );
167 //]]></script>
168
169 <div class="cbi-map">
170         <h2><%:System%></h2>
171         <div class="cbi-section-node">
172                 <div class="cbi-value"><label class="cbi-value-title"><%:System%></label><div class="cbi-value-field"><%=system%></div></div>
173                 <div class="cbi-value"><label class="cbi-value-title"><%:Processor%></label><div class="cbi-value-field"><%=model%></div></div>
174                 <div class="cbi-value"><label class="cbi-value-title"><%:Load%></label><div class="cbi-value-field" id="dynload"><%=load%></div></div>
175                 <div class="cbi-value"><label class="cbi-value-title"><%:Memory%></label><div class="cbi-value-field" id="dynmem"><%=mem%></div></div>
176                 <div class="cbi-value"><label class="cbi-value-title"><%:Local Time%></label><div class="cbi-value-field" id="dyntime"><%=local_time%></div></div>
177                 <div class="cbi-value"><label class="cbi-value-title"><%:Uptime%></label><div class="cbi-value-field" id="dynuptime"><%=uptime%></div></div>
178         </div>
179 </div>
180
181 <% if devices[1] then %>
182
183 <div class="cbi-map">
184         <h2><%:Wireless Overview%></h2>
185
186                 <% if not has_iwinfo then %>
187                         <div class="errorbox">
188                                 <strong><%:Package libiwinfo required!%></strong><br />
189                                 <%_The <em>libiwinfo</em> package is not installed. You must install this component for working wireless configuration!%>
190                         </div>
191                 <% end %>
192
193                 <div class="cbi-section">
194                         <div class="cbi-section-node">
195                                 <table class="cbi-section-table">
196                                         <tr class="cbi-section-table-titles">
197                                                 <th class="cbi-section-table-cell"><%:Signal%></th>
198                                                 <th class="cbi-section-table-cell"><%:Bitrate%></th>
199                                                 <th class="cbi-section-table-cell"><%:SSID%></th>
200                                                 <th class="cbi-section-table-cell"><%:BSSID%></th>
201                                                 <th class="cbi-section-table-cell"><%:Channel%></th>
202                                                 <th class="cbi-section-table-cell"><%:Mode%></th>
203                                                 <th class="cbi-section-table-cell"><%:TX%>-<%:Power%></th>
204                                                 <th class="cbi-section-table-cell"><%:Interface%></th>
205                                         </tr>
206         <%
207         for _, dev in ipairs(devices) do
208         local net
209                 for _, net in ipairs(dev:get_wifinets()) do
210                         netlist[#netlist+1] = net:ifname()
211                         netdevs[net:ifname()] = dev:name()
212
213                         if net.iwdata.ifname then
214                                 local signal = net.iwinfo.signal or "N/A"
215                                 local noise = net.iwinfo.noise or "N/A"
216                                 local q = net.iwinfo.quality or "0"
217                                 local qmax = net.iwinfo.quality_max or "100"
218                                 local qperc = q / qmax * 100
219
220                                 if qperc == 0 then
221                                         icon = "signal-none.png"
222                                 elseif qperc < 26 then
223                                         icon = "signal-0-25.png"
224                                 elseif qperc < 51 then
225                                         icon = "signal-25-50.png"
226                                 elseif qperc < 76 then
227                                         icon = "signal-50-75.png"
228                                 elseif qperc < 100 then
229                                         icon = "signal-75-100.png"
230                                 else
231                                         icon = "signal-0.png"
232                                 end
233
234                                 signal_string = "<img src='"..resource.."/icons/"..icon.."' title='Signal: "..signal.." db / Noise: "..noise.." db' alt='Signal Quality'></img>"
235
236                                 local ssid = net.iwinfo.ssid or "N/A"
237                                 local bssid = net.iwinfo.bssid or "N/A"
238                                 local chan = net.iwinfo.channel or "N/A"
239                                 local mode = net.iwinfo.mode or "N/A"
240                                 local txpwr = net.iwinfo.txpower or "N/A"
241                                 if txpwr ~= "N/A" then
242                                         txpwr = txpwr.." dbm"
243                                 end
244                                 local bitrate = net.iwinfo.bitrate or "N/A"
245                                 if bitrate ~= "N/A" then
246                                         bitrate = ( bitrate / 1000 ).."Mb/s"
247                                 end
248                                 local interface = net.iwdata.ifname or "N/A"
249         %>
250                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
251                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-signal"><%=signal_string%></td>
252                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bitrate"><%=bitrate%></td>
253                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-ssid"><%=ssid%></td>
254                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-bssid"><%=bssid%></td>
255                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-channel"><%=chan%></td>
256                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-mode"><%=mode%></td>
257                                                 <td class="cbi-value-field" id="<%=net:ifname()%>-txpower"><%=txpwr%></td>
258                                                 <td class="cbi-value-field"><%=interface%></td>
259                                         </tr>
260                         <% end
261                 end
262         end %>
263                         </table>
264                 </div>
265         </div>
266 </div>
267 <% end %>
268
269 <div class="cbi-map">
270         <h2><%:Default routes%></h2>
271                 <div class="cbi-section">
272                         <div class="cbi-section-node">
273
274 <% if not defroutev4 and not defroutev6 then %>
275         <%:No default routes known.%>
276 <%else%>
277                                 <table class="cbi-section-table">
278                                                 <tr class="cbi-section-table-titles">
279                                                         <th class="cbi-section-table-cell"><%:Network%></th>
280                                                         <th class="cbi-section-table-cell"><%:Interface%></th>
281                                                         <th class="cbi-section-table-cell"><%:Gateway%></th>
282                                                         <th class="cbi-section-table-cell"><%:Metric%></th>
283                                                 </tr>
284
285         <% if defroutev4 then %>
286
287                                                 <tr class="cbi-section-table-row cbi-rowstyle-1">
288                                                         <td class="cbi-value-field" id="v4dst"><%=defroutev4.dest:string()%></td>
289                                                         <td class="cbi-value-field" id="v4dev"><%=defroutev4.device%></td>
290                                                         <td class="cbi-value-field" id="v4gw"><%=defroutev4.gateway:string()%></td>
291                                                         <td class="cbi-value-field" id="v4metr"><%=defroutev4.metric%></td>
292                                                 </tr>
293
294         <% end
295         if defroutev6 then %>
296
297                                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
298                                                         <td class="cbi-value-field" id="v6dst"><%=defroutev6.dest:string()%></td>
299                                                         <td class="cbi-value-field" id="v6dev"><%=defroutev6.device%></td>
300                                                         <td class="cbi-value-field" id="v6gw"><%=defroutev6.nexthop:string()%></td>
301                                                         <td class="cbi-value-field" id="v6metr"><%=defroutev6.metric%></td>
302                                                 </tr>
303
304         <% end %>
305
306                                 </table>
307 <% end %>
308                 </div>
309         </div>
310 </div>
311 <%+footer%>