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, }, }; 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'); } } 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(), ); final appVersion = decoded['appVersion'] as String?; final mismatch = appVersion != null && appVersion.split('.').first != kAppVersion.split('.').first ? appVersion : null; return ParsedDroneImport(profile: profile, versionMismatch: mismatch); }