From ea0c7f08c183c659cf5e2b5c0ef56110c3afee97 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 22 Dec 2024 09:36:46 +0000 Subject: [PATCH] fixing bugs in reservation system, fixing error messages, checking if print has been started from other location --- sys0-code/api/fetch_printer_data.php | 21 +- sys0-code/app/print.php | 383 +++++++++++++++------------ 2 files changed, 229 insertions(+), 175 deletions(-) diff --git a/sys0-code/api/fetch_printer_data.php b/sys0-code/api/fetch_printer_data.php index 100152a..6f5fcda 100644 --- a/sys0-code/api/fetch_printer_data.php +++ b/sys0-code/api/fetch_printer_data.php @@ -83,9 +83,28 @@ while (mysqli_stmt_fetch($stmt)) { $printer["print_status"]="Abgebrochen"; $printer["full_file"]=$json["job"]["file"]["name"]; $printer["view"]=2; - }else if($system_status==0){ + }/*else if($system_status==0){ $printer["print_status"]="Bereit"; $printer["view"]=3; + }*/else if(($is_free == 1 && $system_status==0) or $system_status==99){ //check if a print has been started from another location + exec("curl --max-time 10 $url/api/job?apikey=$apikey > /var/www/html/user_files/" . $_SESSION["username"] . "/json.json"); + $fg = file_get_contents("/var/www/html/user_files/" . $_SESSION["username"] . "/json.json"); + $json = json_decode($fg, true); + if($json['state']=="Printing" or $system_status==99){ + $printer["printer_status"]="Von anderer Quelle aus gestartet."; + $printer["progress"] = (int) $json['progress']['completion']; + $printer["file"] = short_path($json["job"]["file"]["name"], 10, 10); + $printer["full_file"]=$json["job"]["file"]["name"]; + $printer["print_time_total"] = seconds_to_time(intval($json["job"]["estimatedPrintTime"])); + $printer["print_time_left"] = seconds_to_time(intval($json["progress"]["printTimeLeft"])); + $printer["print_time"] = seconds_to_time(intval($json["progress"]["printTime"])); + $printer["view"]=0; + //insert into db that this one is printing + $sql="UPDATE printer SET system_status=99 WHERE id = $printer_id"; + }else{ + $printer["print_status"]="Bereit"; + $printer["view"]=3; + } }else{ $printer["print_status"]="Problem / Nicht betriebsbereit"; $printer["view"]=4; diff --git a/sys0-code/app/print.php b/sys0-code/app/print.php index 7765026..ff510c6 100644 --- a/sys0-code/app/print.php +++ b/sys0-code/app/print.php @@ -1,4 +1,5 @@ = $startTimestamp || $checkTimestamp <= $endTimestamp); - } else { - // Normal case: check if the time is between start and end time - return ($checkTimestamp >= $startTimestamp && $checkTimestamp <= $endTimestamp); +function check_reservation_conflict($link, $class) { + $reservation_conflict = false; + $today = date("Y-m-d"); + $time_now = date("H:i"); + $for_class = []; + + // Query for reservations that start today or extend into today + $sql = " + SELECT day, time_from, time_to, for_class + FROM reservations + WHERE day <= '$today' AND + (day = '$today' AND time_from <= '$time_now' OR day < '$today'); + "; + $stmt = $link->prepare($sql); + $stmt->execute(); + $result = $stmt->get_result(); + + while ($row = $result->fetch_assoc()) { + // Calculate the actual end time of the reservation + $reservation_end = strtotime($row["day"] . " " . $row["time_to"]); + $current_time = strtotime("$today $time_now"); + + if ($current_time <= $reservation_end) { + $reservation_conflict = true; + $for_class[] = $row["for_class"]; + } } + + // Default value for for_class if no conflicts are found + if (empty($for_class)) { + $for_class[] = 0; + } + + // Determine the appropriate response based on the conflict status + $response = ['conflict' => $reservation_conflict, 'block' => false, 'message' => '']; + + if ($reservation_conflict && !in_array($class, $for_class) && $class != 0) { + $response['block'] = true; + $response['message'] = " +
+ +
"; + } elseif ($class == 0 && $reservation_conflict) { + $response['message'] = " +
+ +
"; + } + + return $response; } -function time_to_seconds($print_time) { - $seconds = 0; +function check_print_reservation_conflict($link, $class, $path) { + $reservation_conflict = false; + $for_class = []; + $today = date("Y-m-d"); + $time_now = date("H:i"); - if (preg_match('/(\d+)h/', $print_time, $matches)) { - $seconds += $matches[1] * 3600; // hours to seconds - } - if (preg_match('/(\d+)m/', $print_time, $matches)) { - $seconds += $matches[1] * 60; // minutes to seconds - } - if (preg_match('/(\d+)s/', $print_time, $matches)) { - $seconds += $matches[1]; // seconds + // Calculate the end time of the print + $print_time = find_print_time($path); // Assume this function is already defined + preg_match('/(\d+)h/', $print_time, $hours_match); + preg_match('/(\d+)m/', $print_time, $minutes_match); + $hours = isset($hours_match[1]) ? (int)$hours_match[1] : 0; + $minutes = isset($minutes_match[1]) ? (int)$minutes_match[1] : 0; + //echo("uses ".$minutes." Minutes and ".$hours." hours"); + $start_time = DateTime::createFromFormat('H:i', $time_now); + $end_time = clone $start_time; + $end_time->modify("+{$hours} hour"); + $end_time->modify("+{$minutes} minutes"); + + // Query to get all relevant reservations (today and future overlaps) + $sql = " + SELECT day, time_from, time_to, for_class + FROM reservations + WHERE day >= '$today'; + "; + $stmt = $link->prepare($sql); + $stmt->execute(); + $result = $stmt->get_result(); + + // Check for conflicts with reservations + while ($row = $result->fetch_assoc()) { + $reservation_start = DateTime::createFromFormat('Y-m-d H:i', $row["day"] . ' ' . $row["time_from"]); + $reservation_end = DateTime::createFromFormat('Y-m-d H:i', $row["day"] . ' ' . $row["time_to"]); + + // Adjust reservation end time for multi-day overlaps + if ($reservation_end < $reservation_start) { + $reservation_end->modify('+1 day'); + } + + // Check if the print overlaps with any reservation period + if ($start_time < $reservation_end && $end_time > $reservation_start) { + $reservation_conflict = true; + $for_class[] = $row["for_class"]; + } } - return $seconds; + // Default value for for_class if no conflicts are found + if (empty($for_class)) { + $for_class[] = 0; + } + + // Build response based on conflict and user access + $response = ['conflict' => $reservation_conflict, 'block' => false, 'message' => '']; + + if ($reservation_conflict && !in_array($class, $for_class) && $class != 0) { + $response['block'] = true; + $response['message'] = " +
+ +
"; + } elseif ($class == 0 && $reservation_conflict) { + $response['message'] = " +
+ +
"; + } + + return $response; } - ?> - ");?> +
Datei drucken - -

"); + $global_err="Dieser Dateityp wird nicht unterstüzt."; sys0_log("Could not upload file for ".$_SESSION["username"]." because of unknown file extension",$_SESSION["username"],"PRINT::UPLOAD::FILE::FAILED");//notes,username,type } else @@ -175,6 +277,7 @@ function time_to_seconds($print_time) { mysqli_stmt_execute($stmt); echo("
"); + $global_success="Datei ". basename( $_FILES['file_upload']['name']). " wurde hochgeladen und an die Warteschlange gesendet"; sys0_log("user ".$_SESSION["username"]." uploaded ".basename($path)." to the queue",$_SESSION["username"],"PRINT::UPLOAD::QUEUE");//notes,username,type } else @@ -196,7 +299,7 @@ function time_to_seconds($print_time) { mysqli_stmt_execute($stmt); - echo("
"); + echo("
"); sys0_log("user ".$_SESSION["username"]." uploaded ".basename($path)." to the queue",$_SESSION["username"],"PRINT::UPLOAD::QUEUE"); } @@ -212,7 +315,6 @@ function time_to_seconds($print_time) { mysqli_stmt_fetch($stmt); if($free!=1 or $status!=0) { - echo("
"); sys0_log("Could not start job for ".$_SESSION["username"]." with file ".basename($path)."",$_SESSION["username"],"PRINT::JOB::START::FAILED");//notes,username,type exit; @@ -246,59 +348,16 @@ function time_to_seconds($print_time) { if(true){ mysqli_stmt_close($stmt); if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $path)) { - echo("
"); - echo("
"); - $print_time=find_print_time($path); - //check if the printers are reserved at the time when this print finishes - date_default_timezone_set('Europe/Zurich'); - $reservation_conflict = false; - $today = date("Y-m-d"); - $time_now = date("H:i"); - - preg_match('/(\d+)h/', $print_time, $hours_match); - preg_match('/(\d+)m/', $print_time, $minutes_match); - $hours = isset($hours_match[1]) ? (int)$hours_match[1] : 0; - $minutes = isset($minutes_match[1]) ? (int)$minutes_match[1] : 0; - - // Convert $now into a DateTime object and add hours and minutes - $time = DateTime::createFromFormat('H:i', $time_now); - $time->modify("+{$hours} hour"); - $time->modify("+{$minutes} minutes"); - - // Format $time back into a string - $time_now = $time->format('H:i'); - - // Query to get reservations for the current day - $sql = "SELECT time_from, time_to, for_class FROM reservations WHERE day='$today';"; - $stmt = $link->prepare($sql); - $stmt->execute(); - $result = $stmt->get_result(); - - while ($row = $result->fetch_assoc()) { - // Check for overlap with the entire print time period - if (is_time_between($row["time_from"], $row["time_to"], $time_now)) { - $reservation_conflict = true; - $for_class[] = $row["for_class"]; - } - } - - // Ensure $for_class is set if no conflicts were found - if (!isset($for_class)) { - $for_class[] = 0; - } - - // Display conflict message if necessary - if ($reservation_conflict && !in_array($class, $for_class) && $class != 0) { - $block = true; - } else { - $block = false; - } + //echo("
"); + //echo("
"); + $response=check_print_reservation_conflict($link,$_SESSION["class_id"],$path); + $block=$response['block']; if($block==false){ if(check_file($path) or isset($_POST["ignore_unsafe"])){ exec('curl -k -H "X-Api-Key: '.$apikey.'" -F "select=true" -F "print=true" -F "file=@'.$path.'" "'.$printer_url.'/api/files/local" > /var/www/html/user_files/'.$username.'/json.json'); //file is on printer and ready to be printed $userid=$_SESSION["id"]; - echo("
"); + // echo("
"); sys0_log("user ".$_SESSION["username"]." uploaded ".basename($path)." to printer ".$_POST["printer"]."",$_SESSION["username"],"PRINT::UPLOAD::PRINTER");//notes,username,type $fg=file_get_contents("/var/www/html/user_files/$username/json.json"); $json=json_decode($fg,true); @@ -317,6 +376,7 @@ function time_to_seconds($print_time) { $stmt = mysqli_prepare($link, $sql); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); + echo("
"); } }else{ $warning=true; @@ -353,57 +413,15 @@ function time_to_seconds($print_time) { if(true){ mysqli_stmt_close($stmt); - echo("
"); - $print_time=find_print_time($path); - date_default_timezone_set('Europe/Zurich'); - $reservation_conflict = false; - $today = date("Y-m-d"); - $time_now = date("H:i"); - - preg_match('/(\d+)h/', $print_time, $hours_match); - preg_match('/(\d+)m/', $print_time, $minutes_match); - $hours = isset($hours_match[1]) ? (int)$hours_match[1] : 0; - $minutes = isset($minutes_match[1]) ? (int)$minutes_match[1] : 0; - - // Convert $now into a DateTime object and add hours and minutes - $time = DateTime::createFromFormat('H:i', $time_now); - $time->modify("+{$hours} hour"); - $time->modify("+{$minutes} minutes"); - - // Format $time back into a string - $time_now = $time->format('H:i'); - - // Query to get reservations for the current day - $sql = "SELECT time_from, time_to, for_class FROM reservations WHERE day='$today';"; - $stmt = $link->prepare($sql); - $stmt->execute(); - $result = $stmt->get_result(); - - while ($row = $result->fetch_assoc()) { - // Check for overlap with the entire print time period - if (is_time_between($row["time_from"], $row["time_to"], $time_now)) { - $reservation_conflict = true; - $for_class[] = $row["for_class"]; - } - } - - // Ensure $for_class is set if no conflicts were found - if (!isset($for_class)) { - $for_class[] = 0; - } - - // Display conflict message if necessary - if ($reservation_conflict && !in_array($class, $for_class) && $class != 0) { - $block = true; - } else { - $block = false; - } + //echo("
"); + $response=check_print_reservation_conflict($link,$_SESSION["class_id"],$path); + $block=$response['block']; if($block==false){ if(check_file($path) or isset($_POST["ignore_unsafe"])){ exec('curl -k -H "X-Api-Key: '.$apikey.'" -F "select=true" -F "print=true" -F "file=@'.$path.'" "'.$printer_url.'/api/files/local" > /var/www/html/user_files/'.$username.'/json.json'); //file is on printer and ready to be printed $userid=$_SESSION["id"]; - echo("
"); + //echo("
"); sys0_log("user ".$_SESSION["username"]." uploaded ".basename($path)." to printer ".$_POST["printer"]."",$_SESSION["username"],"PRINT::UPLOAD::PRINTER");//notes,username,type $fg=file_get_contents("/var/www/html/user_files/$username/json.json"); $json=json_decode($fg,true); @@ -415,13 +433,14 @@ function time_to_seconds($print_time) { else { $sql="update printer set free=0, printing=1,mail_sent=0, used_by_userid=$userid where id=$printer_id"; - $stmt = mysqli_prepare($link, $sql); + $stmt = mysqli_prepare($link, $sql); mysqli_stmt_execute($stmt); //delete printer key: $sql="DELETE from print_key where print_key='$print_key'"; $stmt = mysqli_prepare($link, $sql); - mysqli_stmt_execute($stmt); - mysqli_stmt_close($stmt); + mysqli_stmt_execute($stmt); + mysqli_stmt_close($stmt); + echo("
"); } }else{ $warning=true; @@ -434,45 +453,19 @@ function time_to_seconds($print_time) { else{ echo("
"); } - } + } } } - + ?> - +

Datei drucken

prepare($sql); - $stmt->execute(); - $result = $stmt->get_result(); - //$row = $result->fetch_assoc(); - $time_now=date("H:i"); - - while ($row = $result->fetch_assoc()) { - if (is_time_between($row["time_from"], $row["time_to"], $time_now)) { - $reservation_conflict = true; - $for_class[]=$row["for_class"]; - //break; - } - } - if(!isset($for_class)) - $for_class[]=0; - if ($reservation_conflict && !in_array($class,$for_class) && $class!=0) { - echo "
"; - $block=true; - }else if($class==0 && $reservation_conflict){ - $block=false; - echo "
"; - }else{ - $block=false; - } - + $response=check_reservation_conflict($link,$_SESSION["class_id"]); + echo($response['message']); + $block=$response['block']; ?>
@@ -482,7 +475,7 @@ function time_to_seconds($print_time) { echo('
'); echo(''); - echo(' '); + echo(' '); echo('
'); echo('
'); } @@ -514,7 +507,7 @@ function time_to_seconds($print_time) { if(isset($_GET["preselect"])){ $preselect=$_GET["preselect"]; }else{ - $preselect=1; + $preselect=1; } if(!isset($_GET["send_to_queue"])){ while($num_of_printers!=0) @@ -528,7 +521,7 @@ function time_to_seconds($print_time) { mysqli_stmt_store_result($stmt); mysqli_stmt_bind_result($stmt, $id,$color); mysqli_stmt_fetch($stmt); - + $color=intval($color); //get the real color $sql="select name from filament where internal_id=$color"; @@ -537,7 +530,7 @@ function time_to_seconds($print_time) { mysqli_stmt_store_result($stmt); mysqli_stmt_bind_result($stmt,$color); mysqli_stmt_fetch($stmt); - + if($id!=0 && $id!=$last_id) { if($id==$preselect) @@ -563,8 +556,7 @@ function time_to_seconds($print_time) { echo('
'); echo(''); echo('