my $buf;
my $dbg;
my $last_reading;
-my $s; # the serial port Mojo::IOLoop::Stream
+my $ser; # the serial port Mojo::IOLoop::Stream
our $ending = 0;
our $json = JSON->new->canonical(1);
sub on_read
{
- my ($str, $d) = @_;
+ my ($ser, $d) = @_;
$buf .= $d;
$d =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
dbg "read added '$d' buf lth=" . length $buf if isdbg 'raw';
undef $tid;
undef $buf;
$nlcount = 0;
- $s->write("LPS 1 1\n");
+ $ser->write("LPS 1 1\n");
chgstate("waitloop");
} elsif ($state eq "waitloop") {
if ($buf =~ /\x06/) {
{
$nlcount = 1;
dbg "start_loop writing $nlcount \\n" if isdbg 'state';
- $s->write("\n");
+ $ser->write("\n");
Mojo::IOLoop->remove($tid) if $tid;
undef $tid;
$tid = Mojo::IOLoop->recurring(0.6 => sub {
return;
}
dbg "writing $nlcount \\n" if isdbg 'state';
- $s->write("\n");
+ $ser->write("\n");
});
chgstate("waitnl");
}
my $name = shift;
dbg "do reopen on '$name' ending $ending";
unless ($ending) {
- $s = do_open($name);
+ $ser = do_open($name);
start_loop();
chgstate('');
$nlcount = 0;
{
my $name = shift;
- $s->close if $s;
- undef $s;
+ $ser->close if $ser;
+ undef $ser;
my $ob = Serial->new($name, 19200) || die "$name $!\n";
dbg "streaming $name fileno(" . fileno($ob) . ")" if isdbg 'chan';
- my $str = Mojo::IOLoop::Stream->new($ob);
- $str->on(error=>sub {dbg "serial $_[1]"; do_reopen($name) unless $ending});
- $str->on(close=>sub {dbg "serial closing"; do_reopen($name) unless $ending});
- $str->on(timeout=>sub {dbg "serial timeout";});
- $str->on(read=>sub {on_read(@_)});
- $str->start;
+ my $ser = Mojo::IOLoop::Stream->new($ob);
+ $ser->on(error=>sub {dbg "serial $_[1]"; do_reopen($name) unless $ending});
+ $ser->on(close=>sub {dbg "serial closing"; do_reopen($name) unless $ending});
+ $ser->on(timeout=>sub {dbg "serial timeout";});
+ $ser->on(read=>sub {on_read(@_)});
+ $ser->start;
Mojo::IOLoop->remove($tid) if $tid;
undef $tid;
chgstate('');
$nlcount = 0;
- return $str;
+ return $ser;
}
my @min;
if ($crc_calc==0) {
my $ts = time;
my $j;
- my $str;
+ my $s;
if ($ts >= $last_hour + 3600) {
$h{Pressure_Trend} = unpack("C", substr $blk,3,1);
$h{Pressure_Trend_txt} = $bar_trend{$h{Pressure_Trend}};
$last_rain_min = $last_rain_hour = $rain;
$j = $json->encode(\%h);
- $str = qq|{"t":$ts,"h":$j}|;
+ $s = qq|{"t":$ts,"h":$j}|;
$last_hour = int($ts/3600)*3600;
$last_min = int($ts/60)*60;
@hour = ();
$last_rain_min = $rain;
$j = $json->encode(\%h);
- $str = qq|{"t":$ts,"m":$j}|;
+ $s = qq|{"t":$ts,"m":$j}|;
$last_min = int($ts/60)*60;
@min = ();
} else {
my $o = gen_hash_diff($last_reading, \%h);
if ($o) {
$j = $json->encode($o);
- $str = qq|{"t":$ts,"r":$j}|;
+ $s = qq|{"t":$ts,"r":$j}|;
} else {
dbg "loop rec not changed" if isdbg 'chan';
}
}
- output_str($str) if $str;
+ output_str($s) if $s;
$last_reading = \%h;
++$loop_count;
} else {
sub output_str
{
- my $str = shift;
- dbg $str;
-# say $str;
- $dlog->writenow($str);
+ my $s = shift;
+ dbg $s;
+# say $s;
+ $dlog->writenow($s);
}
sub gen_hash_diff