show when ws not connected
[dweather.git] / loop.pl
diff --git a/loop.pl b/loop.pl
index 4046ca9b95b42dd95f00a8313a4fc588b433563b..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) {
@@ -681,21 +685,34 @@ __DATA__
 % my $url = url_for 'weather';
 <!DOCTYPE html>
 <html>
-  <head><title>DWeather</title></head>
+  <head>
+    <title>DWeather</title>
+    <meta charset="utf-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+
+    <!-- Latest compiled and minified CSS -->
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
+
+    <!-- Optional theme -->
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
+
+  </head>
   <body>
-       <h1>DWeather</h1>
+    <center><h1>High View Weather</h1></center>
+
 
     <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]);  
@@ -704,28 +721,35 @@ __DATA__
                        }
                }
        }
-
-
-    ws = new WebSocket('<%= $url->to_abs %>');
-    document.body.innerHTML += 'ws connecting to: <%= $url->to_abs %> type_of: ' + typeof(ws) + '<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">
+<div id="start-template">
 <br><br>
-<table border=1 width=80%>
+<table border=1 width=80% align="center">
 <tr>
 <th>Time:<td><span id="tm"> </span>
 <th>Sunrise:<td><span id="Sunrise"> </span>
@@ -761,5 +785,13 @@ __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) -->
+    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
+    <!-- Latest compiled and minified JavaScript -->
+    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
 </body>
 </html>