X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=loop.pl;h=1a677d52360866d28f8e247bffbf1ca7dd406e21;hb=cbf57212d7962476d2f2e7bb966283ab4fc1e5ba;hp=1fb8998e2361a4a1f8a3f43c7925648fb4c61586;hpb=636f61639c1bfc1ad597c3f2f9f025a84b0405cd;p=dweather.git diff --git a/loop.pl b/loop.pl index 1fb8998..1a677d5 100755 --- 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 @@ -108,7 +110,7 @@ websocket '/weather' => sub { # 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); @@ -128,7 +130,7 @@ websocket '/weather' => sub { $c->on(finish => sub { my ($c, $code, $reason) = @_; app->log->debug("WebSocket closed with status $code."); - dbg 'webwocket closed with status $code' if isdbg 'chan'; + dbg "websocket closed with status $code" if isdbg 'chan'; delete $WS->{$tx}; }); }; @@ -147,7 +149,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() }); @@ -157,6 +164,8 @@ dbg "after app start"; write_ld(); $dataf->close if $dataf; +undef $dataf; + # move all the files along one cycle_loop_data_files(); @@ -432,7 +441,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) { @@ -469,6 +482,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'; @@ -534,7 +549,7 @@ sub gen_hash_diff my $count; while (my ($k, $v) = each %$now) { - if ($last->{$k} ne $now->{$k}) { + if (!exists $last->{$k} || $last->{$k} ne $now->{$k}) { $o{$k} = $v; ++$count; } @@ -698,6 +713,7 @@ sub write_ld sub cycle_loop_data_files { $dataf->close if $dataf; + undef $dataf; rename "$datafn.oooo", "$datafn.ooooo"; rename "$datafn.ooo", "$datafn.oooo"; @@ -706,118 +722,23 @@ sub cycle_loop_data_files copy $datafn, "$datafn.o"; } -__DATA__ - -@@ index.html.ep -% my $url = url_for 'weather'; - - - - DWeather - - - - - - - - - - - - -

High View Weather

- - - -
-
-

- - - - - - - -
Time: - Sunrise: - Sunset: - Console Volts: - TX Battery OK: - -
Pressure: - Trend: - -
Temperature in: - Humidity: - -
Temperature out: - Min: @ - Max: @ - Humidity: - Dew Point: - -
Wind Direction: - Minute Avg: - Speed: - Minute Avg: - -
Rain 30mins: - Day: - 24hrs: - Month: - Year: -
-
-
-
-
- - - - - - + return @out; +}