<?php
set_time_limit(0);
include_once 'php-ofc-library/open-flash-chart.php';
include_once 'cfg/mysql.php';
connectdb();

$roomresult = mysql_query("
						  SELECT name 
						  FROM `save_energy`.`rooms` 
						  ORDER BY name ASC;
						  ");
while($row = mysql_fetch_array($roomresult))
{
	$roomarray[] = ($row['name']);
}

foreach($roomarray as $room)
{
	$description = array();
	$colorcode=0;
	$data = array();
	
	//-- X AXIS RESULTS--\\
	
	$result_x = mysql_query("
							SELECT TIME(`tuples`.start) AS xaxis
							FROM (`save_energy`.`tuples`) 
							JOIN `save_energy`.`sensors` ON (`tuples`.sensorID=`sensors`.sensorID)
							JOIN `save_energy`.`rooms` ON (`sensors`.roomID=`rooms`.roomID)
							WHERE `sensors`.name LIKE '%usage' 
							AND DATE(`tuples`.start) = CURRENT_DATE() - INTERVAL 47 DAY  
							AND `rooms`.name='$room'
							GROUP BY `tuples`.start ASC
							");
	
	while($row = mysql_fetch_array($result_x))
	{
		$xaxis[] = ($row['xaxis']);
	}	
	
	
	//-- DESCRIPTION RESULTS--\\
	
	$result = mysql_query("
						  SELECT DISTINCT `sensors`.description as description
						  FROM (`save_energy`.`sensors`) 
						  JOIN `save_energy`.`tuples` ON (`sensors`.sensorID=`tuples`.sensorID)
						  JOIN `save_energy`.`rooms` ON (`sensors`.roomID=`rooms`.roomID)
						  WHERE `sensors`.name LIKE '%usage' 
						  AND DATE(`tuples`.start) = CURRENT_DATE() - INTERVAL 47 DAY  
						  AND `rooms`.name='$room'
						  ORDER BY `sensors`.description ASC
						  ");
	
	while($row = mysql_fetch_array($result))
	{
		$description[] = ($row['description']);
	}	
	
	
	
	$chart = new open_flash_chart();
	$line_default_dot = new dot();
	$line_default_dot->size(4)->halo_size(2)->colour('#000000');
	
	foreach($description as $value)
	{
		$colorcode++;
		if($colorcode==1) {$color = '#f7921c';}
		if($colorcode==2) {$color = '#bf7115';}
		if($colorcode==3) {$color = '#6c3d06';}
		if($colorcode==4) {$color = '#2d1d0a';}
		if($colorcode==5) {$color = '#2d1d0a';}
		if($colorcode==6) {$color = '#2d1d0a';}
		$name=$value;
		
		$result1 = mysql_query("
							  SELECT value*1000 AS value
							  FROM (`save_energy`.`tuples`) 
							  JOIN `save_energy`.`sensors` ON (`tuples`.sensorID=`sensors`.sensorID)
							  JOIN `save_energy`.`rooms` ON (`sensors`.roomID=`rooms`.roomID)
							  WHERE DATE(`tuples`.start) = CURRENT_DATE() - INTERVAL 47 DAY
							  AND `sensors`.name LIKE '%usage' 
							  AND `rooms`.name='$room'
							  AND `sensors`.description='$name'
							  ORDER BY `tuples`.start ASC
							  ");
		
		while($row = mysql_fetch_array($result1))
		{
			$data[] = intval($row['value']);
		}
		
		$value = new line();
		$value->set_default_dot_style($line_default_dot);
		$value->set_values( $data );
		$value->set_width( 2 );
		$value->set_colour($color);
		$value->set_key($name, 14);
		
		$chart->add_element( $value );
		unset($data);
	}
	
	//-- Y AXIS --\\
	
	$y = new y_axis();
	$y->set_range( 0, 300, 30 );
	$y->set_colour('#000000');
	$y->set_grid_colour('#D8D8D8');
	
	
	//-- X AXIS --\\
	
	$x = new x_axis();
	$x->offset(false)->steps(1);
	$x->colour('#000000')->grid_colour('#D8D8D8');
	
	$x_labels = new x_axis_labels();
	$x_labels->set_steps( 3 );
	$x_labels->set_colour( '#000000' );
	$x_labels->set_labels( $xaxis );
	$x->set_labels( $x_labels );
	
	
	//-- CHART --\\
	
	$title = new title('Electriciteitverbruik in kamer '.$room.' in Wh (Watt per uur)');
	$chart->set_title( $title );
	$chart->set_x_axis( $x );
	$chart->set_y_axis( $y );
	$chart->set_bg_colour( '#FFFFFF' );
	
	
	//-- WRITE TO FILE --\\
	
	echo $chart->toPrettyString();
	$chartwrite = $chart->toPrettyString();
	
	$fh = fopen("graphs/elec_".$room.".txt", 'w') or die("can't open file");
	fwrite($fh, $chartwrite);
	fclose($fh);
	unset($xaxis);
}

?>