From d9a8c83ad96eb1851af1af4bf1f375727b5d5bfe Mon Sep 17 00:00:00 2001 From: Janis Steiner Date: Thu, 28 Nov 2024 14:23:45 +0000 Subject: [PATCH] adding tests --- sys0-code/test/fetch_printer_data.php | 67 +++ sys0-code/test/overview.php | 609 ++++++++++++++++++++++++++ 2 files changed, 676 insertions(+) create mode 100644 sys0-code/test/fetch_printer_data.php create mode 100644 sys0-code/test/overview.php diff --git a/sys0-code/test/fetch_printer_data.php b/sys0-code/test/fetch_printer_data.php new file mode 100644 index 0000000..7762efb --- /dev/null +++ b/sys0-code/test/fetch_printer_data.php @@ -0,0 +1,67 @@ += $firstCharsCount + $lastCharsCount + 3) { + $firstChars = substr($filePath, 0, $firstCharsCount); + $lastChars = substr($filePath, -$lastCharsCount); + return $firstChars . "..." . $lastChars; + } else { + return $filePath; + } +} + +$printers = []; +$sql = "SELECT rotation, free, id, printer_url, apikey, cancel, used_by_userid, system_status, color FROM printer"; +$stmt = mysqli_prepare($link, $sql); +mysqli_stmt_execute($stmt); +mysqli_stmt_store_result($stmt); +mysqli_stmt_bind_result($stmt, $rotation, $is_free, $printer_id, $url, $apikey, $cancel, $userid, $system_status, $filament_color); + +while (mysqli_stmt_fetch($stmt)) { + $printer = [ + "rotation" => $rotation, + "is_free" => $is_free, + "printer_id" => $printer_id, + "url" => $url, + "apikey" => $apikey, + "cancel" => $cancel, + "userid" => $userid, + "system_status" => $system_status, + "filament_color" => $filament_color, + ]; + + if ($is_free == 0 && $system_status == 0) { + 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); + $printer["progress"] = (int) $json['progress']['completion']; + $printer["file"] = short_path($json["job"]["file"]["name"], 10, 10); + $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"])); + } + + $printers[] = $printer; +} + +mysqli_stmt_close($stmt); +echo json_encode($printers); +?> diff --git a/sys0-code/test/overview.php b/sys0-code/test/overview.php new file mode 100644 index 0000000..5ff15dd --- /dev/null +++ b/sys0-code/test/overview.php @@ -0,0 +1,609 @@ + + + + + + + + + + +load_user()"; +?> + +=$firstCharsCount+$lastCharsCount+3){ + $firstChars = substr($filePath, 0, $firstCharsCount); + + // Get the last few characters of the path + $lastChars = substr($filePath, -$lastCharsCount); + + // Return the shortened path + return $firstChars . "..." . $lastChars; + } + else{ + return $filePath; + } + } + $color=$_SESSION["color"]; + include "../assets/components.php"; + if(!isset($_SESSION["rid"])) + $_SESSION["rid"]=0; + $_SESSION["rid"]++; + + if(isset($_GET["set_class"]) && isset($_POST["class"])){ + $class_id=htmlspecialchars($_POST["class"]); + $sql="update users set class_id=$class_id where username='$username'"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + $stmt->close(); + $sql="select name from class where id=$class_id"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + $class_name=""; + mysqli_stmt_bind_result($stmt, $class_name); + $stmt->close(); + $_SESSION["class"]=$class_name; + $_SESSION["class_id"]=$class_id; + } +?> + + Alle Drucker + + + +
+
+ + + +
+
+
+ close(); + //load apikey etc + $url=""; + $apikey=""; + $sql="select printer_url,apikey from printer where id=$printer_id"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $url,$apikey); + mysqli_stmt_fetch($stmt); + $stmt->close(); + //connect to the printer + exec("curl --max-time 10 $url/api/job?apikey=$apikey > /var/www/html/user_files/$username/finish.json"); + $fg=file_get_contents("/var/www/html/user_files/$username/finish.json"); + $json=json_decode($fg,true); + $userid=$_SESSION["id"]; + if(isset($json['job']['filament']['tool0']['volume'])){ + $filament_usage=intval($json['job']['filament']['tool0']['volume']); + $sql="UPDATE users SET filament_usage = COALESCE(filament_usage,0) + $filament_usage WHERE id = $cnt"; + //echo($sql); + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + } + + //echo("used $filament_usage mm of filament"); + } + if(isset($_GET['remove_queue'])&&$_GET["rid"]==($_SESSION["rid"]-1)) + { + $id=htmlspecialchars($_GET['remove_queue']); + $sql="delete from queue where id=$id"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + } + if(isset($_GET['cancel'])&&$_GET["rid"]==($_SESSION["rid"]-1)) + { + $apikey=""; + $printer_url=""; + $printer_id=htmlspecialchars($_GET['cancel']); + $sql="select used_by_userid,apikey,printer_url from printer where id=$printer_id"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $cnt,$apikey,$printer_url); + mysqli_stmt_fetch($stmt); + + exec("curl -k -H \"X-Api-Key: $apikey\" -H \"Content-Type: application/json\" --data '{\"command\":\"cancel\"}' \"$printer_url/api/job\" > /var/www/html/user_files/$username/json.json"); + $fg=file_get_contents("/var/www/html/user_files/$username/json.json"); + $json=json_decode($fg,true); + if($json["error"]!="") + { + echo(""); + } + else + { + $sql="update printer set cancel=1 where id=$printer_id"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + } + + } + + $cnt=0; + $url=""; + $apikey=""; + if(isset($_GET["private"])) + $sql="select count(*) from printer where used_by_userid=".$_SESSION["id"]; + else + $sql="select count(*) from printer"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $cnt); + mysqli_stmt_fetch($stmt); + //echo($cnt); + $is_free=0; + echo("
"); + echo("
"); + echo("
"); + if(isset($_GET["private"])) + echo("
Alle Drucker anzeigen"); + else + echo("
Nur eigene Aufträge anzeigen"); + echo("
"); + $last_id=0; + $system_status=0; + $rotation=0; + while($cnt!=0) + { + $userid=0; + if(isset($_GET["private"])) + $sql="select rotation,free,id,printer_url,apikey,cancel,used_by_userid,system_status,color from printer where id>$last_id and used_by_userid=".$_SESSION["id"]." ORDER BY id"; + else + $sql="select rotation,free,id,printer_url,apikey,cancel,used_by_userid,system_status,color from printer where id>$last_id ORDER BY id"; + $cancel=0; + $filament_color=""; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $rotation,$is_free,$printer_id,$url,$apikey,$cancel,$userid,$system_status,$filament_color); + mysqli_stmt_fetch($stmt); + $last_id=$printer_id; + $filament_color=intval($filament_color); + //get the real color + $sql="select name from filament where internal_id=$filament_color"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt,$filament_color); + mysqli_stmt_fetch($stmt); + + if($is_free==0 && $system_status==0){ + //printer is printing + exec("curl --max-time 10 $url/api/job?apikey=$apikey > /var/www/html/user_files/$username/json.json"); + $fg=file_get_contents("/var/www/html/user_files/$username/json.json"); + $json=json_decode($fg,true); + + $used_by_user=""; + $sql="select username from users where id=$userid"; + $stmt = mysqli_prepare($link, $sql); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $used_by_user); + mysqli_stmt_fetch($stmt); + $username2=explode("@",$used_by_user); + + $progress=(int) $json['progress']['completion']; + if($progress<0) + $progress=-$progress; + $file=$json['job']['file']['name']; + if($progress==100){ + $print_time=seconds_to_time(intval($json["progress"]["printTime"])); + $print_time_left=seconds_to_time(intval($json["progress"]["printTimeLeft"])); + $print_time_total=seconds_to_time(intval($json["job"]["estimatedPrintTime"])); + echo("
"); + echo("
"); + echo("
Drucker $printer_id
"); + echo("
"); + echo("
"); + echo(""); + echo("
"); + echo("
$progress%
"); + echo("
"); + echo(""); + echo(""); + echo(""); + echo(""); + if(!empty($filament_color) && $filament_color!=NULL) + echo(""); + echo(""); + echo(""); + echo(""); + echo(""); + echo(""); + if($userid==$_SESSION["id"] or $role[3]==="1"){ + echo(""); + } + echo(""); + echo("
StatusFertig
Genutzt von".$username2[0]."
Filamentfarbe$filament_color
Erwartete Druckzeit$print_time_total
Verbleibende Druckzeit$print_time_left
Vergangene Druckzeit$print_time
Datei
".short_path($json["job"]["file"]["name"],10,10)."
".$json["job"]["file"]["name"]."
Freigeben
"); + echo("
"); + echo("
"); + } + else if($cancel==1){ + $print_time=seconds_to_time(intval($json["progress"]["printTime"])); + $print_time_left=seconds_to_time(intval($json["progress"]["printTimeLeft"])); + $print_time_total=seconds_to_time(intval($json["job"]["estimatedPrintTime"])); + echo("
"); + echo("
"); + echo("
Drucker $printer_id
"); + echo("
"); + echo("
"); + echo(""); + echo("
"); + echo("
$progress%
"); + echo("
"); + echo(""); + echo(""); + echo(""); + echo(""); + if(!empty($filament_color) && $filament_color!=NULL) + echo(""); + echo(""); + echo(""); + echo(""); + echo(""); + if($userid==$_SESSION["id"] or $role[3]=="1"){ + echo(""); + } + echo(""); + echo("
StatusDruck Abgebrochen
Genutzt von".$username2[0]."
Filamentfarbe$filament_color
Erwartete Druckzeit$print_time_total
Verbleibende Druckzeit$print_time_left
Vergangene Druckzeit$print_time
Datei
".short_path($json["job"]["file"]["name"],10,10)."
".$json["job"]["file"]["name"]."
Freigeben
"); + echo("
"); + echo("
"); + } + else{ + $print_time=seconds_to_time(intval($json["progress"]["printTime"])); + $print_time_left=seconds_to_time(intval($json["progress"]["printTimeLeft"])); + $print_time_total=seconds_to_time(intval($json["job"]["estimatedPrintTime"])); + echo("
"); + echo("
"); + echo("
Drucker $printer_id
"); + echo("
"); + echo("
"); + echo(""); + echo("
"); + echo("
$progress%
"); + echo("
"); + echo(""); + echo(""); + echo(""); + echo(""); + if(!empty($filament_color) && $filament_color!=NULL) + echo(""); + echo(""); + echo(""); + echo(""); + echo(""); + if($userid==$_SESSION["id"] or $role[3]==="1"){ + //echo(""); + echo(""); + } + echo(""); + echo("
StatusDrucken
Genutzt von".$username2[0]."
Filamentfarbe$filament_color
Erwartete Druckzeit$print_time_total
Verbleibende Druckzeit$print_time_left
Vergangene Druckzeit$print_time
Datei
".short_path($json["job"]["file"]["name"],10,10)."
".$json["job"]["file"]["name"]."
Abbrechen
"); + echo("
"); + echo("
"); + } + }else if($system_status==0){ + //printer is free + echo("
"); + echo("
"); + echo("
Drucker $printer_id
"); + echo("
"); + echo("
"); + echo(""); + echo(""); + echo(""); + echo(""); + if(!empty($filament_color) && $filament_color!=NULL) + echo(""); + echo(""); + echo(""); + echo("
StatusBereit
Filamentfarbe$filament_color
Drucken
"); + echo("
"); + echo("
"); + + }else{ + //printer is free but has a problem + echo("
"); + echo("
"); + echo("
Drucker $printer_id
"); + echo("
"); + echo("
"); + echo(""); + echo(""); + echo(""); + echo(""); + echo(""); + echo("
StatusProblem / nicht Betriebsbereit
"); + echo("
"); + echo("
"); + + } + $cnt--; + } + echo("
"); + + ?> +

+ +
+
+
+
+ +
+

Warteschlange

+
"); + $last_id=0; + $form_userid=0; + $print_on=0; + while($cnt!=0) + { + $sql="select id,filepath,from_userid,print_on from queue where id>$last_id order by id"; + $cancel=0; + $stmt = mysqli_prepare($link, $sql); + echo mysqli_error($link); + mysqli_stmt_execute($stmt); + mysqli_stmt_store_result($stmt); + mysqli_stmt_bind_result($stmt, $queue_id,$filepath,$from_userid,$print_on); + mysqli_stmt_fetch($stmt); + $filepath=basename($filepath); + $last_id=$queue_id; + echo(""); + if($print_on==-1) + echo(""); + else + echo(""); + if($_SESSION["role"][3]==="1" or $_SESSION["id"]==$from_userid) + echo(""); + + $cnt--; + } + echo("
DateiDrucken auf Druckeraus der Warteschlange entfernen
$filepathErster verfügbarer Drucker$print_on
"); + ?> +

+ + + + + + + + "); + echo("var modal = document.getElementById('select_class');"); + echo("modal.classList.add('show');"); + echo("modal.style.display = 'block';"); + echo("modal.removeAttribute('aria-hidden');"); + echo("document.body.classList.add('modal-open');"); + echo(""); + } + ?> + + + +