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>
159 lines
6.6 KiB
Dart
159 lines
6.6 KiB
Dart
import 'package:flutter/foundation.dart';
|
||
|
||
import 'uploader.dart';
|
||
|
||
enum RegistrationState { idle, registering, success, failed }
|
||
|
||
/// Camera capture family, mirrors the right-rail Photo/Video/Pano switch.
|
||
enum CaptureMode { photo, video, pano }
|
||
|
||
/// Exposure program — Auto (PROGRAM) vs Pro (MANUAL) tab on Camera settings.
|
||
enum ExposureProgram { auto, pro }
|
||
|
||
/// A media file listed off the aircraft's SD card by the native MediaManager.
|
||
class MediaItem {
|
||
MediaItem({
|
||
required this.index,
|
||
required this.fileName,
|
||
required this.isVideo,
|
||
this.durationSeconds,
|
||
this.sizeBytes,
|
||
this.createdMs,
|
||
this.thumbPath,
|
||
this.localPath,
|
||
});
|
||
|
||
final int index;
|
||
final String fileName;
|
||
final bool isVideo;
|
||
final int? durationSeconds;
|
||
final int? sizeBytes;
|
||
final int? createdMs;
|
||
|
||
/// On-disk path of the fetched thumbnail (cache dir), once available.
|
||
String? thumbPath;
|
||
|
||
/// On-disk path of the fully downloaded original, once available.
|
||
String? localPath;
|
||
|
||
/// `null` for anything that shouldn't carry a duration. The SDK reports
|
||
/// `durationInSeconds` for *every* file, so stills come back with junk —
|
||
/// a Mavic Pro's photos yielded -36:35, -136:23 and 545:26 on the SD card.
|
||
String? get durationLabel {
|
||
if (!isVideo) return null;
|
||
final int? s = durationSeconds;
|
||
if (s == null || s <= 0) return null;
|
||
final String mm = (s ~/ 60).toString();
|
||
final String ss = (s % 60).toString().padLeft(2, '0');
|
||
return '$mm:$ss';
|
||
}
|
||
|
||
factory MediaItem.fromMap(Map<String, dynamic> m) => MediaItem(
|
||
index: (m['index'] as num).toInt(),
|
||
fileName: (m['fileName'] as String?) ?? 'file',
|
||
isVideo: (m['type'] as String?) == 'video',
|
||
durationSeconds: (m['durationSeconds'] as num?)?.toInt(),
|
||
sizeBytes: (m['sizeBytes'] as num?)?.toInt(),
|
||
createdMs: (m['createdMs'] as num?)?.toInt(),
|
||
);
|
||
}
|
||
|
||
/// Live aircraft/session state, shared across every screen. `_HomePageState`
|
||
/// owns the DJI/uploader plumbing and pushes updates here; the screens observe
|
||
/// it via [AnimatedBuilder]. Call [bump] after a batch of field writes.
|
||
class FlightModel extends ChangeNotifier {
|
||
// ── SDK / registration / connection ────────────────────────────────────────
|
||
String sdkVersion = '…';
|
||
RegistrationState registration = RegistrationState.idle;
|
||
String? registrationError;
|
||
bool connected = false;
|
||
String? model;
|
||
String? firmwareVersion;
|
||
/// The remote controller's own firmware — distinct from the aircraft's above.
|
||
String? controllerFirmwareVersion;
|
||
String? serialNumber;
|
||
|
||
// ── Flight controller telemetry ────────────────────────────────────────────
|
||
int? satellites;
|
||
int? gpsSignalLevel; // 0..5
|
||
bool? isFlying;
|
||
bool? motorsOn;
|
||
String? flightMode;
|
||
double? altitude; // m, relative to home
|
||
double? latitude;
|
||
double? longitude;
|
||
double? homeLatitude;
|
||
double? homeLongitude;
|
||
double? homeDistance; // m, aircraft ↔ home
|
||
double? horizontalSpeed; // m/s
|
||
double? verticalSpeed; // m/s (up positive)
|
||
double? heading; // deg
|
||
int? flightTimeSeconds;
|
||
double? goHomeHeight; // m
|
||
|
||
// Phone's own GPS (independent of the drone's fix); streamed to the server as
|
||
// a location fallback for the Web App's automatic bounding box.
|
||
double? phoneLatitude;
|
||
double? phoneLongitude;
|
||
|
||
// ── Battery ─────────────────────────────────────────────────────────────────
|
||
int? batteryPercent;
|
||
double? batteryVoltage; // V
|
||
double? batteryTemperature; // °C
|
||
|
||
// ── Camera ──────────────────────────────────────────────────────────────────
|
||
CaptureMode captureMode = CaptureMode.video;
|
||
String? shootPhotoMode; // SINGLE / HDR / BURST / AEB / INTERVAL / PANORAMA
|
||
bool isRecording = false;
|
||
int recordSeconds = 0;
|
||
bool sdInserted = false;
|
||
int? sdRemainingMB;
|
||
int? sdPhotoCount;
|
||
int? sdVideoCount;
|
||
|
||
// ── Exposure ────────────────────────────────────────────────────────────────
|
||
ExposureProgram exposureProgram = ExposureProgram.auto;
|
||
String? iso; // "100", "AUTO", …
|
||
String? shutter; // "1/240"
|
||
String? aperture; // "f/2.8"
|
||
String? ev; // "-0.3"
|
||
String? whiteBalance; // "5200K" / "AUTO"
|
||
List<double>? histogram; // 0..1 normalized bins
|
||
|
||
// ── Gimbal ──────────────────────────────────────────────────────────────────
|
||
double? gimbalPitch; // deg (−90..30-ish)
|
||
double? gimbalRoll;
|
||
double? gimbalYaw;
|
||
|
||
// ── Flight settings (Safety / Control) ──────────────────────────────────────
|
||
int? maxHeight; // m
|
||
int? maxRadius; // m
|
||
bool? maxRadiusEnabled;
|
||
int? rthHeight; // m
|
||
String? obstacleAvoidance; // "On" / "Bypass" / "Off"
|
||
bool? noviceMode;
|
||
bool arHomePoint = true; // client-side toggle (AR overlay)
|
||
|
||
// ── Missions ────────────────────────────────────────────────────────────────
|
||
String missionState = 'idle'; // idle / ready / uploading / executing / …
|
||
bool missionRunning = false;
|
||
bool tracking = false; // ActiveTrack engaged
|
||
String? missionError;
|
||
|
||
// ── On-drone media ──────────────────────────────────────────────────────────
|
||
List<MediaItem> media = <MediaItem>[];
|
||
bool mediaLoading = false;
|
||
|
||
// ── DJI account (optional, in addition to PilotVault) ───────────────────────
|
||
String djiAccountState = 'unknown'; // notLoggedIn / tokenOutOfDate / authorized / …
|
||
String? djiAccountUser;
|
||
|
||
// ── Telemetry upload channel ────────────────────────────────────────────────
|
||
UploadStatus upload = UploadStatus.disabled;
|
||
|
||
bool get registered => registration == RegistrationState.success;
|
||
|
||
/// Notify observers after a batch of field writes.
|
||
void bump() => notifyListeners();
|
||
}
|