diff --git a/new.pl b/new.pl index 6652bf2..3f9e678 100755 --- a/new.pl +++ b/new.pl @@ -26,11 +26,16 @@ sub newer($$) { } my $setlastrecord = $db->prepare("REPLACE INTO lastrecord (id, timestamp) VALUES (?, ?)") or die $db->errstr; -sub setlastrecord($$) { - my ($id, $time) = @_; +my $recordlevel = $db->prepare("INSERT INTO levels (stationid, timestamp, level) VALUES (?,?,?)") or die $db->errstr; +sub recordlevel($$$) { + my ($id, $time, $level) = @_; + $recordlevel->execute($id, $time, $level); $setlastrecord->execute($id, $time); + } +my $rememberstation = $db->prepare("INSERT INTO stations (id, name, watercourse, datum) VALUES (?,?,?,?)") or die $db->errstr; + # First thing on the line should be the current time print time, "\t" unless $ENV{R_GPLOT}; @@ -43,7 +48,9 @@ foreach my $station (@stations) { my $level; my $stationname; my $lastupdate; - if ($res->is_success) { + my $watercourse; + my $datum; + if ($res->is_success and $res->content =~ m~Current level:~) { $res->content =~ m~

([^<]+)

~; $stationname = $1; @@ -54,12 +61,24 @@ foreach my $station (@stations) { $lastupdate = "$4-$3-$2T$1"; if($ENV{R_GPLOT}) { + $res->content =~ m~Watercourse:( |\s)+([^<]+)~; + $watercourse = $2; + $watercourse =~ s~\s*$~~; + $watercourse =~ s~^\s*~~; + + $res->content =~ m~Site datum:( |\s)+([^<]+) m AOD~; + $datum = $2; + $datum =~ s~\s*$~~; + $datum =~ s~^\s*~~; + print " \"data.tsv\" using 1:$c with linespoints title \"$stationname\", \\\n"; + $rememberstation->execute($station, $stationname, $watercourse, $datum); + } else { if($level) { if(newer($station, $lastupdate)) { print $level; - setlastrecord($station, $lastupdate); + recordlevel($station, $lastupdate, $level); } else { print "-"; } @@ -68,7 +87,7 @@ foreach my $station (@stations) { } print "\t"; } - } + } else { print "-\t";} $c++; }