<html>
<head>
<title>Temperature logger web interface</title>
</head>
<body>
<h1>Temperature logger</h1>

<table border="1"> 
<tr><th>Date available</th><th>Details</th><th>Chart</th><tr>

<tr>

<!-- Create the left column contents with the list dates logged  -->

<td valign="top">
<?php
	try {
		/*** connect to SQLite database ***/
		$dbh = new PDO("sqlite:/var/www/temperatures.sqlite");

		/*** The SQL SELECT statement ***/
		$sql = "SELECT date FROM samples GROUP BY date";

		foreach ($dbh->query($sql) as $row) {
			echo "<a href='?cmd=list&date=$row[date]'>$row[date]</a><br/>";
		}
		$dbh = null;
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
	}
?>
</td>


<?php
	if (isset($_GET["cmd"])) {
		if ($_GET["cmd"]=="list") {
		
			// Create the middle column content with the temperature sample

			echo "<td valign='top'>";
			try {
				/*** connect to SQLite database ***/
				$dbh = new PDO("sqlite:/var/www/temperatures.sqlite");

				/*** The SQL SELECT statement ***/
				$sql = "SELECT * FROM samples WHERE date LIKE '$_GET[date]%'";

				echo "<table border='1'>";
				echo "<tr>";
				echo "<th>Date</th><th>Time</th><th>Temp</th>";
				echo "</tr>";
			
				foreach ($dbh->query($sql) as $row) {
					echo "<tr>";
					echo "<td>" . $row['date'] . "</td><td>" . $row['time'] . "</td><td>" . $row['value'] . "</td>";
					echo "</tr>";
				}
				echo "</table>";
			
				$dbh = null;
			}
			catch(PDOException $e)
			{
				echo $e->getMessage();
			}
			
			// Generates the tmp/data.xml file that will be read by the FusionChart component	

			try {
			
				$handle = fopen("/tmp/data.xml", "w");	
				/*** connect to SQLite database ***/
				$dbh = new PDO("sqlite:/var/www/temperatures.sqlite");

				/*** The SQL SELECT statement ***/
				$sql = "SELECT * FROM samples WHERE date LIKE '$_GET[date]%'";

				fprintf($handle,"<graph xAxisName='Time' yAxisName='Temperature' showNames='1' decimalPrecision='1' formatNumberScale='0'>\n");
			
				foreach ($dbh->query($sql) as $row) {
					fprintf($handle,"<set name='" . substr($row['time'],0,2) . "' value='" . $row['value'] . "' />\n");
					// echo "<td>" . $row['date'] . "</td><td>" . $row['time'] . "</td><td>" . $row['value'] . "</td>";
				}
				fprintf($handle,"</graph>\n");
				$dbh = null;
				fclose($handle);	
			}
			catch(PDOException $e)
			{
				echo $e->getMessage();
			}
			echo "</td>";

			echo "<td>";

			// FusionChart component
			?>
			
			<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="640" height="360" id="Column3D" >
				<param name="movie" value="FusionCharts/FCF_Line.swf" />
				<param name="FlashVars" value="&dataURL=tmp/data.xml&chartWidth=640&chartHeight=360">
				<param name="quality" value="high" />
				<embed src="FusionCharts/FCF_Line.swf" flashVars="&dataURL=tmp/data.xml&chartWidth=640&chartHeight=360" quality="high" width="640" height="360" name="Column3D" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
			</object>
			<?
			echo "</td>";
		}
	}
?>
</tr>
</table>
</body>
</html>


