Fix error flashing and transmission

This commit is contained in:
n07070
2026-05-20 16:34:01 +02:00
parent 26c2de12b6
commit 306cab6606
3 changed files with 107 additions and 103 deletions

View File

@@ -1,6 +1,6 @@
# Importing the module to manage the connection to the printer.
import escpos.printer as escp
import brother-ql-inventree
import escpos.printer
import brother_ql
from time import sleep, gmtime, strftime
import os.path
from PIL import Image, ImageEnhance, ImageOps
@@ -47,7 +47,7 @@ class Printer(object):
case 0:
self.app.logger.error("Printer has no more paper, aborting...")
self.printer.close()
raise Exception("No more paper in the printer")
raise RuntimeError("No more paper in the printer")
case 1:
self.app.logger.warning(
"Printer needs paper to be changed very soon ! "
@@ -60,17 +60,21 @@ class Printer(object):
def init_printer(self):
# Is the printer online ? Is the communication with the printer successfull ?
waiting_elapsed = 30
if os.getenv("FLASK_DEBUG"):
waiting_elapsed = 1
else:
waiting_elapsed = 10
self.app.logger.debug("Waiting for printer to get online...")
while not self.ready:
try:
# This also calls open(), which we need to close()
# or else the device will appear as busy.
p = escp.Usb(self.device_id, self.vendor_id, 0, profile="TM-P80")
p = escpos.printer.Usb(self.device_id, self.vendor_id, 0, profile="TM-P80")
except Exception as e:
self.app.logger.error(
"The USB device is not plugged in, trying again : " + str(e)
"The USB device is not plugged in, trying again %s : %s",waiting_elapsed, str(e)
)
pass
@@ -79,6 +83,8 @@ class Printer(object):
self.ready = True
self.app.logger.debug("Printer online !")
except Exception as e:
self.app.logger.error("Error while getting the printer online %s : %s",waiting_elapsed, str(e)
)
pass
sleep(1)
@@ -149,6 +155,7 @@ class Printer(object):
self.printer.close()
except Exception as e:
self.app.logger.error("Unable to print because : " + str(e))
raise RuntimeError("Unable to print a SMS, the printer couldn't do it.") from e
self.app.logger.info("Printed text")
return True
@@ -194,9 +201,14 @@ class Printer(object):
os.remove(path)
self.app.logger.debug("Removed image : " + str(path))
except Exception as e:
self.printer.close()
self.app.logger.error(str(e))
return False
raise RuntimeError("Could not print the picture") from e
finally:
try:
self.printer.close()
except Exception as e:
self.app.logger.error(str(e))
raise RuntimeError("Could not close the printer connexion. ") from e
self.app.logger.info("Printed a picture")
return True
@@ -285,6 +297,7 @@ def process_image(self, path):
# Convert to JPEG and save
jpeg_path = os.path.splitext(path)[0] + "_processed.jpg"
original_img.save(jpeg_path, format="JPEG", quality=95, optimize=True)
app.logger.debug("Processed and saved image.")
return jpeg_path