www/getfermentlog.php

changeset 179
8adaeecd9dc8
parent 172
684c651dd165
child 194
d202777ebae5
--- a/www/getfermentlog.php	Sun Jan 06 23:30:53 2019 +0100
+++ b/www/getfermentlog.php	Mon Jan 07 12:46:27 2019 +0100
@@ -5,26 +5,55 @@
 if (isset($_GET["code"]))
 	$code = $_GET["code"];
 else
-	$code = "CB0068";
+	$code = "TB0015";
 
-$query = "SELECT datetime,temperature_air,temperature_beer,temperature_chiller,temperature_room,target_low,target_high,heater_power,cooler_power FROM log_fermentation WHERE product_code='".$code."'";
+$query = "SELECT datetime,temperature_air,temperature_beer,temperature_chiller,temperature_room,target_low,target_high,heater_usage,cooler_usage FROM log_fermentation WHERE product_code='".$code."'";
 $connect = mysqli_connect(DBASE_HOST,DBASE_USER,DBASE_PASS,DBASE_NAME);
 if (! $connect) {
 	echo "[]";
 	return;
 }
+$step = 600; // Must be a multiple of 300
+$ld = 0;
+$lh = 0;
+$ch = 0;
+$lc = 0;
+$cc = 0;
 $result = mysqli_query($connect, $query);
 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
-	$logs[] = array(
-		'date' => $row['datetime'],
-		'air' => $row['temperature_air'],
-		'beer' => $row['temperature_beer'],
-		'chiller' => $row['temperature_chiller'],
-		'room' => $row['temperature_room'],
-		'tlo' => $row['target_low'],
-		'thi' => $row['target_high'],
-		'heater' => $row['heater_power'],
-		'cooler' => $row['cooler_power']
-	);
+
+	$dt = new DateTime($row['datetime'], new DateTimeZone('Europe/Amsterdam'));
+	$ts = $dt->getTimestamp();
+	$diff = $ts - $ld;
+
+	if ($diff < $step) {
+		// Read on
+	} else if ($diff > $step) {
+		// Reset, out of sync.
+		$ld = $ts;
+		$lh = $ch = $row['heater_usage'];
+		$lc = $cc = $row['cooler_usage'];
+	} else {
+		// This is a valid timeframe.
+		$ch = $row['heater_usage'];
+		$cc = $row['cooler_usage'];
+		$heat = round(($ch - $lh) / ($step / 100));
+		$cool = round(($cc - $lc) / ($step / 100));
+//		echo $heat . ' ' . $cool . PHP_EOL;
+		$logs[] = array(
+			'date' => $row['datetime'],
+			'air' => $row['temperature_air'],
+			'beer' => $row['temperature_beer'],
+			'chiller' => $row['temperature_chiller'],
+			'room' => $row['temperature_room'],
+			'tlo' => $row['target_low'],
+			'thi' => $row['target_high'],
+			'heater' => $heat,
+			'cooler' => $cool
+		);
+		$ld = $ts;
+		$lh = $ch;
+		$lc = $cc;
+	}
 }
 echo json_encode($logs);

mercurial