Compare commits
No commits in common. "47e89cc2958fffc59d88d9cc4d597981bb34b3fc" and "c9a42a7b8b50d0f5b279717f259ea48dbbaf2274" have entirely different histories.
47e89cc295
...
c9a42a7b8b
@ -41,12 +41,9 @@ Then, setup the project :
|
|||||||
git clone https://git.n07070.xyz/n07070/LittlePrynter
|
git clone https://git.n07070.xyz/n07070/LittlePrynter
|
||||||
virtualenv LittlePrynter
|
virtualenv LittlePrynter
|
||||||
cd LittlePrynter
|
cd LittlePrynter
|
||||||
source bin/activate
|
|
||||||
pip install -r requirements.txt
|
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 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
|
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.
|
# Then we build the web interface, using the simple Jinja2 templating.
|
||||||
|
|
||||||
# Following are the librairies we import,
|
# Following are the librairies we import,
|
||||||
from flask import Flask, request # Used for the web framework
|
from flask import Flask # Used for the web framework
|
||||||
from printer import Printer # The wrapper for the printer class
|
from printer import Printer # The wrapper for the printer class
|
||||||
import toml # Used for the config file parsing
|
import toml # Used for the config file parsing
|
||||||
import pprint
|
import pprint
|
||||||
@ -38,10 +38,11 @@ app.secret_key = configuration_file["secrets"]["flask_secret_key"]
|
|||||||
vendor_id = configuration_file["printer"]["vendor_id"]
|
vendor_id = configuration_file["printer"]["vendor_id"]
|
||||||
device_id = configuration_file["printer"]["device_id"]
|
device_id = configuration_file["printer"]["device_id"]
|
||||||
|
|
||||||
printer = Printer(app,0x04b8, 0x0e28)
|
printer = Printer(app)
|
||||||
printer.init_printer()
|
|
||||||
|
|
||||||
|
|
||||||
|
if not printer.init_printer(vendor_id, device_id):
|
||||||
|
app.logger.error('Could not init printer, aborting.')
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
# API routes
|
# API routes
|
||||||
# The api has the following methods
|
# The api has the following methods
|
||||||
@ -50,21 +51,14 @@ printer.init_printer()
|
|||||||
# api/status/{paper,ping,stats}
|
# api/status/{paper,ping,stats}
|
||||||
|
|
||||||
# If you just call the api route, you get a help back.
|
# 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')
|
@app.route('/api')
|
||||||
def api_help():
|
def api_help():
|
||||||
return "Welcome to the API's help page"
|
return "Welcome to the API's help page"
|
||||||
|
|
||||||
@app.route('/api/print')
|
@app.route('/api/print')
|
||||||
def api_index():
|
def index():
|
||||||
return "Welcome to the printing software's API. Please see the index page to get started."
|
return "Printing..."
|
||||||
|
|
||||||
@app.route('/api/print/sms', methods=['GET'])
|
@app.route('/api/print/sms')
|
||||||
def api_print_sms():
|
def api_print_sms():
|
||||||
texte = request.args.get("txt",type=str)
|
return "Printing short message."
|
||||||
app.logger.debug("Printing : " + texte)
|
|
||||||
return printer.print_sms(texte)
|
|
||||||
|
@ -24,46 +24,32 @@ class Printer(object):
|
|||||||
# Is the printer ready to accept a new print ?
|
# Is the printer ready to accept a new print ?
|
||||||
ready = False
|
ready = False
|
||||||
|
|
||||||
def __init__(self, app, device_id, vendor_id):
|
def __init__(self, app):
|
||||||
super(Printer, self).__init__()
|
super(Printer, self).__init__()
|
||||||
self.app = app
|
self.app = app
|
||||||
self.ready = False
|
self.ready = False
|
||||||
self.printer = None
|
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):
|
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()
|
||||||
|
|
||||||
# Is the printer online ? Is the communication with the printer successfull ?
|
# Is the printer online ? Is the communication with the printer successfull ?
|
||||||
waiting_elapsed = 30
|
while not p.is_online():
|
||||||
self.app.logger.debug('Waiting for printer to get online...')
|
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)
|
sleep(1)
|
||||||
waiting_elapsed -= 1
|
waiting_elapsed -= 1
|
||||||
if waiting_elapsed < 1:
|
if waiting_elapsed < 1:
|
||||||
self.app.logger.error('Printer took more than 30 seconds to get online, aborting...')
|
self.app.logger.error('Printer took more than 10 seconds to get online, aborting...')
|
||||||
waiting_elapsed = 30 # Reset the waiting time for the next print.
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# Let's check paper status.
|
# Let's check paper status.
|
||||||
ps = p.paper_status()
|
ps = p.paper_status()
|
||||||
if ps == 0:
|
if ps == 0:
|
||||||
@ -72,32 +58,30 @@ class Printer(object):
|
|||||||
elif ps == 1:
|
elif ps == 1:
|
||||||
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
|
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
|
||||||
sleep(5)
|
sleep(5)
|
||||||
elif ps == 2:
|
elif ps == 0:
|
||||||
self.app.logger.debug('Printer has paper, good to go')
|
self.app.logger.debug('Printer has paper, good to go')
|
||||||
|
|
||||||
# Setting up the printing options.
|
# We're going to print a little text to let know that we're good, as a final check.
|
||||||
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("Hello world ! \nStarting up at " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||||
# Beware : if we print this every time the printer becomes ready, it means
|
# 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)
|
||||||
# we are printing before and after every print !
|
# p.textln("------------------")
|
||||||
|
# p.qr("Hello World ! :)",size=1)
|
||||||
|
p.image("configuration/hello_world.png",center=True,impl='bitImageColumn')
|
||||||
|
# p.cut()
|
||||||
|
|
||||||
self.printer = p;
|
self.printer = p;
|
||||||
self.ready = True;
|
self.ready = True;
|
||||||
self.printer.close();
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def print_sms(self, msg):
|
def print_sms(self, msg):
|
||||||
clean_msg = str(msg)
|
clean_msg = str(msg)
|
||||||
|
|
||||||
if len(clean_msg) > 256 or len(clean_msg) < 10 :
|
if len(clean_msg) > 256 or len(clean_msg) < 10 :
|
||||||
self.app.logger.warning("Could not print message of this length :" + str(len(clean_msg)))
|
self.app.logger.warning("Could not print message of this length :" + len(clean_msg))
|
||||||
return "Could not print message of this length :" + str(len(clean_msg)) + ", needs to be 10 < x < 256"
|
return False
|
||||||
else :
|
else :
|
||||||
self.printer.open(self.usb_args);
|
|
||||||
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||||
self.printer.textln(clean_msg)
|
self.printer.textln(clean_msg)
|
||||||
self.printer.cut()
|
|
||||||
self.printer.close()
|
|
||||||
return "Message printed : " + clean_msg
|
|
||||||
|
|
||||||
def print_img(self, path):
|
def print_img(self, path):
|
||||||
|
|
||||||
@ -107,11 +91,8 @@ class Printer(object):
|
|||||||
self.app.logger.warning("File does not exist : " + str(path))
|
self.app.logger.warning("File does not exist : " + str(path))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.printer.open(self.usb_args)
|
|
||||||
self.printer.image(path)
|
self.printer.image(path)
|
||||||
self.printer.close()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.printer.close()
|
|
||||||
return e
|
return e
|
||||||
else:
|
else:
|
||||||
self.app.logging.debug("Printed an image : " + str(path))
|
self.app.logging.debug("Printed an image : " + str(path))
|
||||||
|
Loading…
Reference in New Issue
Block a user