add three wind dials
[dweather.git] / loop.pl
diff --git a/loop.pl b/loop.pl
index b0e430055b5cebe676ea338e091255938619502a..2768a009e92bbf74401deccf70db33d688b05da0 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -40,6 +40,8 @@ our $json = JSON->new->canonical(1);
 our $WS = {};                                  # websocket connections
 
 our $ld = {};
+our @last10minsr = ();
+our @last5daysh = ();
 
 our $loop_count;                               # how many LOOPs we have done, used as start indicator
 
@@ -109,11 +111,10 @@ websocket '/weather' => sub {
   $c->send($ld->{lasthour_h}) if exists $ld->{lasthour_h};
   $c->send($ld->{lastmin_h}) if exists $ld->{lastmin_h};
 
-  # send the last 24 hour's worth of data to the graph
-  my $lg = SMGLog->new('day');
-  my $tnow = time;
-  my $dayno = int($tnow/86400);
-  send_history($c, $lg, $tnow, $_) for ($dayno-1, $dayno);  
+  # send the 5 days worth of data to the graph
+  say "last10min = " . scalar @last10minsr . " last5day = " . scalar  @last5daysh;
+  $c->send($_) for @last10minsr;
+  $c->send($_) for @last5daysh;
   
   # disable timeout
   $c->inactivity_timeout(3615);
@@ -153,7 +154,12 @@ dbg "*** starting $0";
 dbg '***';
 
 read_ld();
-       
+
+my $tnow = time;
+my $dayno = int ($tnow/86400);
+@last5daysh = grab_history(SMGLog->new("day"), "h", $tnow-(86400*5), $_) for ($dayno-4, $dayno-3, $dayno-2, $dayno-1, $dayno);  
+@last10minsr = map {my ($t, $js) = split(/\s/, $_, 2); $js} grab_history(SMGLog->new("debug"), "r", $tnow-(60*3), $dayno);
+
 our $dlog = SMGLog->new("day");
 dbg "before next tick";
 Mojo::IOLoop->next_tick(sub { loop() });       
@@ -440,7 +446,11 @@ sub process
                        @{$ld->{wind_hour}} = ();
                        @{$ld->{wind_min}} = ();
 
-                       output_str($s, 1) if $s;
+                       if ($s) {
+                               output_str($s, 1);
+                               push @last5daysh, $s;
+                               shift @last5daysh if @last5daysh > 5*24;
+                       }
                        write_ld();
                        
                } elsif ($ts >= $ld->{last_min} + 60) {
@@ -477,6 +487,8 @@ sub process
                        my $o = gen_hash_diff($ld->{last_h}, \%h);
                        if ($o) {
                                $s = genstr($ts, 'r', $o);
+                               push @last10minsr, $s;
+                               shift @last10minsr if @last10minsr > 240;
                        }
                        else {
                                dbg "loop rec not changed" if isdbg 'chan';
@@ -498,7 +510,7 @@ sub genstr
        my $h = shift;
        
        my $j =  $json->encode($h);
-       my $tm = clocktime($ts, $let eq 'r' ? 1 : 0);
+       my $tm = clocktime($ts, 1);
        return qq|{"tm":"$tm","t":$ts,"$let":$j}|;
 }
 
@@ -715,21 +727,23 @@ sub cycle_loop_data_files
        copy $datafn, "$datafn.o";
 }
 
-sub send_history
+sub grab_history
 {
-       my $c = shift;
        my $lg = shift;
-       my $tnow = shift;
+       my $let = shift;
+       my $start = shift || time - 86400;
        my $dayno = shift;
+       my @out;
+       
        if ($lg->open($dayno, 'r+')) {
                while (my $l = $lg->read) {
-                       next unless $l =~ /,"h":/;
+                       next unless $l =~ /,"$let":/;
                        my ($t) = $l =~ /"t":(\d+)/;
-                       if ($t && $t >= $tnow-86400) {
-                               $c->send($l);
-#                              dbg "sending: $l";
+                       if ($t && $t >= $start) {
+                               push @out, $l;
                        }
                }
                $lg->close;
        }
+       return @out;
 }