Add GET /api/v1/config (Phase 2)
Returns the device's settings document: the stored JSONB, plus the settings that live in real columns, plus config_version last - mirroring configApplyJson, which applies the version only after the rest of the payload has been processed. No response model, deliberately. devices.config is one JSONB document rather than forty columns because the firmware grows tunables faster than a schema should change, and the device merges and clamps whatever arrives: unknown keys ignored, absent keys left alone, dangerous values clamped on the device itself. Partial documents are the normal case, not a degraded one. Validating this shape belongs on the write path, when the admin UI saves a config. Unknown keys are passed through rather than filtered, so the admin UI can push a new firmware tunable without waiting for the server to learn it. Column-backed keys win over any stale copy of themselves in the JSONB. The columns are the modelled source of truth, and a leftover key shadowing one would mean a change to the column never reached the camera. This also closes a loop left open at enrollment. Enrollment refuses to let a device overwrite a rename made in the admin UI, which raised the fair question of how the rename ever reaches the device. It reaches it here: configApplyJson merges device_name like any other key (app_config.cpp:307). The operator owns the name, the server pushes it down, and the device adopts it - so the two decisions are consistent rather than merely defensible. Named device_config.py so it does not read as poorbird.config. The route is /config as specified. Tests: 99 -> 109. Verified by letting the JSONB win over the columns: the precedence test fails, as intended. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5fd3c094a4
commit
e83fc757d0
3 files changed
+244
-3
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""GET /api/v1/config -- the device's settings document.
|
||||
|
||||
Named device_config.py so it does not read as poorbird.config, which is the
|
||||
server's own settings. The route is /config, as API.md specifies.
|
||||
|
||||
The response is a free-form JSON object by design. ``devices.config`` is one
|
||||
JSONB document rather than forty columns because the firmware grows tunables
|
||||
faster than a schema should change, and the device merges and clamps whatever
|
||||
arrives: keys it does not know are ignored, keys that are absent leave its
|
||||
current value alone, and dangerous values are clamped on the device itself
|
||||
(app_config.cpp, configApplyJson). Partial documents are therefore not a
|
||||
degraded case -- they are the normal one.
|
||||
|
||||
That is also why there is no response model. Validating this shape belongs on
|
||||
the write path, when the admin UI saves a config, not on the way out to a
|
||||
device that is going to re-validate it anyway.
|
||||
|
||||
This is a read. It deliberately does not touch last_seen_at: a GET should not
|
||||
have side effects, and /heartbeat is what records liveness (API.md).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from ..deps import DeviceDep
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Settings that live in real columns on `devices` rather than in the JSONB,
|
||||
# because the server queries and lists on them. They are still config keys as
|
||||
# far as the firmware is concerned, so they have to be merged into the
|
||||
# document on the way out or a change to the column would never reach the
|
||||
# camera.
|
||||
#
|
||||
# device_name is the interesting one: enrollment deliberately does not let a
|
||||
# device overwrite an operator's rename, and this is the path by which that
|
||||
# rename actually reaches the device (app_config.cpp:307).
|
||||
_COLUMN_BACKED = (
|
||||
("device_name", "name"),
|
||||
("heartbeat_interval_s", "heartbeat_interval_s"),
|
||||
("status_interval_s", "status_interval_s"),
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/config",
|
||||
summary="The device's full settings document",
|
||||
tags=["devices"],
|
||||
)
|
||||
def get_config(device: DeviceDep) -> dict[str, Any]:
|
||||
# Start from the stored document, then let the column-backed keys win.
|
||||
# The columns are the modelled source of truth; a stale copy of one of
|
||||
# them inside the JSONB must not shadow it.
|
||||
document: dict[str, Any] = dict(device.config or {})
|
||||
|
||||
for key, attr in _COLUMN_BACKED:
|
||||
document[key] = getattr(device, attr)
|
||||
|
||||
# Last, mirroring configApplyJson, which applies config_version only after
|
||||
# the rest of the payload has been processed. This is the only value the
|
||||
# device compares, so it must describe the document it arrived with.
|
||||
document["config_version"] = device.config_version
|
||||
|
||||
return document
|
||||
Reference in new issue
Block a user