$self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
$self->{fn} .= ".$self->{suffix}" if $self->{suffix};
- $self->{mode} = $mode || 'r';
+ $self->{mode} = $mode || 'a+';
my $fh = new IO::File $self->{fn}, $mode, 0666;
return unless $fh;
{
my ($self, $dayno, $line) = @_;
if (!$self->{fh} || $self->{mode} ne "r" || $dayno != $self->{dayno}) {
- $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
+ $self->open($dayno, "a+") or confess "can't open $self->{fn} $!";
}
return $self->{fh}->print("$line\n");
my $rain_mult = 0.2; # 0.1 or 0.2 mm or 0.01 inches
my $tid;
my $rid;
+our $did;
my $count;
my $state = "ready";
my $buf;
my $dbg;
my $last_reading;
+my $s; # the serial port Mojo::IOLoop::Stream
+
+our $last_min = int(time/60)*60;
+our $last_hour = int(time/3600)*3600;
our @crc_table = (
0x0, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
$bar_trend{20} = "Rising Slowly";
$bar_trend{60} = "Rising Rapidly";
-#$SIG{TERM} = $SIG{INT} = sub {Mojo::IOLoop->stop if Mojo::IOLoop->is_running && !$DB::VERSION};
+my $ending;
+
+$SIG{TERM} = $SIG{INT} = sub {++$ending; Mojo::IOLoop->stop;};
dbginit();
if (@ARGV) {
}
dbgadd('chan');
+dbg '***';
+dbg "*** starting $0";
+dbg '***';
-my $dlog = SMGLog->new("day");
-$dlog->mode('a');
+dbg scalar gmtime($last_min);
+dbg scalar gmtime($last_hour);
-my $s = do_open($devname);
-start_loop();
+my $dlog = SMGLog->new("day");
+$did = Mojo::IOLoop->recurring(1 => sub {$dlog->flushall});
-Mojo::IOLoop->recurring(0.1 => sub { $dlog->flushall });
+do_reopen($devname);
-Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
+dbg '***';
+dbg "*** ending $0";
+dbg '***';
exit 0;
my ($str, $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');
+ dbg "read added '$d' buf lth=" . length $buf if isdbg 'raw';
if ($state eq 'waitnl' && $buf =~ /[\cJ\cM]+/) {
- Mojo::IOLoop->remove($tid);
+ dbg "Got \\n" if isdbg 'chan';
+ Mojo::IOLoop->remove($tid) if $tid;
+ undef $tid;
undef $buf;
$s->write("LPS 1 1\n");
chgstate("waitloop");
} elsif ($state eq "waitloop") {
if ($buf =~ /\x06/) {
+ dbg "Got ACK 0x06" if isdbg 'chan';
chgstate('waitlooprec');
undef $buf;
}
} elsif ($state eq 'waitlooprec') {
if (length $buf >= 99) {
- dbg "got loop record\n" if isdbg('chan');
-
+ dbg "got loop record" if isdbg 'chan';
chgstate('');
process($buf);
undef $buf;
sub start_loop
{
- dbg "writing \\n" if isdbg('chan');
-
+ dbg "start_loop writing \\n" if isdbg 'chan';
$s->write("\n");
- $tid = Mojo::IOLoop->recurring(0.6 => sub {dbg "writing \\n" if isdbg('chan'); $s->write("\n")});
+ $tid = Mojo::IOLoop->recurring(0.6 => sub {dbg "writing \\n" if isdbg 'chan'; $s->write("\n")});
chgstate("waitnl");
}
sub chgstate
{
- dbg "state '$state' -> '$_[0]'" if isdbg('chan');
-
+ dbg "state '$state' -> '$_[0]'" if isdbg 'chan';
$state = $_[0];
}
+sub do_reopen
+{
+ my $name = shift;
+ dbg "do reopen on '$name' ending $ending";
+ unless ($ending) {
+ $s->close if defined $s;
+ Mojo::IOLoop->reset;
+ $s = do_open($name);
+ start_loop();
+ Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
+ }
+}
+
sub do_open
{
my $name = shift;
my $ob = Serial->new($name, 19200) || die "$name $!\n";
- dbg "streaming $name fileno(", fileno($ob), ")" if isdbg('chan');
+ dbg "streaming $name fileno(" . fileno($ob) . ")" if isdbg 'chan';
my $str = Mojo::IOLoop::Stream->new($ob);
- $str->on(error=>sub {dbg "serial $_[1]"; undef $s; Mojo::IOLoop->reset;});
-
- $str->on(close=>sub {dbg "serial closing"; undef $s; Mojo::IOLoop->reset;});
+ $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;
return $str;
}
-our $last_min = time;
-our $last_hour = time;
-
my @min;
my @hour;
my $blk = shift;
my $loo = substr $blk,0,3;
unless ( $loo eq 'LOO') {
- dbg "Block invalid loo -> $loo" if isdbg('chan'); return;
+ dbg "Block invalid loo -> $loo" if isdbg 'chan'; return;
}
my $t;
$j = encode_json(\%h);
$s = qq|{"t":$t,"m":$j}|;
$last_min = $t;
+ @min = ();
if ($t >= $last_hour + 3600) {
my $a = average(@hour);
$j = encode_json(\%h);
$s = qq|{"t":$t,"h":$j}|;
$last_hour = $t;
+ @hour = ();
}
} elsif ($o) {
$j = encode_json($o);
$s = qq|{"t":$t,"r":$j}|;
+ } else {
+ dbg "loop rec not changed" if isdbg 'chan';
}
if ($s) {
dbg $s;
+ say $s;
$dlog->writenow($s);
}
} else {