add auto reopen and general tidy up
authorDirk Koopman <djk@tobit.co.uk>
Sun, 13 Jul 2014 21:50:22 +0000 (22:50 +0100)
committerDirk Koopman <djk@tobit.co.uk>
Sun, 13 Jul 2014 21:50:22 +0000 (22:50 +0100)
Add mS to debug times
Open Log files as a+ rather than r as default

Debug.pm
SMGLog.pm
loop.pl

index 5bf2b5c735f2c2708c5e3334c5c2f2cfa790582d..995e7fe65e493573a6b936f4849470e6c741d70c 100644 (file)
--- a/Debug.pm
+++ b/Debug.pm
@@ -18,9 +18,11 @@ $VERSION = 1.23;
 
 use strict;
 use vars qw(%dbglevel $fp);
+use 5.01001;
 
 use SMGLog ();
 use Carp qw(cluck);
+use Time::HiRes qw(gettimeofday);
 
 %dbglevel = ();
 $fp = undef;
@@ -55,8 +57,8 @@ dbginit();
 
 sub dbg
 {
-       my $t = time
-       my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
+       my ($t,$ut) = gettimeofday
+       my $ts = sprintf "%02d:%02d:%02d:%03d", (gmtime($t))[2,1,0], $ut/1000;
        for (@_) {
                my $r = $_;
                chomp $r;
index ddff2502bb0ac066b12aa2e1811c95d401fe7621..d52d9b3f57ec65e244a64d66cb15a4891fe585fe 100644 (file)
--- a/SMGLog.pm
+++ b/SMGLog.pm
@@ -88,7 +88,7 @@ sub open
        $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;
@@ -125,7 +125,7 @@ sub write
 {
        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");
diff --git a/loop.pl b/loop.pl
index cbdeefd8ab1912ab97a016ae14a43e3d1f5389e6..7fda04181f0d2a1eaaae809e68e0175ba83d81da 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -14,11 +14,16 @@ my $devname = "/dev/davis";
 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,
@@ -65,7 +70,9 @@ $bar_trend{0} = "Steady";
 $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) {
@@ -73,16 +80,21 @@ 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;
 
@@ -91,21 +103,23 @@ sub on_read
        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;
@@ -115,31 +129,41 @@ sub on_read
 
 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;
@@ -151,9 +175,6 @@ sub do_open
        return $str;
 }
 
-our $last_min = time;
-our $last_hour = time;
-
 my @min;
 my @hour;
 
@@ -162,7 +183,7 @@ sub process
        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;
@@ -256,6 +277,7 @@ sub process
                        $j = encode_json(\%h);
                        $s = qq|{"t":$t,"m":$j}|;
                        $last_min = $t;
+                       @min = ();
                        if ($t >= $last_hour + 3600) {
                                my $a = average(@hour);
                                
@@ -267,13 +289,17 @@ sub process
                                $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 {