syntax errors removed, wip
authorDirk Koopman <djk@tobit.co.uk>
Sun, 28 Dec 2014 23:26:32 +0000 (23:26 +0000)
committerDirk Koopman <djk@tobit.co.uk>
Sun, 28 Dec 2014 23:26:32 +0000 (23:26 +0000)
Loop.pm

diff --git a/Loop.pm b/Loop.pm
index ce96d96d4c900bbd71e576fa803a5925302ac49d..e76f1ff33b3b2ce597150e087c00fa4abb660638 100755 (executable)
--- a/Loop.pm
+++ b/Loop.pm
@@ -91,13 +91,19 @@ sub new
 
 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};
 }
@@ -146,7 +152,7 @@ sub start_loop
        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';
@@ -173,11 +179,10 @@ sub do_reopen
        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});
        }
 }
 
@@ -187,7 +192,7 @@ sub do_open
        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';
@@ -200,9 +205,9 @@ sub do_open
        $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};
                                                                   });
@@ -380,7 +385,7 @@ sub output_str
        my $s = shift;
        dbg $s;
 #      say $s;
-       $dlog->writenow($s);
+       $main::dlog->writenow($s);
 }
 
 sub gen_hash_diff
@@ -515,10 +520,10 @@ sub read_ld
        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;
@@ -542,9 +547,9 @@ sub write_ld
        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;