modules/freifunk: convert public status template to XHR.poll()
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 2 Oct 2011 09:59:07 +0000 (09:59 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 2 Oct 2011 09:59:07 +0000 (09:59 +0000)
modules/freifunk/luasrc/view/freifunk/public_status.htm

index 3cb57082e27297db59f138ade7ddddee6be9084b..633613e552e73dbe5ecdb2eb9bbeaf3aaef7c534 100644 (file)
@@ -59,119 +59,111 @@ local defroutev6 = sys.net.defaultroute6()
 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
 
 <script type="text/javascript">//<![CDATA[
-       var iwxhr = new XHR();
-
-       var update_wifi_status = function() {
-               iwxhr.get('<%=luci.dispatcher.build_url("freifunk", "status", "public_status_json", table.concat(netlist, ","))%>', null,
-                       function(x, st)
+       XHR.poll(<%=interval%>, '<%=luci.dispatcher.build_url("freifunk", "status", "public_status_json", table.concat(netlist, ","))%>', null,
+               function(x, st)
+               {
+                       if (st)
                        {
-                               if (st)
+                               for( var i = 0; i < st.length; i++ )
                                {
-                                       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;
-                                       }
+                                       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;
                                }
 
-                               window.setTimeout(update_wifi_status, <%=interval%>);
-                       }
-               )
-       };
+                               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;
 
-       update_wifi_status();
+                                       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">
@@ -190,7 +182,7 @@ local defroutev6 = sys.net.defaultroute6()
 
 <div class="cbi-map">
        <h2><%:Wireless Overview%></h2>
-       
+
                <% if not has_iwinfo then %>
                        <div class="errorbox">
                                <strong><%:Package libiwinfo required!%></strong><br />
@@ -211,7 +203,7 @@ local defroutev6 = sys.net.defaultroute6()
                                                <th class="cbi-section-table-cell"><%:TX%>-<%:Power%></th>
                                                <th class="cbi-section-table-cell"><%:Interface%></th>
                                        </tr>
-       <% 
+       <%
        for _, dev in ipairs(devices) do
        local net
                for _, net in ipairs(dev:get_wifinets()) do