<!DOCTYPE html>
<html><head><title>Sensor Data</title>
<style>
    a {
     text-decoration: none;
	 color:green;
    }
  </style>



</head><body><font size=5>
 
<?php
//ESP Temperature, humitidy and pressure logger based on BME280 sensor and ESP32. Pushes the data to a web database for display. 
//See full project at http://projects.xief.net
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files.
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//This file displays the summary table of the available sensors. 

//get parameters and helper functions
include 'config.php';


date_default_timezone_set($timezone);


// Create connection
$conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


//Get latest outside sample to get outside dew point
$sqlGetLatestOutside = "SELECT * FROM SensorData WHERE location = '" . $outsideSensor . "' ORDER BY reading_time DESC LIMIT 1";
$ResultOutsideTemp = $conn->query($sqlGetLatestOutside) or trigger_error($conn->error);
$rowOutSideTemp = $ResultOutsideTemp->fetch_array();
$outsideDewPoint = dewPoint($rowOutSideTemp["temperature"], $rowOutSideTemp["humidity"]);
//echo "Outside Dew Point: " . number_format($outsideDewPoint,2) . "C";




//$sql = "SELECT id, location, temperature, humidity, pressure, reading_time FROM SensorData";
$sql = "SELECT * FROM SensorData INNER JOIN (SELECT MAX(id) as id FROM SensorData GROUP BY location) last_updates ON last_updates.id = SensorData.id";


echo '<table cellspacing="5" cellpadding="5">
      <tr> 
        <td><b>ID</b></td> 
        <td><b>Location</b></td> 
        <td><b>Temp.<br>(C)</b></td> 
        <td><b>Hum.<br>(%RH)</b></td> 
		<td><b>Dew Point<br>(C)</b></td>
		<td><b>Pressure<br>(hPa)</b></td>
        <td><b>Age<br>(min)</b></td> 
      </tr>';
 
if ($result = $conn->query($sql)) {
    while ($row = $result->fetch_assoc()) {
        $row_id = $row["id"];
        $row_location = $row["location"];
        $row_temperature = $row["temperature"];
        $row_pressure = $row["pressure"]; 
        $row_humidity = $row["humidity"]; 
		$row_reading_time = $row["reading_time"];
		$age = time() - strtotime($row_reading_time) ; //age of datapoint in S
		
		
		if ($age < $timeNotShow) {
			$fontColor = "#000000";
			if ($age > $timeGreyOldData) {
				$fontColor = $greyOldData;
			}
			echo '<font color = ' . $fontColor .'>';
			
			
			$dew = dewPoint($row_temperature,$row_humidity);
			
			$dewBgColor = "#ffffff";
			if ($row_temperature < $outsideDewPoint) {$dewBgColor = $highlightDewPoint;} //highlight temperatures that are below outside dew point
			$noWindowOpenBgColor ="#ffffff";
			if ($dew < $outsideDewPoint) {$noWindowOpenBgColor = $noWindowOpen;} //highlight dew points that are below outside dew point
			
			
			echo '<tr> 
					<td><font color = ' . $fontColor .'>' . $row_id . '</font></td> 
					<td><font color = ' . $fontColor .'><a href="linegraph.php?sensorName=' . $row_location . '">' . $row_location . '</a></font></td> 
					<td bgcolor= ' . $dewBgColor . '>' . $row_temperature . '</font></td> 
					<td><font color = ' . $fontColor .'>' . $row_humidity . '</font></td> 
					<td bgcolor= ' . $noWindowOpenBgColor . '><font color = ' . $fontColor .'>' . number_format($dew,2) . '</font></td>
					<td><font color = ' . $fontColor .'>' . $row_pressure . '</font></td>
					<td><font color = ' . $fontColor .'>' . number_format($age/60,0) . '</font></td> 
				  </tr>';
    
		}
	}
    $result->free();
}

$conn->close();
?> 
</table>
</body>
</html>








