Repository navigation
fix(api): speak the real TRMNL device protocol - #1
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, allUNKNOWN-<hex>, one per polldevice_logs: 0 rows after thousands of acceptedPOST /api/logbattery_readings: 0 rowsCauses and fixes
1. Identification (
IDheader). The firmware sends its MAC inID, notX-Device-MAC(terminus doc/api.adoc)._get_device_macnow readsIDfirst, and returnsNonefor unidentified callers instead of minting a randomUNKNOWN-<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-Ratein 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/batterystays for manual reporting.Also in scope:
last_seenwas 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_levelconfig: 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:IDheader 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.shpasses ruff/black/flake8; mypy is down one pre-existing error, the rest are the untouched yaml-stub baseline.Follow-ups (not in this PR)
device_logs,battery_readings,devicesGET /api/setupmints a fresh randomapi_keyper call and never persists it🤖 Generated with Claude Code