www/includes/db_profile_fermentation.php

Tue, 31 Aug 2021 20:48:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 31 Aug 2021 20:48:37 +0200
changeset 774
92e1e8f175a2
parent 768
ae1195153fa2
permissions
-rw-r--r--

Split batch, adjust mash step volume. In the duplicated log_brew handle the missing values. In save product, round the mash step sg to 4 decimals. In prod_edit, ingredients are stored as strings, not arrays. This triggered a memory corruption that only happened in rare circumstances. Don't fix mash step fields in the javascript, it is already done during load from the database. Calculation of the mash volume is rounded to 6 decimals. Enter mash step Brix/Plato value, the SG result is rounded to 4 decimals.

<?php

require($_SERVER['DOCUMENT_ROOT']."/config.php");
require($_SERVER['DOCUMENT_ROOT']."/version.php");

#Connect to the database
$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $connect) {
	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8" );

$response = array(
   'error' => false,
   'msg' => 'Ok',
);

$rescapers = array("'");
$rreplacements = array("\\'");

if (isset($_POST['insert']) || isset($_POST['update'])) {
	if (isset($_POST['insert'])) {
		$sql  = "INSERT INTO `profile_fermentation` SET ";
	}
	if (isset($_POST['update'])) {
		$sql  = "UPDATE `profile_fermentation` SET ";
	}

	if (isset($_POST['uuid']) && (strlen($_POST['uuid']) == 36)) {
		$sql .= "uuid='" . $_POST['uuid'];
	} else {
		$uuid = str_replace("\n", "", file_get_contents('/proc/sys/kernel/random/uuid'));
		$sql .= "uuid='" . $uuid;
	}

	$sql .= "', name='" . mysqli_real_escape_string($connect, $_POST['name']);
	$sql .= "', inittemp_lo='" . floatval($_POST['inittemp_lo']);
	$sql .= "', inittemp_hi='" . floatval($_POST['inittemp_hi']);
	($_POST['fridgemode'] == 'true') ? $sql .= "', fridgemode='1" : $sql .= "', fridgemode='0";
	$array = $_POST['steps'];
	// Don't believe given duration and number of steps, recalculate.
	$duration = 0;
	$totalsteps = 0;
	$steps = '[';
	foreach($array as $key => $item) {
		if ($totalsteps > 0)
			$steps.= ',';
		$totalsteps++;
		$duration += $item['steptime'] + $item['resttime'];
		$steps .= '{"name":"' . str_replace($rescapers,$rreplacements,$item['name']);
		$steps .= '","steptime":' . $item['steptime'];
		$steps .= ',"resttime":' . $item['resttime'];
		$steps .= ',"target_lo":' . $item['target_lo'];
		$steps .= ',"target_hi":' . $item['target_hi'];
		$steps .= ',"fridgemode":' . $item['fridgemode'] . '}';
	}
	$steps .= ']';
	$sql .= "', totalsteps='" . $totalsteps;
	$sql .= "', duration='" . $duration;
	//syslog(LOG_NOTICE, $steps);
	$sql .= "', steps='" . str_replace($rescapers,$rreplacements,json_encode($array));
	if (isset($_POST['insert'])) {
		$sql .= "';";
	}
	if (isset($_POST['update'])) {
		$sql .= "' WHERE record='" . $_POST['record'] . "';";
	}
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_profile_fermentation: ".$sql." result: ".mysqli_error($connect));
                $response['error'] = true;
                $response['msg'] = "SQL fout: ".mysqli_error($connect);
        }
	exit(json_encode($response));

} else if (isset($_POST['delete'])) {
	// DELETE COMMAND
	$sql = "DELETE FROM `profile_fermentation` WHERE record='".$_POST['record']."';";
	$result = mysqli_query($connect, $sql);
	if (! $result) {
		syslog(LOG_NOTICE, "db_profile_fermentation: ".$sql." result: ".mysqli_error($connect));
                $response['error'] = true;
                $response['msg'] = "SQL fout: ".mysqli_error($connect);
        }
	exit(json_encode($response));

} else {
	// SELECT COMMAND
	$query = "SELECT record,JSON_QUOTE(uuid),JSON_QUOTE(name),inittemp_lo,inittemp_hi,fridgemode,totalsteps,duration,steps ";
	$query .= "FROM profile_fermentation ORDER BY name;";
	$result = mysqli_query($connect, $query) or die("SQL Error 1: " . mysqli_error($connect));
	$profiles = '[';
	$comma = FALSE;
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		// Manual encode to JSON.
		if ($comma) {
			$profiles .= ',';
		}
		$comma = TRUE;
		$profiles .= '{"record":' . $row['record'];
		$profiles .= ',"uuid":' . $row['JSON_QUOTE(uuid)'];
		$profiles .= ',"name":'  . $row['JSON_QUOTE(name)'];
		$profiles .= ',"inittemp_lo":' . $row['inittemp_lo'];
		$profiles .= ',"inittemp_hi":' . $row['inittemp_hi'];
		$profiles .= ',"fridgemode":' . $row['fridgemode'];
		$profiles .= ',"totalsteps":' . $row['totalsteps'];
		$profiles .= ',"duration":' . $row['duration'];
		$profiles .= ',"steps":' . $row['steps'];
		$profiles .= '}';
	}
	$profiles .= ']';
	header("Content-type: application/json");
	exit($profiles);
}

syslog(LOG_NOTICE, "db_profile_fermentation: missing arguments");
$response['error'] = true;
$response['msg'] = "missing arguments";
echo json_encode($response);

?>

mercurial