Better error management, change signature print

This commit is contained in:
n07070 committed 2026-11-28 23:56:54 +01:00
1 parent ac2f3fe969
commit 5574fc89ef
1 file changed
+28 -54
+28 -54
View File
@@ -513,14 +513,12 @@ class BrotherPrinter(Printer):
def _state(self): def _state(self):
return self.ready return self.ready
def _print_txt(self, msg, signature="", bold=False, do_cut=False): def _print_txt(self, msg, signature="", do_cut=False):
"""Print text after converting it to an image""" """Print text after converting it to an image"""
self._print_img(text_to_image(msg)) if signature == "":
if signature != "": self._print_img(text_to_image(msg), do_cut=do_cut)
self._print_img(text_to_image(signature)) else:
self._print_img(text_to_image(msg), signature, do_cut=do_cut)
if do_cut:
self._cut()
def _print_img(self, data, signature="", process=False, do_cut=False): def _print_img(self, data, signature="", process=False, do_cut=False):
""" """
@@ -533,9 +531,7 @@ class BrotherPrinter(Printer):
) )
if process: if process:
try: try:
self.app.logger.debug( self.app.logger.debug("\t[⚙ %s] Proccessing the image", self.id)
"\t[⚙ %s] Proccessing the image", threading.get_ident()
)
processed_path = _process_image(self, data) processed_path = _process_image(self, data)
except RuntimeError as e: except RuntimeError as e:
self.app.logger.error( self.app.logger.error(
@@ -547,8 +543,8 @@ class BrotherPrinter(Printer):
self.app.logger.warning("Not proccessing the image") self.app.logger.warning("Not proccessing the image")
try: try:
selected_backend = guess_backend(self.printer_info.identifier) # selected_backend = guess_backend(self.printer_info.identifier)
BACKEND_CLASS = backend_factory(selected_backend)["backend_class"] # BACKEND_CLASS = backend_factory(selected_backend)["backend_class"]
BACKEND_STRING_DESCR = self.printer_info.identifier BACKEND_STRING_DESCR = self.printer_info.identifier
except Exception as e: except Exception as e:
self.app.logger.error( self.app.logger.error(
@@ -576,31 +572,38 @@ class BrotherPrinter(Printer):
dither=True, dither=True,
compress=False, compress=False,
dpi_600=False, dpi_600=False,
cut=False, cut=do_cut,
) )
success = send( return_from_printer = send(
instructions=instructions, instructions=instructions,
printer_identifier=BACKEND_STRING_DESCR, printer_identifier=BACKEND_STRING_DESCR,
backend_identifier="pyusb", backend_identifier="pyusb",
blocking=True, blocking=True,
) )
self.app.logger.debug(str(success)) self.app.logger.debug(str(return_from_printer))
match (return_from_printer.get("outcome")):
case ("printed"):
self.app.logger.debug("\t[⚙ %s] Printed image", self.id) self.app.logger.debug("\t[⚙ %s] Printed image", self.id)
time.sleep(2) time.sleep(2)
case ("error"):
state = return_from_printer.get("printer_state")
errors = [str(error) for error in state.get("errors") if error]
raise RuntimeError(errors)
case (_):
raise RuntimeError("Unknown print error")
except usb.core.USBError as e: except usb.core.USBError as e:
if "timeout error" in str(e): if "timeout error" in str(e):
self.app.logger.error("USB Timeout Error %s", str(e)) self.app.logger.error("USB Timeout Error %s", str(e))
raise e
self.app.logger.error("USB Error : %s", str(e))
raise e
except Exception as e: except Exception as e:
raise e raise e
# finally:
# try:
# os.remove(processed_path)
# self.app.logger.debug("Removed image : " + str(processed_path))
# except OSError as e:
# raise e
if signature != "": if signature != "":
self.app.logger.debug("Printing signature for the image %s", data) self.app.logger.debug("Printing signature for the image %s", data)
@@ -641,42 +644,13 @@ class BrotherPrinter(Printer):
raise e raise e
finally: finally:
try: try:
# os.remove(signature_file) os.remove(signature_file)
self.app.logger.debug("Removed image : " + str(signature_file)) self.app.logger.debug("Removed image : " + str(signature_file))
except OSError as e: except OSError as e:
raise e raise e
if do_cut: self.ready = True
self._cut()
def _cut(self):
self.app.logger.debug("Cutting via Brother Printer")
try:
selected_backend = guess_backend(self.printer_info.identifier)
BACKEND_CLASS = backend_factory(selected_backend)["backend_class"]
BACKEND_STRING_DESCR = self.printer_info.identifier
except Exception as e:
self.app.logger.error(
"Couldn't guess the backend to use from the printer string descriptor"
)
raise e
data = "src/static/images/1x1.png"
try:
im = Image.open(data)
qlr = BrotherQLRaster(self.printer_info.model)
create_label(
qlr, im, self.printer_info.label_type, threshold=70, cut=True, rotate=0
)
# noinspection PyCallingNonCallable
be = BACKEND_CLASS(BACKEND_STRING_DESCR)
be.write(qlr.data)
be.dispose()
del be
self.app.logger.debug("Finished cutting via Brother Printer")
except Exception as e:
raise e
def _qr(self): def _qr(self):
# This should not be reached by a print on a BrotherQL # This should not be reached by a print on a BrotherQL
@@ -714,10 +688,10 @@ class BrotherPrinter(Printer):
raise RuntimeError("This task type is not supported") raise RuntimeError("This task type is not supported")
except ValueError as ve: except ValueError as ve:
self.app.logger.error( self.app.logger.error(
"\t[⚙ %s] The printer couldn't be found, the printing failed for task %s of type , error : %s ", "\t[⚙ %s] The printer couldn't be found, the printing failed for task %s of type %s , error : %s ",
self.id, self.id,
task.task_id, task.task_id,
task.value, task.task_type,
str(ve), str(ve),
) )
raise ve raise ve