<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

<!-- config -->
<?php
include "config.php"; # DB and password config

$html_title = "Nättsjö 1:15 temperature summary";
$title = $html_title;
$update_sample = "hour";
$REFRESH = 3600;
$warning_timeout = 2*60*60; # 2h
$plotsdir = "plots/temp_";
$timezone = "Europe/Berlin";
$this_document = $_SERVER["SCRIPT_NAME"];


date_default_timezone_set($timezone);
$x = localtime();
$dst =$x[8];
$offset = 1 + $dst;
$zenith=90+50/60;
if ($show_sunrise) {
    $sunrise = date_sunrise(time() , SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset );
    $sunset = date_sunset(time() , SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
}
$time = "time";


$sunh_query = "select yearweek(a.time,3) as yearweek, 
DATE_FORMAT(a.time,'%M %D') AS wk_time, 
DATE_FORMAT(a.time,'%W %M %D') AS day_time, 
WEEK(a.time,3) AS week, date_format(a.time,'%Y%m%d') as filedate,  
date_format(a.time,'%m') as month, date_format(a.time,'%M') as monthname, 
date_format(a,time,'%Y') as year, 
round(count(a.time)/2,1) as sun_h 
from nattsjo_solarout b, nattsjo_solarin a 
where (date(a.time) = date(b.time)) and (hour(a.time)=hour(b.time)) and (minute(a.time)=minute(b.time)) and ((a.temp-b.temp) > 10) ";

$sunh_daily_clause = "group by day(a.time) order by -a.time limit 10";
$sunh_weekly_clause = "group by yearweek(a.time,3) order by -a.time limit 5";
$sunh_monthly_clause = "group by month(a.time) order by -a.time limit 5";


$query_past = "select yearweek($time,3) as yearweek, 
date_format($time,'%Y%m%d') as filedate,  
DATE_FORMAT($time,'%M %D') AS wk_time, 
DATE_FORMAT($time,'%W %M %D') AS day_time, 
WEEK($time,3) AS week, 
MONTHNAME($time) as month,
date_format($time,'%Y%m') as filedate_month 
from nattsjo_ack1 ";

$clause_pastdays = "group by day_time order by -$time limit 10";
$clause_pastweeks = "group by yearweek order by -$time limit 5";
$clause_pastmonths = "group by month, year($time) order by -$time limit 7";


$init_query = "select date_format($time,'%Y%m%d') as dayfile, yearweek($time,3) as weekfile, date_format($time,'%Y%m') as monthfile, 
(select round(temp/10,1) from nattsjo_outdoor1 order by -$time limit 1) as outdoor1, 
(select $time from nattsjo_outdoor1 order by -$time limit 1) as time_outdoor1,  
(select unix_timestamp($time) from nattsjo_outdoor1 order by -$time limit 1) as timestamp_outdoor1,  

(select round(temp/10,1) from nattsjo_indoor1 order by -$time limit 1) as indoor1, 
(select $time from nattsjo_indoor1 order by -$time limit 1) as time_indoor1,  
(select unix_timestamp($time) from nattsjo_indoor1 order by -$time limit 1) as timestamp_indoor1,  

(select round(temp/10,1) from nattsjo_ack1 order by -$time limit 1) as ack1, 
(select $time from nattsjo_ack1 order by -$time limit 1) as time_ack1,  
(select unix_timestamp($time) from nattsjo_ack1 order by -$time limit 1) as timestamp_ack1,  

(select round(temp/10,1) from nattsjo_solarin order by -$time limit 1) as solarin, 
(select $time from nattsjo_solarin order by -$time limit 1) as time_solarin,  
(select unix_timestamp($time) from nattsjo_solarin order by -$time limit 1) as timestamp_solarin,  

(select round(temp/10,1) from nattsjo_solarout order by -$time limit 1) as solarout,
(select $time from nattsjo_solarout order by -$time limit 1) as time_solarout, 
(select unix_timestamp($time) from nattsjo_solarout order by -$time limit 1) as timestamp_solarout 

from nattsjo_outdoor1 ORDER BY -$time";


$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);
$result = mysql_query("SET time_zone='$timezone'") or die("MySQL error!");
$result = mysql_query($init_query) or die("MySQL error!");
$row = mysql_fetch_array($result);

$day_plot = $plotsdir . $row["dayfile"] . ".png";
$week_plot = $plotsdir . $row["weekfile"] . "w.png";
$month_plot = $plotsdir . $row["monthfile"] . ".png";
$t_outdoor1 = $row["outdoor1"];
$time_outdoor1 = $row["time_outdoor1"];
$timestamp_outdoor1 = $row["timestamp_outdoor1"];
$t_indoor1 = $row["indoor1"];
$time_indoor1 = $row["time_indoor1"];
$timestamp_indoor1 = $row["timestamp_indoor1"];
$t_ack1 = $row["ack1"];
$time_ack1 = $row["time_ack1"];
$timestamp_ack1 = $row["timestamp_ack1"];
$t_solarin = $row["solarin"];
$time_solarin = $row["time_solarin"];
$timestamp_solarin = $row["timestamp_solarin"];
$t_solarout = $row["solarout"];
$time_solarout = $row["time_solarout"];
$timestamp_solarout = $row["timestamp_solarout"];
mysql_free_result($result);
?>


<!-- main -->
<HTML>
<HEAD>
<TITLE><?php echo "$html_title" ?></TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<LINK href="temp.css" type="text/css" rel="stylesheet">
<META http-equiv="Refresh" content="<?php echo "$REFRESH" ?>">
<META http-equiv="Pragma" content="no-cache">
</HEAD>

<BODY>
<h1 class="center"><?php print "$title"; ?></h1>

<!-- plots --> 
<TABLE>
<TBODY>
<!-- Daily plot -->
   <TR><TD class="update">Updated every <?php echo $update_sample ?>.</TD></TR>
  <TR><TD>
   <IMG alt="Temperature today" src="<?php print $day_plot ?>"></TD></TR>
  <TR><TD>&nbsp;</TD></TR>

<!-- Weekly plot -->
  <TR><TD>
    <IMG src="<?php print $week_plot ?>" alt="Temperature this week"></TD>
  </TR>

</TBODY>
</TABLE>
<p> </p>


<?php
function time_warning($t, $tw) {
	if ( (time()-$t) > $tw) {
		echo "<span class='warning'> WARNING - Not updating!</span>";
	}
}
?>


<!-- details -->
<table>
  <tr><td colspan="3" class="head">Detailed graphs</td></tr>
<tbody>
<tr><td colspan='3'></td></tr>
<tr><td colspan='3'><?php print "Sunrise / Sunset: $sunrise -> $sunset\n"; ?></td></tr>
<tr><td colspan='3'></td></tr>
<tr>
     <td class="cntrlfirst"><b>Sensor</b></td>
     <td class="cntrlnext"><b>Current value</b></td>
     <td class="cntrlnext"><b>Sample time</b></td>
</tr>

<tr class='row'><td colspan='1'>
<a href="../nattsjo/">Outdoor temperature</a><?php time_warning($timestamp_outdoor1, $warning_timeout) ?></td>
<td><?php echo $t_outdoor1; ?>&deg; C</td>
<td><?php echo $time_outdoor1; ?></td></tr>

<tr class='row'><td colspan='1'>
<a href="../nattsjo_indoor1/">Indoor temperature</a><?php time_warning($timestamp_indoor1, $warning_timeout) ?></td>
<td><?php echo $t_indoor1; ?>&deg; C</td>
<td><?php echo $time_indoor1; ?></td></tr>

<tr class='row'><td colspan='1'>
<a href="../nattsjo_ack1/">Ackumulator heat tank temperature</a>
<?php time_warning($timestamp_ack1, $warning_timeout) ?></td>
<td><?php echo $t_ack1; ?>&deg; C</td>
<td><?php echo $time_ack1; ?></td></tr>

<tr class='row'><td colspan='1'>
<a href="../nattsjo_solarin/">Incoming solar panel temperature</a>
<?php time_warning($timestamp_solarin, $warning_timeout) ?></td>
<td><?php echo $t_solarin; ?>&deg; C</td>
<td><?php echo $time_solarin; ?></td></tr>

<tr class='row'><td colspan='1'>
<a href="../nattsjo_solarout/">Outgoing solar panel temperature</a>
<?php time_warning($timestamp_solarout, $warning_timeout) ?></td>
<td><?php echo $t_solarout; ?>&deg; C</td>
<td><?php echo $time_solarout; ?></td></tr>

<tr class="links"><td colspan='3'></td></tr>
<tr class="links"><td colspan='3'></td></tr>
<tr class="links"><td colspan='3'><a href='info/'>Description</a> of the hardware</td></tr>
<tr class="links"><td colspan='3'>Set <a href='control.php'>control parameters</a></td></tr>
<tr class='links'><td colspan='3'><a href='events.php'>Event log</a></td></tr>
</tbody></table>
<p> </p>



<!-- past days -->
<table>
  <tr><td colspan="2" class='head'>Old daily graphs</td></tr>
  <tbody>
  <tr><td colspan="2"></tr>

<?php
$result = mysql_query($query_past . $clause_pastdays);
while ($row = mysql_fetch_array($result)) {
  echo "<tr class='row'>";
  $plot_file = "plots/temp_" . $row["filedate"] . ".png";
  echo "<td colspan='2'>";
  echo "\t<a href=\"javascript:void(window.open('$plot_file','graph_window','resizable,scrollbars=no,width=768,height=288,left=100,top=100'))\">";
  echo $row["day_time"] . "</a></td>";
  echo "</tr>\n";
}
mysql_free_result($result);
print "</tbody></table>\n\n";
?>
<p></p>



<!-- past weeks  -->
<table>
  <tr><td colspan="2" class='head'>Old weekly graphs</td></tr>
  <tbody>
  <tr><td colspan="2"></tr>

<?php
$result = mysql_query($query_past . $clause_pastweeks);
while ($row = mysql_fetch_array($result)) {
  echo "<tr class='row'>";
  $plot_file = "plots/temp_" . $row["yearweek"] . "w.png";
  echo "<td colspan='2'>";
  echo "<a href=\"javascript:void(window.open('$plot_file','graph_window','resizable,scrollbars=no,width=768,height=288,left=100,top=100'))\">";
  echo " Week " . $row["week"] . " (" . $row["wk_time"] . ")</a></td>";
  echo "</tr>\n";
}
mysql_free_result($result);
print "</tbody></table>\n\n";
?>
<p> </p>



<!-- past months  -->
<table>
  <tr><td colspan="2" class='head'>Old monthly graphs</td></tr>
  <tbody>
  <tr><td colspan="2"></tr>

<?php
  $result = mysql_query($query_past . $clause_pastmonths);
while ($row = mysql_fetch_array($result)) {
  echo "<tr class='row'>";
  $plot_file = "plots/temp_" . $row["filedate_month"] . ".png";
  echo "<td colspan='2'>";
  echo "<a href=\"javascript:void(window.open('$plot_file','graph_window','resizable,scrollbars=no,width=768,height=288,left=100,top=100'))\">";
  echo "" . $row["month"] . "</a></td>";
  echo "</tr>\n";
}
mysql_free_result($result); 
?>
  <TR><TD colspan='2'> </TD></TR>
  <TR><TD colspan='2'> </TD></TR>
  <TR><TD colspan='2'>
    <IMG src="<?php print $month_plot ?>" alt="Temperature this month"></TD>
  </TR>
</tbody></table>
<p> </p>


<?php 
#include "sun_hour.php";
mysql_close($DB);
?>



<!-- Footer with links -->
<ul>
 <li><a href = "plots?C=M;O=D">Old graphs</a></li>
 <li><a href="../hw">About the hardware</a></li>
</ul>
<hr>
<p class="right">(c) Daniel Vindev&aring;g 2010-2013</p>
</BODY></HTML>