diff -r 69fadd1aded2 -r 5e2424bfbc4a www/upl_brew.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/www/upl_brew.php Wed Feb 27 14:39:25 2019 +0100 @@ -0,0 +1,123 @@ + 500000 && $uploadOk) { + echo "Fout 2: het bestand is te groot. "; + $uploadOk = 0; +} +// Allow certain file formats +if ($imageFileType != "json" && $uploadOk) { + echo "Fout 3: alleen JSON 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"]). "
"; +} else { + echo "Fout 4: er gieng iets fout met de upload."; + exit; +} + +$json= file_get_contents($target_file); +$brew = json_decode($json, true); +$records = count($brew); +if ($records != 1) { + echo "Fout 5: dit is geen JSON brouw logfile"; + exit; +} + +$connect = mysqli_connect(DBASE_HOST, DBASE_USER, DBASE_PASS, DBASE_NAME); +if (! $connect) { + echo "Fout 6: ".mysqli_connect_errno()." ".mysqli_connect_error(); + exit; +} +mysqli_set_charset($connect, "utf8" ); +date_default_timezone_set('Europe/Amsterdam'); + +foreach($brew as $brewdata) { + + foreach($brewdata as $item) { + $temps = $item['brewdata']; + $events = $item['annotations']; + $date = date("Y-m-d H:i:s" , strtotime($item['Date'])); + $code = strtok($item['Recipe'], " "); + $name = strtok('\0'); + $insert = 0; + $update = 0; + $delete = 0; + + $sql = "SELECT uuid FROM products WHERE code='".$code."';"; + $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); + if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { + $uuid = $row['uuid']; + } + echo "Brouw: ".$code." ".$name."
"; + + $sql = "DELETE FROM log_brews WHERE code='".$code."';"; + $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); + $delete += mysqli_affected_rows($connect); + + foreach($temps as $temp) { + // Create full datetime from the Label + $t = floatval(substr($temp['Label'], 0, 2)) * 60 + floatval(substr($temp['Label'], 3, 2)); + $time = new DateTime($date); + $time->add(new DateInterval('PT' . $t . 'M')); + $stamp = $time->format('Y-m-d H:i:s'); + + $sql = "INSERT INTO log_brews SET version='2"; + $sql .= "', datetime='" . $stamp; + $sql .= "', uuid='" . $uuid; + $sql .= "', code='" . $code; + $sql .= "', name='" . mysqli_real_escape_string($connect, $name); + $sql .= "', pv_mlt='" . $temp['MLT_pv']; + $sql .= "', sp_mlt='" . $temp['MLT_sp']; + $sql .= "', pwm_mlt='" . $temp['MLT_pwm']; + if (isset($temp['HLT_pv'])) + $sql .= "', pv_hlt='" . $temp['HLT_pv']; + if (isset($temp['HLT_sp'])) + $sql .= "', sp_hlt='" . $temp['HLT_sp']; + if (isset($temp['HLT_pwm'])) + $sql .= "', pwm_hlt='" . $temp['HLT_pwm']; + $sql .= "';"; + $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); + $insert += mysqli_affected_rows($connect); + } + + // The events are annotations in the JSON file. + foreach($events as $event) { + $t = floatval(substr($event['value'], 0, 2)) * 60 + floatval(substr($event['value'], 3, 2)); + $time = new DateTime($date); + $time->add(new DateInterval('PT' . $t . 'M')); + $stamp = $time->format('Y-m-d H:i:s'); + + $sql = "UPDATE log_brews SET event='".mysqli_real_escape_string($connect, $event['label']['content'])."' WHERE "; + $sql .= "datetime='".$stamp."' AND uuid='".$uuid."';"; + $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); + $update += mysqli_affected_rows($connect); + } + + $sql = "UPDATE products SET log_brew='1' WHERE code='".$code."';"; + $result = mysqli_query($connect, $sql) or die("SQL Error 1: " . mysqli_error($connect)); + $update += mysqli_affected_rows($connect); + echo "Database delete: ".$delete.", insert: ".$insert.", update: ".$update." records." . PHP_EOL; + } +} +// Don't clutter the upload directory. +unlink($target_file); + +?>