Update configuration options in the web methods

This commit is contained in:
n07070
2026-11-13 19:16:56 +01:00
parent 8309399827
commit 836d6b0be9
2 changed files with 38 additions and 28 deletions

View File

@@ -16,7 +16,7 @@ class Web:
self.print_queue = print_queue self.print_queue = print_queue
self.app = app self.app = app
def print_sms(self, texte, sign: str, do_cut: bool) -> bool: def print_sms(self, texte, sign: str, do_cut: bool, many: int = 1) -> bool:
""" """
Get text and a signature, prints the text and cuts after that. Get text and a signature, prints the text and cuts after that.
""" """
@@ -28,19 +28,20 @@ class Web:
# !! there is no garantee !! that both the SMS task and the Cut task # !! there is no garantee !! that both the SMS task and the Cut task
# are added back to back, another task could be # are added back to back, another task could be
# inserted between the two. # inserted between the two.
for i in range(many):
sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign)) sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign))
self.app.logger.info("Added a SMS task at position %s", sms) self.app.logger.info("Added a SMS task at position %s", sms)
if do_cut: if do_cut:
cut = self.print_queue.enqueue(CutTask()) cut = self.print_queue.enqueue(CutTask())
self.app.logger.info("Added a CUT task at position %s", cut) self.app.logger.info("Added a CUT task at position %s", cut)
except Exception as e: except Exception as e:
self.app.logger.error(e) self.app.logger.error(e)
raise RuntimeError("Could not add SMS to queue, " + str(e)) from e raise RuntimeError("Could not add SMS to queue, " + str(e)) from e
return True return True
def print_image(self, image, sign: str, do_cut: bool, process_image: bool) -> bool: def print_image(self, image, sign: str, do_cut: bool, process_image: bool, many: int = 1) -> 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 after that.
""" """
@@ -53,6 +54,7 @@ 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...")
try: try:
for i in range(many):
img = self.print_queue.enqueue( img = self.print_queue.enqueue(
ImageTask( ImageTask(
os.path.join( os.path.join(

View File

@@ -6,7 +6,6 @@ This is the main printing thread. A worker thread consums Tasks from
import threading import threading
import time import time
import traceback import traceback
from printers import Printers
class PrintWorker(threading.Thread): class PrintWorker(threading.Thread):
@@ -21,7 +20,7 @@ class PrintWorker(threading.Thread):
tasks. TODO : Change that tasks. TODO : Change that
""" """
def __init__(self, app, print_queue, printers_pool): def __init__(self, app, print_queue, printers_pool, printer_index=0):
super().__init__(daemon=True) super().__init__(daemon=True)
self.app = app self.app = app
self.print_queue = print_queue self.print_queue = print_queue
@@ -33,7 +32,9 @@ class PrintWorker(threading.Thread):
# All workers share the same pool of printers. # All workers share the same pool of printers.
self.printers = printers_pool self.printers = printers_pool
self.printers_obj = self.printers.printers self.printers_obj = self.printers.printers
self._printer_index = 0 # If we create several workers, we can assign an index
# so that we lookup the nth printer for the nth worker
self._printer_index = printer_index
self.app.logger.debug("Worker initialized.") self.app.logger.debug("Worker initialized.")
self.app.logger.debug("Printers connected:") self.app.logger.debug("Printers connected:")
@@ -44,11 +45,13 @@ class PrintWorker(threading.Thread):
self.app.logger.debug("Ho great, I'm alive... I'm ready to work another day...") self.app.logger.debug("Ho great, I'm alive... I'm ready to work another day...")
def _acquire_available_printer(self): def _acquire_available_printer(self):
result = self.printers.acquire_available_printer(self._printer_index) result = self.printers.acquire_available_printer(start_index=self._printer_index)
if result is None: if result is None:
return None return None
index, printer = result index, printer = result
# In case we need to lookup another printer, we don't
# look at the same printer in the printer list
self._printer_index = (index + 1) % len(self.printers_obj) self._printer_index = (index + 1) % len(self.printers_obj)
return printer return printer
@@ -78,6 +81,7 @@ class PrintWorker(threading.Thread):
) )
self.printer = self._acquire_available_printer() self.printer = self._acquire_available_printer()
self.app.logger.debug( self.app.logger.debug(
"The worker got a %s printer and it's %s", "The worker got a %s printer and it's %s",
self.printer.printer_type, self.printer.printer_type,
"Ready" if self.printer.ready else "Not ready", "Ready" if self.printer.ready else "Not ready",
@@ -86,6 +90,8 @@ class PrintWorker(threading.Thread):
if self.printer is None: if self.printer is None:
time.sleep(1) time.sleep(1)
self.printer.use_printer() self.printer.use_printer()
# If the printer is not ready, we wait for it to become ready. # If the printer is not ready, we wait for it to become ready.
@@ -128,7 +134,6 @@ class PrintWorker(threading.Thread):
self.app.logger.error("Could not print : %s", str(e)) self.app.logger.error("Could not print : %s", str(e))
self.app.logger.error(traceback.format_exc()) self.app.logger.error(traceback.format_exc())
self.printer.free_printer() self.printer.free_printer()
raise e
task.status = "completed" task.status = "completed"
self.print_queue.mark_completed(task.task_id, "completed") self.print_queue.mark_completed(task.task_id, "completed")
@@ -156,7 +161,10 @@ class PrintWorker(threading.Thread):
""" """
self.app.logger.debug("Giving the worker a break") self.app.logger.debug("Giving the worker a break")
self.state = "drinking-a-beer" self.state = "drinking-a-beer"
# Free the printer during the worker break
# so it can be used by someone else
self.printer.free_printer() self.printer.free_printer()
self.printer = None
self.running = False self.running = False
def start_worker(self): def start_worker(self):