import 'dart:convert'; import '../../app_info.dart'; import '../../domain/mission/drone_profile.dart'; /// Baut die Export-Datei fuer ein Drohnenprofil (Doku 3.5, analog /// buildDroneExportObject()/shareDrone() im HTML-Demonstrator). Die id wird /// bewusst nicht mitexportiert - beim Import entsteht immer ein neues, /// eigenstaendiges Profil (siehe parseDroneImportJson()). String buildDroneExportJson(DroneProfile profile) { final map = { 'appVersion': kAppVersion, 'exportedAt': DateTime.now().toUtc().toIso8601String(), 'drone': { 'name': profile.name, 'minSpeed': profile.minSpeed, 'cruiseSpeed': profile.cruiseSpeed, 'maxSpeed': profile.maxSpeed, 'minTurnRadius': profile.minTurnRadius, 'loiterRadius': profile.loiterRadius, 'catchRadius': profile.catchRadius, 'maxClimbRate': profile.maxClimbRate, 'maxDescentRate': profile.maxDescentRate, 'maxRangeM': profile.maxRangeM, 'maxEnduranceMin': profile.maxEnduranceMin, 'maxAltitudeM': profile.maxAltitudeM, 'gpsSatGreenMin': profile.gpsSatGreenMin, 'gpsSatRedMin': profile.gpsSatRedMin, 'hdopGreenMax': profile.hdopGreenMax, 'hdopRedMax': profile.hdopRedMax, 'currentGreenMaxA': profile.currentGreenMaxA, 'currentRedMaxA': profile.currentRedMaxA, 'batteryCellCount': profile.batteryCellCount, 'batteryVoltageGreenMinPerCellV': profile.batteryVoltageGreenMinPerCellV, 'batteryVoltageRedMinPerCellV': profile.batteryVoltageRedMinPerCellV, 'rxQualityGreenMin': profile.rxQualityGreenMin, 'rxQualityRedMin': profile.rxQualityRedMin, 'rxSnrGreenMinDb': profile.rxSnrGreenMinDb, 'rxSnrRedMinDb': profile.rxSnrRedMinDb, }, }; return const JsonEncoder.withIndent(' ').convert(map); } /// Vorschlagsname fuer die geteilte Datei (Doku 3.5, analog /// `'drone-' + safeName + '.json'` im HTML-Demonstrator). String droneExportFilename(String name) { final safeName = name.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '_'); return 'drone-$safeName.json'; } class ParsedDroneImport { const ParsedDroneImport({required this.profile, this.versionMismatch}); final DroneProfile profile; /// Enthaelt die appVersion der Datei, falls sie sich von [kAppVersion] in /// der Hauptversionsnummer unterscheidet - sonst null. final String? versionMismatch; } const _requiredNumericFields = [ 'minSpeed', 'cruiseSpeed', 'maxSpeed', 'minTurnRadius', 'loiterRadius', 'catchRadius', 'maxClimbRate', 'maxDescentRate', 'maxRangeM', 'maxEnduranceMin', 'maxAltitudeM', ]; /// Parst eine importierte Drohnenprofil-Datei - wirft [FormatException] bei /// ungueltigem JSON oder fehlenden Pflichtfeldern (Doku 3.5, analog dem /// `change`-Handler von importDroneInput im HTML-Demonstrator). ParsedDroneImport parseDroneImportJson(String jsonText) { final Object? decoded; try { decoded = jsonDecode(jsonText); } on FormatException { throw const FormatException('Invalid drone profile file'); } if (decoded is! Map) { throw const FormatException('Invalid drone profile file'); } final droneData = decoded['drone'] ?? decoded; if (droneData is! Map) { throw const FormatException('Invalid drone profile file'); } final name = droneData['name']; if (name is! String || name.isEmpty) { throw const FormatException('File is not a valid drone profile'); } for (final field in _requiredNumericFields) { if (droneData[field] is! num) { throw const FormatException('File is not a valid drone profile'); } } // Ampel-Schwellwerte (siehe DroneProfile-Doku) sind bewusst NICHT Teil von // _requiredNumericFields - aeltere, vor deren Einfuehrung exportierte // Profildateien haben diese Felder noch nicht und sollen trotzdem // importierbar bleiben (dann gelten die DroneProfile.default*-Werte). double numOrDefault(String field, double fallback) { final v = droneData[field]; return v is num ? v.toDouble() : fallback; } int intOrDefault(String field, int fallback) { final v = droneData[field]; return v is num ? v.round() : fallback; } final profile = DroneProfile( name: name, minSpeed: (droneData['minSpeed'] as num).toDouble(), cruiseSpeed: (droneData['cruiseSpeed'] as num).toDouble(), maxSpeed: (droneData['maxSpeed'] as num).toDouble(), minTurnRadius: (droneData['minTurnRadius'] as num).toDouble(), loiterRadius: (droneData['loiterRadius'] as num).toDouble(), catchRadius: (droneData['catchRadius'] as num).toDouble(), maxClimbRate: (droneData['maxClimbRate'] as num).toDouble(), maxDescentRate: (droneData['maxDescentRate'] as num).toDouble(), maxRangeM: (droneData['maxRangeM'] as num).toDouble(), maxEnduranceMin: (droneData['maxEnduranceMin'] as num).toDouble(), maxAltitudeM: (droneData['maxAltitudeM'] as num).toDouble(), gpsSatGreenMin: numOrDefault('gpsSatGreenMin', DroneProfile.defaultGpsSatGreenMin), gpsSatRedMin: numOrDefault('gpsSatRedMin', DroneProfile.defaultGpsSatRedMin), hdopGreenMax: numOrDefault('hdopGreenMax', DroneProfile.defaultHdopGreenMax), hdopRedMax: numOrDefault('hdopRedMax', DroneProfile.defaultHdopRedMax), currentGreenMaxA: numOrDefault('currentGreenMaxA', DroneProfile.defaultCurrentGreenMaxA), currentRedMaxA: numOrDefault('currentRedMaxA', DroneProfile.defaultCurrentRedMaxA), // batteryCellCount/*PerCellV sind Schema-v9-Felder (Doku: "zellen anzahl // ... hinzufuegen") - Dateien, die vor deren Einfuehrung exportiert // wurden, haben stattdessen ggf. noch die alten absoluten // batteryVoltage*MinV-Felder (feste Pack-Spannung). Die werden hier // bewusst NICHT mehr gelesen (koennten ohne bekannte Zellenzahl der // Quelldrohne nicht sinnvoll in Pro-Zelle-Werte umgerechnet werden) - // stattdessen gelten wie bei allen anderen Schwellwerten die // DroneProfile.default*-Werte. batteryCellCount: intOrDefault('batteryCellCount', DroneProfile.defaultBatteryCellCount), batteryVoltageGreenMinPerCellV: numOrDefault( 'batteryVoltageGreenMinPerCellV', DroneProfile.defaultBatteryVoltageGreenMinPerCellV, ), batteryVoltageRedMinPerCellV: numOrDefault( 'batteryVoltageRedMinPerCellV', DroneProfile.defaultBatteryVoltageRedMinPerCellV, ), rxQualityGreenMin: numOrDefault('rxQualityGreenMin', DroneProfile.defaultRxQualityGreenMin), rxQualityRedMin: numOrDefault('rxQualityRedMin', DroneProfile.defaultRxQualityRedMin), rxSnrGreenMinDb: numOrDefault('rxSnrGreenMinDb', DroneProfile.defaultRxSnrGreenMinDb), rxSnrRedMinDb: numOrDefault('rxSnrRedMinDb', DroneProfile.defaultRxSnrRedMinDb), ); final appVersion = decoded['appVersion'] as String?; final mismatch = appVersion != null && appVersion.split('.').first != kAppVersion.split('.').first ? appVersion : null; return ParsedDroneImport(profile: profile, versionMismatch: mismatch); }