updating queue system
This commit is contained in:
@@ -1,68 +1,64 @@
|
|||||||
<?php
|
<?php
|
||||||
function test_queue($link)//function to check if any printer is free and if there are jobs in queue. If yes push jo to printer
|
function test_queue($link) // Function to check if any printer is free and process all jobs in the queue
|
||||||
{
|
{
|
||||||
|
$sql = "SELECT id, from_userid, filepath, print_on FROM queue ORDER BY id";
|
||||||
|
$stmt = mysqli_prepare($link, $sql);
|
||||||
|
mysqli_stmt_execute($stmt);
|
||||||
|
mysqli_stmt_store_result($stmt);
|
||||||
|
mysqli_stmt_bind_result($stmt, $qid, $quserid, $qfilepath, $print_on);
|
||||||
|
|
||||||
$sql="select id, from_userid,filepath,print_on from queue order by id";
|
while (mysqli_stmt_fetch($stmt)) {
|
||||||
$qid=0;
|
$num_of_printers = 0;
|
||||||
$quserid=0;
|
if ($print_on == -1)
|
||||||
$qfilepath="";
|
$sql_printers = "SELECT COUNT(*) FROM printer";
|
||||||
$print_on=0;
|
else
|
||||||
$stmt = mysqli_prepare($link, $sql);
|
$sql_printers = "SELECT COUNT(*) FROM printer WHERE id = $print_on";
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
mysqli_stmt_store_result($stmt);
|
$stmt_printers = mysqli_prepare($link, $sql_printers);
|
||||||
mysqli_stmt_bind_result($stmt, $qid,$quserid,$qfilepath,$print_on);
|
mysqli_stmt_execute($stmt_printers);
|
||||||
mysqli_stmt_fetch($stmt);
|
mysqli_stmt_store_result($stmt_printers);
|
||||||
$num_of_printers=0;
|
mysqli_stmt_bind_result($stmt_printers, $num_of_printers);
|
||||||
if($print_on==-1)
|
mysqli_stmt_fetch($stmt_printers);
|
||||||
$sql="select count(*) from printer";
|
|
||||||
else
|
|
||||||
$sql="select count(*) from printer where id=$print_on";
|
|
||||||
$stmt = mysqli_prepare($link, $sql);
|
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
mysqli_stmt_store_result($stmt);
|
|
||||||
mysqli_stmt_bind_result($stmt, $num_of_printers);
|
|
||||||
mysqli_stmt_fetch($stmt);
|
|
||||||
$last_id=0;
|
|
||||||
$printers_av=0;
|
|
||||||
if($num_of_printers!=0)
|
|
||||||
{
|
|
||||||
$id=0;
|
|
||||||
$papikey="";
|
|
||||||
$userid=$_SESSION["id"];
|
|
||||||
$username=$_SESSION["username"];
|
|
||||||
$purl="";
|
|
||||||
if($print_on==-1)
|
|
||||||
$sql="Select id,apikey,printer_url from printer where id>$last_id and free=1 order by id";
|
|
||||||
else
|
|
||||||
$sql="Select id,apikey,printer_url from printer where id=$print_on and free=1";
|
|
||||||
//echo $sql;
|
|
||||||
$stmt = mysqli_prepare($link, $sql);
|
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
mysqli_stmt_store_result($stmt);
|
|
||||||
mysqli_stmt_bind_result($stmt, $id,$papikey,$purl);
|
|
||||||
mysqli_stmt_fetch($stmt);
|
|
||||||
if($id!=0)
|
|
||||||
{
|
|
||||||
exec('curl -k -H "X-Api-Key: '.$papikey.'" -F "select=true" -F "print=true" -F "file=@'.$qfilepath.'" "'.$purl.'/api/files/local" > /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['effectivePrint']==true and $json["effectiveSelect"]==true)
|
|
||||||
{
|
|
||||||
$sql="update printer set free=0, printing=1,mail_sent=0, used_by_userid=$quserid where id=$id";
|
|
||||||
$stmt = mysqli_prepare($link, $sql);
|
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
|
|
||||||
$sql="delete from queue where id=$qid";
|
|
||||||
$stmt = mysqli_prepare($link, $sql);
|
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
echo("failed sending file to printer!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($num_of_printers > 0) {
|
||||||
|
$id = 0;
|
||||||
|
$papikey = "";
|
||||||
|
$purl = "";
|
||||||
|
if ($print_on == -1)
|
||||||
|
$sql_free_printers = "SELECT id, apikey, printer_url FROM printer WHERE free = 1 ORDER BY id";
|
||||||
|
else
|
||||||
|
$sql_free_printers = "SELECT id, apikey, printer_url FROM printer WHERE id = $print_on AND free = 1";
|
||||||
|
|
||||||
|
$stmt_free_printers = mysqli_prepare($link, $sql_free_printers);
|
||||||
|
mysqli_stmt_execute($stmt_free_printers);
|
||||||
|
mysqli_stmt_store_result($stmt_free_printers);
|
||||||
|
mysqli_stmt_bind_result($stmt_free_printers, $id, $papikey, $purl);
|
||||||
|
|
||||||
|
if (mysqli_stmt_fetch($stmt_free_printers)) { // Found a free printer
|
||||||
|
$username = $_SESSION["username"];
|
||||||
|
$curl_cmd = 'curl -k -H "X-Api-Key: ' . $papikey . '" -F "select=true" -F "print=true" -F "file=@' . $qfilepath . '" "' . $purl . '/api/files/local" > /var/www/html/user_files/' . $username . '/json.json';
|
||||||
|
exec($curl_cmd);
|
||||||
|
|
||||||
|
$fg = file_get_contents("/var/www/html/user_files/$username/json.json");
|
||||||
|
$json = json_decode($fg, true);
|
||||||
|
|
||||||
|
if ($json['effectivePrint'] == true && $json["effectiveSelect"] == true) {
|
||||||
|
$sql_update_printer = "UPDATE printer SET free = 0, printing = 1, mail_sent = 0, used_by_userid = $quserid WHERE id = $id";
|
||||||
|
$stmt_update_printer = mysqli_prepare($link, $sql_update_printer);
|
||||||
|
mysqli_stmt_execute($stmt_update_printer);
|
||||||
|
|
||||||
|
$sql_delete_queue = "DELETE FROM queue WHERE id = $qid";
|
||||||
|
$stmt_delete_queue = mysqli_prepare($link, $sql_delete_queue);
|
||||||
|
mysqli_stmt_execute($stmt_delete_queue);
|
||||||
|
} else {
|
||||||
|
echo "Failed sending file to printer for queue ID $qid!";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "No free printer available for queue ID $qid!";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "No printers available for queue ID $qid!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user