www/getfermentlog.php

changeset 194
d202777ebae5
parent 179
8adaeecd9dc8
child 296
69fadd1aded2
--- a/www/getfermentlog.php	Sat Jan 19 20:42:11 2019 +0100
+++ b/www/getfermentlog.php	Mon Jan 21 20:45:18 2019 +0100
@@ -3,57 +3,116 @@
 require_once('config.php');
 
 if (isset($_GET["code"]))
-	$code = $_GET["code"];
+	$code = $_GET["code"] . ' ' . $_GET["name"];
 else
-	$code = "TB0015";
+	$code = "TB0015 Winterbier-2";
 
-$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;
+$filename = 'log/fermentation/' . $code . '.log';
+/*
+ * From Stackoverflow, the fastest way to count the lines in a file.
+ */
+$file = new \SplFileObject($filename, 'r');
+$file->seek(PHP_INT_MAX);
+// $file->key() = lines in the file.
+
+define ('MAX_INTERVALS', 10);
+$GRAPH_INTERVAL = array ( 0, 1, 5, 15, 30, 60, 120, 240, 480, 720 );
+$GRAPH_DATALINES = array ( 0, 800, 3200, 12000, 24000, 48000, 96000, 192000, 384000, 768000 );
+
+
+for ($graphstep = 1; $graphstep <= MAX_INTERVALS; $graphstep++) {
+	if ($file->key() < $GRAPH_DATALINES[$graphstep])
+		break;
 }
-$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)) {
+if ($graphstep > MAX_INTERVALS)
+	$graphstep = MAX_INTERVALS;
+
+//echo $code . '.log lines=' . $file->key() . ' interval=' . $GRAPH_INTERVAL[$graphstep] . ' graphstep=' . $graphstep . PHP_EOL;
 
-	$dt = new DateTime($row['datetime'], new DateTimeZone('Europe/Amsterdam'));
-	$ts = $dt->getTimestamp();
-	$diff = $ts - $ld;
+/*
+ * 2014-11-15 18:39,BEER,PRIMARY,20.312,19.750,-1.500,20.5,18.6,18.8,35,12345,0,67890,Whatsup
+ *          |         |     |      |      |      |      |    |    |   |   |   |   |      |
+ * datetime +         |     |      |      |      |      |    |    |   |   |   |   |      |
+ * mode --------------+     |      |      |      |      |    |    |   |   |   |   |      |
+ * stage -------------------+      |      |      |      |    |    |   |   |   |   |      |
+ * temp air -----------------------+      |      |      |    |    |   |   |   |   |      |
+ * temp beer -----------------------------+      |      |    |    |   |   |   |   |      |
+ * temp chiller ---------------------------------+      |    |    |   |   |   |   |      |
+ * temp room -------------------------------------------+    |    |   |   |   |   |      |
+ * setpoint low ---------------------------------------------+    |   |   |   |   |      |
+ * setpoint high -------------------------------------------------+   |   |   |   |      |
+ * heater power ------------------------------------------------------+   |   |   |      |
+ * heater usage ----------------------------------------------------------+   |   |      |
+ * cooler power --------------------------------------------------------------+   |      |
+ * cooler usage ------------------------------------------------------------------+      |
+ * event --------------------------------------------------------------------------------+
+ */
+
+$heater_l = 0;
+$cooler_l = 0;
+
+$handle = @fopen($filename, "r");
+if ($handle) {
+	$lines = 0;
+	while (($buffer = fgets($handle, 4096)) !== false) {
+
+		$buffer = preg_replace( "/\r|\n/", "", $buffer);
+		$row = explode(",", $buffer);
+		if ($row[1] != 'Mode') {
 
-	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;
+			$hr = intval(substr($buffer, 11, 2));
+			if ((($graphstep == 1)) ||
+			    (($graphstep == 2) && ((substr($buffer, 15, 1) == '0') || (substr($buffer, 15, 1) == '5'))) ||
+			    (($graphstep == 3) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '15') ||
+						   (substr($buffer, 14, 2) == '30') || (substr($buffer, 14, 2) == '45'))) ||
+			    (($graphstep == 4) && ((substr($buffer, 14, 2) == '00') || (substr($buffer, 14, 2) == '30'))) ||
+			    (($graphstep == 5) && (substr($buffer, 14, 2) == '00')) ||
+			    (($graphstep == 6) && (substr($buffer, 14, 2) == '00') && (($hr % 2) == 0)) ||
+			    (($graphstep == 7) && (substr($buffer, 14, 2) == '00') && (($hr % 4) == 0)) ||
+			    (($graphstep == 8) && (substr($buffer, 14, 2) == '00') && (($hr % 8) == 0)) ||
+			    (($graphstep == 9) && (substr($buffer, 14, 2) == '00') && (($hr % 12) == 0))) {
+//				echo $hr . ' ' . $row[1] . ' ' . $buffer;
+				$lines++;
+
+				$heat_used = $cool_used = 0;
+				if ($row[10] && ($row[10] != "NA") && ($heater_l > 0)) {
+					$heat_used = round((intval($row[10]) - $heater_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
+				}
+				if ($row[12] && ($row[12] != "NA") && ($cooler_l > 0)) {
+					$cool_used = round((intval($row[12]) - $cooler_l) * 100 / ($GRAPH_INTERVAL[$graphstep] * 60));
+				}
+
+
+//				echo $GRAPH_INTERVAL[$graphstep] . ' ' . $heat_used . ' ' . $cool_used . PHP_EOL;
+
+				if (($heat_used <= 100) && ($cool_used <= 100)) {
+					$logs[] = array(
+						'date' => $row[0],
+						'air' => $row[3],
+						'beer' => $row[4],
+						'chiller' => $row[5],
+						'room' => $row[6],
+						'tlo' => $row[7],
+						'thi' => $row[8],
+						'heater' => $heat_used,
+						'cooler' => $cool_used,
+						'event' => $row[13]
+					);
+				}
+
+				if ($row[10] && ($row[10] != "NA"))
+					$heater_l = intval($row[10]);
+				if ($row[12] && ($row[12] != "NA"))
+					$cooler_l = intval($row[12]);
+			}
+		}
+
 	}
+	if (!feof($handle)) {
+		echo "Error: unexpected fgets() fail\n";
+	}
+	fclose($handle);
 }
+
 echo json_encode($logs);
+//echo $lines . ' lines' . PHP_EOL;

mercurial