Compare commits
2 Commits
c9a42a7b8b
...
47e89cc295
Author | SHA1 | Date | |
---|---|---|---|
47e89cc295 | |||
b2593a5a97 |
@ -41,9 +41,12 @@ Then, setup the project :
|
||||
git clone https://git.n07070.xyz/n07070/LittlePrynter
|
||||
virtualenv LittlePrynter
|
||||
cd LittlePrynter
|
||||
source bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
> tip : when you're done, you can get out of the virtualenv either by closing your terminal, or by running `deactivate`.
|
||||
|
||||
You should see a folder named `configuration`. Enter it, and duplicate the file named `config.toml.sample`, and rename the copy to `config.toml`. Now, edit this file by following the comments in the file itself.
|
||||
|
||||
You can now start the web server with
|
||||
|
24
src/main.py
24
src/main.py
@ -7,7 +7,7 @@
|
||||
# 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 flask import Flask, request # Used for the web framework
|
||||
from printer import Printer # The wrapper for the printer class
|
||||
import toml # Used for the config file parsing
|
||||
import pprint
|
||||
@ -38,11 +38,10 @@ app.secret_key = configuration_file["secrets"]["flask_secret_key"]
|
||||
vendor_id = configuration_file["printer"]["vendor_id"]
|
||||
device_id = configuration_file["printer"]["device_id"]
|
||||
|
||||
printer = Printer(app)
|
||||
printer = Printer(app,0x04b8, 0x0e28)
|
||||
printer.init_printer()
|
||||
|
||||
|
||||
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
|
||||
@ -51,14 +50,21 @@ if not printer.init_printer(vendor_id, device_id):
|
||||
# api/status/{paper,ping,stats}
|
||||
|
||||
# If you just call the api route, you get a help back.
|
||||
@app.route('/')
|
||||
def index():
|
||||
return "Welcome ! Please type your message in the URL bar in such way : /api/print/sms?txt=<message here>"
|
||||
|
||||
|
||||
@app.route('/api')
|
||||
def api_help():
|
||||
return "Welcome to the API's help page"
|
||||
|
||||
@app.route('/api/print')
|
||||
def index():
|
||||
return "Printing..."
|
||||
def api_index():
|
||||
return "Welcome to the printing software's API. Please see the index page to get started."
|
||||
|
||||
@app.route('/api/print/sms')
|
||||
@app.route('/api/print/sms', methods=['GET'])
|
||||
def api_print_sms():
|
||||
return "Printing short message."
|
||||
texte = request.args.get("txt",type=str)
|
||||
app.logger.debug("Printing : " + texte)
|
||||
return printer.print_sms(texte)
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user