Multi-printers #32
No files matched your search
+2
-2
@@ -109,9 +109,9 @@ class RemoveTask(PrintTask):
|
|||||||
This class is used to clear images and leftovers after a print
|
This class is used to clear images and leftovers after a print
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, task_id, image_path):
|
def __init__(self, image_path):
|
||||||
super().__init__(TaskType.REMOVE)
|
super().__init__(TaskType.REMOVE)
|
||||||
self.task_id = self._generate_id() if not task_id else task_id
|
self.task_id = self._generate_id()
|
||||||
self.image_path = image_path
|
self.image_path = image_path
|
||||||
|
|
||||||
# There is no print data,
|
# There is no print data,
|
||||||
|
|||||||
+15
-12
@@ -5,7 +5,7 @@ Manage all of the inputs from a web source
|
|||||||
import os
|
import os
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
import uuid
|
import uuid
|
||||||
from task import TextTask, ImageTask
|
from task import TextTask, ImageTask, RemoveTask
|
||||||
|
|
||||||
|
|
||||||
class Web:
|
class Web:
|
||||||
@@ -24,11 +24,6 @@ class Web:
|
|||||||
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
|
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# We create two new tasks and add them directly to the queue
|
|
||||||
# TODO: this might need to be improved because
|
|
||||||
# !! there is no garantee !! that both the SMS task and the Cut task
|
|
||||||
# are added back to back, another task could be
|
|
||||||
# inserted between the two.
|
|
||||||
for i in range(many):
|
for i in range(many):
|
||||||
sms = self.print_queue.enqueue(
|
sms = self.print_queue.enqueue(
|
||||||
TextTask(content=texte, signature=sign, do_cut=do_cut)
|
TextTask(content=texte, signature=sign, do_cut=do_cut)
|
||||||
@@ -49,7 +44,7 @@ class Web:
|
|||||||
self, image, sign: str, do_cut: bool, process_image: bool, many: int = 1
|
self, image, sign: str, do_cut: bool, process_image: bool, many: int = 1
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Get an image and a signature, prints the image and cuts after that.
|
Get an image and a signature, prints the image and cuts if asked after that.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
image_uuid = str(uuid.uuid4())
|
image_uuid = str(uuid.uuid4())
|
||||||
@@ -62,14 +57,18 @@ class Web:
|
|||||||
|
|
||||||
if file_uploaded:
|
if file_uploaded:
|
||||||
self.app.logger.debug("File has been uploaded, printing...")
|
self.app.logger.debug("File has been uploaded, printing...")
|
||||||
|
image_path = os.path.join( self.app.config["UPLOAD_FOLDER"],
|
||||||
|
secure_filename(
|
||||||
|
str(
|
||||||
|
file_uploaded.split("/").pop()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
for i in range(many):
|
for _ in range(many):
|
||||||
img = self.print_queue.enqueue(
|
img = self.print_queue.enqueue(
|
||||||
ImageTask(
|
ImageTask(
|
||||||
os.path.join(
|
image_path,
|
||||||
self.app.config["UPLOAD_FOLDER"],
|
|
||||||
secure_filename(str(file_uploaded.split("/").pop())),
|
|
||||||
),
|
|
||||||
signature=sign,
|
signature=sign,
|
||||||
process=process_image,
|
process=process_image,
|
||||||
task_id=str(uuid.uuid4()),
|
task_id=str(uuid.uuid4()),
|
||||||
@@ -85,6 +84,10 @@ class Web:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError("Could not add IMG to queue " + str(e)) from e
|
raise RuntimeError("Could not add IMG to queue " + str(e)) from e
|
||||||
|
|
||||||
|
self.app.logger.debug("Adding a remove task for image of UUID %s", image_uuid)
|
||||||
|
self.print_queue.enqueue(
|
||||||
|
RemoveTask(image_path=image_path)
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# def login(self, username: str, password: str) -> bool:
|
# def login(self, username: str, password: str) -> bool:
|
||||||
|
|||||||
+11
-5
@@ -6,8 +6,8 @@ This is the main printing thread. A worker thread consums Tasks from
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import random
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class PrintWorker(threading.Thread):
|
class PrintWorker(threading.Thread):
|
||||||
@@ -67,7 +67,6 @@ class PrintWorker(threading.Thread):
|
|||||||
while True:
|
while True:
|
||||||
# If the printer is dead or asleep, it can't work.
|
# If the printer is dead or asleep, it can't work.
|
||||||
if not self.running:
|
if not self.running:
|
||||||
self.app.logger.debug("\t[⚙ %s] Worker is asleep", self.id)
|
|
||||||
time.sleep(0.4)
|
time.sleep(0.4)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -138,13 +137,20 @@ class PrintWorker(threading.Thread):
|
|||||||
|
|
||||||
task.status = "processing"
|
task.status = "processing"
|
||||||
|
|
||||||
print_data = task.get_print_data()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if task.task_type.value == "remove":
|
||||||
|
try:
|
||||||
|
self.app.logger.debug("Removing file %s", task.image_path)
|
||||||
|
os.remove(task.image_path)
|
||||||
|
except Exception as e:
|
||||||
|
self.app.logger.error("Could not remove file %s because %s", task.image_path, str(e))
|
||||||
|
raise RuntimeError from e
|
||||||
|
else:
|
||||||
|
print_data = task.get_print_data()
|
||||||
self.printer.print_task(task, print_data)
|
self.printer.print_task(task, print_data)
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
self.state = "idle"
|
self.state = "idle"
|
||||||
self.app.logger.error("Could not print : %s", str(e))
|
self.app.logger.error("Could not process task %s because: %s", task.task_id, str(e))
|
||||||
self.print_queue.mark_completed(task.task_id, "failed", log=str(e))
|
self.print_queue.mark_completed(task.task_id, "failed", log=str(e))
|
||||||
self.app.logger.error(traceback.format_exc())
|
self.app.logger.error(traceback.format_exc())
|
||||||
self.printer.free_printer()
|
self.printer.free_printer()
|
||||||
|
|||||||
Reference in new issue
Block a user