Added printer class

This commit is contained in:
n07070 2022-03-12 04:29:39 +01:00
parent 113c7a761e
commit 2b8480c89d

View File

@ -1,4 +1,5 @@
# Importing the module to mage the connection to the printer.
from flask import flash
from escpos.printer import Usb, USBNotFoundError
from time import sleep, gmtime, strftime
import os.path
@ -35,6 +36,27 @@ class Printer(object):
self.usb_args['idVendor'] = self.device_id
self.usb_args['idProduct'] = self.vendor_id
def check_paper(self) -> bool:
# Let's check paper status
self.app.logger.debug('Checking paper status...')
self.printer.open(self.usb_args)
status = self.printer.paper_status()
match status:
case 0:
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
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):
# Is the printer online ? Is the communication with the printer successfull ?
@ -53,6 +75,7 @@ class Printer(object):
try:
if p.is_online():
self.ready = True
self.app.logger.debug('Printer online !')
except Exception as e:
pass
@ -64,40 +87,48 @@ class Printer(object):
return False
# Let's check paper status.
ps = p.paper_status()
if ps == 0:
self.app.logger.error('Printer has no more paper, aborting...')
return False
elif ps == 1:
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
sleep(5)
elif ps == 2:
self.app.logger.debug('Printer has paper, good to go')
# Setting up the printing options.
p.set(align='center', font='a', bold=False, underline=0, width=1, height=1, density=9, invert=False, smooth=False, flip=False, double_width=False, double_height=False, custom_size=False)
# Beware : if we print this every time the printer becomes ready, it means
# Beware : if we print every time the printer becomes ready, it means
# we are printing before and after every print !
self.printer = p;
self.printer = p
self.ready = True;
self.printer.close();
if not self.check_paper():
return False
return True
def print_sms(self, msg):
def print_sms(self, msg, signature) -> bool:
clean_msg = str(msg)
clean_signature = str(signature)
if len(clean_msg) > 256 or len(clean_msg) < 10 :
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)))
return "Could not print message of this length :" + str(len(clean_msg)) + ", needs to be 10 < x < 256"
else :
flash("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 256 caracters long.",category='error')
return False
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
if not os.getenv('LIPY_DEBUG') == True:
self.printer.open(self.usb_args);
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
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(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()
return "Message printed : " + clean_msg
flash("Message printed : " + clean_msg ,category='info')
return True
def print_img(self, path):