Removed old code, added new files in src/ using classes for each moving part.
This commit is contained in:
95
src/printer.py
Normal file
95
src/printer.py
Normal file
@@ -0,0 +1,95 @@
|
||||
# Importing the module to mage the connection to the printer.
|
||||
from escpos.printer import Usb, USBNotFoundError
|
||||
from time import sleep, gmtime, strftime
|
||||
import os.path
|
||||
|
||||
|
||||
class Printer(object):
|
||||
"""
|
||||
# The connection is based on the ESC/POS library
|
||||
|
||||
## Connection to the USB printer
|
||||
|
||||
## Making sure the printer is alive
|
||||
|
||||
## Making sure it has paper
|
||||
|
||||
## Define default print settings
|
||||
|
||||
## Print starting message, log time of first print, cut.
|
||||
|
||||
## Annonce readyness : return a positive pong message.
|
||||
"""
|
||||
|
||||
# Is the printer ready to accept a new print ?
|
||||
ready = False
|
||||
|
||||
def __init__(self, app):
|
||||
super(Printer, self).__init__()
|
||||
self.app = app
|
||||
self.ready = False
|
||||
self.printer = None
|
||||
|
||||
def init_printer(self, device_id,vendor_id):
|
||||
try:
|
||||
p = Usb(vendor_id, device_id)
|
||||
except USBNotFoundError as e:
|
||||
self.app.logger.error("The USB device is not plugged in, aborting...")
|
||||
exit(-1)
|
||||
waiting_elapsed = 10
|
||||
|
||||
# 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...')
|
||||
sleep(1)
|
||||
waiting_elapsed -= 1
|
||||
if waiting_elapsed < 1:
|
||||
self.app.logger.error('Printer took more than 10 seconds to get online, aborting...')
|
||||
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 == 0:
|
||||
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 ! :)")
|
||||
p.image("../configuration/hello_world.png",center=True)
|
||||
p.cut()
|
||||
|
||||
self.printer = p;
|
||||
self.ready = True;
|
||||
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
|
||||
else :
|
||||
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||
self.printer.textln(clean_msg)
|
||||
|
||||
def print_img(self, path):
|
||||
|
||||
if os.path.isfile(str(path)):
|
||||
pass
|
||||
else:
|
||||
self.app.logger.warning("File does not exist : " + str(path))
|
||||
|
||||
try:
|
||||
self.printer.image(path)
|
||||
except Exception as e:
|
||||
return e
|
||||
else:
|
||||
self.app.logging.debug("Printed an image : " + str(path))
|
||||
Reference in New Issue
Block a user