Update the way a printer is found, update the configuration of Brother

Printers
This commit is contained in:
n07070
2026-11-11 16:58:44 +01:00
parent fd0507ff4f
commit 704b7b83cd
5 changed files with 231 additions and 125 deletions

View File

@@ -18,9 +18,9 @@ class PrintWorker(threading.Thread):
will choose an available Printer from a list of Printers.
If a print fails, it's not retried, but will be added to a list of completed
tasks.
tasks. TODO : Change that
"""
def __init__(self, app, print_queue):
def __init__(self, app, print_queue, printers_pool):
super().__init__(daemon=True)
self.app = app
self.print_queue = print_queue
@@ -28,16 +28,29 @@ class PrintWorker(threading.Thread):
self._lock = threading.Lock()
self.running = True
self.state = "idle" # idle, printing, dead, drinking-a-beer
# All workers share the same pool of printers.
self.printers = printers_pool
self.printers_obj = self.printers.printers
self._printer_index = 0
self.app.logger.debug("Worker initialized.")
self.app.logger.debug("Printers connected:")
for printer in self.printers_obj:
self.app.logger.debug("Printer type: %s", printer.printer_type)
self.app.logger.debug("Printer state: %s", printer.ready)
self.app.logger.debug("Ho great, I'm alive... I'm ready to work another day...")
try:
self.printers = Printers(self.app)
self.printers_obj = self.printers.printers # Get the number of printers connected to the system
self.printers_list = iter(self.printers.printers)
def _acquire_available_printer(self):
result = self.printers.acquire_available_printer(self._printer_index)
if result is None:
return None
index, printer = result
self._printer_index = (index + 1) % len(self.printers_obj)
return printer
except RuntimeError as e:
self.app.logger.warning("Could not get any Printers")
raise e
def run(self):
"""Background thread that processes queue items"""
@@ -58,37 +71,23 @@ class PrintWorker(threading.Thread):
# When we are finished with a printer, we release it to the world.
# If we do not have a printer, we try to find one
while not self.printer:
time.sleep(1)
try:
self.app.logger.debug("Trying to find a available printer for the worker.")
# From the list of printer, we get one.
# If no printer in the list is avaible, we try again from the start of the loop, that we reset.
while self.printer.used or not self.printer:
self.printer = next(self.printers_list)
try:
self.printer = next(self.printer)
except StopIteration as e:
self.app.error("We do not have any available printers. Resetting our list of printers and trying again. " + str(e))
self.printers_list = iter(self.printers.printers)
time.sleep(1)
# TODO : Here, in case there is really no printer available, we will stay in a loop forever. Maybe after a number of tries, we should die.
while self.running and self.printer is None:
self.app.logger.debug("Trying to find a available printer for the worker.")
self.printer = self._acquire_available_printer()
self.app.logger.debug(
"The worker got a %s printer and it's %s",
self.printer.printer_type,
"Ready" if self.printer.ready else "Not ready",
)
# Here, we must have found a Printer() that is
# Printer.used = False, which means it's available.
# Therefore, we mark it used, so no other worker uses it.
self.printer.use_printer()
if self.printer is None:
time.sleep(1)
self.printer.use_printer()
self.app.logger.debug(
"The worker got a %s printer and it's %s",
self.printer.printer_type,
"Ready" if self.printer.ready else "Not ready",
)
except Exception as e:
self.app.logger.error("No printer detected" + str(e))
self.printer = None
# If the printer is not ready, we wait for it to become ready.
while not self.printer.ready:
@@ -116,7 +115,7 @@ class PrintWorker(threading.Thread):
if task:
try:
self.app.logger.info("Got a new task")
self.app.logger.debug("Got task %s", task.task_id)
self.app.logger.debug("Got task %s %s", task.task_type, task.task_id)
task.status = "processing"
print_data = task.get_print_data()