Fixed the printing, added init and small API

This commit is contained in:
2022-03-11 17:13:42 +01:00
parent b2593a5a97
commit 47e89cc295
2 changed files with 58 additions and 33 deletions

View File

@@ -24,32 +24,46 @@ class Printer(object):
# Is the printer ready to accept a new print ?
ready = False
def __init__(self, app):
def __init__(self, app, device_id, vendor_id):
super(Printer, self).__init__()
self.app = app
self.ready = False
self.printer = None
self.device_id = device_id
self.vendor_id = vendor_id
self.usb_args = {}
self.usb_args['idVendor'] = self.device_id
self.usb_args['idProduct'] = self.vendor_id
def init_printer(self, device_id,vendor_id):
try:
p = Usb(0x04b8, 0x0e28, 0)
except USBNotFoundError as e:
self.app.logger.error("The USB device is not plugged in, aborting..." + str(e))
return False
waiting_elapsed = 10
# p.open()
def init_printer(self):
# Is the printer online ? Is the communication with the printer successfull ?
while not p.is_online():
self.app.logger.debug('Waiting for printer to get online...')
waiting_elapsed = 30
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 = Usb(self.device_id, self.vendor_id, 0, profile="TM-P80")
except USBNotFoundError as e:
self.app.logger.error("The USB device is not plugged in, trying again : " + str(e))
pass
try:
if p.is_online():
self.ready = True
except Exception as e:
pass
sleep(1)
waiting_elapsed -= 1
if waiting_elapsed < 1:
self.app.logger.error('Printer took more than 10 seconds to get online, aborting...')
self.app.logger.error('Printer took more than 30 seconds to get online, aborting...')
waiting_elapsed = 30 # Reset the waiting time for the next print.
return False
# Let's check paper status.
ps = p.paper_status()
if ps == 0:
@@ -58,30 +72,32 @@ class Printer(object):
elif ps == 1:
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
sleep(5)
elif ps == 0:
elif ps == 2:
self.app.logger.debug('Printer has paper, good to go')
# We're going to print a little text to let know that we're good, as a final check.
# p.textln("Hello world ! \nStarting up at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
# 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)
# p.textln("------------------")
# p.qr("Hello World ! :)",size=1)
p.image("configuration/hello_world.png",center=True,impl='bitImageColumn')
# p.cut()
# 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
# we are printing before and after every print !
self.printer = p;
self.ready = True;
self.printer.close();
return True
def print_sms(self, msg):
clean_msg = str(msg)
if len(clean_msg) > 256 or len(clean_msg) < 10 :
self.app.logger.warning("Could not print message of this length :" + len(clean_msg))
return False
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 :
self.printer.open(self.usb_args);
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
self.printer.textln(clean_msg)
self.printer.cut()
self.printer.close()
return "Message printed : " + clean_msg
def print_img(self, path):
@@ -91,8 +107,11 @@ class Printer(object):
self.app.logger.warning("File does not exist : " + str(path))
try:
self.printer.open(self.usb_args)
self.printer.image(path)
self.printer.close()
except Exception as e:
self.printer.close()
return e
else:
self.app.logging.debug("Printed an image : " + str(path))