WIP: Acces-libre : Raspberry Pi integration #8

Draft
n07070 wants to merge 14 commits from acces-libre into master
2 changed files with 9 additions and 13 deletions
Showing only changes of commit eac737b889 - Show all commits

View File

@ -1,5 +1,4 @@
# Importing the module to mage the connection to the printer. # Importing the module to mage the connection to the printer.
from flask import flash
from escpos.printer import Usb, USBNotFoundError from escpos.printer import Usb, USBNotFoundError
from time import sleep, gmtime, strftime from time import sleep, gmtime, strftime
import os.path import os.path
@ -46,12 +45,10 @@ class Printer(object):
match status: match status:
case 0: case 0:
self.app.logger.error('Printer has no more paper, aborting...') self.app.logger.error('Printer has no more paper, aborting...')
flash("No more paper on the printer. Sorry.",category='error')
self.printer.close() self.printer.close()
raise Exception("No more paper in the printer") raise Exception("No more paper in the printer")
case 1: case 1:
self.app.logger.warning('Printer needs paper to be changed very soon ! ') 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() self.printer.close()
case 2: case 2:
self.app.logger.debug('Printer has paper, good to go') self.app.logger.debug('Printer has paper, good to go')
@ -101,7 +98,7 @@ class Printer(object):
return True return True
def print_sms(self, msg, signature) -> None: def print_sms(self, msg, signature="Guest") -> None:
clean_msg = str(msg) clean_msg = str(msg)
clean_signature = str(signature) clean_signature = str(signature)
@ -109,7 +106,7 @@ class Printer(object):
self.app.logger.warning("Could not print message of this length: " + str(len(clean_msg))) self.app.logger.warning("Could not print message of this length: " + str(len(clean_msg)))
raise Exception("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 4096 caracters long.") raise Exception("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 4096 caracters long.")
if len(signature) > 256 or len(signature) < 3: if len(signature) > 256 or len(signature) < 1:
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature))) 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.") raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
@ -133,13 +130,12 @@ class Printer(object):
self.app.logger.error("Unable to print because : " + str(e)) self.app.logger.error("Unable to print because : " + str(e))
raise e raise e
flash("Message printed : " + clean_msg ,category='info')
return True return True
def print_img(self, path, sign) -> None: def print_img(self, path, sign) -> None:
clean_signature = str(sign) clean_signature = str(sign)
if len(sign) > 256 or len(sign) < 3: if len(sign) > 256 or len(sign) < 1:
self.app.logger.warning("Could not print signature of this length: " + str(len(clean_signature))) 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.") raise Exception("Could not print signature of this length :" + str(len(clean_signature)) + ", needs to between 3 and 256 caracters long.")
@ -160,10 +156,10 @@ class Printer(object):
try: try:
self.check_paper() self.check_paper()
self.printer.open(self.usb_args) self.printer.open(self.usb_args)
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.image(path)
self.printer.textln("Printed by LittlePrynter ")
self.printer.textln("n07070.xyz/articles/littleprynter")
self.printer.textln(clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.cut() self.printer.cut()
self.printer.close() self.printer.close()
self.app.logger.debug("Printed an image : " + str(path)) self.app.logger.debug("Printed an image : " + str(path))
@ -214,7 +210,7 @@ def process_image(self, path):
# Final resize check # Final resize check
if original_img.height > max_height: if original_img.height > max_height:
flash("Image is too long, sorry! Keep it below 575×1000 pixels.", 'error') raise ValueError("Image is too long, sorry! Keep it below 575×1000 pixels.")
self.app.logger.error("Image is too long, sorry! Keep it below 575×1000 pixels.") self.app.logger.error("Image is too long, sorry! Keep it below 575×1000 pixels.")
return False return False

View File

@ -21,7 +21,7 @@ class Web(object):
self.app.logger.error(e) self.app.logger.error(e)
flash("Error while printing the SMS : "+ str(e)) flash("Error while printing the SMS : "+ str(e))
flash("You message has been printed :)") flash("You message " + str( texte ) + " has been printed :)")
def print_image(self, image, sign: str): def print_image(self, image, sign: str):
@ -57,7 +57,7 @@ class Web(object):
self.app.logger.debug("File saved") self.app.logger.debug("File saved")
except Exception as e: except Exception as e:
self.app.logger.error("Could not save file") self.app.logger.error("Could not save file")
flash(e,'error') flash(str(e),'error')
return False return False
self.app.logger.debug("File saved to " + str(os.path.join(self.app.config['UPLOAD_FOLDER'], filename))) self.app.logger.debug("File saved to " + str(os.path.join(self.app.config['UPLOAD_FOLDER'], filename)))