Every Gitea Actions run failed in actions/checkout: the stpvm runner is registered against http://localhost:3000, which an ubuntu-latest job container can't reach. Add .gitea/workflows/ci.yml, which Gitea uses in place of the GitHub workflow, following web_server's pattern: host mode, a plain git fetch over Gitea's SSH port, and ruff + pytest inside python:<version>-slim containers. That surfaced a real failure: test_serve_refuses_without_token imports uvicorn, which only the web extra installed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
147 lines
7.2 KiB
Markdown
147 lines
7.2 KiB
Markdown
# Receipt Maker
|
|
|
|
Prints images, text notes, and QR codes to an Epson TM-T20II thermal receipt printer, including random images from 4chan's /bant/ board.
|
|
|
|
## How it works
|
|
|
|
1. `connect_receipt.sh` — brings up the USB Ethernet interface, assigns `192.168.192.10/24`, and waits until the printer at `192.168.192.168:9100` is reachable
|
|
2. `receipt.py` — CLI with subcommands; images are resized in memory to the printer's full 576px width before printing
|
|
|
|
The CLI runs the connect script automatically before printing (unless `--usb` is used).
|
|
|
|
## Requirements
|
|
|
|
- Python 3.10+
|
|
- `python-escpos`, `Pillow`, `requests` (see `pyproject.toml`)
|
|
- `textual` (optional, only for the `tui` subcommand; `pip install '.[tui]'`)
|
|
- `fastapi`, `uvicorn`, `python-multipart` (optional, only for the `serve` subcommand; `pip install '.[web]'`)
|
|
- Epson TM-T20II connected via a USB-to-Ethernet adapter (or directly over USB with `--usb`)
|
|
|
|
Install the dependencies (or `pip install .` to also get a `receipt` command on your PATH):
|
|
|
|
```bash
|
|
pip install python-escpos Pillow requests
|
|
```
|
|
|
|
## Usage
|
|
|
|
```bash
|
|
# Print a random image from /bant/ (optionally more than one)
|
|
./receipt.py image
|
|
./receipt.py image 3
|
|
|
|
# Print a text note (from an argument or stdin), word-wrapped to 48 columns
|
|
./receipt.py note "your message here"
|
|
fortune | ./receipt.py note
|
|
./receipt.py note -t "with a date/time header"
|
|
./receipt.py note --size 2 "bigger text" # 2x: 24 columns, 3x: 16 columns
|
|
|
|
# Print a local image (with optional caption)
|
|
./receipt.py print path/to/image.jpg
|
|
./receipt.py print path/to/image.jpg "optional caption"
|
|
|
|
# Print a QR code (with optional caption)
|
|
./receipt.py qr "https://example.com" "scan me"
|
|
|
|
# Reprint a cached image (most recent, or by name from `list`)
|
|
./receipt.py reprint
|
|
./receipt.py reprint 1783716801701399
|
|
|
|
# Check the printer is reachable
|
|
./receipt.py status
|
|
|
|
# List or delete the downloaded image cache
|
|
./receipt.py list
|
|
./receipt.py clean
|
|
|
|
# Open the interactive terminal UI (needs `pip install textual`)
|
|
./receipt.py tui
|
|
|
|
# Use a direct USB connection instead of Ethernet
|
|
./receipt.py --usb note "hello"
|
|
|
|
# Point at a different printer (or set RECEIPT_PRINTER_IP / RECEIPT_PRINTER_PORT)
|
|
./receipt.py --ip 10.0.0.5 --port 9100 status
|
|
```
|
|
|
|
Images are prepared for thermal output automatically: transparency is composited onto white, then the image is resized to full width, autocontrast-stretched, and Floyd-Steinberg dithered. Nothing prints longer than about 1 m: a very tall image is shrunk to that height (and centered), and a note that long is refused.
|
|
|
|
Each print is rendered to one complete ESC/POS job (printer reset, content, cut) and sent over a fresh connection. A failed connect is retried once; a connection that drops mid-job is reported, never resent, so a flaky link can't print the same thing twice.
|
|
|
|
## Printing from the website
|
|
|
|
`./receipt.py serve` runs a small web server (`receipt_web.py`, needs `pip install '.[web]'`) with a page for printing a note or an uploaded image. It runs on stppi (the Raspberry Pi). The printer has no Wi-Fi (its UB-E04 interface needs an optional Epson wireless adapter), so it is cabled to the Pi's `eth0` at its default `192.168.192.168`; the Pi itself is online over Wi-Fi. The site's nginx on stpvm gates `https://www.adam-french.co.uk/receipt/` on the admin login, then forwards over Tailscale to this server and adds an `X-Receipt-Token` header. The server answers nothing but `/healthz` without that token.
|
|
|
|
```
|
|
browser ─https─> nginx (stpvm, admin auth_request) ─Tailscale─> receipt serve (stppi:8765) ─eth0─> printer
|
|
```
|
|
|
|
`serve` never runs the sudo connect script (`--no-setup` does the same for other commands), so the printer must already be reachable. The printer doesn't hand out addresses, so the port it is cabled to needs a permanent one:
|
|
|
|
```bash
|
|
sudo nmcli con add type ethernet ifname eth0 con-name receipt-printer connection.autoconnect-priority 10 \
|
|
ipv4.method manual ipv4.addresses 192.168.192.10/24 ipv4.never-default yes ipv6.method disabled
|
|
```
|
|
|
|
### Setting up the server machine
|
|
|
|
1. Clone from Gitea to `~/Coding/receipt_maker` (the path the units use), and install into a venv there:
|
|
|
|
```bash
|
|
git clone ssh://[email protected]:2222/adamf/receipt_maker.git ~/Coding/receipt_maker
|
|
cd ~/Coding/receipt_maker
|
|
python3 -m venv .venv && .venv/bin/pip install '.[web,dev]'
|
|
```
|
|
|
|
2. Create the settings file. Put the same token in the web_server `.env` as `RECEIPT_WEB_TOKEN`:
|
|
|
|
```bash
|
|
(umask 077; printf 'RECEIPT_WEB_TOKEN=%s\nRECEIPT_WEB_LISTEN=%s:8765\nRECEIPT_PRINTER_IP=192.168.192.168\n' \
|
|
"$(openssl rand -hex 32)" "$(tailscale ip -4)" > ~/.config/receipt-web.env)
|
|
```
|
|
|
|
3. Install the units (the server runs from the venv) and start everything at boot:
|
|
|
|
```bash
|
|
cp deploy/receipt-web.service deploy/receipt-update.{service,timer} ~/.config/systemd/user/
|
|
systemctl --user daemon-reload
|
|
systemctl --user enable --now receipt-web receipt-update.timer
|
|
sudo loginctl enable-linger "$USER"
|
|
```
|
|
|
|
4. On stpvm, set `RECEIPT_WEB_UPSTREAM=<tailscale ip>:8765` in web_server's `.env`, then `docker compose up -d nginx`.
|
|
|
|
Jobs are checked and rendered when submitted (a bad note or image is rejected straight away), then queued and printed one at a time. The page shows each job as queued, printing, then done or failed, and the status pill shows "printer busy" during a print rather than waiting for it. Each job is logged to the service's journal with the signed-in user nginx forwards. If the server machine is off, the site shows "printer machine offline".
|
|
|
|
### Automatic updates
|
|
|
|
`receipt-update.timer` runs `deploy/update.sh` every 5 minutes. When `master` on Gitea has moved, the script:
|
|
|
|
1. runs the tests on the new commit in a scratch worktree (with a throwaway venv if `pyproject.toml` changed)
|
|
2. fast-forwards the clone and, if needed, reinstalls the dependencies
|
|
3. restarts `receipt-web`
|
|
4. waits up to 20 s for `/healthz` to answer
|
|
|
|
If the tests fail, the live clone is never touched. If `/healthz` doesn't answer, the previous commit is put back and restarted. Either way, the old code keeps serving and that commit is skipped until a newer one is pushed. Its output is in `journalctl --user -u receipt-update`. Pushing to Gitea is the whole deploy.
|
|
|
|
## Development
|
|
|
|
```bash
|
|
pip install -e '.[dev]'
|
|
pytest # run the tests
|
|
ruff check . # lint
|
|
```
|
|
|
|
Both checks also run in CI on every push, on Python 3.10, 3.12 and 3.13. Gitea Actions uses `.gitea/workflows/ci.yml`: the runner on stpvm runs it in host mode and does the checks inside `python:<version>-slim` containers, because its docker-mode jobs can't reach Gitea for `actions/checkout`. `.github/workflows/ci.yml` is the same CI for GitHub.
|
|
|
|
## Files
|
|
|
|
- `receipt.py` — main script
|
|
- `receipt_tui.py` — Textual terminal UI (`./receipt.py tui`)
|
|
- `receipt_web.py` — web server for printing from the site (`./receipt.py serve`)
|
|
- `deploy/receipt-web.service` — systemd user unit for the web server
|
|
- `deploy/update.sh`, `deploy/receipt-update.{service,timer}` — automatic updates from Gitea
|
|
- `connect_receipt.sh` — network setup for the USB Ethernet adapter
|
|
- `tests/` — pytest suite for the CLI, TUI and web server (no printer or network needed)
|
|
- `img/` — downloaded images cache (gitignored; clear with `./receipt.py clean`)
|