Multi-printers #32
+30
-56
@@ -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
|
||||||
@@ -785,7 +759,7 @@ def text_to_image(
|
|||||||
background="white",
|
background="white",
|
||||||
fill="black",
|
fill="black",
|
||||||
output_dir="src/static/uploads",
|
output_dir="src/static/uploads",
|
||||||
):
|
):
|
||||||
"""Converts a string to an image"""
|
"""Converts a string to an image"""
|
||||||
|
|
||||||
font = ImageFont.truetype(font_path, font_size)
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
@@ -862,7 +836,7 @@ def _resize_image(
|
|||||||
image_path: str,
|
image_path: str,
|
||||||
device_pixel_width: int,
|
device_pixel_width: int,
|
||||||
printer_model: object,
|
printer_model: object,
|
||||||
):
|
):
|
||||||
# """
|
# """
|
||||||
# Resize an image to the specs needed for a label. Only used in Brother printers
|
# Resize an image to the specs needed for a label. Only used in Brother printers
|
||||||
# """
|
# """
|
||||||
|
|||||||
Reference in new issue
Block a user