start a real(ish) dweather webapp
[dweather.git] / dweather
diff --git a/dweather b/dweather
new file mode 100755 (executable)
index 0000000..75e2c4e
--- /dev/null
+++ b/dweather
@@ -0,0 +1,138 @@
+#!/usr/bin/perl
+use strict;
+
+use v5.10.1;
+
+use Mojolicious::Lite;
+use Loop;
+use Debug;
+use SMGLog;
+
+our $dlog;                                             # the day log
+our $loop;                                             # the Davis VP2 driver
+
+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 {
+  my $c = shift;
+  # Opened
+  $c->app->log->debug('WebSocket opened.');
+  dbg 'WebSocket opened' if isdbg 'chan';
+   
+  # Increase inactivity timeout for connection a bit
+  $c->inactivity_timeout(60*60);
+  # Incoming message
+  $c->on(
+                message => sub {
+                        my ($c, $msg) = @_;
+                        dbg "websocket: $msg" if isdbg 'chan';
+                },
+                json => sub {
+                        my ($c, $msg) = @_;
+                        dbg "websocket: $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';
+  });
+
+  $c->render;
+  
+};
+
+app->debug_start;
+app->dlog_start;
+app->loop_start;
+
+app->start;
+
+app->loop_stop;
+app->dlog_stop;
+app->debug_stop;
+
+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.';
+      }
+
+      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>
+
+  </body>
+</html>