X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=dweather;fp=dweather;h=75e2c4ea47d3ee20d55e901185303b7b0f196265;hb=3083449b22d2ee8f3cd6f78f74f5993d43d76a3f;hp=0000000000000000000000000000000000000000;hpb=febfa4909da2e98b4627186193d22ade2361fbae;p=dweather.git diff --git a/dweather b/dweather new file mode 100755 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 + + + DWeather + + +

DWeather

+ + +