saner loop_data opening/closing
[dweather.git] / loop.pl
diff --git a/loop.pl b/loop.pl
index ef8a6734c64ef6ebf14418cf978e71964a86b885..4046ca9b95b42dd95f00a8313a4fc588b433563b 100755 (executable)
--- a/loop.pl
+++ b/loop.pl
@@ -14,6 +14,7 @@ use Debug;
 use SMGLog;
 use Math::Round qw(nearest);
 use File::Copy;
+use Data::Random qw(rand_chars);
 
 use constant pi => 3.14159265358979; 
 
@@ -155,12 +156,7 @@ write_ld();
 close $dataf if $dataf;
 
 # move all the files along one 
-copy "$datafn.oooo", "$datafn.ooooo";
-copy "$datafn.ooo", "$datafn.oooo";
-copy "$datafn.oo", "$datafn.ooo";
-copy "$datafn.o", "$datafn.oo";
-copy $datafn, "$datafn.o";
-
+cycle_loop_data_files();
 
 dbg '***';
 dbg "*** ending $0";
@@ -172,10 +168,6 @@ exit 0;
        
 sub loop
 {
-
-       open $dataf, "+>>", $datafn or die "cannot open $datafn $!";
-       $dataf->autoflush(1);
-       
        read_ld();
        
        dbg "last_min: " . scalar gmtime($ld->{last_min});
@@ -384,6 +376,8 @@ sub process
                if ($dayno > $ld->{last_day}) {
                        $ld->{Temp_Out_Max} = $ld->{Temp_Out_Min} = $temp;
                        $ld->{last_day} = $dayno;
+                       write_ld();
+                       cycle_loop_data_files();
                }
                $ld->{Temp_Out_Max} = $temp if $temp > $ld->{Temp_Out_Max};
                $ld->{Temp_Out_Min} = $temp if $temp < $ld->{Temp_Out_Min};
@@ -627,7 +621,10 @@ sub calc_rain
 
 sub read_ld
 {
-       return unless $dataf;
+       unless ($dataf) {
+               open $dataf, "+>>", $datafn or die "cannot open $datafn $!";
+               $dataf->autoflush(1);
+       }
 
        seek $dataf, 0, 0;
        my $s = <$dataf>;
@@ -654,7 +651,10 @@ sub read_ld
 
 sub write_ld
 {
-       return unless $dataf;
+       unless ($dataf) {
+               open $dataf, "+>>", $datafn or die "cannot open $datafn $!";
+               $dataf->autoflush(1);
+       }
 
        seek $dataf, 0, 0;
        truncate $dataf, 0;
@@ -664,6 +664,16 @@ sub write_ld
        print $dataf "$s\n";
 }
 
+sub cycle_loop_data_files
+{
+       close $dataf if $dataf;
+       
+       rename "$datafn.oooo", "$datafn.ooooo";
+       rename "$datafn.ooo", "$datafn.oooo";
+       rename "$datafn.oo", "$datafn.ooo";
+       rename "$datafn.o", "$datafn.oo";
+       copy $datafn, "$datafn.o";
+}
 
 __DATA__
  
@@ -745,7 +755,7 @@ __DATA__
 <th>Minute Avg:<td> <span id="Wind_1m">  </span>
 
 <tr>
-<th>Rain Hour:<td> <span id="Rain_1h"> </span>
+<th>Rain 30mins:<td> <span id="Rain_1h"> </span>
 <th>Day:<td> <span id="Rain_Day"> </span>
 <th>24hrs:<td> <span id="Rain_24h"> </span>
 <th>Month:<td> <span id="Rain_Month"> </span>