Compare commits
No commits in common. "bb7e12a5618a5a741011d64bf8601b8a09c42453" and "35fc990bc0e0d3a54b35bbcb8ddd9fc18d3dcce0" have entirely different histories.
bb7e12a561
...
35fc990bc0
91
src/main.py
@ -7,16 +7,11 @@
|
||||
# Then we build the web interface, using the simple Jinja2 templating.
|
||||
|
||||
# Following are the librairies we import,
|
||||
from flask import Flask, request, render_template, flash, abort, redirect, url_for, make_response, jsonify # Used for the web framework
|
||||
from flask_limiter import Limiter
|
||||
from flask_limiter.util import get_remote_address
|
||||
from flask import Flask, request # Used for the web framework
|
||||
from printer import Printer # The wrapper for the printer class
|
||||
from web import Web # Wrapper for the web routes and API
|
||||
import toml # Used for the config file parsing
|
||||
import pprint # To pretty print JSON
|
||||
import time # To sleep
|
||||
import os # For VARS from the shell.
|
||||
# Variables
|
||||
import pprint
|
||||
import time
|
||||
|
||||
# Load the configuration file
|
||||
try:
|
||||
@ -30,12 +25,6 @@ except toml.TomlDecodeError:
|
||||
except Exception as e:
|
||||
print("Error while loading file : " + str(e))
|
||||
|
||||
# Define the USB connections here.
|
||||
vendor_id = configuration_file["printer"]["vendor_id"]
|
||||
device_id = configuration_file["printer"]["device_id"]
|
||||
|
||||
# Output the config file
|
||||
if os.getenv('LIPY_DEBUG') == True:
|
||||
pprint.pprint(configuration_file)
|
||||
|
||||
# We define the app module used by Flask
|
||||
@ -45,24 +34,16 @@ app.secret_key = configuration_file["secrets"]["flask_secret_key"]
|
||||
|
||||
# Printer connection
|
||||
# Uses the class defined in the printer.py file
|
||||
|
||||
# Define the USB connections here.
|
||||
vendor_id = configuration_file["printer"]["vendor_id"]
|
||||
device_id = configuration_file["printer"]["device_id"]
|
||||
|
||||
printer = Printer(app,0x04b8, 0x0e28)
|
||||
printer.init_printer()
|
||||
|
||||
# Web routes
|
||||
web = Web(app, printer)
|
||||
|
||||
|
||||
limiter = Limiter(
|
||||
app,
|
||||
key_func=get_remote_address,
|
||||
default_limits=["200 per day", "50 per hour"]
|
||||
)
|
||||
|
||||
@app.route('/')
|
||||
@limiter.limit("1/second", override_defaults=False)
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
# API routes
|
||||
# The api has the following methods
|
||||
# api/print/{sms,img,letter,qr,barcode}
|
||||
@ -70,50 +51,22 @@ def index():
|
||||
# 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')
|
||||
@limiter.limit("1/second", override_defaults=False)
|
||||
def api_index():
|
||||
return render_template("api.html")
|
||||
return "Welcome to the printing software's API. Please see the index page to get started."
|
||||
|
||||
|
||||
@app.route('/api/print/sms', methods=['POST'])
|
||||
@limiter.limit("2/minute", override_defaults=False)
|
||||
@app.route('/api/print/sms', methods=['GET'])
|
||||
def api_print_sms():
|
||||
try:
|
||||
txt = request.form["txt"]
|
||||
sign = request.form["signature"]
|
||||
except Exception as e:
|
||||
flash(e)
|
||||
redirect(url_for('index'))
|
||||
|
||||
flash(web.print_sms(txt,sign))
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/api/print/image', methods=['POST'])
|
||||
@limiter.limit("2/minute", override_defaults=False)
|
||||
def api_print_image():
|
||||
flash(web.print_image())
|
||||
return "print image"
|
||||
|
||||
@app.route('/login')
|
||||
@limiter.limit("1/second", override_defaults=False)
|
||||
def login_page():
|
||||
# web.login(username,password)
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route('/logout')
|
||||
@limiter.limit("1/second", override_defaults=False)
|
||||
def logout_page():
|
||||
# web.logout(username, password)
|
||||
return render_template("index.html")
|
||||
|
||||
@app.errorhandler(429)
|
||||
def ratelimit_handler(e):
|
||||
flash("Rate limit reached, please slow down :) ( Currently at "+ e.description + ")", 'error')
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route("/ping")
|
||||
@limiter.exempt
|
||||
def ping():
|
||||
return "PONG"
|
||||
texte = request.args.get("txt",type=str)
|
||||
app.logger.debug("Printing : " + texte)
|
||||
time.sleep(10)
|
||||
return printer.print_sms(texte)
|
||||
|
@ -1,5 +1,4 @@
|
||||
# Importing the module to mage the connection to the printer.
|
||||
from flask import flash
|
||||
from escpos.printer import Usb, USBNotFoundError
|
||||
from time import sleep, gmtime, strftime
|
||||
import os.path
|
||||
@ -36,27 +35,6 @@ class Printer(object):
|
||||
self.usb_args['idVendor'] = self.device_id
|
||||
self.usb_args['idProduct'] = self.vendor_id
|
||||
|
||||
def check_paper(self) -> bool:
|
||||
# Let's check paper status
|
||||
self.app.logger.debug('Checking paper status...')
|
||||
self.printer.open(self.usb_args)
|
||||
status = self.printer.paper_status()
|
||||
match status:
|
||||
case 0:
|
||||
self.app.logger.error('Printer has no more paper, aborting...')
|
||||
flash("No more paper on the printer. Sorry.",category='error')
|
||||
self.printer.close()
|
||||
return False
|
||||
case 1:
|
||||
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
|
||||
flash('Printer needs paper to be changed very soon ! ', category='info')
|
||||
self.printer.close()
|
||||
return True
|
||||
case 2:
|
||||
self.app.logger.debug('Printer has paper, good to go')
|
||||
self.printer.close()
|
||||
return True
|
||||
|
||||
def init_printer(self):
|
||||
|
||||
# Is the printer online ? Is the communication with the printer successfull ?
|
||||
@ -75,7 +53,6 @@ class Printer(object):
|
||||
try:
|
||||
if p.is_online():
|
||||
self.ready = True
|
||||
self.app.logger.debug('Printer online !')
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
@ -87,48 +64,40 @@ class Printer(object):
|
||||
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 == 2:
|
||||
self.app.logger.debug('Printer has paper, good to go')
|
||||
|
||||
# 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 every time the printer becomes ready, it means
|
||||
# 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.printer = p;
|
||||
self.ready = True;
|
||||
self.printer.close();
|
||||
|
||||
if not self.check_paper():
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def print_sms(self, msg, signature) -> bool:
|
||||
def print_sms(self, msg):
|
||||
clean_msg = str(msg)
|
||||
clean_signature = str(signature)
|
||||
|
||||
if not self.check_paper():
|
||||
return False
|
||||
|
||||
if len(clean_msg) > 256 or len(clean_msg) < 3 :
|
||||
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)))
|
||||
flash("Could not print message of this length :" + str(len(clean_msg)) + ", needs to between 3 and 256 caracters long.",category='error')
|
||||
return False
|
||||
|
||||
if len(signature) > 256 or len(signature) < 3:
|
||||
self.app.logger.warning("Could not print message without a signature.")
|
||||
flash("Could not print message without a signature.",category='error')
|
||||
return False
|
||||
|
||||
if not os.getenv('LIPY_DEBUG') == True:
|
||||
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.set(align='left', font='a', bold=False, underline=0, width=1, height=1, density=8, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
|
||||
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||
self.printer.textln(clean_msg)
|
||||
self.printer.set(align='left', font='b', bold=False, underline=1, width=1, height=1, density=9, invert=False, smooth=True, flip=False, double_width=False, double_height=False, custom_size=False)
|
||||
self.printer.textln("> " + clean_signature + " @ " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||
self.printer.cut()
|
||||
self.printer.close()
|
||||
flash("Message printed : " + clean_msg ,category='info')
|
||||
return True
|
||||
return "Message printed : " + clean_msg
|
||||
|
||||
def print_img(self, path):
|
||||
|
||||
|
7
src/static/css/bootstrap.min.css
vendored
Before Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 14 KiB |
@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LittlePrynter API</title>
|
||||
</head>
|
||||
<body>
|
||||
"Welcome to the printing software's API. Please see the index page to get started.
|
||||
</body>
|
||||
</html>
|
@ -1,60 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LittlePrynter</title>
|
||||
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
|
||||
<link rel="stylesheet" href="{{ url_for('static',filename='css/style.css') }}">
|
||||
</head>
|
||||
<body class="container">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
<img class="rounded" width="100px" src="{{ url_for('static', filename='images/little-printer.png') }}" alt="LittlePrynter icon"><h1 class="card-title font-weight-bold">Little Prynter</h1>
|
||||
<hr>
|
||||
<br>
|
||||
{% with messages = get_flashed_messages(category_filter=('error')) %}
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-warning" role="alert">{{ message }}</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{% with messages = get_flashed_messages(category_filter=('info')) %}
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-info" role="alert">{{ message }}</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
<!-- <br>
|
||||
<h3>Imprimer une image aléatoire</h3>
|
||||
<a class="btn btn-primary btn-lg" href="/api/print/image"> Imprimer</a>
|
||||
<hr>
|
||||
<br> -->
|
||||
<div class="card">
|
||||
<h3 class="card-header">Print a short message</h3>
|
||||
<div class="card-body">
|
||||
|
||||
<form class="form-group" action="/api/print/sms" method="post">
|
||||
<input class="form-control" type="text" name="txt" placeholder="200 chars or less " maxlength="200"><br>
|
||||
<input class="form-control" type="text" name="signature" placeholder="Signature or pseudo" maxlength="200"><br>
|
||||
|
||||
<input class="btn btn-primary float-right" type="submit" value="Imprimer" name="imprimer">
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div id="pbar" style="display:none">
|
||||
<img src="{{ url_for('static',filename='load.gif') }}" alt="loading wheel">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<br>
|
||||
<br>
|
||||
<hr>
|
||||
<footer>Little Prynter is built for fun by <a href="https://n07070.xyz/about-me/">n07070</a> for fun :) </footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -1,6 +0,0 @@
|
||||
class User(object):
|
||||
"""docstring for User."""
|
||||
|
||||
def __init__(self, arg):
|
||||
super(User, self).__init__()
|
||||
self.arg = arg
|
33
src/web.py
@ -1,29 +1,12 @@
|
||||
from flask import Flask, request
|
||||
from printer import Printer
|
||||
import time
|
||||
import os
|
||||
|
||||
class Web(object):
|
||||
class web(object):
|
||||
"""docstring for web."""
|
||||
|
||||
def __init__(self, app, printer ):
|
||||
super(Web).__init__()
|
||||
self.printer = printer
|
||||
self.app = app
|
||||
def __init__(self, arg):
|
||||
super(web, self).__init__()
|
||||
self.arg = arg
|
||||
|
||||
def print_sms(self, texte, sign: str):
|
||||
# TODO: verify the texte before printing it here ?
|
||||
self.app.logger.debug("Printing : " + str(texte))
|
||||
if not os.getenv('LIPY_DEBUG'):
|
||||
time.sleep(10)
|
||||
def login_page():
|
||||
return "login page"
|
||||
|
||||
return self.printer.print_sms(texte, sign)
|
||||
|
||||
def print_image(self, image):
|
||||
pass
|
||||
|
||||
def login(username: str,password: str) -> bool:
|
||||
pass
|
||||
|
||||
def logout(username: str, password: str) -> bool:
|
||||
pass
|
||||
def logout_page():
|
||||
return "logout"
|
||||
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 280 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 67 KiB After Width: | Height: | Size: 67 KiB |
Before Width: | Height: | Size: 236 KiB After Width: | Height: | Size: 236 KiB |
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
Before Width: | Height: | Size: 149 KiB After Width: | Height: | Size: 149 KiB |
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 207 KiB After Width: | Height: | Size: 207 KiB |
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 83 KiB |
Before Width: | Height: | Size: 8.7 KiB After Width: | Height: | Size: 8.7 KiB |
75
templates/index.html
Normal file
@ -0,0 +1,75 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>LittlePrynter</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="{{ url_for('static',filename='css/style.css') }}">
|
||||
</head>
|
||||
<body class="container">
|
||||
<div class="col-md-6 offset-md-3">
|
||||
<h1 class="font-weight-bold">Little Prynter</h1>
|
||||
<hr>
|
||||
<br>
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-danger" role="alert">{{ message }}</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
<br>
|
||||
<h3>Imprimer une image aléatoire</h3>
|
||||
<a class="btn btn-primary btn-lg btn-block" href="/print/image"> Imprimer</a>
|
||||
<hr>
|
||||
<br>
|
||||
<h3>Imprimer ton propre texte</h3>
|
||||
<form class="form-group" action="/print/text" method="post">
|
||||
<input class="form-control" type="text" name="message" placeholder="200 caractères ou moins " maxlength="200"><br>
|
||||
<input class="btn btn-primary float-right" type="submit" value="Imprimer" name="imprimer">
|
||||
</form>
|
||||
|
||||
<div id="pbar" style="display:none">
|
||||
<img src="{{ url_for('static',filename='load.gif') }}" alt="loading wheel">
|
||||
</div>
|
||||
<br>
|
||||
<br>
|
||||
<footer><a href="/logout">Se déconnecter</a></footer>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<script>
|
||||
|
||||
function printImage() {
|
||||
// Make an ajax call to the flask server on the /print/image route to trigger an image to be printer.
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
var x = document.getElementById("pbar");
|
||||
if (x.style.display === "none") {
|
||||
x.style.display = "inline";
|
||||
}
|
||||
if (this.readyState == 4 && this.status == 429) {
|
||||
x.style.display = "none";
|
||||
alert("Hé, attends un peu avant d'imprimer une autre image, je me repose...");
|
||||
} else if (this.readyState == 4 && this.status == 200) {
|
||||
x.style.display = "none";
|
||||
}
|
||||
};
|
||||
xhttp.open("GET", "/print/image", true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
function printText(){
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 400) {
|
||||
alert("Le text est trop long !");
|
||||
}
|
||||
};
|
||||
xhttp.open("GET", "/print/text", true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
</script>
|
||||
</html>
|