<?php
// Detect the host computer where this script is running
if (getHostByName(getHostName()) == '192.168.2.50' ) {
$servername = "192.168.2.50";
$username = "*****";
$password = "*****";
$dbname = "be";
$selectionDate = date('Y-m-d H:i:s', time()) ;
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
switch($_GET['interval']) {
case 'minute':
$sql = "select datum, cur_used, rate from be.meter" .
" where datum > subdate('" . $selectionDate . "', interval 1 MINUTE)" .
" order by datum asc";
break;
case 'fiveminutes':
$sql = "select datum, avg(cur_used) as cur_used, max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 5 MINUTE) " .
"group by date(datum), hour(datum), minute(datum), second(datum) DIV 5 " .
"order by datum asc";
break;
case 'tenminutes':
$sql = "select datum, avg(cur_used) as cur_used, max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 10 MINUTE) " .
"group by date(datum), hour(datum), minute(datum), second(datum) DIV 10 " .
"order by datum asc";
break;
case 'thirtyminutes':
$sql = "select datum, avg(cur_used) as cur_used, max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 30 MINUTE) " .
"group by date(datum), hour(datum), minute(datum), second(datum) DIV 20 " .
"order by datum asc";
break;
case 'hour':
$sql = "select datum, avg(cur_used) as cur_used, max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 60 MINUTE) " .
"group by date(datum), hour(datum), minute(datum) " .
"order by datum asc";
break;
case 'fourhours':
$sql = "select datum,avg(cur_used) as cur_used,max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 240 MINUTE) " .
"group by date(datum), hour(datum), minute(datum) " .
"order by datum asc";
break;
case '12hours':
$sql = "select datum,avg(cur_used) as cur_used,max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 720 MINUTE) " .
"group by date(datum), hour(datum), minute(datum) DIV 3 " .
"order by datum asc";
break;
case '24hours':
$sql = "select datum,avg(cur_used) as cur_used,max(rate) as rate from be.meter " .
"where datum > subdate('" . $selectionDate . "', interval 1440 MINUTE) " .
"group by date(datum), hour(datum), minute(datum) DIV 8 " .
"order by datum asc";
break;
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonObject->Datum = $row["datum"];
$jsonObject->Cur_Used = $row["cur_used"];
$jsonObject->Rate = $row["rate"];
array_push($jsonArray, $jsonObject);
unset($jsonObject);
};
echo json_encode($jsonArray);
} else {
echo "0 results";
}
$conn->close();
?>