Removed old code, added new files in src/ using classes for each moving part.
This commit is contained in:
51
src/main.py
Normal file
51
src/main.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# Welcome to the LittlePrynter's source code.
|
||||
# This program expose a web interface, with user authentification, that makes it possible to print messages from the web.
|
||||
# It also exposes a API, making it possible to print and interface with much of the printer's abilities.
|
||||
|
||||
# We first define the connection to the printer itself,
|
||||
# Then we build the API around Flask,
|
||||
# Then we build the web interface, using the simple Jinja2 templating.
|
||||
|
||||
# Following are the librairies we import,
|
||||
from flask import Flask # Used for the web framework
|
||||
from printer import Printer
|
||||
|
||||
# We define the app module used by Flask
|
||||
app = Flask(__name__)
|
||||
app.secret_key = b'\x98>3nM[D\xa4\xd4\xd0K\xab?oM.`\x98'
|
||||
# TODO: Get this secret key from a config file
|
||||
|
||||
|
||||
# Printer connection
|
||||
# Uses the class defined in the printer.py file
|
||||
|
||||
# Define the USB connections here.
|
||||
# TODO: move this to a config file
|
||||
vendor_id = 0x04b8
|
||||
device_id = 0x0e28
|
||||
|
||||
|
||||
printer = Printer(app)
|
||||
|
||||
if not printer.init_printer(vendor_id, device_id):
|
||||
app.logger.error('Could not init printer, aborting.')
|
||||
exit(-1)
|
||||
|
||||
# API routes
|
||||
# The api has the following methods
|
||||
# api/print/{sms,img,letter,qr,barcode}
|
||||
# api/auth/{login,logout}
|
||||
# api/status/{paper,ping,stats}
|
||||
|
||||
# If you just call the api route, you get a help back.
|
||||
@app.route('/api')
|
||||
def api_help():
|
||||
return "Welcome to the API's help page"
|
||||
|
||||
@app.route('/api/print')
|
||||
def index():
|
||||
return "Printing..."
|
||||
|
||||
@app.route('/api/print/sms')
|
||||
def api_print_sms():
|
||||
return "Printing short message."
|
||||
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