use Serial;
use Mojo::IOLoop;
use Mojo::IOLoop::Stream;
-use Mojo::JSON qw(decode_json encode_json);
+#use Mojo::JSON qw(decode_json encode_json);
+use JSON;
use Debug;
use SMGLog;
my $tid;
my $rid;
our $did;
-my $count;
+my $nlcount;
my $state = "ready";
my $buf;
my $dbg;
my $last_reading;
my $s; # the serial port Mojo::IOLoop::Stream
+our $json = JSON->new->canonical(1);
+
our $last_min = int(time/60)*60;
our $last_hour = int(time/3600)*3600;
Mojo::IOLoop->remove($tid) if $tid;
undef $tid;
undef $buf;
+ $nlcount = 0;
$s->write("LPS 1 1\n");
chgstate("waitloop");
} elsif ($state eq "waitloop") {
sub start_loop
{
- dbg "start_loop writing \\n" if isdbg 'chan';
+ $nlcount = 1;
+ dbg "start_loop writing $nlcount \\n" if isdbg 'chan';
$s->write("\n");
- $tid = Mojo::IOLoop->recurring(0.6 => sub {dbg "writing \\n" if isdbg 'chan'; $s->write("\n")});
-
+ Mojo::IOLoop->remove($tid) if $tid;
+ undef $tid;
+ $tid = Mojo::IOLoop->recurring(0.6 => sub {
+ if (++$nlcount > 10) {
+ dbg "\\n count > 10, closing connection" if isdbg 'chan';
+ do_reopen($devname);
+ return;
+ }
+ dbg "writing $nlcount \\n" if isdbg 'chan';
+ $s->write("\n");
+ });
chgstate("waitnl");
}
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();
+ chgstate('');
+ $nlcount = 0;
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
}
}
sub do_open
{
my $name = shift;
+
+ $s->close if $s;
+ undef $s;
+
my $ob = Serial->new($name, 19200) || die "$name $!\n";
dbg "streaming $name fileno(" . fileno($ob) . ")" if isdbg 'chan';
$str->on(read=>sub {on_read(@_)});
$str->start;
+ Mojo::IOLoop->remove($tid) if $tid;
+ undef $tid;
+ Mojo::IOLoop->remove($rid) if $rid;
+ undef $rid;
$rid = Mojo::IOLoop->recurring(2.5 => sub {
- start_loop() if !$state || $state eq "waitnl";
+ start_loop() if !$state;
});
-
+ chgstate('');
+ $nlcount = 0;
+
return $str;
}
$h{Wind_1m} = sprintf("%0.1f", $a->{w})+0;
$h{Dir_1m} = sprintf("%0.0f", $a->{d})+0;
- $j = encode_json(\%h);
+ $j = $json->encode(\%h);
$s = qq|{"t":$t,"m":$j}|;
$last_min = $t;
@min = ();
$h{Wind_1h} = sprintf("%0.1f", $a->{w})+0;
$h{Dir_1h} = sprintf("%0.0f", $a->{d})+0;
- $j = encode_json(\%h);
+ $j = $json->encode(\%h);
$s = qq|{"t":$t,"h":$j}|;
$last_hour = $t;
@hour = ();
}
} elsif ($o) {
- $j = encode_json($o);
+ $j = $json->encode($o);
$s = qq|{"t":$t,"r":$j}|;
} else {
dbg "loop rec not changed" if isdbg 'chan';