Send wind even if it hasn't changed
[dweather.git] / dweather
1 #!/usr/bin/perl
2 use strict;
3
4 use v5.10.1;
5
6 use Mojolicious::Lite;
7 use Loop;
8 use Debug;
9 use SMGLog;
10
11 $SIG{TERM} = $SIG{INT} = sub {++$Loop::ending; Mojo::IOLoop->stop;};
12 $SIG{HUP} = 'IGNORE';
13
14
15 # WebSocket weather service
16 websocket '/weather' => sub {
17   my $c = shift;
18   my $msg = shift;
19   my $tx = $c->tx;
20
21   # Opened
22   app->log->debug('WebSocket opened.');
23   dbg 'WebSocket opened' if isdbg 'chan';
24   $WS->{$tx} = $tx; 
25
26   # send historical data
27   $c->send($ld->{lasthour_h}) if exists $ld->{lasthour_h};
28   $c->send($ld->{lastmin_h}) if exists $ld->{lastmin_h};
29
30   # disable timeout
31   $c->inactivity_timeout(3615);
32  
33   # Incoming message
34   $c->on(
35                  message => sub {
36                          my ($c, $msg) = @_;
37                          dbg "websocket: text $msg" if isdbg 'chan';
38                  },
39                  json => sub {
40                          my ($c, $msg) = @_;
41                          dbg "websocket: json $msg" if isdbg 'chan';
42                  }
43   );
44  
45   # Closed
46   $c->on(finish => sub {
47     my ($c, $code, $reason) = @_;
48     app->log->debug("WebSocket closed with status $code.");
49         dbg "websocket closed with status $code" if isdbg 'chan';
50         delete $WS->{$tx};
51   });
52 };
53
54 get '/' => {template => 'index'};
55
56
57 dbginit();
58 if (@ARGV) {
59         dbgadd(@ARGV);
60
61 dbgadd('chan');
62
63 dbg '***';
64 dbg "*** starting $0";
65 dbg '***';
66
67 exit 0;
68