From: Dirk Koopman Date: Mon, 14 Jul 2014 16:33:50 +0000 (+0100) Subject: Fix rain, move things about in hour/min recs X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=849ae984242339ff7b8baa14517a4453e285800d;p=dweather.git Fix rain, move things about in hour/min recs Add a series of rain deltas: rain, rain_1m, rain_1h (per loop, per minute, per hour). Moved the state machine debugging into isdbg 'state'. --- diff --git a/loop.pl b/loop.pl index a01ff5c..e4a1fb6 100755 --- a/loop.pl +++ b/loop.pl @@ -22,11 +22,17 @@ my $buf; my $dbg; my $last_reading; my $s; # the serial port Mojo::IOLoop::Stream +our $ending = 0; our $json = JSON->new->canonical(1); our $last_min = int(time/60)*60; -our $last_hour = int(time/3600)*3600; +our $last_hour = 0; +our $last_rain_hour; +our $last_rain_min; +our $last_rain; + +our $loop_count; # how many LOOPs we have done, used as start indicator our @crc_table = ( 0x0, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, @@ -73,9 +79,10 @@ $bar_trend{0} = "Steady"; $bar_trend{20} = "Rising Slowly"; $bar_trend{60} = "Rising Rapidly"; -my $ending; +our $ending = 0; $SIG{TERM} = $SIG{INT} = sub {++$ending; Mojo::IOLoop->stop;}; +$SIG{HUP} = 'IGNORE'; dbginit(); if (@ARGV) { @@ -108,7 +115,7 @@ sub on_read $d =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; dbg "read added '$d' buf lth=" . length $buf if isdbg 'raw'; if ($state eq 'waitnl' && $buf =~ /[\cJ\cM]+/) { - dbg "Got \\n" if isdbg 'chan'; + dbg "Got \\n" if isdbg 'state'; Mojo::IOLoop->remove($tid) if $tid; undef $tid; undef $buf; @@ -117,7 +124,7 @@ sub on_read chgstate("waitloop"); } elsif ($state eq "waitloop") { if ($buf =~ /\x06/) { - dbg "Got ACK 0x06" if isdbg 'chan'; + dbg "Got ACK 0x06" if isdbg 'state'; chgstate('waitlooprec'); undef $buf; } @@ -134,7 +141,7 @@ sub on_read sub start_loop { $nlcount = 1; - dbg "start_loop writing $nlcount \\n" if isdbg 'chan'; + dbg "start_loop writing $nlcount \\n" if isdbg 'state'; $s->write("\n"); Mojo::IOLoop->remove($tid) if $tid; undef $tid; @@ -144,7 +151,7 @@ sub start_loop do_reopen($devname); return; } - dbg "writing $nlcount \\n" if isdbg 'chan'; + dbg "writing $nlcount \\n" if isdbg 'state'; $s->write("\n"); }); chgstate("waitnl"); @@ -152,7 +159,7 @@ sub start_loop sub chgstate { - dbg "state '$state' -> '$_[0]'" if isdbg 'chan'; + dbg "state '$state' -> '$_[0]'" if isdbg 'state'; $state = $_[0]; } @@ -210,125 +217,147 @@ sub process dbg "Block invalid loo -> $loo" if isdbg 'chan'; return; } - my $t; + my $tmp; + my $rain; my %h; # Common ones - $h{Pressure_Trend} = unpack("C", substr $blk,3,1); - $h{Pressure_Trend_txt} = $bar_trend{$h{'Pressure_Trend'}}; - $t = unpack("s", substr $blk,7,2) / 1000; - $h{Pressure} = sprintf("%.0f",in2mb($t))+0; + $tmp = unpack("s", substr $blk,7,2) / 1000; + $h{Pressure} = sprintf("%.0f",in2mb($tmp))+0; - $t = unpack("s", substr $blk,9,2) / 10; - $h{Temp_In} = sprintf("%.1f", f2c($t))+0; + $tmp = unpack("s", substr $blk,9,2) / 10; + $h{Temp_In} = sprintf("%.1f", f2c($tmp))+0; - $t = unpack("s", substr $blk,12,2) / 10; - $h{Temp_Out} = sprintf("%.1f", f2c($t))+0; + $tmp = unpack("s", substr $blk,12,2) / 10; + $h{Temp_Out} = sprintf("%.1f", f2c($tmp))+0; - $t = unpack("C", substr $blk,14,1); - $h{Wind} = sprintf("%.1f",mph2mps($t))+0; + $tmp = unpack("C", substr $blk,14,1); + $h{Wind} = sprintf("%.1f",mph2mps($tmp))+0; $h{Dir} = unpack("s", substr $blk,16,2)+0; my $wind = {w => $h{Wind}, d => $h{Dir}}; push @min, $wind; - $h{'Humidity_Out'} = unpack("C", substr $blk,33,1)+0; - $h{'Humidity_In'} = unpack("C", substr $blk,11,1)+0; + $h{Humidity_Out} = unpack("C", substr $blk,33,1)+0; + $h{Humidity_In} = unpack("C", substr $blk,11,1)+0; - $t = unpack("C", substr $blk,43,1)+0; - $h{'UV'} = $t unless $t >= 255; - $t = unpack("s", substr $blk,44,2)+0; # watt/m**2 - $h{'Solar'} = $t unless $t >= 32767; + $tmp = unpack("C", substr $blk,43,1)+0; + $h{UV} = $tmp unless $tmp >= 255; + $tmp = unpack("s", substr $blk,44,2)+0; # watt/m**2 + $h{Solar} = $tmp unless $tmp >= 32767; - $h{'Rain_Rate'} = sprintf("%0.1f",unpack("s", substr $blk,41,2) * $rain_mult)+0; - $h{'Rain_Day'} = sprintf("%0.1f", unpack("s", substr $blk,50,2) * $rain_mult)+0; +# $h{Rain_Rate} = sprintf("%0.1f",unpack("s", substr $blk,41,2) * $rain_mult)+0; + $rain = $h{Rain_Day} = sprintf("%0.1f", unpack("s", substr $blk,50,2) * $rain_mult)+0; + $h{Rain} = ($rain >= $last_rain ? $rain - $last_rain : $rain) if $loop_count; + $last_rain = $rain; # what sort of packet is it? - $t = unpack("C", substr $blk,4,1); - if ($t) { + my $sort = unpack("C", substr $blk,4,1); + if ($sort) { # Newer LOOP2 packet - $t = unpack("C", substr $blk,18,2); -# $h{Wind_Avg_10} = sprintf("%.1f",mph2mps($t/10))+0; - $t = unpack("C", substr $blk,20,2); -# $h{Wind_Avg_2} = sprintf("%.1f",mph2mps($t/10))+0; - $t = unpack("C", substr $blk,22,2); -# $h{Wind_Gust_10} = sprintf("%.1f",mph2mps($t/10))+0; - + $tmp = unpack("C", substr $blk,18,2); +# $h{Wind_Avg_10} = sprintf("%.1f",mph2mps($tmp/10))+0; + $tmp = unpack("C", substr $blk,20,2); +# $h{Wind_Avg_2} = sprintf("%.1f",mph2mps($tmp/10))+0; + $tmp = unpack("C", substr $blk,22,2); +# $h{Wind_Gust_10} = sprintf("%.1f",mph2mps($tmp/10))+0; + # $h{Dir_Avg_10} = unpack("C", substr $blk,24,2)+0; - $t = unpack("C", substr $blk,30,2); - $h{Dew_Point} = sprintf("%0.1f", f2c($t))+0; + $tmp = unpack("C", substr $blk,30,2); + $h{Dew_Point} = sprintf("%0.1f", f2c($tmp))+0; } else { - + # Older LOOP packet - $t = unpack("C", substr $blk,15,1); -# $h{Wind_Avg_10} = sprintf("%.1f",mph2mps($t))+0; - $h{'Dew_Point'} = sprintf("%0.1f", dew_point($h{Temp_Out}, $h{'Humidity_Out'}))+0; - - $h{'Rain_Month'} = sprintf("%0.1f", unpack("s", substr $blk,52,2) * $rain_mult)+0; - $h{'Rain_Year'} = sprintf("%0.1f", unpack("s", substr $blk,54,2) * $rain_mult)+0; - - - $h{'Batt_TX_OK'} = (unpack("C", substr $blk,86,1)+0) ^ 1; - $h{'Batt_Console'} = sprintf("%0.2f", unpack("s", substr $blk,87,2) * 0.005859375)+0; - $h{'Forecast_Icon'} = unpack("C", substr $blk,89,1); - $h{'Forecast_Rule'} = unpack("C", substr $blk,90,1); - - $h{'Sunrise'} = sprintf( "%04d", unpack("S", substr $blk,91,2) ); - $h{'Sunrise'} =~ s/(\d{2})(\d{2})/$1:$2/; - $h{'Sunset'} = sprintf( "%04d", unpack("S", substr $blk,93,2) ); - $h{'Sunset'} =~ s/(\d{2})(\d{2})/$1:$2/; + $tmp = unpack("C", substr $blk,15,1); +# $h{Wind_Avg_10} = sprintf("%.1f",mph2mps($tmp))+0; + $h{Dew_Point} = sprintf("%0.1f", dew_point($h{Temp_Out}, $h{Humidity_Out}))+0; + $h{Rain_Month} = sprintf("%0.1f", unpack("s", substr $blk,52,2) * $rain_mult)+0; + $h{Rain_Year} = sprintf("%0.1f", unpack("s", substr $blk,54,2) * $rain_mult)+0; + } - my $crc = unpack "%n", substr($blk,97,2); my $crc_calc = CRC_CCITT($blk); if ($crc_calc==0) { - my $o = gen_hash_diff($last_reading, \%h); - $last_reading = \%h; - my $t = time; + my $ts = time; my $j; - my $s; - if ($t >= $last_min + 60) { - my $a = average(@min); - push @hour, $a; + my $str; + if ($ts >= $last_hour + 3600) { + $h{Pressure_Trend} = unpack("C", substr $blk,3,1); + $h{Pressure_Trend_txt} = $bar_trend{$h{Pressure_Trend}}; + $h{Batt_TX_OK} = (unpack("C", substr $blk,86,1)+0) ^ 1; + $h{Batt_Console} = sprintf("%0.2f", unpack("s", substr $blk,87,2) * 0.005859375)+0; + $h{Forecast_Icon} = unpack("C", substr $blk,89,1); + $h{Forecast_Rule} = unpack("C", substr $blk,90,1); + $h{Sunrise} = sprintf( "%04d", unpack("S", substr $blk,91,2) ); + $h{Sunrise} =~ s/(\d{2})(\d{2})/$1:$2/; + $h{Sunset} = sprintf( "%04d", unpack("S", substr $blk,93,2) ); + $h{Sunset} =~ s/(\d{2})(\d{2})/$1:$2/; + + if ($loop_count) { # i.e not the first + my $a = average(scalar @hour ? @hour : {w => $h{Wind}, d => $h{Dir}}); - $h{Wind_1m} = sprintf("%0.1f", $a->{w})+0; - $h{Dir_1m} = sprintf("%0.0f", $a->{d})+0; - - $j = $json->encode(\%h); - $s = qq|{"t":$t,"m":$j}|; - $last_min = $t; - @min = (); - if ($t >= $last_hour + 3600) { - my $a = average(@hour); - - delete $h{Wind_1m}; - delete $h{Dir_1m}; $h{Wind_1h} = sprintf("%0.1f", $a->{w})+0; $h{Dir_1h} = sprintf("%0.0f", $a->{d})+0; + $h{Rain_1h} = $rain >= $last_rain_hour ? $rain - $last_rain_hour : $rain; + + $h{Wind_1m} = sprintf("%0.1f", $a->{w})+0; + $h{Dir_1m} = sprintf("%0.0f", $a->{d})+0; + $h{Rain_1m} = $rain >= $last_rain_min ? $rain - $last_rain_min : $rain; + } + $last_rain_min = $last_rain_hour = $rain; + + $j = $json->encode(\%h); + $str = qq|{"t":$ts,"h":$j}|; + $last_hour = int($ts/3600)*3600; + $last_min = int($ts/60)*60; + @hour = (); + @min = (); + } elsif ($ts >= $last_min + 60) { + my $a = average(@min); + my %save; + + push @hour, $a; - $j = $json->encode(\%h); - $s = qq|{"t":$t,"h":$j}|; - $last_hour = $t; - @hour = (); + if ($loop_count) { # i.e not the first + $h{Wind_1m} = sprintf("%0.1f", $a->{w})+0; + $h{Dir_1m} = sprintf("%0.0f", $a->{d})+0; + $h{Rain_1h} = $rain >= $last_rain_hour ? $rain - $last_rain_hour : $rain; # this is the rate for this hour, so far + $h{Rain_1m} = $rain >= $last_rain_min ? $rain - $last_rain_min : $rain; } - } elsif ($o) { - $j = $json->encode($o); - $s = qq|{"t":$t,"r":$j}|; + $last_rain_min = $rain; + + $j = $json->encode(\%h); + $str = qq|{"t":$ts,"m":$j}|; + $last_min = int($ts/60)*60; + @min = (); } else { - dbg "loop rec not changed" if isdbg 'chan'; - } - if ($s) { - dbg $s; - say $s; - $dlog->writenow($s); + my $o = gen_hash_diff($last_reading, \%h); + if ($o) { + $j = $json->encode($o); + $str = qq|{"t":$ts,"r":$j}|; + } else { + dbg "loop rec not changed" if isdbg 'chan'; + } } + output_str($str) if $str; + $last_reading = \%h; + ++$loop_count; } else { dbg "CRC check failed for LOOP data!"; } + return; +} + +sub output_str +{ + my $str = shift; + dbg $str; +# say $str; + $dlog->writenow($str); } sub gen_hash_diff