www/upl_hops.php

Mon, 18 May 2020 11:00:59 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 18 May 2020 11:00:59 +0200
changeset 679
48f8f3fce7c0
parent 311
f6fafccd8a6d
child 785
aa79acfdf8a9
permissions
-rw-r--r--

Added reconnecting-websocket.js to automatic reconnect the websocket if the connection is lost. Usefull for mobile devices that go to sleep after a while. Changed mon_fermenters to use websockets instead of polling. Fixed wrong temperature color ranges on the fermenter monior. Increased the websocket receive buffer to 2048. In cannot overflow, but larger messages are chunked and the application does not handle these split messages. Needs termferm 0.9.9 or newer.

<?php
require_once('config.php');
require("version.php");
require("includes/formulas.php");


$target_dir = "tmp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
	echo "Fout 1: bestand bestaat al. ";
	$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000 && $uploadOk) {
	echo "Fout 2: het bestand is te groot. ";
	$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "xml" && $uploadOk) {
	echo "Fout 3: alleen XML bestanden toegestaan. ";
	$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
	exit;
}

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
	echo "Verwerken bestand ". basename( $_FILES["fileToUpload"]["name"]). "<br />";
} else {
	echo "Fout 4: er ging iets fout met de upload.";
	exit;
}


$db = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME);
if (! $db) {
	echo "Fout 5: ".mysqli_connect_errno()." ".mysqli_connect_error();
	exit;
}
mysqli_set_charset($db, "utf8" );
date_default_timezone_set('Europe/Amsterdam');


$imported = 0;
$hops = simplexml_load_file($target_file);


foreach ($hops->HOP as $hop) {

	$sql  = "INSERT INTO inventory_hops SET name='" . mysqli_real_escape_string($db, $hop->NAME);
	if ($hop->ALPHA)
		$sql .= "', alpha='" . floatval($hop->ALPHA);
	if ($hop->BETA)
		$sql .= "', beta='" . floatval($hop->BETA);
	if ($hop->HUMULENE)
		$sql .= "', humulene='" . floatval($hop->HUMULENE);
	if ($hop->CAROPHYLLENE)
		$sql .= "', caryophyllene='" . floatval($hop->CAROPHYLLENE);
	if ($hop->COHUMULONE)
		$sql .= "', cohumulone='" . floatval($hop->COHUMULONE);
	if ($hop->MYRCENE)
		$sql .= "', myrcene='" . floatval($hop->MYRCENE);
	if ($hop->HSI)
		$sql .= "', hsi='" . floatval($hop->HSI);

	if ($hop->TYPE == 'Bittering')
		$sql .= "', type='0";
	else if ($hop->TYPE == 'Aroma')
		$sql .= "', type='1";
	else if ($hop->TYPE == 'Both')
		$sql .= "', type='2";
	else
		echo "Unknown TYPE " . $hop->TYPE . "<br />";

	if ($hop->FORM == 'Pellet')
		$sql .= "', form='0";
	else if ($hop->FORM == 'Plug')
		$sql .= "', form='1";
	else if ($hop->FORM == 'Leaf')
		$sql .= "', form='2";
	else
		echo "Unknown FORM " . $hop->FORM . "<br />";

	$sql .= "', notes='" . mysqli_real_escape_string($db, $hop->NOTES);
	$sql .= "', origin='" . mysqli_real_escape_string($db, $hop->ORIGIN);
	$sql .= "', substitutes='" . mysqli_real_escape_string($db, $hop->SUBSTITUTES);
	($hop->ALWAYS_ON_STOCK == 'TRUE') ? $sql .= "', always_on_stock='1" : $sql .= "', always_on_stock='0";
	if ($hop->INVENTORY)
		$sql .= "', inventory='" . floatval($hop->INVENTORY) / 1000.0;
	if ($hop->COST)
		$sql .= "', cost='" . floatval($hop->COST);
	if ($hop->TOTAL_OIL)
		$sql .= "', total_oil='" . floatval($hop->TOTAL_OIL);
	if ($hop->HARVEST_DATE) {
		$date = substr($hop->HARVEST_DATE, 6, 4).'-'.substr($hop->HARVEST_DATE, 3, 2).'-'.substr($hop->HARVEST_DATE, 0, 2);
		$sql .= "', production_date='" . $date;
	}
	$sql .= "';";
	if (! $result = mysqli_query($db, $sql)) {
		echo "Fout 8: " . mysqli_error($db) . "<br />";
	} else {
		echo "+ " . $hop->NAME . "<br />";
		$imported++;
	}
}

if ($imported == 0) {
	echo "Fout 7: geen hoppen in dit bestand.<br />";
} else {
	echo $imported . " hoppen toegevoegd.<br />";
}



// Don't clutter the upload directory.
unlink($target_file);

?>

mercurial