show when ws not connected
[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 our $dlog;                                              # the day log
12 our $loop;                                              # the Davis VP2 driver
13
14 helper debug_start => sub {
15         dbginit();
16         if (@ARGV) {
17                 dbgadd(@ARGV);
18         } 
19         dbgadd('chan');
20         
21         dbg '***';
22         dbg "*** starting $0";
23         dbg '***';
24 };
25
26 helper debug_stop => sub {
27         dbg '***';
28         dbg "*** ending $0";
29         dbg '***';
30 };
31
32 helper loop_start => sub {
33         $loop = Loop->new;
34         $loop->start;
35 };
36
37 helper loop_stop => sub {
38         $loop->stop;
39 };
40
41 helper dlog_start => sub {
42         $dlog = SMGLog->new("day");
43 };
44
45
46 helper dlog_stop => sub {
47         $dlog->close;
48 };
49
50 # setup base route
51 any '/' => sub {
52         my $c = shift;
53         $c->render('index');
54 }; 
55
56 # WebSocket weather service
57 websocket '/weather_data' => sub {
58   my $c = shift;
59  
60   # Opened
61   $c->app->log->debug('WebSocket opened.');
62   dbg 'WebSocket opened' if isdbg 'chan';
63    
64   # Increase inactivity timeout for connection a bit
65   $c->inactivity_timeout(60*60);
66  
67   # Incoming message
68   $c->on(
69                  message => sub {
70                          my ($c, $msg) = @_;
71                          dbg "websocket: $msg" if isdbg 'chan';
72                  },
73                  json => sub {
74                          my ($c, $msg) = @_;
75                          dbg "websocket: $msg" if isdbg 'chan';
76                  }
77   );
78  
79   # Closed
80   $c->on(finish => sub {
81     my ($c, $code, $reason) = @_;
82     $c->app->log->debug("WebSocket closed with status $code.");
83         dbg 'WebSocket closed with status $code' if isdbg 'chan';
84   });
85
86   $c->render;
87   
88 };
89
90 app->debug_start;
91 app->dlog_start;
92 app->loop_start;
93
94 app->start;
95
96 app->loop_stop;
97 app->dlog_stop;
98 app->debug_stop;
99
100 exit 0;
101
102
103 __DATA__
104 @@ index.html.ep
105 <!DOCTYPE html>
106 <html>
107   <head><title>DWeather</title></head>
108   <body>
109     <script>
110       var ws;
111       if ("WebSocket" in window) {
112         ws = new WebSocket('<%= url_for('index')->to_abs %>');
113                 //ws = new WebSocket();
114       }
115       if(typeof(ws) !== 'undefined') {
116         ws.onmessage = function (event) {
117           document.body.innerHTML += JSON.parse(event.data).test;
118         };
119         ws.onopen = function (event) {
120           ws.send(JSON.stringify({weather: 'WebSocket support works! ♥'}));
121         };
122       }
123       else {
124         document.body.innerHTML += 'Browser does not support WebSockets.';
125       }
126
127       var ws = new WebSocket('<%= url_for('weather')->to_abs %>');
128  
129       // Incoming messages
130       ws.onmessage = function(event) {
131         document.body.innerHTML += event.data + '<br/>';
132       };
133  
134     </script>
135         <h1>DWeather</h1>
136
137   </body>
138 </html>