X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=loop.pl;h=9420e2b95c95876b17a2b8a3043674d00b608d3a;hb=3e913f5e4a04510bc7144639da5fa97b20d335e2;hp=ef0c4e031a5c37150046cd13981e8047b2b5c265;hpb=34fb2f9bb6432b6dffe2f79fe6c4e31801565aad;p=dweather.git diff --git a/loop.pl b/loop.pl index ef0c4e0..9420e2b 100755 --- a/loop.pl +++ b/loop.pl @@ -7,13 +7,17 @@ use DBI; use Serial; use Mojo::IOLoop; use Mojo::IOLoop::Stream; +use Mojo::JSON qw(decode_json encode_json); my $devname = "/dev/davis"; +my $rain_mult = 0.1; # 0.1 or 0.2 mm or 0.01 inches my $tid; my $rid; my $count; my $state = "ready"; my $buf; +my $dbg; +my $last_reading; our @crc_table = ( 0x0, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, @@ -60,13 +64,15 @@ $bar_trend{0} = "Steady"; $bar_trend{20} = "Rising Slowly"; $bar_trend{60} = "Rising Rapidly"; -#$SIG{TERM} = $SIG{INT} = sub {Mojo::IOLoop->reset if Mojo::IOLoop->is_running && !$DB::VERSION}; +#$SIG{TERM} = $SIG{INT} = sub {Mojo::IOLoop->stop if Mojo::IOLoop->is_running && !$DB::VERSION}; my $s = do_open($devname); start_loop(); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; +$s->close; + exit 0; sub on_read @@ -74,7 +80,7 @@ sub on_read my ($str, $d) = @_; $buf .= $d; $d =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; -# say "read added '$d' buf lth=" . length $buf; +# say "read added '$d' buf lth=" . length $buf if $dbg; if ($state eq 'waitnl' && $buf =~ /[\cJ\cM]+/) { undef $tid; undef $buf; @@ -87,7 +93,8 @@ sub on_read } } elsif ($state eq 'waitlooprec') { if (length $buf >= 99) { - say "got loop record\n"; + say "got loop record\n" if $dbg; + chgstate(''); process($buf); undef $buf; @@ -97,15 +104,18 @@ sub on_read sub start_loop { - say "writing \\n"; + say "writing \\n" if $dbg; + $s->write("\n"); - $tid = Mojo::IOLoop->timer(0.6 => sub {say "writing \\n"; $s->write("\n")}); + $tid = Mojo::IOLoop->timer(0.6 => sub {say "writing \\n" if $dbg; $s->write("\n")}); + chgstate("waitnl"); } sub chgstate { - say "state '$state' -> '$_[0]'"; + say "state '$state' -> '$_[0]'" if $dbg; + $state = $_[0]; } @@ -113,9 +123,11 @@ sub do_open { my $name = shift; my $ob = Serial->new($name, 19200) || die "$name $!\n"; - say "streaming $name fileno(", fileno($ob), ")"; + say "streaming $name fileno(", fileno($ob), ")" if $dbg; + my $str = Mojo::IOLoop::Stream->new($ob); $str->on(error=>sub {say "serial $_[1]"; undef $s; Mojo::IOLoop->reset;}); + $str->on(close=>sub {say "serial closing"; undef $s; Mojo::IOLoop->reset;}); $str->on(timeout=>sub {say "serial timeout";}); $str->on(read=>sub {on_read(@_)}); @@ -131,68 +143,63 @@ sub process my $blk = shift; my $loo = substr $blk,0,3; unless ( $loo eq 'LOO') { - say "Block invalid loo -> $loo"; return; + say "Block invalid loo -> $loo" if $dbg; return; } my $t; - my %hsh; + my %h; - #$hsh{'next_rec'} = unpack("s", substr $blk,5,2); + #$h{'next_rec'} = unpack("s", substr $blk,5,2); - $hsh{'Barometric_Trend'} = unpack("C", substr $blk,3,1); - $hsh{'Barometric_Trend_txt'} = $bar_trend{$hsh{'Barometric_Trend'}}; + $h{'Barometric_Trend'} = unpack("C", substr $blk,3,1); + $h{'Barometric_Trend_txt'} = $bar_trend{$h{'Barometric_Trend'}}; $t = unpack("s", substr $blk,7,2) / 1000; - # $hsh{'Barometric_Press_hg'} = $t; - $hsh{'Barometric_Press_mb'} = sprintf("%.2f",$t*33.8637526); + $h{'Barometric_Press_mb'} = sprintf("%.0f",$t*33.8637526); $t = unpack("s", substr $blk,9,2) / 10; - # $hsh{'Air_Temp_Inside_f'} = $t; - $hsh{'Air_Temp_Inside_c'} = sprintf("%.1f",($t - 32) * 5/9); + $h{'Air_Temp_Inside_c'} = sprintf("%.1f",($t - 32) * 5/9); my $tf = unpack("s", substr $blk,12,2) / 10; - # $hsh{'Air_Temp_Outside_f'} = $tf; - $hsh{'Air_Temp_Outside_c'} = sprintf("%.1f",($tf - 32) * 5/9); + $h{'Air_Temp_Outside_c'} = sprintf("%.1f",($tf - 32) * 5/9); - $hsh{'Wind_Speed_mph'} = unpack("C", substr $blk,14,1); - # $hsh{'Wind_Speed_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_mph'}*0.44704); - $hsh{'Wind_Speed_10min_Avg_mph'} = unpack("C", substr $blk,15,1); - # $hsh{'Wind_Speed_10min_Avg_mps'} = sprintf("%.1f",$hsh{'Wind_Speed_10min_Avg_mph'}*0.44704); - $hsh{'Wind_Dir'} = unpack("s", substr $blk,16,2); + $h{'Wind_Speed_mph'} = unpack("C", substr $blk,14,1); + $h{'Wind_Speed_mps'} = sprintf("%.1f",$h{'Wind_Speed_mph'}*0.44704); + $h{'Wind_Speed_10min_Avg_mph'} = unpack("C", substr $blk,15,1); + $h{'Wind_Speed_10min_Avg_mps'} = sprintf("%.1f",$h{'Wind_Speed_10min_Avg_mph'}*0.44704); + $h{'Wind_Dir'} = unpack("s", substr $blk,16,2); - $hsh{'Humidity_Outside'} = unpack("C", substr $blk,33,1); - $hsh{'Humidity_Inside'} = unpack("C", substr $blk,11,1); - $hsh{'Dew_Point'} = dew_point($tf, $hsh{'Humidity_Outside'}); + $h{'Humidity_Outside'} = unpack("C", substr $blk,33,1); + $h{'Humidity_Inside'} = unpack("C", substr $blk,11,1); + $h{'Dew_Point'} = dew_point($h{Air_Temp_Outside_c}, $h{'Humidity_Outside'}); - # $hsh{'UV'} = unpack("C", substr $blk,43,1); - # $hsh{'Solar'} = unpack("s", substr $blk,44,2); # watt/m**2 + $h{'UV'} = unpack("C", substr $blk,43,1); + $h{'Solar'} = unpack("s", substr $blk,44,2); # watt/m**2 - $hsh{'Rain_Rate'} = (unpack("s", substr $blk,41,2) / 100) * 25.4; # Inches per hr converted to mm - $hsh{'Rain_Storm'} = (unpack("s", substr $blk,46,2) / 100) * 25.4; # Inches per storm - #$hsh{'Storm_Date'} = unpack("s", substr $blk,48,2); # Need to parse data (not sure what this is) - $hsh{'Rain_Day'} = (unpack("s", substr $blk,50,2)/100) * 25.4; - $hsh{'Rain_Month'} = (unpack("s", substr $blk,52,2)/100) * 25.4; - $hsh{'Rain_Year'} = (unpack("s", substr $blk,54,2)/100) * 25.4; + $h{'Rain_Rate'} = unpack("s", substr $blk,41,2) * $rain_mult; + $h{'Rain_Day'} = unpack("s", substr $blk,50,2) * $rain_mult; + $h{'Rain_Month'} = unpack("s", substr $blk,52,2) * $rain_mult; + $h{'Rain_Year'} = unpack("s", substr $blk,54,2) * $rain_mult; - $hsh{'ET_Day'} = unpack("s", substr $blk,56,2)/1000; - $hsh{'ET_Month'} = unpack("s", substr $blk,58,2)/100; - $hsh{'ET_Year'} = unpack("s", substr $blk,60,2)/100; + $h{'ET_Day'} = unpack("s", substr $blk,56,2)/1000; + $h{'ET_Month'} = unpack("s", substr $blk,58,2)/100; + $h{'ET_Year'} = unpack("s", substr $blk,60,2)/100; - #$hsh{'Alarms_Inside'} = unpack("b8", substr $blk,70,1); - #$hsh{'Alarms_Rain'} = unpack("b8", substr $blk,70,1); - #$hsh{'Alarms_Outside'} = unpack("b8", substr $blk,70,1); + #$h{'Alarms_Inside'} = unpack("b8", substr $blk,70,1); + #$h{'Alarms_Rain'} = unpack("b8", substr $blk,70,1); + #$h{'Alarms_Outside'} = unpack("b8", substr $blk,70,1); - $hsh{'Batt_Transmitter'} = unpack("C", substr $blk,86,1); # * 0.005859375 - $hsh{'Batt_Console'} = unpack("s", substr $blk,87,2) * 0.005859375; + $h{'Batt_TX_OK'} = (unpack("C", substr $blk,86,1)+0) ^ 1; + $h{'Batt_Console'} = unpack("s", substr $blk,87,2) * 0.005859375; - $hsh{'Forecast_Icon'} = unpack("C", substr $blk,89,1); - $hsh{'Forecast_Rule'} = unpack("C", substr $blk,90,1); + $h{'Forecast_Icon'} = unpack("C", substr $blk,89,1); + $h{'Forecast_Rule'} = unpack("C", substr $blk,90,1); - $hsh{'Sunrise'} = sprintf( "%04d", unpack("S", substr $blk,91,2) ); - $hsh{'Sunrise'} =~ s/(\d{2})(\d{2})/$1:$2/; - $hsh{'Sunset'} = sprintf( "%04d", unpack("S", substr $blk,93,2) ); - $hsh{'Sunset'} =~ s/(\d{2})(\d{2})/$1:$2/; + $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/; #my $nl = ord substr $blk,95,1; #my $cr = ord substr $blk,96,1; @@ -201,16 +208,39 @@ sub process my $crc_calc = CRC_CCITT($blk); if ($crc_calc==0) { - say "inside: $hsh{Air_Temp_Inside_c} degC $hsh{Humidity_Inside}\% outside: $hsh{Air_Temp_Outside_c} degC $hsh{Humidity_Outside}\% wind: $hsh{Wind_Speed_mph} $hsh{Wind_Dir} deg $hsh{Barometric_Press_mb} mB"; - + my $o = gen_hash_diff($last_reading, \%h); + $last_reading = \%h; + if (time % 60 == 0) { + my $oo = {t => time, r =>\%h}; + say encode_json($oo); + } elsif ($o) { + my $oo = {t => time, r =>$o}; + say encode_json($oo); + } } else { - print "CRC check failed for LOOP data!\n"; + say "CRC check failed for LOOP data!"; return 1; } - #delete @hsh{'crc', 'crc_calc', 'next_rec'}; - #delete($hsh{crc})||die"cant delete crc"; - #delete($hsh{crc_calc})||die"cant delete crc_calc"; - #delete($hsh{next_rec})||die"cant delete next_rec"; + #delete @h{'crc', 'crc_calc', 'next_rec'}; + #delete($h{crc})||die"cant delete crc"; + #delete($h{crc_calc})||die"cant delete crc_calc"; + #delete($h{next_rec})||die"cant delete next_rec"; +} + +sub gen_hash_diff +{ + my $last = shift; + my $now = shift; + my %o; + my $count; + + while (my ($k, $v) = each %$now) { + if ($last->{$k} ne $now->{$k}) { + $o{$k} = $v; + ++$count; + } + } + return $count ? \%o : undef; } sub dew_point