Send wind even if it hasn't changed
[dweather.git] / dweather
index 75e2c4ea47d3ee20d55e901185303b7b0f196265..e877cb06c758b265e0d2c12502f61be5407c4735 100755 (executable)
--- a/dweather
+++ b/dweather
@@ -8,131 +8,61 @@ use Loop;
 use Debug;
 use SMGLog;
 
-our $dlog;                                             # the day log
-our $loop;                                             # the Davis VP2 driver
+$SIG{TERM} = $SIG{INT} = sub {++$Loop::ending; Mojo::IOLoop->stop;};
+$SIG{HUP} = 'IGNORE';
 
-helper debug_start => sub {
-       dbginit();
-       if (@ARGV) {
-               dbgadd(@ARGV);
-       } 
-       dbgadd('chan');
-       
-       dbg '***';
-       dbg "*** starting $0";
-       dbg '***';
-};
-
-helper debug_stop => sub {
-       dbg '***';
-       dbg "*** ending $0";
-       dbg '***';
-};
-
-helper loop_start => sub {
-       $loop = Loop->new;
-       $loop->start;
-};
-
-helper loop_stop => sub {
-       $loop->stop;
-};
-
-helper dlog_start => sub {
-       $dlog = SMGLog->new("day");
-};
-
-
-helper dlog_stop => sub {
-       $dlog->close;
-};
-
-# setup base route
-any '/' => sub {
-       my $c = shift;
-       $c->render('index');
-}; 
 
 # WebSocket weather service
-websocket '/weather_data' => sub {
+websocket '/weather' => sub {
   my $c = shift;
+  my $msg = shift;
+  my $tx = $c->tx;
+
   # Opened
-  $c->app->log->debug('WebSocket opened.');
+  app->log->debug('WebSocket opened.');
   dbg 'WebSocket opened' if isdbg 'chan';
-   
-  # Increase inactivity timeout for connection a bit
-  $c->inactivity_timeout(60*60);
+  $WS->{$tx} = $tx; 
+
+  # send historical data
+  $c->send($ld->{lasthour_h}) if exists $ld->{lasthour_h};
+  $c->send($ld->{lastmin_h}) if exists $ld->{lastmin_h};
+
+  # disable timeout
+  $c->inactivity_timeout(3615);
  
   # Incoming message
   $c->on(
                 message => sub {
                         my ($c, $msg) = @_;
-                        dbg "websocket: $msg" if isdbg 'chan';
+                        dbg "websocket: text $msg" if isdbg 'chan';
                 },
                 json => sub {
                         my ($c, $msg) = @_;
-                        dbg "websocket: $msg" if isdbg 'chan';
+                        dbg "websocket: json $msg" if isdbg 'chan';
                 }
   );
  
   # Closed
   $c->on(finish => sub {
     my ($c, $code, $reason) = @_;
-    $c->app->log->debug("WebSocket closed with status $code.");
-       dbg 'WebSocket closed with status $code' if isdbg 'chan';
+    app->log->debug("WebSocket closed with status $code.");
+       dbg "websocket closed with status $code" if isdbg 'chan';
+       delete $WS->{$tx};
   });
-
-  $c->render;
-  
 };
 
-app->debug_start;
-app->dlog_start;
-app->loop_start;
+get '/' => {template => 'index'};
 
-app->start;
 
-app->loop_stop;
-app->dlog_stop;
-app->debug_stop;
+dbginit();
+if (@ARGV) {
+       dbgadd(@ARGV);
+} 
+dbgadd('chan');
 
-exit 0;
-
-
-__DATA__
-@@ index.html.ep
-<!DOCTYPE html>
-<html>
-  <head><title>DWeather</title></head>
-  <body>
-    <script>
-      var ws;
-      if ("WebSocket" in window) {
-        ws = new WebSocket('<%= url_for('index')->to_abs %>');
-               //ws = new WebSocket();
-      }
-      if(typeof(ws) !== 'undefined') {
-        ws.onmessage = function (event) {
-          document.body.innerHTML += JSON.parse(event.data).test;
-        };
-        ws.onopen = function (event) {
-          ws.send(JSON.stringify({weather: 'WebSocket support works! ♥'}));
-        };
-      }
-      else {
-        document.body.innerHTML += 'Browser does not support WebSockets.';
-      }
+dbg '***';
+dbg "*** starting $0";
+dbg '***';
 
-      var ws = new WebSocket('<%= url_for('weather')->to_abs %>');
-      // Incoming messages
-      ws.onmessage = function(event) {
-        document.body.innerHTML += event.data + '<br/>';
-      };
-    </script>
-       <h1>DWeather</h1>
+exit 0;
 
-  </body>
-</html>