start to add state ready for graphs
authorDirk Koopman <djk@tobit.co.uk>
Tue, 27 Jan 2015 22:28:19 +0000 (22:28 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Tue, 27 Jan 2015 22:28:19 +0000 (22:28 +0000)
loop.pl

diff --git a/loop.pl b/loop.pl
index bb28f1843e7372b2164aa90c7427ab2ae32b3b2f..bc9bf7f6718d97343342a92a4fb4b6b5a69296a9 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -733,33 +733,40 @@ __DATA__
 
     <script>
        var ws;
+    var h = new Object(); 
 
-    function process(key,value) {
+    function update_h(key, value) {
+               h[key] = value;
+       }
+
+    function fill_html(key,value) {
                var d = document.getElementById(key);
                if (d !== null) {
                        d.innerHTML = value;
                }
        }
                
-       function traverse(o) {
+       function traverse(o, func) {
                console.log(o);
                for (var i in o) {
-                       process(i, o[i]);  
                        if (o[i] !== null && typeof(o[i])=="object") {
-                               traverse(o[i]);
+                               traverse(o[i], func);
+                       } else {
+                               func(i, o[i]);  
                        }
                }
        }
-               
-       window.onload = function() {
 
+    function startws() {
                ws = new WebSocket('<%= $url->to_abs %>');
 
-               if (typeof(ws) === 'object') {
+               if (typeof(ws) !== null) {
                        ws.onmessage = function (event) {
                                var js = JSON.parse(event.data);
                                if (js !== null && typeof(js) === 'object') {
-                                       traverse(js);
+                                       traverse(js, fill_html);
+                                       traverse(js, update_h);
+                                       document.getElementById("hh").innerHTML = JSON.stringify(h);
                                }
                        };
                        ws.onopen = function (event) {
@@ -768,10 +775,19 @@ __DATA__
                        };
                        ws.onclose = function(event) {
                                document.getElementById("wsconnect").innerHTML = 'ws disconnected, refresh to restart';
+                               ws = null;
                        }
                } else {
                        document.body.innerHTML += 'Webserver only works with Websocket aware browsers';
                }
+    }
+
+       window.onload = function() {
+               startws();
+               window.setInterval(function() { 
+                       if (ws === null) 
+                               startws();
+               }, 15000);
        } 
     </script>
 
@@ -816,6 +832,8 @@ __DATA__
        </table>
        <br>
        <div id="wsconnect" align="center"> </div>
+       <br>
+       <div id="hh" align="center"> </div>
        </div>
        </div>
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->