X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=dweather;h=e877cb06c758b265e0d2c12502f61be5407c4735;hb=9b5d862ac17d07b815f9ce05f9adc52ba078be1c;hp=75e2c4ea47d3ee20d55e901185303b7b0f196265;hpb=3083449b22d2ee8f3cd6f78f74f5993d43d76a3f;p=dweather.git diff --git a/dweather b/dweather index 75e2c4e..e877cb0 100755 --- 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 - - - DWeather - - -

DWeather

+exit 0; - -