Send each print as one job, queue web prints, and harden deploys
Render every print to a complete ESC/POS payload (reset, content, cut)
and send it once over a fresh connection. Only the connect is retried,
so a link that drops mid-job can no longer print a partial image followed
by a full one, and no text size leaks into the next job. Prints are
capped at about 1 m of paper: tall images shrink, over-long notes are
refused.
Replace the argparse/stdout plumbing with PrinterConfig, InvalidJob and
PrinterUnavailable, and one run_command() dispatcher shared by the CLI
and TUI. Status now really connects; before, escpos's lazy open meant
the web server always reported the printer ready. Rendering with the
TM-T20II profile makes QR centering take effect.
The web server validates and renders jobs in the request (400 on bad
input), queues them for a single worker and answers 202; the page
long-polls /api/jobs/{id}, so no request outlives nginx's proxy timeout
while its job still prints. Status reports busy instead of waiting, and
jobs are logged with the nginx user.
update.sh tests new commits in a scratch worktree before touching the
live clone, and rolls back if /healthz doesn't answer after the restart.
The unit runs from the venv, and the web server defaults to localhost.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+31
-16
@@ -2,30 +2,45 @@
|
||||
|
||||
import argparse
|
||||
|
||||
from escpos import printer
|
||||
|
||||
import receipt
|
||||
|
||||
|
||||
class FakePrinter:
|
||||
"""Records escpos calls instead of talking to hardware."""
|
||||
"""Records the connection lifecycle and raw job payloads instead of talking to hardware."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, fail_open=0, fail_raw=False):
|
||||
self.calls = []
|
||||
self.payloads = []
|
||||
self.fail_open = fail_open # how many open() calls raise before one succeeds
|
||||
self.fail_raw = fail_raw
|
||||
|
||||
def textln(self, text=""):
|
||||
self.calls.append(("textln", text))
|
||||
def open(self):
|
||||
self.calls.append("open")
|
||||
if self.fail_open:
|
||||
self.fail_open -= 1
|
||||
raise OSError("no route to printer")
|
||||
|
||||
def image(self, img, **kwargs):
|
||||
self.calls.append(("image", img))
|
||||
|
||||
def qr(self, content, **kwargs):
|
||||
self.calls.append(("qr", content))
|
||||
|
||||
def set(self, **kwargs):
|
||||
self.calls.append(("set", kwargs))
|
||||
|
||||
def cut(self):
|
||||
self.calls.append(("cut", None))
|
||||
def _raw(self, data):
|
||||
self.calls.append("raw")
|
||||
if self.fail_raw:
|
||||
raise OSError("connection reset")
|
||||
self.payloads.append(data)
|
||||
|
||||
def close(self):
|
||||
self.calls.append(("close", None))
|
||||
self.calls.append("close")
|
||||
|
||||
|
||||
def escpos_bytes(build):
|
||||
"""What the given escpos calls render to, for asserting on payloads."""
|
||||
p = printer.Dummy(profile=receipt.PRINTER_PROFILE)
|
||||
build(p)
|
||||
return p.output
|
||||
|
||||
|
||||
INIT = escpos_bytes(lambda p: p.hw("INIT"))
|
||||
CUT = escpos_bytes(lambda p: p.cut())
|
||||
|
||||
|
||||
def make_args(**overrides):
|
||||
|
||||
Reference in New Issue
Block a user