sub start
{
- $self->{did} = Mojo::IOLoop->recurring(1 => sub {$dlog->flushall});
- do_reopen($self->{devname});
+ my $self = shift;
+
+ $self->{did} = Mojo::IOLoop->recurring(1 => sub {$main::dlog->flushall});
+ $self->do_reopen($self->{devname});
}
sub stop
{
+ my $self = shift;
+
$self->write_ld;
+
+ $self->{ser}->close if $self->{ser};
close $self->{dataf} if $self->{dataf};
delete $self->{dataf};
}
dbg "start_loop writing $self->{nlcount} \\n" if isdbg 'state';
$self->{ser}->write("\n");
Mojo::IOLoop->remove($self->{tid}) if $self->{tid};
- undef $self->{tid};
+ delete $self->{tid};
$self->{tid} = Mojo::IOLoop->recurring(0.6 => sub {
if (++$self->{nlcount} > 10) {
dbg "\\n count > 10, closing connection" if isdbg 'chan';
dbg "do reopen on '$name' ending $ending";
unless ($ending) {
$self->{ser} = do_open($name);
- start_loop();
+ $self->start_loop;
$self->chgstate('');
$self->{nlcount} = 0;
- Mojo::IOLoop->next_tick(sub {do_reopen {
- } while (_); }) unless Mojo::IOLoop->is_running;
+ Mojo::IOLoop->next_tick(sub {$self->do_reopen});
}
}
my $name = shift;
$self->{ser}->close if $self->{ser};
- undef $self->{ser};
+ delete $self->{ser};
my $ob = Serial->new($name, 19200) || die "$name $!\n";
dbg "streaming $name fileno(" . fileno($ob) . ")" if isdbg 'chan';
$self->{ser}->start;
Mojo::IOLoop->remove($self->{tid}) if $self->{tid};
- undef $self->{tid};
+ delete $self->{tid};
Mojo::IOLoop->remove($self->{rid}) if $self->{rid};
- undef $self->{rid};
+ delete $self->{rid};
$self->{rid} = Mojo::IOLoop->recurring($self->{poll_interval} => sub {
start_loop() if !$self->{state};
});
my $s = shift;
dbg $s;
# say $s;
- $dlog->writenow($s);
+ $main::dlog->writenow($s);
}
sub gen_hash_diff
return unless $self->{dataf};
seek $self->{dataf}, 0, 0;
- my $s = <$self->{dataf}>;
+ my $s = $self->{dataf};
chomp $s;
dbg "read loop data: $s" if isdbg 'json';
- $ld = $json->decode($s) if length $s;
+ $self->{ld} = $json->decode($s) if length $s;
# sort out rain stats
my $c;
seek $self->{dataf}, 0, 0;
truncate $self->{dataf}, 0;
$self->{ld}->{ts} = time;
- my $s = $json->encode($ld);
+ my $s = $json->encode($self->{ld});
dbg "write loop data: $s" if isdbg 'json';
- print $self->{dataf} "$s\n";
+ $self->{dataf}->print("$s\n");
}
1;