#!/usr/bin/perl # David Brenner use strict; use DBD::mysql; use DBI(); my $file = "/home/user/gnuplot.dat"; my $dbh = DBI->connect("DBI:mysql:database=windturbine;host=localhost", "user", "password", {'RaiseError' => 1}); my $sth = $dbh->prepare("SELECT * from windturbine WHERE cur_timestamp >= NOW() - INTERVAL 1 HOUR"); $sth->execute(); open(FILE, ">$file") or die $!; my $i = 1; while (my $ref = $sth->fetchrow_hashref()){ #print "time: $ref->{'cur_timestamp'}, power: $ref->{'power'}\n"; print FILE "$ref->{'cur_timestamp'} $ref->{'power'}\n"; $i++; } close FILE; my $date = `date +'%Y-%m-%d-%H:%M:%S'`; chomp $date; open (GNUPLOT, "|gnuplot"); print GNUPLOT <prepare("SELECT * from windturbine WHERE cur_timestamp >= NOW() - INTERVAL 1 DAY"); $sth->execute(); open(FILE, ">$file") or die $!; my @times = ("00", "10", "20", "30", "40", "50"); my $i = 0; my $total = 0; my $num_data = 0; my $print_ts; my $first = 1; while (my $ref = $sth->fetchrow_hashref()){ my $timestamp = substr($ref->{'cur_timestamp'},14,2); #my $val = $times[($i+1)%5]; #print "$timestamp eq $val; ",($i+1)%5," ",$timestamp eq $val,"\n"; if( (not $first) and $timestamp eq $times[($i+1)%5]){ my $avg = $total/$num_data; $print_ts = substr($print_ts,0,15) . "0:00"; print FILE "$print_ts $avg\n"; $total = 0; $num_data = 0; $i++; } else { if($first){ $i = substr($ref->{'cur_timestamp'},14,1); #print "$ref->{'cur_timestamp'};; i = $i\n"; $first = 0; } $print_ts = $ref->{'cur_timestamp'}; $total += $ref->{'power'}; $num_data++; } } my $avg = $total/$num_data; print FILE "$print_ts $avg\n"; close FILE; chomp $date; open (GNUPLOT, "|gnuplot"); print GNUPLOT <