<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<!-- config -->
<?php
include "config.php"; # DB and password config
#$PASSWORD = ""; # debug mode, print CGI output, otherwise as html comment

$html_title = "Nättsjö 1:15 temperature and relay control";
$title = $html_title;
$REFRESH = 3600; # Seconds for html refresh
$sheevaplug_cgi = "../../sheevaplug.cgi";

$sql_init = "SELECT time, type, ROUND(temp/10,0) AS temp FROM nattsjo_control WHERE type = 'indoor1_high' OR type = 'indoor1_low'";
$sql_relay = "SELECT b.id, a.descr, b.hour, b.minute FROM nattsjo_devices a, nattsjo_relays_cron b WHERE b.relay = a.id ORDER BY hour, minute, descr";
$sql_new = "SELECT id, descr FROM nattsjo_devices WHERE type<2";

$indoor_low_min = 5;
$indoor_low_max = 22;
$indoor_high_min = 10;
$indoor_high_max = 25;

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

?>


<!-- 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>


<!-- Post relays -->
<?php
if (isset($_POST["pw2"])) {
  if ($_POST["pw2"] == $PASSWORD) {
    if ($PASSWORD != "") { echo "<!--\n"; }
    $id =  $_POST["id"];
    $hour = $_POST["hour_$id"];
    $minute = $_POST["minute_$id"]  +  $_POST["minute_plus_$id"];
    echo "<pre>\n";

    if (isset($_POST["update"])) {
      system("$sheevaplug_cgi 5 $id $hour $minute");
    }
    if (isset($_POST["delete"])) {
      system("$sheevaplug_cgi 6 $id");
    }
    if (isset($_POST["executenow"])) {
      $new_id =  $_POST["new"];
      system("$sheevaplug_cgi 7 $new_id");
    }
    if (isset($_POST["addnew"])) {
      $new_id =  $_POST["new"];
      system("$sheevaplug_cgi 8 $new_id $hour $minute");
    }
    echo "</pre>\n";
    if ($PASSWORD != "") { echo "\n-->\n\n"; }    
  } else {
    echo "<p><strong><h1>Wrong password!</h1></strong></p>\n";
  } 
}
?>



<!-- Post triggers -->
<?php
if (isset($_POST["pw1"])) {
  if ($_POST["pw1"] == $PASSWORD) {
    if ($PASSWORD != "") { echo "<!--\n"; }
    if (isset($_POST["trigger"])) {
       $low = intval($_POST["low"] * 10);
       $high = intval($_POST["high"] * 10);
       $pw = $_POST["pw"];
       echo $_POST["trigger"] . ", $low, $high, $pw";
       echo "<pre>";
       system("$sheevaplug_cgi 4 $low $high");
       echo "</pre>";
    }
    if (isset($_POST["elpatron_on"])) {
       $pw = $_POST["pw"];
       echo $_POST["elpatron_on"];
       echo "<pre>";
       system("$sheevaplug_cgi 2");
       echo "</pre>";
    }
    if (isset($_POST["elpatron_off"])) {
       echo $_POST["elpatron_off"];
       echo "<pre>";
       system("$sheevaplug_cgi 3");
       echo "</pre>";
    }
    if ($PASSWORD != "") { echo "\n-->\n\n"; }
  } else {
    echo "<p><h1 class='bold'>Wrong password!</h1></p>\n";
  }
} 
?>



<!-- Init DB -->
<?php
$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);
mysql_query("SET NAMES UTF8", $DB);
$result = mysql_query($sql_init) or die("MySQL error!");

while ($row = mysql_fetch_array($result)) {
  if ($row["type"] == "indoor1_low") {
      $low_temp = $row["temp"];
      $low_date = $row["time"];
    }
  if ($row["type"] == "indoor1_high") {
      $high_temp = $row["temp"];
      $high_date = $row["time"];
    }
}
mysql_free_result($result);

if (isset($_POST["low"])) {
  $low_temp = intval($_POST["low"]);
}  
if ($indoor_high_min < $low_temp) { $indoor_high_min = $low_temp; }
?>



<!-- Temperature triggers -->
<form name='triggers' method="POST" action="" enctype="multipart/form-data">
<table>
  <tr>
    <td colspan="3" class="head">Temperature triggers</td>
  </tr>
  <tbody>
    <tr>
     <td class="onethird"><b>Type</b></td>
     <td class="onethird"><b>Last set</b></td>
     <td class="onethird"><b>Temperature</b></td>
</tr>

<tr class="row">
<td colspan='1'>Indoor low trigger:</td>
<td><?php echo $low_date ?></td>
<td>
<?php
echo "<select id='low'  name='low' onChange='document.triggers.submit();'>";
for ($x=$indoor_low_min;$x<=$indoor_low_max;$x++) {
  echo "<option ";
  if ($x == $low_temp) { echo "selected='selected' ";}
  echo "value='$x'>$x &deg;C</option>\n";
}
echo "</select>\n";
?>

</td></tr>

<tr class='row'>
<td colspan='1'>Indoor high trigger:</td>
<td><?php echo $high_date ?></td>
<td>
<?php
echo "<select name='high'>\n";
for ($x=$indoor_high_min;$x<=$indoor_high_max;$x++) {
  echo "<option ";
  if ($x == $high_temp) { echo "selected='selected' ";}
  echo "value='$x'>$x &deg;C</option>\n";
}
echo "</select>\n";
?>
</td></tr>
<tr class='row'><td colspan='3'></td></tr>

<tr class='row'>
<td colspan='1'>Password:</td>
<td colspan='1'><input name="pw1" type="password"></td>
<td><strong> 
  <?php system("$sheevaplug_cgi 1"); ?>
</strong></td></tr>


<tr><td colspan='3'>
<input type="submit" name="trigger" value="Set triggers">
</td></tr>

<tr><td colspan='3'></tr>
</tbody>
</table>
</form>
<p> </p>



<!-- relays -->
<form name='relays' method="POST" action="" enctype="multipart/form-data">

<table>
  <tr><td colspan="4" class='head'>Relay timers and control</td></tr>
  <tbody>
  <tr><td colspan="4"><input type='hidden' name='id' value='-1'></td></tr>
  <tr>
    <td class='onethird'><b>Type</b></td>
    <td class='onethird'><b>Hour : Minute (+Minute)</b></td>
    <td colspan = "2" class='onethird'><b>Action</b></td>
  </tr>

<?php
mysql_select_db($DBNAME, $DB);
$result = mysql_query($sql_relay) or die("MySQL error!");
while ($row = mysql_fetch_array($result)) {
  echo "<tr class='row'>\n";
  echo "<td";
  if (preg_match("/OFF/", $row["descr"])) { echo " class='relay_off'>"; }
  if (preg_match("/ON/", $row["descr"])) { echo " class='relay_on'>"; }
  echo $row["descr"];
  echo "</td><td colspan='1'>\n";
  print_timer($row["hour"], $row["minute"], $row["id"]);
  echo "</td>\n<td colspan='1'>";
  echo "<input type='submit' name='update' value='Update' onclick='submitform(" . $row["id"] .",5)' >";
  echo "</td>\n<td>";
  echo "<input type='submit' name='delete' value='Delete' onclick='submitform(" . $row["id"] .",6)' >";
  echo "</td></tr>\n\n";
}
mysql_free_result($result);

echo "<tr class='row'><td colspan='4'></td></tr>\n\n";

mysql_select_db($DBNAME, $DB);
$result = mysql_query($sql_new) or die("MySQL error!");

echo "<tr class='row'><td colspan='1'>\n";
echo "<select name='new'>\n";
while ($row = mysql_fetch_array($result)) {
  echo "   <option ";
  echo "value='" . $row["id"] . "'>" . $row["descr"] . "</option>\n";
}
mysql_free_result($result);

echo "</select></td>\n<td>";
print_timer(0,0,0);
echo "</td>\n<td>";
echo "<input type='submit' name='addnew' value='Add new' onclick='submitform(0,8)'></td>\n<td>";
echo "<input type='submit' name='executenow' value='Execute now' onclick='submitform(" . $row["id"] .",7)'></td>";
echo "</tr>\n";
echo "<tr class='row'><td colspan='4'></td></tr>\n\n";

?>
<tr class='row'>
  <td colspan='1'>Password:</td>
  <td colspan='1'><input name="pw2" type="password"></td>
  <td colspan='2'> <?php print "Sunrise / Sunset: $sunrise -> $sunset\n"; ?></td>
</tr>
</tbody></table>
</form>

<p> </p>



<!-- java-script submit script -->
<script type="text/javascript">
  function submitform(id, action) {  
    document.relays.id.value=id;
    if (action == 5) {  document.relays.update(); }
    if (action == 6) {  document.relays.delete(); }
    if (action == 7) {  document.relays.executenow(); }
    if (action == 8) {  document.relays.addnew(); }
  }
</script> 



<!-- Footer with link -->
<hr>
<p><a href=".">Main</a></p>
<p class="right">(c) Daniel Vindev&aring;g 2011-2013</p>
</BODY></HTML>



<!-- php functions -->
<?php
function print_timer($hour, $minute, $id) {
  echo "<select name='hour_$id'>\n";
  for ($x=0;$x<=23;$x++) {
    echo "   <option ";
    if ($x == $hour) { echo "selected='selected' ";}
    echo "value='$x'>$x</option>\n";
  }
  echo "</select>";

  $minute_1 = floor(($minute / 15)) * 15;
  $minute_2 = $minute - $minute_1;

  echo ": \n";
  echo "<select name='minute_$id'>\n";
  for ($x=0;$x<60;$x=$x+15) {
    echo "   <option ";
    if ($x == $minute_1) { echo "selected='selected' ";}
    printf("value='%d'>%02d</option>\n",$x,$x);
  }
  echo "</select>\n";

  echo "<select name='minute_plus_$id'>\n";
  for ($x=0;$x<15;$x++) {
    echo "   <option ";
    if ($x == $minute_2) { echo "selected='selected' ";}
    echo "value='$x'>+$x</option>\n";
  }
  echo "</select>\n";
}
?>