Update little printer requirements

This commit is contained in:
nono
2025-06-23 23:08:32 +02:00
parent cf557c89cd
commit 981eba100d
8 changed files with 311 additions and 127 deletions

View File

@@ -48,16 +48,14 @@ class Printer(object):
self.app.logger.error('Printer has no more paper, aborting...')
flash("No more paper on the printer. Sorry.",category='error')
self.printer.close()
return False
raise Exception("No more paper in the printer")
case 1:
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
flash('Printer needs paper to be changed very soon ! ', category='info')
self.printer.close()
return True
case 2:
self.app.logger.debug('Printer has paper, good to go')
self.printer.close()
return True
def init_printer(self):
@@ -99,57 +97,55 @@ class Printer(object):
self.ready = True;
self.printer.close();
if not self.check_paper():
return False
self.check_paper()
return True
def print_sms(self, msg, signature) -> bool:
def print_sms(self, msg, signature) -> None:
clean_msg = str(msg)
clean_signature = str(signature)
if not self.check_paper():
return False
if len(clean_msg) > 256 or len(clean_msg) < 3 :
self.app.logger.warning("Could not print message of this length: " + str(len(clean_msg)))
flash("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 256 caracters long.",category='error')
return False
raise Exception("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 256 caracters long.")
if len(signature) > 256 or len(signature) < 3:
self.app.logger.warning("Could not print message without a signature.")
flash("Could not print message without a signature.",category='error')
return False
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature)))
raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
if not os.getenv('LIPY_DEBUG') == True:
try:
self.printer.open(self.usb_args);
self.printer.set(align='left', font='a', bold=False, underline=0, width=1, height=1, density=8, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
self.printer.textln("> Printed by LittlePrinter | https://n07070.xyz/articles/littleprynter")
self.printer.textln(clean_msg)
self.printer.set(align='left', font='b', bold=False, underline=1, width=1, height=1, density=9, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.cut()
self.printer.close
except Exception as e:
flash("Unable to print because : " + e)
self.check_paper()
try:
self.printer.open(self.usb_args);
self.printer.set(align='left', font='a', bold=False, underline=0, width=1, height=1, density=8, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
self.printer.set(align='left', font='a', bold=True, underline=1, width=1, height=1, density=6, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
self.printer.textln(clean_msg)
self.printer.textln("")
self.printer.set(align='left', font='b', bold=False, underline=1, width=1, height=1, density=9, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.textln("")
self.printer.textln("Printed by LittlePrinter ")
self.printer.textln("n07070.xyz/articles/littleprynter")
self.printer.textln("")
self.printer.cut()
self.printer.close()
except Exception as e:
self.app.logger.error("Unable to print because : " + str(e))
raise e
flash("Message printed : " + clean_msg ,category='info')
return True
def print_img(self, path, sign):
def print_img(self, path, sign) -> None:
clean_signature = str(sign)
if len(clean_signature) > 256 or len(clean_signature) < 3:
self.app.logger.warning("Could not print message without a signature.")
flash("Could not print message without a signature.",category='error')
return False
if len(sign) > 256 or len(sign) < 3:
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature)))
raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
if not os.path.isfile(str(path)):
self.app.logger.warning("File does not exist : " + str(path))
flash('The file path for this image :' + str(path) + " wasn't found. Please try again.", 'error')
return False
raise Exception('The file path for this image :' + str(path) + " wasn't found. Please try again.")
else:
self.app.logger.debug("Printing file from " + str(path))
@@ -157,12 +153,15 @@ class Printer(object):
self.app.logger.debug("Proccessing the image")
path = process_image(self, path)
except Exception as e:
flash(str(e))
self.app.logger.error(str(e))
raise e
try:
self.check_paper()
self.printer.open(self.usb_args)
self.printer.textln("> Printed by LittlePrinter | https://n07070.xyz/articles/littleprynter")
self.printer.textln("Printed by LittlePrinter ")
self.printer.textln("n07070.xyz/articles/littleprynter")
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.image(path)
self.printer.cut()
@@ -170,11 +169,10 @@ class Printer(object):
self.app.logger.debug("Printed an image : " + str(path))
os.remove(path)
self.app.logger.debug("Removed image.")
return True
except Exception as e:
self.printer.close()
flash(str(e),'error')
return False
self.app.logger.error(str(e))
raise e
def process_image(self, path):
brightness_factor = 1.5 # Used only if image is too dark