Files
dmc/app/lib/services/database/app_database.dart
T
Constantin LeueandClaude Sonnet 5 35351140ae Persist the ground elevation profile with the mission
The Mini-Altitude-Profile terrain data only ever lived in TerrainNotifier's
in-memory state (one slot, keyed by route). Every app restart, or every
switch away from and back to a mission, forced a full re-fetch of all
AWS Terrarium elevation tiles for the route - the noticeably slow load
the user reported for some missions was this happening on every visit,
not just once.

Adds a nullable terrainProfileJson column to the missions table
(schema v6->v7) and a saveTerrainProfile() repository method, kept
separate from the regular waypoint upsert() so an ordinary autosave never
clobbers an already-cached profile. TerrainNotifier persists a profile
right after a successful fetch (fire-and-forget) and gains restore(),
called from CurrentMissionMetaNotifier whenever a mission is loaded/
started so a previously fetched profile is available immediately -
ensureFor() still validates its routeKey before use, so a stale restored
profile is never shown for a route that has since changed.

Also included in mission export/import (MissionExportData/
ParsedMissionImport) so sharing a mission carries its terrain cache along
instead of forcing the recipient to refetch it.

Sample-count/resolution stays as-is for now (still fixed 30m spacing,
10-2000 samples) - adapting the sampling density to terrain variance
(e.g. coarser sampling over flat ground) is a separate follow-up.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-04 08:10:41 +02:00

171 lines
6.6 KiB
Dart

import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import '../../transport/connection_type.dart';
part 'app_database.g.dart';
/// Persistenz fuer Missionen (Architektur-Doku 4.15): Drift statt Hive, weil
/// indexierte Abfragen (Suche nach Name/Ort/Datum) benoetigt werden und das
/// Schema kompatibel zu einem kuenftigen lokal-first Server-Sync-Muster
/// (Dirty-Flags, Server-IDs) bleiben soll.
class Missions extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
TextColumn get location => text().nullable()();
DateTimeColumn get createdAt =>
dateTime().withDefault(currentDateAndTime)();
DateTimeColumn get updatedAt =>
dateTime().withDefault(currentDateAndTime)();
TextColumn get waypointsJson => text()();
BoolColumn get isDirty => boolean().withDefault(const Constant(true))();
TextColumn get serverId => text().nullable()();
/// Gecachtes Bodenhoehenprofil (Doku 3.9), an einen routeKeyForTerrain()
/// gebunden (siehe TerrainProfile) - null, solange fuer die aktuelle Route
/// noch keins abgerufen wurde. Persistiert statt nur im TerrainNotifier
/// im Speicher gehalten, damit ein erneutes Oeffnen derselben Mission
/// (oder ein Import auf einem anderen Geraet) nicht jedes Mal alle
/// AWS-Terrarium-Kacheln neu laden muss.
TextColumn get terrainProfileJson => text().nullable()();
}
/// Drohnen-Profile (Doku 3.5/7.3) - FC-Typ bewusst nicht Teil des Schemas,
/// siehe DroneProfile-Domainklasse.
class DroneProfiles extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
RealColumn get minSpeed => real()();
RealColumn get cruiseSpeed => real()();
RealColumn get maxSpeed => real()();
RealColumn get minTurnRadius => real()();
RealColumn get loiterRadius => real()();
RealColumn get catchRadius => real()();
RealColumn get maxClimbRate => real()();
RealColumn get maxDescentRate => real()();
RealColumn get maxRangeM => real()();
RealColumn get maxEnduranceMin => real()();
RealColumn get maxAltitudeM => real()();
}
/// Einzeilige Einstellungstabelle (id fest 0) fuer app-weite Auswahl, die
/// einen Neustart ueberleben soll: zuletzt aktives Drohnenprofil, die
/// zuletzt bearbeitete (autosaved) Mission, um sie beim naechsten Start
/// fortzusetzen (Doku 7.5: Autosave/Mission-Drafts), sowie die
/// Verbindungseinstellungen (Doku Kommunikationsschicht Abschnitt 7).
class AppSettingsTable extends Table {
IntColumn get id => integer()();
IntColumn get activeDroneProfileId => integer().nullable()();
IntColumn get currentMissionId => integer().nullable()();
/// Gemerkte MAC-Adresse des Bluetooth-Zielgeraets (Doku Kommunikations-
/// schicht 3.2a) - entweder manuell gewaehlt oder automatisch anhand des
/// "mLRS"-Namenspraefix aufgeloest.
TextColumn get connectionDeviceAddress => text().nullable()();
/// "Beim Wechsel in Fly automatisch verbinden" (Doku 7), Standard an.
BoolColumn get connectionAutoConnect =>
boolean().withDefault(const Constant(true))();
/// Gewaehlte Verbindungsart (Doku 7: "Art: WLAN * USB (spaeter)"),
/// gespeichert als [ConnectionType.name]. App-Default ist WLAN; die
/// Nutzerauswahl wird danach dauerhaft gemerkt.
TextColumn get connectionType =>
text().withDefault(const Constant('wifi'))();
/// Vom Nutzer eingetragene Ziel-IP der mLRS-WLAN-Bruecke (Doku 3:
/// "Zieladresse niemals fest eincodieren") - null, solange keine Vorgabe
/// gesetzt ist und die Gegenstelle erst aus dem Verkehr gelernt wird.
TextColumn get connectionWifiHost => text().nullable()();
/// Lokaler UDP-Port fuer die mLRS-WLAN-Bruecke (Doku 3), Standard 14550.
IntColumn get connectionWifiPort =>
integer().withDefault(const Constant(kDefaultWifiPort))();
/// SSID-Praefix fuer den app-initiierten WLAN-Verbindungsaufbau per
/// `WifiNetworkSpecifier` (Doku Kommunikationsschicht v2 Abschnitt 4/8),
/// Standard "mLRS-", ueberschreibbar.
TextColumn get connectionWifiSsidPrefix =>
text().withDefault(const Constant(kDefaultWifiSsidPrefix))();
/// Exakte SSID der zuletzt erfolgreich verbundenen mLRS-Bruecke (Doku
/// Abschnitt 8: "verbundenes WLAN merken") - null, solange noch nie
/// erfolgreich verbunden wurde. Wird fuer kuenftige Verbindungsversuche
/// als exakter WifiNetworkSpecifier statt Praefix verwendet, damit Android
/// ohne erneuten Systemdialog automatisch verbinden kann.
TextColumn get connectionWifiLastSsid => text().nullable()();
@override
Set<Column> get primaryKey => {id};
}
@DriftDatabase(tables: [Missions, DroneProfiles, AppSettingsTable])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
AppDatabase.forTesting(super.executor);
@override
int get schemaVersion => 7;
@override
MigrationStrategy get migration => MigrationStrategy(
onCreate: (m) => m.createAll(),
onUpgrade: (m, from, to) async {
if (from < 2) {
await m.addColumn(missions, missions.updatedAt);
await m.createTable(droneProfiles);
await m.createTable(appSettingsTable);
}
if (from < 3) {
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionDeviceAddress,
);
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionAutoConnect,
);
}
if (from < 4) {
await m.addColumn(appSettingsTable, appSettingsTable.connectionType);
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionWifiHost,
);
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionWifiPort,
);
}
if (from < 5) {
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionWifiSsidPrefix,
);
}
if (from < 6) {
await m.addColumn(
appSettingsTable,
appSettingsTable.connectionWifiLastSsid,
);
}
if (from < 7) {
await m.addColumn(missions, missions.terrainProfileJson);
}
},
);
static LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'dmc_missions.sqlite'));
return NativeDatabase.createInBackground(file);
});
}
}