updated python script to use printservant instead of local lpr

pull/2/head
frederic 2024-02-01 23:56:34 +01:00
parent f1dbd2b0b1
commit e563f01c72
3 changed files with 16 additions and 63 deletions

View File

@ -36,11 +36,12 @@ RUN apt-get update && apt-get upgrade -y \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
WORKDIR /labelmaker
COPY ./poetry.lock ./pyproject.toml /labelmaker/
RUN git clone https://code.hackerspace.pl/etorameth/labelmaker
WORKDIR /labelmaker
RUN poetry install --no-interaction --no-ansi --sync
COPY . /labelmaker
RUN useradd web
USER web
CMD poetry run python labelmaker/__main__.py
CMD poetry run python labelmaker/main.py

View File

@ -6,14 +6,15 @@ import time
import cairocffi as cairo
import flask
import img2pdf
import pangocffi as pango
import pangocairocffi as pangocairo
import requests
class App(flask.Flask):
def __init__(self, *args, **kwargs):
super(App, self).__init__(*args, **kwargs)
self.last = 0
self.health = (0, False, "unknown")
app = App(__name__)
@ -73,68 +74,33 @@ class Renderer(object):
def index():
return flask.render_template('index.html')
def healthcheck():
last_checked, last_status, last_details = app.health
if time.time() - last_checked < 1:
return last_status, last_details
output = subprocess.check_output(['lpstat', '-p', '-d']).decode()
mark = False
for line in output.split('\n'):
line = line.strip()
if line.startswith('printer DYMO_LabelWriter450'):
if 'is idle.' in line:
return True, 'Idle'
mark = True
continue
if mark:
if line.startswith('Ready to print'):
app.health = (time.time(), True, line)
return True, line
else:
app.health = (time.time(), False, line)
return False, line
mark = False
return False, "PRINTER_NOT_CONNECTED"
@app.route('/health')
def health():
ok, details = healthcheck()
return json.dumps({'ok': ok, 'details': details})
@app.route('/api/preview/<int:size>/')
def stuff_preview(size):
text = flask.request.args.get('text')
html = flask.request.args.get('html') == '1'
r = Renderer()
r.render_text(text, 'Sans {}'.format(size), 0, -1, html)
renderer = Renderer()
renderer.render_text(text, 'Sans {}'.format(size), 0, -1, html)
preview = r.surface.write_to_png()
preview = renderer.surface.write_to_png()
return flask.Response(preview, mimetype='image/png')
DELAY = 5
@app.route('/api/print/<int:size>/', methods=['POST'])
def stuff_print(size):
if not healthcheck()[0]:
return 'Printer is down.'
last = app.last
print(last, time.time() - last)
if time.time() - last < DELAY:
return 'Please wait {} more seconds before next print.'.format(int(DELAY - (time.time() - last)))
text = flask.request.args.get('text')
html = flask.request.args.get('html') == '1'
r = Renderer()
r.render_text(text, 'Sans {}'.format(size), 0, -1, html)
path = '/tmp/hslabel'
f = open(path, 'wb')
renderer = Renderer()
renderer.render_text(text, 'Sans {}'.format(size), 0, -1, html)
r.surface.write_to_png(f)
f.flush()
f.close()
time.sleep(1)
ex = 'lpr -o PageSize=w118h252 -P DYMO_LabelWriter450 {}'.format(path)
os.system(ex)
data = img2pdf.convert(renderer.surface.write_to_png())
payload = {'printer': 'dymo_labelwriter450', 'copies': 1, 'body': data}
r = requests.post(os.environ.get('PRINTSERVANT_HOST') + '/print', params=payload)
print('Printing job response', r.text)
f.close()
app.last = time.time()
return 'ok'

View File

@ -50,7 +50,6 @@
<div class="container">
<div class="page-header">
<h1>Hackerspace Printing System For Printing Labels</h1>
<h4>System status: <span class="label label-default" id="systemstatus">unknown</span></h4>
</div>
<h3>Box 'o Stuff Label <small>For SAMLA boxes with common equipment</small></h3>
<div class="row">
@ -132,19 +131,6 @@ $ curl -d "" http://label.waw.hackerspace.pl/api/print/60/?text=foobar&amp;html=
print();
});
generatePreview();
var updateStatus = function () {
$.getJSON("/health", function (data) {
if (data.ok) {
$("#systemstatus").attr("class", "label label-success");
$("#systemstatus").html(data.details);
} else {
$("#systemstatus").attr("class", "label label-danger");
$("#systemstatus").html(data.details);
}
});
};
updateStatus();
setInterval(updateStatus, 1000);
});
</script>
</body>