luci-app-shadowsocks-libev: auto update instance running state 1249/head
authorYousong Zhou <yszhou4tech@gmail.com>
Mon, 24 Jul 2017 05:00:37 +0000 (13:00 +0800)
committerYousong Zhou <yszhou4tech@gmail.com>
Mon, 24 Jul 2017 06:53:36 +0000 (14:53 +0800)
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
applications/luci-app-shadowsocks-libev/luasrc/controller/shadowsocks-libev.lua
applications/luci-app-shadowsocks-libev/luasrc/model/cbi/shadowsocks-libev/instances.lua
applications/luci-app-shadowsocks-libev/luasrc/view/shadowsocks-libev/add_instance.htm

index e191e59bb13226288a478549ce09fe2829cb80e6..05d12e38b25d198eebd3cae79241bce22029c29b 100644 (file)
@@ -19,4 +19,15 @@ function index()
        entry({"admin", "services", "shadowsocks-libev", "rules"},
                cbi("shadowsocks-libev/rules"),
                _("Redir Rules"), 30).leaf = true
+
+       entry({"admin", "services", "shadowsocks-libev", "status"}, call("ss_status"), nil).leaf = true
+
+end
+
+function ss_status()
+       local ut = require "luci.util"
+       local rv = ut.ubus("service", "list", {name = "shadowsocks-libev"})["shadowsocks-libev"] or {_=0}
+
+       luci.http.prepare_content("application/json")
+       luci.http.write_json(rv)
 end
index 15e57df6bdc98565cec491248e21713124aa22b3..62a90fb416e78d3ff5077715519a37af15af06e9 100644 (file)
@@ -15,19 +15,10 @@ m = Map("shadowsocks-libev",
 
 local instances = {}
 local cfgtypes = { "ss_local", "ss_redir", "ss_server", "ss_tunnel" }
-local instances_data = ut.ubus("service", "list", {name = "shadowsocks-libev"})["shadowsocks-libev"]
-if instances_data ~= nil then
-       instances_data = instances_data["instances"]
-end
 
 for sname, sdata in pairs(m:get()) do
        local key, value = ss.cfgvalue_overview(sdata)
        if key ~= nil then
-               if instances_data and instances_data[key] and instances_data[key]["running"] then
-                       value["running"] = "yes"
-               else
-                       value["running"] = "no"
-               end
                instances[key] = value
        end
 end
index 67e1c4f72cc82eaee4024e3adfb28e2c6dd22bf7..219d89b07466121754a0385c1328cd9c617cb077 100644 (file)
                </tr>
        </table>
 </div>
+<script type="text/javascript">//<![CDATA[
+       XHR.poll(5, '<%=url('admin/services/shadowsocks-libev/status')%>', null,
+               function(x, st)
+               {
+                       var names = [
+                               <%-
+                                       for _, name in ipairs(self:cfgsections()) do
+                                               write("%q," % name)
+                                       end
+                               -%>
+                       ];
+                       var instances = st["instances"] || {};
+                       for (var i = 0, len = names.length; i < len; i++) {
+                               var name = names[i];
+                               var el = document.getElementById('cbi-table-' + name + '-running');
+                               if (el) {
+                                       var running = instances.hasOwnProperty(name)? instances[name].running : false;
+                                       el.innerText = running ? 'yes' : 'no';
+                               }
+                       }
+               }
+       );
+//]]></script>