* * This code was created as a test for the procedure to extract * date based data from a database and display it as a line graph * and to get some experiance in php programming * * * 2013-06-25 Peter Sjoberg * New defaults for bigger image, pass rounding * 2013-07-01 Peter Sjoberg * merged powerplot with drawimage = one file for all of it * 2013-07-10 Peter Sjoberg * Moved out db account info, made a pre alpha version accessible on internet * 2019-05-09 Peter Sjoberg * Changed "mysql_*" to "mysqli_*" * 2021-02-28 Peter Sjoberg * changed sql part to only use the channel for data * * */ date_default_timezone_set('America/Toronto'); //ini_set('max_execution_time', 30); //die after 30 seconds set_time_limit (30); $debug=0; $debug = isset($_GET["debug"]) ? $_GET["debug"] : 0; $debug_file="/usr/local/src/peters/shared/plotpower/web/DEBUG_plotpower.log"; /* if ( $debug == 0){ #print ("
\n");
  print ("

Temporarily out of service

"); #print("\n
"); exit; } */ /* includ some db account info */ /* ../db_account_info.inc.php contains something like $dbhost="localhost"; $dbuser="thedbuser"; $dbpsw="thepassword"; $dbname="powermon"; */ include "../db_account_info.inc.php"; if ( $debug==1 ) { system ("echo start >$debug_file"); system ("echo >>$debug_file"); system ("echo KEYstart >>$debug_file"); foreach($_GET AS $key => $value) { system ("echo $key=$value >>$debug_file"); ${$key} = $value; } system ("echo KEYend >>$debug_file"); } /* foreach ($_GET as $key => $value) echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."
"; */ //TODO: make this POST instead of GET // but then it can't be bookmarked $firstTime = isset($_GET["firstTime"]) ? $_GET["firstTime"] : ""; $firstYearIn = isset($_GET["firstYearIn"]) ? $_GET["firstYearIn"] : ""; $firstMonthIn = isset($_GET["firstMonthIn"]) ? $_GET["firstMonthIn"] : ""; $firstDayIn = isset($_GET["firstDayIn"]) ? $_GET["firstDayIn"] : ""; $firstHourIn = isset($_GET["firstHourIn"]) ? $_GET["firstHourIn"] : ""; $firstMinuteIn = isset($_GET["firstMinuteIn"]) ? $_GET["firstMinuteIn"] : ""; $firstPoint = isset($_GET["firstPoint"]) ? $_GET["firstPoint"] : ""; $lastTime = isset($_GET["lastTime"]) ? $_GET["lastTime"] : ""; $lastYearIn = isset($_GET["lastYearIn"]) ? $_GET["lastYearIn"] : ""; $lastMonthIn = isset($_GET["lastMonthIn"]) ? $_GET["lastMonthIn"] : ""; $lastDayIn = isset($_GET["lastDayIn"]) ? $_GET["lastDayIn"] : ""; $lastHourIn = isset($_GET["lastHourIn"]) ? $_GET["lastHourIn"] : ""; $lastMinuteIn = isset($_GET["lastMinuteIn"]) ? $_GET["lastMinuteIn"] : ""; $lastPoint = isset($_GET["lastPoint"]) ? $_GET["lastPoint"] : ""; $XSIZE_in = isset($_GET["XSIZE_in"]) ? $_GET["XSIZE_in"] : 1200; $YSIZE_in = isset($_GET["YSIZE_in"]) ? $_GET["YSIZE_in"] : 600; $maxy_in = isset($_GET["maxy_in"]) ? $_GET["maxy_in"] : ""; $miny_in = isset($_GET["miny_in"]) ? $_GET["miny_in"] : ""; $rounding = isset($_GET["rounding"]) ? $_GET["rounding"] : 300; include("../phplot.php"); $plot = new PHPlot($XSIZE_in, $YSIZE_in); $plot->SetPrintImage(False); // No automatic output (to get it embedded in this file) if ( $debug==1 ) { ini_set("display_errors","Yes"); } if ( $debug==1 ) { system ("echo before drawImage >>$debug_file"); } if ( $debug==1 ) { if ( $firstYearIn != "" ) { system ("echo firstYear1=".$firstYearIn." >>$debug_file"); }} if ( $firstYearIn == "") { $firstTime=strtotime("today"); $firstYearIn = date("Y",$firstTime); $firstMonthIn = date("n",$firstTime); $firstDayIn = date("j",$firstTime); $firstHourIn = "00"; $firstMinuteIn= "00"; } if ( $debug==1 ) { if ( $firstYearIn != "" ) { system ("echo firstYear2=".$firstYearIn." >>$debug_file"); }} if ( $debug==1 ) { if ( $firstPoint != "" ) { system ("echo firstPoint1=\>".$firstPoint."\< >>$debug_file"); }} if ( $firstPoint == "" ) {$firstPoint="$firstYearIn-$firstMonthIn-$firstDayIn $firstHourIn:$firstMinuteIn:00"; } if ( $debug==1 ) { system ("echo firstPoint2=\>".$firstPoint."\< >>$debug_file"); } if ( $lastYearIn == "") { $lastTime=strtotime("today"); $lastYearIn = date("Y",$lastTime); $lastMonthIn = date("n",$lastTime); $lastDayIn = date("j",$lastTime); $lastHourIn = "23"; $lastMinuteIn= "59"; } if ( $lastPoint == "" ) {$lastPoint="$lastYearIn-$lastMonthIn-$lastDayIn $lastHourIn:$lastMinuteIn:00"; } if ( $debug==1 ) { system ("echo lastPoint=".$lastPoint." >>$debug_file"); } if ( !isset($XSIZE_in) ) { $XSIZE_in=1200;} if ( !isset($YSIZE_in) ) { $YSIZE_in=600;} if ( !isset($maxy_in) ) { $maxy_in=100;} if ( !isset($miny_in) ) { $miny_in=0;} if ( !isset($rounding) ) { $rounding=301;} if ( $debug==1 ) { system ("echo starting drawImage >>$debug_file"); } /***************************************************************** Start of draw image */ //Required Settings if ( $debug>=1 ) { system ("pwd >>$debug_file"); system ("echo in drawImage >>$debug_file\n"); system ("echo XSIZE_in=$XSIZE_in >>$debug_file\n"); system ("echo YSIZE_in=$YSIZE_in >>$debug_file\n"); system ("echo firstPoint=$firstPoint >>$debug_file\n"); system ("echo lastPoint=$lastPoint >>$debug_file\n"); system ("echo rounding=$rounding >>$debug_file\n"); } // if ( $debug>=1 ) { $foo=print_r($_SESSION,true); } // if ( $debug>=1 ) { system ("cat <>$debug_file\n$foo\nEOF\n"); } if ( $debug>=1 ) { $foo=print_r($GLOBALS,true); system ("cat <>$debug_file\n$foo\nEOF\n"); } $which_plot_type="lines"; // Can be: bars, lines, linepoints, area, points, and pie // $which_plot_type="linepoints"; // Can be: bars, lines, linepoints, area, points, and pie // $which_dot="diamond"; // Can be: rect,circle,diamond,triangle,dot,line,halfline $which_vti=0; $which_hti=0; $which_xap=0; $imgFormat="png"; // Can be png or jpg //////////////////////////////////////////////// $link = mysqli_connect($dbhost,$dbuser,$dbpsw) or die("Could not connect to the database"); mysqli_select_db($link,$dbname) or die("Could not select database"); if ( $firstPoint == '' ) $firstPoint='2013-07-01 08:00:00'; if ( $lastPoint == '' ) $lastPoint='2013-07-01 08:59:59'; if ( $rounding == '' ) $rounding=302; # s_date,cs.channel,(sqrt(cs_value/s_samples)*s_step/1000000)/.015928233 # 2.5/1.414/111=.015928233V per Amp # cs_value and s_step are on mV /* Main1 goes to 01 - 02 05 - 06 09 - 10 13 - 14 17 - 18 21 - 22 25 - 26 29 - 30 Main2 goes to 03 - 04 07 - 08 11 - 12 15 - 16 19 - 20 23 - 24 27 - 28 31 - 31 ================================ 1, 2nd floor 1 3, 2nd floor 2 5, 2nd floor 3 7, 1st floor family 9, 1st floor kitchen 11, 1st floor living 13, Washer 15, Garage 17, Fridge 19, Furnace 21, Basement East+light,dishwasher, foyer 23, Panel plug, central vac 25, (freezer) 27, AC2 29, AC1 31, empty 2, Stove 4, " 6, Dryer 8, " 10, Split plug 12, " 14, Split plug 16, " 18, Split plug 20, " 22, Basement W 24, Basement E 26, Computer1 28, Computer2 30, empty 32, empty */ /* FIXME: Need to put this list in a separate table that table also need to have it's own web page for configuration Need to add something to have a per _sensor_ factor since each sensor is different and with that a table that connects sensor->ADC input->breaker and can follow any movement of the sensor. Tables created but need to be used here mysql> mysql> SELECT if(b.b_no<50,b.b_no+100,b.b_no) "channel",b.b_desc "desc",b.b_color "color",b.b_style "style",st.st_cal*(sen_cal1190+sen_cal0540)/2000 "factor" FROM Breakers b,Interconnects i,SensorTypes st,Sensors s WHERE b.b_no=i.b_no AND st.sen_type=s.sen_type AND i.sen_id=s.sen_id AND i.i_date > "2013-07-01 00:00:00"; +---------+-------------------+-------+--------+--------------------+ | channel | desc | color | style | factor | +---------+-------------------+-------+--------+--------------------+ | 107 | 1st floor family | NULL | dashed | 0.0337783014504112 | | 109 | 1st floor kitchen | NULL | solid | 0.0336966332308464 | | 111 | 1st floor living | NULL | dashed | 0.0337026354128511 | | 124 | Basement E | NULL | dashed | 0.0337299680900571 | | 126 | Computer1 | NULL | solid | 0.0329277181519672 | | 128 | Computer2 | NULL | dashed | 0.0329974853221415 | | 129 | AC1 | NULL | solid | 0.0339011345852214 | | 127 | AC2 | NULL | dashed | 0.0329308016959828 | | 51 | Main1 | NULL | solid | 0.0170982053010566 | | 52 | Main2 | NULL | dashed | 0.0170798088801662 | +---------+-------------------+-------+--------+--------------------+ 10 rows in set (0.00 sec) Also missing a "view" table, what to show and order v_viewno smallint unsigned v_desc varchar(40) //title of the view b_no foreign v_order tinyint(3) unsigned v_show boolean */ #map channel to breaker/input #channel 0-49=adc channel #channel 50=Incoming AC #channel 51=mainA #channel 52=mainB #channel 100+=100+breaker (calculated at time of INSERT) // Another thing that could be good to do - reverse the +100 part, ADC1 goes to channel 101 and Breaker 1 goes to channel 1 // but that's not needed until it's >49 ADC channels and that is way more then breakers to start with. // /* ( [b_no] => 1 [b_desc] => 2nd_floor_1 [b_descLong] => 2nd floor, master and Guestroom [b_side] => left [b_main] => 1 [b_color] => DarkGreen [b_style] => solid ) */ #$query="SET SESSION MAX_EXECUTION_TIME=20;"; #$result = mysqli_query($link,$query) or die("QueryA failed:".mysqli_error($link)); #$query="SET GLOBAL MAX_EXECUTION_TIME=20;"; #$result = mysqli_query($link,$query) or die("QueryB failed:".mysqli_error($link)); #$query="SELECT b_desc AS 'desc',b_longDesc AS 'descLong',b_no,b_color AS 'color',b_style AS 'style' from Breakers"; $query="select b_desc AS 'desc',b_descLong AS 'descLong',b.b_no,b_color AS 'color',b_style AS 'style',i_date,'true' as 'show' FROM Breakers b left join Interconnects i ON i.b_no=b.b_no where i_date is not null;"; $result = mysqli_query($link,$query) or die("Query1 failed:".mysqli_error($link)); #print ("
");print_r(mysqli_fetch_assoc($result));print("\n
"); //print ("
");
$Channels=[];

while ($row = mysqli_fetch_assoc($result)) {
  $Channels[]=$row;
  //print_r($row);
  //print("\n");
 }

//print("\n
"); //print ("
");print_r($Channels);print("\n
"); /* ARCHIVE: old info $Channels2=array(array('desc'=>"Main1", 'channel'=>51,'factor'=>.015928233,'show'=>true), array('desc'=>"Main2", 'channel'=>52,'factor'=>.015928233,'show'=>true), array('desc'=>"Main1B", 'channel'=>61,'factor'=>.033333333,'show'=>false), array('desc'=>"Main2B", 'channel'=>62,'factor'=>.033333333,'show'=>false), array('desc'=>"2nd_floor_1", 'channel'=>101,'factor'=>.033333333,'show'=>true), array('desc'=>"2nd_floor_2", 'channel'=>103,'factor'=>.033333333,'show'=>false), array('desc'=>"2nd_floor_3", 'channel'=>105,'factor'=>.033333333,'show'=>true), array('desc'=>"1FloorFamily", 'channel'=>107,'factor'=>.033333333,'show'=>true), array('desc'=>"1FloorKitchen", 'channel'=>109,'factor'=>.033333333,'show'=>true), array('desc'=>"1FloorLiving", 'channel'=>111,'factor'=>.033333333,'show'=>true), array('desc'=>"Washer", 'channel'=>113,'factor'=>.033333333,'show'=>false), array('desc'=>"Garage", 'channel'=>115,'factor'=>.033333333,'show'=>false), array('desc'=>"Fridge", 'channel'=>117,'factor'=>.033333333,'show'=>false), array('desc'=>"Furnace", 'channel'=>119,'factor'=>.033333333,'show'=>false), array('desc'=>"BasementLight", 'channel'=>121,'factor'=>.033333333,'show'=>false), array('desc'=>"Panelplug", 'channel'=>123,'factor'=>.033333333,'show'=>false), array('desc'=>"CentralVac", 'channel'=>125,'factor'=>.033333333,'show'=>false), array('desc'=>"AC1", 'channel'=>129,'factor'=>.033333333,'show'=>true), array('desc'=>"AC2", 'channel'=>127,'factor'=>.033333333,'show'=>true), array('desc'=>"Stove1", 'channel'=>102,'factor'=>.033333333,'show'=>false), array('desc'=>"Stove2", 'channel'=>104,'factor'=>.033333333,'show'=>false), array('desc'=>"Dryer1", 'channel'=>106,'factor'=>.033333333,'show'=>false), array('desc'=>"Dryer2", 'channel'=>108,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugA1", 'channel'=>110,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugA2", 'channel'=>112,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugB1", 'channel'=>114,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugB2", 'channel'=>116,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugC1", 'channel'=>118,'factor'=>.033333333,'show'=>false), array('desc'=>"SplitPlugC2", 'channel'=>120,'factor'=>.033333333,'show'=>false), array('desc'=>"BasementWest", 'channel'=>122,'factor'=>.033333333,'show'=>false), array('desc'=>"BasementEast", 'channel'=>124,'factor'=>.033333333,'show'=>true), # true array('desc'=>"Computer1", 'channel'=>126,'factor'=>.033333333,'show'=>true), # true array('desc'=>"Computer2", 'channel'=>128,'factor'=>.033333333,'show'=>true), # true array('desc'=>"port0", 'channel'=>0,'factor'=>1,'show'=>false), array('desc'=>"port1", 'channel'=>1,'factor'=>.015928233,'show'=>false), array('desc'=>"port2", 'channel'=>2,'factor'=>.015928233,'show'=>false), array('desc'=>"port3", 'channel'=>3,'factor'=>.033333333,'show'=>false), array('desc'=>"port4", 'channel'=>4,'factor'=>.033333333,'show'=>false), array('desc'=>"port5", 'channel'=>5,'factor'=>.033333333,'show'=>false), array('desc'=>"port6", 'channel'=>6,'factor'=>.033333333,'show'=>false), array('desc'=>"port7", 'channel'=>7,'factor'=>.033333333,'show'=>false), array('desc'=>"port8", 'channel'=>8,'factor'=>.033333333,'show'=>false), array('desc'=>"port9", 'channel'=>9,'factor'=>.033333333,'show'=>false), array('desc'=>"port10", 'channel'=>10,'factor'=>.033333333,'show'=>false), array('desc'=>"port11", 'channel'=>11,'factor'=>.033333333,'show'=>false), array('desc'=>"port12", 'channel'=>12,'factor'=>.033333333,'show'=>false), array('desc'=>"port13", 'channel'=>13,'factor'=>.033333333,'show'=>false), array('desc'=>"port14", 'channel'=>14,'factor'=>.033333333,'show'=>false) ); */ #$rounding = isset($_GET["rounding"]) ? $_GET["rounding"] : 300; #Main1=on&Main2=on&1FloorFamily=on&1FloorKitchen=on&1FloorLiving=on&AC1=on if (isset($_GET["firstYearIn"])) { # if a form was submitted=something may have changed // echo "
\n";print_r($Channels[0]);echo "
\n"; foreach($Channels as $key => $values){ if (isset($_GET[$values['desc']])){ $Channels[$key]['show']=true; }else{ $Channels[$key]['show']=false; } } // $Channels[0,'show']=false; // echo "
\n";print_r($Channels[0]);echo "
\n"; } #$query = "SET STATEMENT MAX_STATEMENT_TIME = 30 FOR SELECT UNIX_TIMESTAMP(s_date) sampledate"; # time is in seconds, 30 sec should be enough $query = "SET STATEMENT MAX_STATEMENT_TIME = 30 FOR SELECT /*+ MAX_EXECUTION_TIME(1000) */ UNIX_TIMESTAMP(s_date) sampledate"; #$query = "SELECT /*+ MAX_EXECUTION_TIME(1000) */ UNIX_TIMESTAMP(s_date) sampledate"; foreach ($Channels as $key => $values ){ if ($values["show"]){ #$query .=", avg(if(cs.cs_channel=".$values['channel'].",(sqrt(cs_value)*s_step/1000000)/".mysqli_real_escape_string($link,$values['factor']).",null)) as ".mysqli_real_escape_string($link,$values['desc']); # avg(if(t.Description="Main1",(sqrt(cs_value)*s_step/1000000)/COALESCE(factor,1),null)) AS "Main1", $query .=",\n avg(if(t.Description='".$values['desc']."',(sqrt(cs_value)*s_step/1000000)/COALESCE(factor,1),null)) as '".$values['desc']."'"; } } if ( "$rounding" == "0" || "$rounding" == "1" ) { $GROUPBY=" GROUP BY sampledate";} else {$GROUPBY=" GROUP BY truncate(sampledate/".mysqli_real_escape_string($link,$rounding).",0)";}; $query .= " FROM channelsamples cs LEFT JOIN sample s USE INDEX(date) ON s.s_key=cs.s_key INNER JOIN ( SELECT i1.i_date AS 'Date', i1.i_adcch AS 'ADC_channel', COALESCE( MIN(i2.i_date), '9999-12-31 23:59:59' ) AS 'next_sd_date', i1.b_no, b_desc AS 'Description', i1.sen_id, st.st_cal*(sen_cal1190+sen_cal0540)/2000 'factor' FROM Interconnects i1 LEFT JOIN Interconnects i2 ON i1.sen_id=i2.sen_id AND i1.i_adcch=i2.i_adcch AND i1.i_date = t.Date AND s.s_date < t.next_sd_date LEFT JOIN Interconnects i ON i.b_no=t.b_no WHERE s.s_date BETWEEN '" . mysqli_real_escape_string($link,$firstPoint) . "' AND '" . mysqli_real_escape_string($link,$lastPoint) . "' $GROUPBY ORDER BY s.s_date LIMIT 1000; "; // $query .=" FROM sample s,channelsamples cs WHERE s.s_date BETWEEN \"".mysqli_real_escape_string($link,$firstPoint)."\" and \"".mysqli_real_escape_string($link,$lastPoint)."\" AND s.s_key=cs.s_key $GROUPBY"; if ( $debug>1 ) { echo "
\nsql query:\n$query\n
\n"; } if ( $debug>=1 ) { system ("echo -n query:\ >>$debug_file\n");} if ( $debug>=1 ) { system ("cat <>$debug_file\n$query\nEOF\n"); } $result = mysqli_query($link,$query) or die("Query2 failed:".mysqli_error($link)); $num_rows = mysqli_num_rows($result); if ( $debug>1 ) { echo "Rows returned:$num_rows
"; } if ( $debug>=1 ) { system ("echo 'Rows returned:$num_rows ' >>$debug_file"); } //The channel and factor is hardcoded, to lazy to do it the right way (tm) right now // (select ep_price/100 from electricityprice where ep_date between "2023-11-05 11:11:11" and "2023-11-07 11:11:11" order by ep_date desc limit 1) AS "hydro", $queryP='SELECT ( avg(if(cs.cs_channel=1,(sqrt(cs_value)*s_step/1000000)/.015928233,null))+ avg(if(cs.cs_channel=2,(sqrt(cs_value)*s_step/1000000)/.015928233,null))) AS "Amp", (select ep_price/100 from electricityprice order by ep_date desc limit 1) AS "hydro", (select ep_tou from electricityprice order by ep_date desc limit 1) AS "tou" FROM sample s,channelsamples cs WHERE s.s_key=cs.s_key AND s.s_date = (select max(s_date) from sample s,channelsamples cs where s.s_key=cs.s_key AND cs.cs_channel IN (1,2) and s.s_date > Date_sub(now(),interval 1 hour));'; // AND s.s_date BETWEEN Date_sub(now(),interval 15 minute) AND NOW();'; $price_result = mysqli_query($link,$queryP) or die("QueryP failed"); $row=mysqli_fetch_row($price_result); $watt=$row[0]*120; $currPrice=$row[1]; $tou=$row[2]; if ($currPrice > 300){ $currPrice = $currPrice/100; } $price=$watt*$currPrice/1000; //print("
\n");print_r($row);print("\n
\n"); //print("
\nthe price is now: ");print_r($price);print(" cent per hour\nCurrent price:");print_r($currPrice);print(" cent\n
\n"); if ( $num_rows == 0) { if ( $debug>=1 ) {system ("echo 'in the 0 trap' >>$debug_file"); } // print ("Powermon values between $firstPoint and $lastPoint returned 0 rows
\n"); $message = "I'm sorry, Dave. I'm afraid I can not do that.\n\n" . "Powermon values between $firstPoint and $lastPoint returned 0 rows\n\n"; $query = "SELECT min(s_date) as minDate, max(s_date) as maxDate FROM sample"; $result = mysqli_query($link,$query) or die("Query2 failed"); $row = mysqli_fetch_assoc($result); // print ("Oldest data found is ".$row["minDate"].", newest is ".$row["maxDate"]."
\n"); $message .= "Oldest data found is ".$row["minDate"].", newest is ".$row["maxDate"]."\n\n" . "Please change date to somewhere between that."; mysqli_close($link); if ( $debug>=1 ) {system ("echo 'sqlquery done' >>$debug_file"); } # Note: This font name is system dependent: // $plot->SetFontTTF('generic', 'LiberationSans-Italic.ttf', 14); $plot->SetFont('generic', 4, 14); $plot->SetBackgroundColor('#ffcc99'); $plot->SetImageBorderWidth(8); $plot->SetImageBorderColor('blue'); $plot->SetImageBorderType('raised'); if ( $debug>=1 ) {system ("echo 'd1' >>$debug_file"); } $plot->SetBrowserCache("0"); if ( $debug>=1 ) {system ("echo 'd2' >>$debug_file"); } $plot->SetFileFormat($imgFormat); if ( $debug>=1 ) {system ("echo 'About to do plot' >>$debug_file"); } $plot->DrawMessage($message, array( 'draw_background' => TRUE, 'draw_border' => TRUE, 'force_print' => FALSE, 'reset_font' => FALSE, 'wrap_width' => 100, 'text_color' => 'navy')); } else { // if sql worked # $debug=2; if ( $debug>1 ) { system ("echo -n 'SQLresult: ' >>$debug_file"); } $header=0; while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) { if ($header==0){ $HeaderArray=array_keys($line); array_shift($HeaderArray); # drop the first field, date is X label $header=1; if ( $debug>1 ) { $foo=print_r($HeaderArray,true); system ("cat <>$debug_file\nHeaderArray:\n$foo\nEOF\n"); } } $data[]=array_merge((array)'',$line); if ( $debug>1 ) { $foo=print_r($line,true); system ("cat <>$debug_file\n$foo\nEOF\n"); } } mysqli_free_result($result); if ( $debug>=1 ) { $foo=print_r($data[0],true); system ("cat <>$debug_file\ndata[0]\n$foo\nEOF\n"); } if ( $debug>1 ) { system ("echo 'SQLend' >>$debug_file"); } $debug=0; $miny = 0; $maxy = 20; $miny = NULL; //Need to calculate them instead $maxy = NULL; if ( $debug>=1 ) { system ("echo Creating new PHPlot >>$debug_file"); } $plot->SetPrintImage(False); // No automatic output $plot->SetTitle("Energy monitoring - pre alpha test version 3\n $firstPoint - $lastPoint"); $plot->SetDataValues($data); $plot->SetDataType("data-data"); # $plot->SetImageBorderType('plain'); # $plot->SetXDataLabelAngle(90); // $plot->SetXDataLabelMaxlength(15); $plot->SetBrowserCache("0"); $plot->SetFileFormat($imgFormat); // $plot->SetImageArea($XSIZE_in, $YSIZE_in); // $plot->SetPlotAreaPixels(100,50,$XSIZE_in-50, $YSIZE_in-150); $plot->SetPlotType($which_plot_type); # $plot->SetUseTTF("0"); # $plot->SetIsInline(0); # $plot->SetVertTickIncrement($which_vti); # $plot->SetHorizTickIncrement($which_hti); $plot->SetXLabelAngle(90); $plot->SetXLabelType('time', '%h-%d %H:%M'); # $plot->SetXTickIncrement(48*30*60); // 30 min # $plot->SetNumXTicks(24); # $plot->SetXTickIncrement(60); # $plot->TuneXAutoTicks(NULL, 'date'); # $plot->SetLineWidth("1"); # $plot->SetDrawYGrid("1"); // 1 = true # $plot->SetPlotAreaWorld($firstPoint, $miny,$lastPoint,$maxy); # $plot->SetPlotAreaWorld($firstPoint,null,$lastPoint); # $plot->SetPlotAreaWorld(); # $plot->SetXGridLabelType("time"); # $plot->SetXTimeFormat("%b %d-%H:%M:%S ") ; # $plot->SetXTimeFormat("%H:%M ") ; # $plot->SetXTickPos('none'); # $plot->SetPointShape("$which_dot"); # $plot->SetErrorBarShape("$which_error_type"); # $plot->SetXLabel("Date"); # $plot->SetXDataLabelType('time',"%H:%M"); # $plot->SetXLabelType('time',"%H:%M"); $plot->SetYLabel("Amps"); $plot->SetLegendUseShapes(true); # $plot->SetXAxisPosition($which_xap); // http://phplot.sourceforge.net/phplotdocs/conc-colors.html#conc-colors-datacolors // SkyBlue green orange blue red DarkGreen purple peru cyan salmon SlateBlue YellowGreen magenta aquamarine1 gold violet /* $plot->SetDataColors( array( "SkyBlue", // Main1 "green", // Main2 "green", // Main2B "SkyBlue", // Main1B "red", // "purple", // "cyan", // "peru", // DryerA "cyan", // DryerB ), //Data Colors array("black") //Border Colors ); */ $plot->SetMarginsPixels(NULL,150); $plot->SetLegendPosition(0, 0, 'image', 1, 0, -130,40); # $plot->SetLineStyles(array("solid","solid","dashed","dashed","solid","solid","solid","solid","solid","solid","solid","solid","solid","solid")); $plot->SetLineStyles(array("solid","solid","solid","solid","solid","solid","solid","solid","solid","solid","solid","solid","solid")); # $plot->SetLegend(array("Utility","BattPct","UPSLoad","BattV","OutV","UpsTemp","UtilMax","UtilMin")); # $plot->SetLineStyles("solid"); # $plot->SetLegend(array("Main1","Main2","CompA","CompB","BasemE","Washer","KitchenRoom","Dryer1","Dryer2")); $plot->SetLegend($HeaderArray); $plot->DrawGraph(); } // if $num_rows = 0 else if ( $debug>=1 ) { system ("echo end of drawImage >>$debug_file"); } /* end of draw image *****************************************************************/ print(''); print("\n"); print("\n"); print("
\n"); print("\n"); print("Energy monitoring data\n"); print("\n"); print("\n"); print("
\n"); print("\n"); /* print(" \n"); */ print("EncodeImage()."\" alt=\"Plot Image\">"); print("
\n"); //printf("

Current hydro cost is %4.2f cent.

\n",$currPrice); //printf("

Current hydro cost is %4.2f cent.
The current usage of %3.2f Watt costs %4.2f cent per hour or $%4.2f dollar per day = $%4.2f in 30 days

\n",$currPrice,$watt,$price,$price*24/100,$price*24/100*30); printf("

Current hydro TOU is \"%s\" and cost is %4.2f cent.
The current usage of %3.2f Watt (%3.2f kW) costs %4.2f cent per hour

\n",$tou,$currPrice,$watt,$watt/1000,$price); print("

Bar Details

\n"); print("
\n"); print(" Width of Graph in Pixels
\n"); print(" Height of Graph in Pixels
\n"); print(" Maximum Height of Graph in Y Axis Units
\n"); print(" Minimum Height of Graph in Y Axis Units
\n"); print(" average over so many seconds
\n"); $now=time(); print("Select time for graph
\n"); print("\n"); print("\n"); print("\n"); print("\n"); // Month print(" \n"); print(" \n"); print(" \n"); print(" \n"); print(" \n"); //////////////////////////////// // End // Year, still got result from sql query earlier print("\n"); print("\n"); // Month print(" \n"); print(" \n"); print(" \n"); print(" \n"); print(" \n"); /////// print("
 YearMonthDayHourMinute
Start
End
\n"); print("
\n"); print("

Channels

\n"); print("Select channels to display
\n"); print("\n"); $cnt=0; foreach ($Channels as $key => $values ){ if ($cnt==0){print("");} $cnt++; // print("channel: ".$values['desc']."=".$values['show']." => "); print(""); if ($cnt==4){print("\n");$cnt=0;} } print("
". $values['desc']."


\n"); print("\n"); print("\n"); print(" \n"); print("
\n"); print("
\n"); print("This code was created to figure out how to get date based data from a database \n"); print("and display it as a line graph.
\n"); print("The code is licensed under GPL and you can see the (messy) code in http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."s
\n"); print("
\n"); //echo "
\n";print_r($_SERVER);echo "
\n"; print("
\n"); print("\n"); print("\n");