Files
tajniak81andClaude Opus 4.8 33595c99e8 Add a Drones fleet section that fills itself in on connect
The fleet lived as a tab inside the Logbook, which buried it, and every
drone had to be typed in by hand — model, serial and firmware copied off
an airframe the app was already talking to.

Promote it to its own nav section above Logbook, and let a connecting
drone register itself. The Fly App already forwarded model, serial and
firmware upstream; the hub was keeping only the model. It now carries the
identity through to DeviceState, and the Web App offers it to a new
POST /api/drones/auto, which upserts keyed by serial. The auto path only
writes what the aircraft is authoritative about (model, both firmware
versions) and never touches what the pilot curates.

Serial and the firmware versions resolve on their own schedules after
connect — the serial in seconds, the aircraft firmware sometimes a minute
later — so nothing along the path treats an absent value as a cleared one,
and a later event filling firmware in still reaches the server. The auto
call rides every telemetry frame, so the client remembers the identity
tuple it last sent and only a change goes out; a 4xx is the server's
settled answer and is not retried, or one drone connected for an hour
would mean one request per frame for an hour.

New fields on drones: firmware, controller_firmware, and registration for
the FAA/CAA aircraft number — distinct from operator_number, which stays
the EU operator ID. Controller firmware is the remote controller's own
version, read from its component; the flight controller's version is a
different quantity and stays off this field (see 002e484). name becomes
optional and is now the pilot's custom name: auto-added drones arrive
unnamed, so the API serves a computed displayName (name, else model +
serial) for the fleet table, the flight picker and the CSV export. A
unique index on serial is what keeps the find-then-create path from
forking a drone's history across two records.

The schema is applied to the remote PocketBase; the migration is here for
fresh deployments, which the remote does not read.

Verified against a simulated device over the real socket with identity
resolving late: one record from four events, both firmware versions
filled, curated fields intact across re-registration, and a drone deleted
while connected coming back on the next frame.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-16 17:04:12 +02:00

59 lines
2.6 KiB
JavaScript

/// <reference path="../pb_data/types.d.ts" />
// Extends `drones` with the identity a connected aircraft reports over the wire
// (serial + firmware were already modelled; the firmware versions and the
// aircraft registration were not), so that connecting a drone in the Fly App can
// auto-populate its fleet entry in the Web App's Drones tab.
//
// Added fields:
// - firmware aircraft firmware package version (auto-filled)
// - controller_firmware remote-controller firmware version (auto-filled)
// - registration FAA/CAA aircraft registration number (hand-entered).
// Distinct from `operator_number`, which is the EU/
// Trafikstyrelsen *operator* ID displayed on the drone.
//
// `name` is also relaxed to optional: it is the pilot's custom label, and a
// drone auto-added on connection has no label until the pilot gives it one (the
// UI falls back to model + serial).
//
// Apply by copying into your PocketBase deployment's `pb_migrations/` directory
// and restarting. Written for PocketBase v0.22+/v0.23. Idempotent: each field is
// added only if absent, so re-running is a no-op.
//
// Depends on 1720300700_add_logbook.js (drones).
migrate(
(app) => {
const drones = app.findCollectionByNameOrId('drones')
const add = (field) => {
if (!drones.fields.find((f) => f.name === field.name)) drones.fields.add(new Field(field))
}
add({ name: 'firmware', type: 'text', max: 80 })
add({ name: 'controller_firmware', type: 'text', max: 80 })
add({ name: 'registration', type: 'text', max: 60 })
// The custom name is optional — auto-added drones arrive unnamed.
const name = drones.fields.find((f) => f.name === 'name')
if (name) name.required = false
// One fleet entry per serial: the auto-add path keys off the serial, and a
// duplicate would silently fork a drone's history across two records.
const idx = 'CREATE UNIQUE INDEX `idx_drones_serial` ON `drones` (`serial`) WHERE `serial` != \'\''
if (!drones.indexes.find((i) => i.includes('idx_drones_serial'))) drones.indexes.push(idx)
app.save(drones)
},
(app) => {
const drones = app.findCollectionByNameOrId('drones')
for (const n of ['firmware', 'controller_firmware', 'registration']) {
const f = drones.fields.find((x) => x.name === n)
if (f) drones.fields.removeById(f.id)
}
const name = drones.fields.find((f) => f.name === 'name')
if (name) name.required = true
drones.indexes = drones.indexes.filter((i) => !i.includes('idx_drones_serial'))
app.save(drones)
},
)