Skip to content

fix(api): speak the real TRMNL device protocol - #1

Merged
glani merged 1 commit into
mainfrom
fix/trmnl-device-protocol
Aug 5, 2026
Merged

glani merged 1 commit into
mainfrom
fix/trmnl-device-protocol

Conversation

@glani

@glani glani commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Problem

The server implements a protocol the TRMNL firmware doesn't use, so all device data was lost while endpoints answered 200 OK. Observed in production (mg-1.giglabo.com, 17 days uptime):

  • devices: 4740 rows, all UNKNOWN-<hex>, one per poll
  • device_logs: 0 rows after thousands of accepted POST /api/log
  • battery_readings: 0 rows

Causes and fixes

1. Identification (ID header). The firmware sends its MAC in ID, not X-Device-MAC (terminus doc/api.adoc). _get_device_mac now reads ID first, and returns None for unidentified callers instead of minting a random UNKNOWN-<hex> — that fallback was what registered a new device on every request.

2. Log payload shape. Current firmware posts {"logs": [...]}; only {"log": {"logs_array": [...]}} was handled, so real payloads fell through to a silent debug-level "unknown format" branch. Both shapes are stored now, and an unrecognized body is logged at WARNING with its content — a silent drop is how this went unnoticed for weeks.

3. Battery telemetry. The TRMNL protocol has no battery endpoint. Devices report Battery-Voltage / RSSI / FW-Version / Refresh-Rate in request headers and repeat them inside log entries. Readings are taken from there, throttled to one sample per device per 15 min (the firmware polls every ~2 min). POST /api/battery stays for manual reporting.

Also in scope:

  • last_seen was written in local time while the DB defaults are UTC — a skew equal to the server's offset, visible in the data.
  • LOG_LEVEL / server.log_level config: handler diagnostics are DEBUG and the level was hardcoded to INFO, so nothing could be debugged without rebuilding the image.

Tests

tests/test_device_api.py — 14 tests over a throwaway SQLite DB: ID header identification, anonymous callers registering nothing, both log formats, battery extraction from headers and log entries, sampling throttle, and the loud unrecognized-payload path.

./scripts/check_code.sh passes ruff/black/flake8; mypy is down one pre-existing error, the rest are the untouched yaml-stub baseline.

Follow-ups (not in this PR)

  • Retention/pruning for device_logs, battery_readings, devices
  • One-off cleanup of the 4739 junk device rows in prod
  • GET /api/setup mints a fresh random api_key per call and never persists it

🤖 Generated with Claude Code

The server implemented a protocol the firmware does not use, so every piece of
device data was lost while the endpoints answered 200 OK. In production this
showed up as 4740 rows in `devices` (all `UNKNOWN-<hex>`, one per poll for 17
days), an empty `device_logs` after thousands of accepted `POST /api/log`, and
an empty `battery_readings`.

Three causes, all fixed here:

* Identification: the firmware sends its MAC in the `ID` header, not
  `X-Device-MAC`. `_get_device_mac` now reads `ID` first and returns None for
  unidentified callers instead of minting a random `UNKNOWN-<hex>` identity —
  that fallback was what registered a new device on every request.

* Log payload: current firmware posts `{"logs": [...]}` (usetrmnl/terminus
  doc/api.adoc). Only `{"log": {"logs_array": [...]}}` was handled, so real
  payloads fell through to a silent debug-level "unknown format" branch.
  Both shapes are now stored, and an unrecognized body is logged at WARNING
  with its content.

* Battery: the TRMNL protocol has no battery endpoint. Devices report voltage,
  RSSI and firmware version in request headers and inside log entries, so
  telemetry is read from there, throttled to one sample per device per 15 min.
  `POST /api/battery` stays for manual reporting.

Also: `last_seen` was written in local time while the DB defaults are UTC,
skewing the two by the server's offset; log level is now configurable via
`LOG_LEVEL` / `server.log_level`, since the handler diagnostics are DEBUG and
the level was hardcoded to INFO.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@glani
glani merged commit e8abee0 into main Aug 5, 2026
2 checks passed
@glani
glani deleted the fix/trmnl-device-protocol branch August 5, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant