Files
PilotVault/Fly App/lib/fly/telemetry_reducer.dart
T
tajniak81andClaude Opus 5.5 f6bbb1cfee Batch the Fly App's telemetry and split it per subsystem
The GO 4-style HUD needs far more live data than the bridge sent, and
pushing every SDK callback straight to Dart would rebuild the screen
dozens of times a second. This is the telemetry foundation (plan P1).

On the Kotlin side, every source now writes into a TelemetryHub that
emits one telemetry event per tick: flight-controller state, gimbal and
vision at up to 10 Hz, battery, links, RC, storage and camera settings
folded in at up to 2 Hz. Each event carries the full latest snapshot, so
a consumer that drops packets never loses a slow group. New bridges
cover batteries (every pack, cell voltages, warning thresholds), link
quality and RC battery, diagnostics, and obstacle sensing per sensor.
KeyBridge adds generic getKey/setKey/listenKey over KeyManager for the
settings rows to come. The flight controller also gains smart-RTH
confirmation and simulator start/stop.

On the Dart side, FlightModel now owns one ChangeNotifier per subsystem
(aircraft, battery, links, camera, gimbal, vision, alerts, mission), so
a HUD widget can listen to just the one it draws. TelemetryReducer
routes events into them, only notifies groups that changed, and gives
FlightModel one notification per tick. Flat getters keep the existing
screens working. Diagnostics become graded, de-duplicated alerts.
Settings > About > Telemetry inspector shows every field live and can
start the simulator.

Battery no longer arrives as its own event, so the API Server reads the
percentage from the telemetry snapshot (the old event is still accepted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:34:56 +02:00

80 lines
3.1 KiB
Dart

import '../flight_model.dart';
import 'state/parse.dart';
/// Routes the bridge's telemetry-bearing events into [FlightModel]'s sub-models
/// (plan P1). Kept out of the widget tree so it can be unit-tested against
/// recorded events.
///
/// A `telemetry` event is the native [TelemetryHub]'s full snapshot: the flight
/// controller's flat keys plus one nested group per subsystem (`battery`,
/// `links`, `rc`, `storage`, `cameraSettings`, `gimbal`, `vision`). It arrives at
/// up to 10 Hz with the slow groups repeated unchanged between their 2 Hz
/// refreshes, so each group is compared with the last one seen and its
/// sub-model only notified when it actually moved. A group missing from the
/// snapshot was never reported by this aircraft and leaves its sub-model empty.
class TelemetryReducer {
TelemetryReducer(this.model);
final FlightModel model;
final Map<String, Object?> _lastGroups = <String, Object?>{};
static const List<String> _groups = <String>['battery', 'links', 'rc', 'storage', 'cameraSettings', 'gimbal', 'vision'];
/// Applies [event] if it is one of the telemetry types; returns whether it was.
bool apply(Map<String, dynamic> event) {
switch (event['type'] as String?) {
case 'telemetry':
// Stray packets trailing a disconnect would repopulate what was cleared.
if (model.connected) model.batch(() => _telemetry(event));
return true;
case 'camera':
if (model.connected) model.camera.applySystem(event);
return true;
case 'exposure':
if (model.connected) model.camera.applyExposure(event);
return true;
case 'diagnostics':
if (model.connected) model.alerts.apply(asMapList(event['items']));
return true;
case 'mission':
model.mission.apply(event);
return true;
}
return false;
}
/// Forgets the last-seen groups, so the next aircraft's first tick applies in full.
void reset() => _lastGroups.clear();
void _telemetry(Map<String, dynamic> t) {
// Flight-controller keys change every tick; absent until the FC first reports.
if (t.containsKey('satelliteCount')) model.aircraft.apply(t);
final Set<String> changed = <String>{};
for (final String g in _groups) {
final Object? v = t[g];
if (_lastGroups.containsKey(g) && deepEquals(_lastGroups[g], v)) continue;
_lastGroups[g] = v;
changed.add(g);
}
if (changed.isEmpty) return;
if (changed.contains('battery')) {
final Map<String, dynamic>? b = asMap(t['battery']);
b == null ? model.battery.clear() : model.battery.apply(b);
}
if (changed.contains('links') || changed.contains('rc')) {
model.links.apply(links: asMap(t['links']), rc: asMap(t['rc']));
}
if (changed.contains('storage') || changed.contains('cameraSettings')) {
model.camera.applyTelemetry(storage: asMap(t['storage']), settings: asMap(t['cameraSettings']));
}
if (changed.contains('gimbal')) {
final Map<String, dynamic>? g = asMap(t['gimbal']);
g == null ? model.gimbal.clear() : model.gimbal.apply(g);
}
if (changed.contains('vision')) model.vision.apply(asMap(t['vision']));
}
}