fix multiple \ns, use JSON
authorDirk Koopman <djk@tobit.co.uk>
Mon, 14 Jul 2014 11:26:33 +0000 (12:26 +0100)
committerDirk Koopman <djk@tobit.co.uk>
Mon, 14 Jul 2014 11:26:33 +0000 (12:26 +0100)
Make sure that existing timers are removed before replacement timers
are activated.

Replace Mojo::JSON with JSON(::XS) to allow sorting of keys in the output
JSON sentences

loop.pl

diff --git a/loop.pl b/loop.pl
index 7fda04181f0d2a1eaaae809e68e0175ba83d81da..a01ff5c9fbc50c7eddc4d27d525f2f8d3d859621 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -6,7 +6,8 @@ use v5.10.1;
 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;
 
@@ -15,13 +16,15 @@ my $rain_mult = 0.2;                        # 0.1 or 0.2 mm or 0.01 inches
 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;
 
@@ -109,6 +112,7 @@ sub on_read
                Mojo::IOLoop->remove($tid) if $tid;
                undef $tid;
                undef $buf;
+               $nlcount = 0;
                $s->write("LPS 1 1\n");
                chgstate("waitloop");
        } elsif ($state eq "waitloop") {
@@ -129,10 +133,20 @@ sub on_read
 
 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");
 }
 
@@ -147,10 +161,10 @@ 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();
+               chgstate('');
+               $nlcount = 0;
                Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
        }
 }
@@ -158,6 +172,10 @@ sub do_reopen
 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';
        
@@ -168,10 +186,16 @@ sub do_open
        $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;
 }
 
@@ -274,7 +298,7 @@ sub process
                        $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 = ();
@@ -286,13 +310,13 @@ sub process
                                $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';