show when ws not connected
authorDirk Koopman <djk@tobit.co.uk>
Sun, 25 Jan 2015 22:02:30 +0000 (22:02 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Sun, 25 Jan 2015 22:02:30 +0000 (22:02 +0000)
loop.pl

diff --git a/loop.pl b/loop.pl
index bb9582e92da6a48d2a7a5882d851eebe8703e294..2e9549120f84faee71707dd079360d97d9bd9e59 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -145,6 +145,8 @@ dbg '***';
 dbg "*** starting $0";
 dbg '***';
 
+read_ld();
+       
 our $dlog = SMGLog->new("day");
 dbg "before next tick";
 Mojo::IOLoop->next_tick(sub { loop() });       
@@ -168,8 +170,6 @@ exit 0;
        
 sub loop
 {
-       read_ld();
-       
        dbg "last_min: " . scalar gmtime($ld->{last_min});
        dbg "last_hour: " . scalar gmtime($ld->{last_hour});
        
@@ -419,6 +419,7 @@ sub process
                        @{$ld->{wind_hour}} = ();
                        @{$ld->{wind_min}} = ();
 
+                       output_str($s, 1) if $s;
                        write_ld();
                        
                } elsif ($ts >= $ld->{last_min} + 60) {
@@ -446,6 +447,7 @@ sub process
                        $ld->{last_min} = int($ts/60)*60;
                        @{$ld->{wind_min}} = ();
                        
+                       output_str($s, 1) if $s;
                        write_ld();
 
                } else {
@@ -456,8 +458,8 @@ sub process
                        else {
                                dbg "loop rec not changed" if isdbg 'chan';
                        }
+                       output_str($s, 0) if $s;
                }
-               output_str($s) if $s;
                $ld->{last_h} = \%h;
                ++$loop_count;
        } else {
@@ -482,9 +484,11 @@ sub genstr
 sub output_str
 {
        my $s = shift;
+       my $logit = shift;
+       
        dbg $s;
 #      say $s;
-       $dlog->writenow($s);
+       $dlog->writenow($s) if $logit;
        foreach my $ws (keys $WS) {
                my $tx = $WS->{$ws};
                if ($tx) {
@@ -701,14 +705,14 @@ __DATA__
     <script>
        var ws;
 
-       function process(key,value) {
+    function process(key,value) {
                var d = document.getElementById(key);
                if (d !== null) {
                        d.innerHTML = value;
                }
        }
-
-    function traverse(o) {
+               
+       function traverse(o) {
                console.log(o);
                for (var i in o) {
                        process(i, o[i]);  
@@ -717,24 +721,29 @@ __DATA__
                        }
                }
        }
-
-
-    ws = new WebSocket('<%= $url->to_abs %>');
-    document.body.innerHTML += '<center>ws connecting to: <%= $url->to_abs %> type_of: ' + typeof(ws) + '</center><br>';
-    if (typeof(ws) === 'object') {
-               ws.onmessage = function (event) {
-                       var js = JSON.parse(event.data);
-                       if (js !== null && typeof(js) === 'object') {
-                               traverse(js);
+               
+       window.onload = function() {
+
+               ws = new WebSocket('<%= $url->to_abs %>');
+
+               if (typeof(ws) === 'object') {
+                       ws.onmessage = function (event) {
+                               var js = JSON.parse(event.data);
+                               if (js !== null && typeof(js) === 'object') {
+                                       traverse(js);
+                               }
+                       };
+                       ws.onopen = function (event) {
+                               document.getElementById("wsconnect").innerHTML = 'ws connected to: <%= $url->to_abs %>';
+                               ws.send('WebSocket support works! ♥');
+                       };
+                       ws.onclose = function(event) {
+                               document.getElementById("wsconnect").innerHTML = 'ws disconnected, refresh to restart';
                        }
-               };
-               ws.onopen = function (event) {
-                       ws.send('WebSocket support works! ♥');
-               };
-       } else {
-               document.body.innerHTML += 'Webserver only works with Websocket aware browsers';
-       }
+               } else {
+                       document.body.innerHTML += 'Webserver only works with Websocket aware browsers';
+               }
+       } 
     </script>
 
 <div id="container">
@@ -776,6 +785,8 @@ __DATA__
 <th>Month:<td> <span id="Rain_Month"> </span>
 <th>Year:<td> <span id="Rain_Year"> </span>
 </table>
+<br>
+<div id="wsconnect" align="center"> </div>
 </div>
 </div>
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->