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>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3e066c0a99
commit
33595c99e8
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -33,38 +34,66 @@ func (s *Server) requireUser(next http.HandlerFunc) http.HandlerFunc {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type droneRecord struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
OperatorNumber string `json:"operator_number"`
|
||||
MtomGrams float64 `json:"mtom_grams"`
|
||||
IsToy bool `json:"is_toy"`
|
||||
AutologsFlights bool `json:"autologs_flights"`
|
||||
CClass string `json:"c_class"`
|
||||
Organization string `json:"organization"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"` // pilot's custom label; blank on auto-added drones
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
Firmware string `json:"firmware"`
|
||||
ControllerFirmware string `json:"controller_firmware"`
|
||||
Registration string `json:"registration"`
|
||||
OperatorNumber string `json:"operator_number"`
|
||||
MtomGrams float64 `json:"mtom_grams"`
|
||||
IsToy bool `json:"is_toy"`
|
||||
AutologsFlights bool `json:"autologs_flights"`
|
||||
CClass string `json:"c_class"`
|
||||
Organization string `json:"organization"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
type droneView struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
OperatorNumber string `json:"operatorNumber"`
|
||||
MtomGrams float64 `json:"mtomGrams"`
|
||||
IsToy bool `json:"isToy"`
|
||||
AutologsFlights bool `json:"autologsFlights"`
|
||||
CClass string `json:"cClass"`
|
||||
Organization string `json:"organization"`
|
||||
Created string `json:"created"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
Firmware string `json:"firmware"`
|
||||
ControllerFirmware string `json:"controllerFirmware"`
|
||||
Registration string `json:"registration"`
|
||||
OperatorNumber string `json:"operatorNumber"`
|
||||
MtomGrams float64 `json:"mtomGrams"`
|
||||
IsToy bool `json:"isToy"`
|
||||
AutologsFlights bool `json:"autologsFlights"`
|
||||
CClass string `json:"cClass"`
|
||||
Organization string `json:"organization"`
|
||||
Created string `json:"created"`
|
||||
}
|
||||
|
||||
// displayName is what to call the drone in lists, logbook entries and the CSV
|
||||
// export. The custom name wins; a drone auto-added on connection has none, so
|
||||
// fall back to what the aircraft reported about itself.
|
||||
func (d droneRecord) displayName() string {
|
||||
if n := strings.TrimSpace(d.Name); n != "" {
|
||||
return n
|
||||
}
|
||||
if m := strings.TrimSpace(d.Model); m != "" {
|
||||
if s := strings.TrimSpace(d.Serial); s != "" {
|
||||
return m + " · " + s
|
||||
}
|
||||
return m
|
||||
}
|
||||
if s := strings.TrimSpace(d.Serial); s != "" {
|
||||
return s
|
||||
}
|
||||
return "Unnamed drone"
|
||||
}
|
||||
|
||||
func (d droneRecord) view() droneView {
|
||||
return droneView{
|
||||
ID: d.ID, Name: d.Name, Model: d.Model, Serial: d.Serial,
|
||||
OperatorNumber: d.OperatorNumber, MtomGrams: d.MtomGrams, IsToy: d.IsToy,
|
||||
ID: d.ID, Name: d.Name, DisplayName: d.displayName(), Model: d.Model, Serial: d.Serial,
|
||||
Firmware: d.Firmware, ControllerFirmware: d.ControllerFirmware,
|
||||
Registration: d.Registration, OperatorNumber: d.OperatorNumber,
|
||||
MtomGrams: d.MtomGrams, IsToy: d.IsToy,
|
||||
AutologsFlights: d.AutologsFlights, CClass: d.CClass,
|
||||
Organization: d.Organization, Created: d.Created,
|
||||
}
|
||||
@@ -142,7 +171,7 @@ func (f flightRecord) view(drones map[string]droneRecord) flightView {
|
||||
}
|
||||
name := ""
|
||||
if d != nil {
|
||||
name = d.Name
|
||||
name = d.displayName()
|
||||
}
|
||||
return flightView{
|
||||
ID: f.ID, OperationDate: f.OperationDate, StartTime: f.StartTime, EndTime: f.EndTime,
|
||||
@@ -285,7 +314,9 @@ func (s *Server) dronesInScope(ctx context.Context, who *callerIdentity) (map[st
|
||||
var list struct {
|
||||
Items []droneRecord `json:"items"`
|
||||
}
|
||||
if _, err := s.listRecords(ctx, "drones", droneScopeFilter(who), "name", &list); err != nil {
|
||||
// Sorted by creation, not name: the custom name is optional, so sorting by it
|
||||
// would bunch every auto-added drone together under a blank key.
|
||||
if _, err := s.listRecords(ctx, "drones", droneScopeFilter(who), "created", &list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := make(map[string]droneRecord, len(list.Items))
|
||||
@@ -388,15 +419,18 @@ func (s *Server) handleListDrones(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
type droneInput struct {
|
||||
Name string `json:"name"`
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
OperatorNumber string `json:"operatorNumber"`
|
||||
MtomGrams float64 `json:"mtomGrams"`
|
||||
IsToy bool `json:"isToy"`
|
||||
AutologsFlights bool `json:"autologsFlights"`
|
||||
CClass string `json:"cClass"`
|
||||
Organization *string `json:"organization"` // superadmin may target any org
|
||||
Name string `json:"name"` // custom label; optional
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
Firmware string `json:"firmware"`
|
||||
ControllerFirmware string `json:"controllerFirmware"`
|
||||
Registration string `json:"registration"`
|
||||
OperatorNumber string `json:"operatorNumber"`
|
||||
MtomGrams float64 `json:"mtomGrams"`
|
||||
IsToy bool `json:"isToy"`
|
||||
AutologsFlights bool `json:"autologsFlights"`
|
||||
CClass string `json:"cClass"`
|
||||
Organization *string `json:"organization"` // superadmin may target any org
|
||||
}
|
||||
|
||||
func (in droneInput) payload(who *callerIdentity) map[string]any {
|
||||
@@ -405,18 +439,30 @@ func (in droneInput) payload(who *callerIdentity) map[string]any {
|
||||
org = strings.TrimSpace(*in.Organization)
|
||||
}
|
||||
return map[string]any{
|
||||
"name": strings.TrimSpace(in.Name),
|
||||
"model": strings.TrimSpace(in.Model),
|
||||
"serial": strings.TrimSpace(in.Serial),
|
||||
"operator_number": strings.TrimSpace(in.OperatorNumber),
|
||||
"mtom_grams": in.MtomGrams,
|
||||
"is_toy": in.IsToy,
|
||||
"autologs_flights": in.AutologsFlights,
|
||||
"c_class": strings.TrimSpace(in.CClass),
|
||||
"organization": org,
|
||||
"name": strings.TrimSpace(in.Name),
|
||||
"model": strings.TrimSpace(in.Model),
|
||||
"serial": strings.TrimSpace(in.Serial),
|
||||
"firmware": strings.TrimSpace(in.Firmware),
|
||||
"controller_firmware": strings.TrimSpace(in.ControllerFirmware),
|
||||
"registration": strings.TrimSpace(in.Registration),
|
||||
"operator_number": strings.TrimSpace(in.OperatorNumber),
|
||||
"mtom_grams": in.MtomGrams,
|
||||
"is_toy": in.IsToy,
|
||||
"autologs_flights": in.AutologsFlights,
|
||||
"c_class": strings.TrimSpace(in.CClass),
|
||||
"organization": org,
|
||||
}
|
||||
}
|
||||
|
||||
// identifiable reports whether the input says *anything* about which aircraft
|
||||
// this is. The custom name is optional (auto-added drones have none), but a
|
||||
// record with no name, model and serial is not a drone, it is an empty row.
|
||||
func (in droneInput) identifiable() bool {
|
||||
return strings.TrimSpace(in.Name) != "" ||
|
||||
strings.TrimSpace(in.Model) != "" ||
|
||||
strings.TrimSpace(in.Serial) != ""
|
||||
}
|
||||
|
||||
// POST /api/drones — register a drone (assigned to the caller's org).
|
||||
func (s *Server) handleCreateDrone(w http.ResponseWriter, r *http.Request) {
|
||||
who := caller(r)
|
||||
@@ -425,8 +471,8 @@ func (s *Server) handleCreateDrone(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "invalid json")
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
writeError(w, http.StatusBadRequest, "drone name is required")
|
||||
if !in.identifiable() {
|
||||
writeError(w, http.StatusBadRequest, "give the drone a custom name, model or serial")
|
||||
return
|
||||
}
|
||||
data, status, err := s.admin.do(r.Context(), http.MethodPost,
|
||||
@@ -444,6 +490,121 @@ func (s *Server) handleCreateDrone(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"drone": d.view()})
|
||||
}
|
||||
|
||||
// autoDroneInput is the identity a connected aircraft reports about itself.
|
||||
// Everything the pilot curates by hand (custom name, registration, MTOM, class)
|
||||
// is deliberately absent — the auto path never touches those.
|
||||
type autoDroneInput struct {
|
||||
Model string `json:"model"`
|
||||
Serial string `json:"serial"`
|
||||
Firmware string `json:"firmware"`
|
||||
ControllerFirmware string `json:"controllerFirmware"`
|
||||
}
|
||||
|
||||
// findDroneBySerial looks a drone up across *all* orgs, ignoring caller scope:
|
||||
// the serial is unique per airframe, so the caller's own scope is not enough to
|
||||
// know whether the record already exists.
|
||||
func (s *Server) findDroneBySerial(ctx context.Context, serial string) (droneRecord, bool, error) {
|
||||
var list struct {
|
||||
Items []droneRecord `json:"items"`
|
||||
}
|
||||
filter := "serial = " + strconv.Quote(serial)
|
||||
if _, err := s.listRecords(ctx, "drones", filter, "created", &list); err != nil {
|
||||
return droneRecord{}, false, err
|
||||
}
|
||||
if len(list.Items) == 0 {
|
||||
return droneRecord{}, false, nil
|
||||
}
|
||||
return list.Items[0], true, nil
|
||||
}
|
||||
|
||||
// POST /api/drones/auto — upsert the drone the caller just connected, keyed by
|
||||
// serial. Called by the Web App when a device reports a connected aircraft, so
|
||||
// the fleet fills itself in without the pilot typing anything.
|
||||
//
|
||||
// Idempotent by design: it runs on every connection event, so an existing entry
|
||||
// is refreshed (firmware changes as the pilot updates the aircraft) rather than
|
||||
// duplicated, and the response says which happened.
|
||||
func (s *Server) handleAutoDrone(w http.ResponseWriter, r *http.Request) {
|
||||
who := caller(r)
|
||||
var in autoDroneInput
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid json")
|
||||
return
|
||||
}
|
||||
serial := strings.TrimSpace(in.Serial)
|
||||
if serial == "" {
|
||||
// No serial means no stable identity to key on — auto-adding here would
|
||||
// mint a fresh drone on every reconnect.
|
||||
writeError(w, http.StatusBadRequest, "serial is required to auto-add a drone")
|
||||
return
|
||||
}
|
||||
|
||||
existing, found, err := s.findDroneBySerial(r.Context(), serial)
|
||||
if err != nil {
|
||||
gatewayError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if found {
|
||||
if !canManageDrone(who, existing) {
|
||||
writeError(w, http.StatusConflict, "this drone is registered to another organisation")
|
||||
return
|
||||
}
|
||||
// Refresh only what the aircraft is authoritative about, and only when it
|
||||
// actually reported a value — a nil/absent field means "not resolved yet"
|
||||
// (serial and firmware resolve on different schedules), never "cleared".
|
||||
patch := map[string]any{}
|
||||
if m := strings.TrimSpace(in.Model); m != "" && m != existing.Model {
|
||||
patch["model"] = m
|
||||
}
|
||||
if f := strings.TrimSpace(in.Firmware); f != "" && f != existing.Firmware {
|
||||
patch["firmware"] = f
|
||||
}
|
||||
if cf := strings.TrimSpace(in.ControllerFirmware); cf != "" && cf != existing.ControllerFirmware {
|
||||
patch["controller_firmware"] = cf
|
||||
}
|
||||
if len(patch) == 0 {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"drone": existing.view(), "created": false, "updated": false})
|
||||
return
|
||||
}
|
||||
data, status, err := s.admin.do(r.Context(), http.MethodPatch,
|
||||
"/api/collections/drones/records/"+url.PathEscape(existing.ID), patch)
|
||||
if err != nil {
|
||||
gatewayError(w, err)
|
||||
return
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
relayRaw(w, status, data)
|
||||
return
|
||||
}
|
||||
var d droneRecord
|
||||
_ = json.Unmarshal(data, &d)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"drone": d.view(), "created": false, "updated": true})
|
||||
return
|
||||
}
|
||||
|
||||
// New airframe: record what it reported and leave the curated fields blank
|
||||
// for the pilot to fill in on the Drones tab.
|
||||
payload := droneInput{
|
||||
Model: strings.TrimSpace(in.Model),
|
||||
Serial: serial,
|
||||
Firmware: strings.TrimSpace(in.Firmware),
|
||||
ControllerFirmware: strings.TrimSpace(in.ControllerFirmware),
|
||||
}.payload(who)
|
||||
data, status, err := s.admin.do(r.Context(), http.MethodPost, "/api/collections/drones/records", payload)
|
||||
if err != nil {
|
||||
gatewayError(w, err)
|
||||
return
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
relayRaw(w, status, data)
|
||||
return
|
||||
}
|
||||
var d droneRecord
|
||||
_ = json.Unmarshal(data, &d)
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"drone": d.view(), "created": true, "updated": false})
|
||||
}
|
||||
|
||||
// PATCH /api/drones/{id} — update a drone (must be in the caller's scope).
|
||||
func (s *Server) handleUpdateDrone(w http.ResponseWriter, r *http.Request) {
|
||||
who := caller(r)
|
||||
@@ -466,8 +627,8 @@ func (s *Server) handleUpdateDrone(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "invalid json")
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
writeError(w, http.StatusBadRequest, "drone name is required")
|
||||
if !in.identifiable() {
|
||||
writeError(w, http.StatusBadRequest, "give the drone a custom name, model or serial")
|
||||
return
|
||||
}
|
||||
// Preserve org ownership unless a superadmin explicitly retargets it.
|
||||
|
||||
@@ -41,7 +41,7 @@ func (s *Server) handleExportLogbook(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
_ = cw.Write([]string{
|
||||
"operation_date", "start_time", "end_time",
|
||||
"drone_name", "drone_model", "drone_serial", "operator_number",
|
||||
"drone_name", "drone_model", "drone_serial", "drone_registration", "operator_number",
|
||||
"area_or_route", "max_altitude_agl_m",
|
||||
"remote_pilot", "certificate_ref",
|
||||
"category", "purpose", "logging_path", "fdr_log_url", "authorisation_ref",
|
||||
@@ -55,9 +55,10 @@ func (s *Server) handleExportLogbook(w http.ResponseWriter, r *http.Request) {
|
||||
d = &dr
|
||||
}
|
||||
c := computeCompliance(f, d)
|
||||
droneName, model, serial, opNo := "", "", "", ""
|
||||
droneName, model, serial, reg, opNo := "", "", "", "", ""
|
||||
if d != nil {
|
||||
droneName, model, serial, opNo = d.Name, d.Model, d.Serial, d.OperatorNumber
|
||||
droneName, model, serial = d.displayName(), d.Model, d.Serial
|
||||
reg, opNo = d.Registration, d.OperatorNumber
|
||||
}
|
||||
alt := ""
|
||||
if f.MaxAltitudeAGL > 0 {
|
||||
@@ -65,7 +66,7 @@ func (s *Server) handleExportLogbook(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_ = cw.Write([]string{
|
||||
day(f.OperationDate), f.StartTime, f.EndTime,
|
||||
droneName, model, serial, opNo,
|
||||
droneName, model, serial, reg, opNo,
|
||||
f.AreaRoute, alt,
|
||||
f.PilotName, f.CertificateRef,
|
||||
f.Category, f.Purpose, c.LoggingPath, f.RawFDRLogURL, f.AuthorisationRef,
|
||||
|
||||
@@ -150,6 +150,7 @@ func (s *Server) Handler() http.Handler {
|
||||
// inside the handlers, so the shared requireUser gate suffices.
|
||||
mux.HandleFunc("GET /api/drones", s.requireUser(s.handleListDrones))
|
||||
mux.HandleFunc("POST /api/drones", s.requireUser(s.handleCreateDrone))
|
||||
mux.HandleFunc("POST /api/drones/auto", s.requireUser(s.handleAutoDrone))
|
||||
mux.HandleFunc("PATCH /api/drones/{id}", s.requireUser(s.handleUpdateDrone))
|
||||
mux.HandleFunc("DELETE /api/drones/{id}", s.requireUser(s.handleDeleteDrone))
|
||||
mux.HandleFunc("GET /api/flights", s.requireUser(s.handleListFlights))
|
||||
|
||||
@@ -182,11 +182,22 @@ func (h *Hub) Ingest(deviceID string, raw map[string]any) {
|
||||
s.Connected = c
|
||||
if !c {
|
||||
s.Telemetry = Telemetry{} // drone unlinked: live telemetry is no longer valid
|
||||
s.Serial, s.Firmware, s.ControllerFirmware = "", "", ""
|
||||
}
|
||||
}
|
||||
if m, ok := raw["model"].(string); ok {
|
||||
s.Model = m
|
||||
}
|
||||
if s.Connected {
|
||||
applyIdentity(s, raw)
|
||||
}
|
||||
case "identity":
|
||||
// Ignore identity that arrives after a disconnect: the Fly App forwards
|
||||
// every SDK event upstream before its own connected-check, so a callback
|
||||
// resolving late would otherwise repopulate what the disconnect cleared.
|
||||
if s.Connected {
|
||||
applyIdentity(s, raw)
|
||||
}
|
||||
case "battery":
|
||||
if p, ok := toInt(raw["percent"]); ok {
|
||||
s.Telemetry.BatteryPercent = &p
|
||||
|
||||
@@ -23,13 +23,22 @@ type Telemetry struct {
|
||||
|
||||
// DeviceState is the server's aggregated view of one app/drone.
|
||||
type DeviceState struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
Online bool `json:"online"` // app's websocket is connected to the server
|
||||
Connected bool `json:"connected"` // a drone is connected to the app
|
||||
Model string `json:"model"`
|
||||
Registration string `json:"registration"`
|
||||
Telemetry Telemetry `json:"telemetry"`
|
||||
LastSeenMs int64 `json:"lastSeenMs"`
|
||||
DeviceID string `json:"deviceId"`
|
||||
Online bool `json:"online"` // app's websocket is connected to the server
|
||||
Connected bool `json:"connected"` // a drone is connected to the app
|
||||
Model string `json:"model"`
|
||||
// Registration is the *SDK* registration state reported by the Fly App
|
||||
// ("success", "failed", …) — not the aircraft's FAA/CAA registration number,
|
||||
// which lives on the drone's logbook record.
|
||||
Registration string `json:"registration"`
|
||||
// Identity of the connected aircraft, as it reports itself. Serial and the
|
||||
// two firmware versions resolve asynchronously after connect, each on its own
|
||||
// schedule, so these fill in over several events rather than all at once.
|
||||
Serial string `json:"serial"`
|
||||
Firmware string `json:"firmware"`
|
||||
ControllerFirmware string `json:"controllerFirmware"`
|
||||
Telemetry Telemetry `json:"telemetry"`
|
||||
LastSeenMs int64 `json:"lastSeenMs"`
|
||||
}
|
||||
|
||||
// TrackPoint is one sample of the drone's GPS track (for the map trail).
|
||||
@@ -84,6 +93,22 @@ func toInt(v any) (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// applyIdentity copies the aircraft's self-reported identity from a raw event.
|
||||
// A field the event omits (or reports null) means "not resolved yet" — the SDK
|
||||
// answers serial in seconds but firmware can take far longer — so an absent
|
||||
// value never clears one an earlier event already delivered.
|
||||
func applyIdentity(s *DeviceState, raw map[string]any) {
|
||||
if v, ok := raw["serial"].(string); ok && v != "" {
|
||||
s.Serial = v
|
||||
}
|
||||
if v, ok := raw["firmware"].(string); ok && v != "" {
|
||||
s.Firmware = v
|
||||
}
|
||||
if v, ok := raw["controllerFirmware"].(string); ok && v != "" {
|
||||
s.ControllerFirmware = v
|
||||
}
|
||||
}
|
||||
|
||||
// applyTelemetry copies any present telemetry fields from a raw event map.
|
||||
func applyTelemetry(t *Telemetry, raw map[string]any) {
|
||||
if v, ok := toInt(raw["satelliteCount"]); ok {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/// <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)
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user