obd_state.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /// Latest values read from the ELM327 adapter.
  2. class ObdState {
  3. const ObdState({
  4. this.connected = false,
  5. this.connecting = false,
  6. this.speedKmh,
  7. this.throttlePercent,
  8. this.mafGramsPerSecond,
  9. this.fuelRateLph,
  10. this.ambientCelsius,
  11. this.error,
  12. this.updatedAt = 0,
  13. this.retryExhausted = false,
  14. });
  15. final bool connected;
  16. final bool connecting;
  17. final double? speedKmh;
  18. final double? throttlePercent;
  19. final double? mafGramsPerSecond;
  20. /// ECU-reported engine fuel rate (PID `5E`), when the vehicle supports it.
  21. final double? fuelRateLph;
  22. final double? ambientCelsius;
  23. final String? error;
  24. final int updatedAt;
  25. /// The native worker stopped retrying after repeated connection failures.
  26. final bool retryExhausted;
  27. bool get hasVehicleData =>
  28. connected && (speedKmh != null || throttlePercent != null);
  29. bool get hasTemperature => connected && ambientCelsius != null;
  30. factory ObdState.fromMap(Map<dynamic, dynamic> map) {
  31. double? number(String key) => (map[key] as num?)?.toDouble();
  32. return ObdState(
  33. connected: map['connected'] == true,
  34. connecting: map['connecting'] == true,
  35. speedKmh: number('speedKmh'),
  36. throttlePercent: number('throttle'),
  37. mafGramsPerSecond: number('maf'),
  38. fuelRateLph: number('fuelRateLph'),
  39. ambientCelsius: number('ambient'),
  40. error: map['error'] as String?,
  41. updatedAt: (map['updatedAt'] as num?)?.toInt() ?? 0,
  42. retryExhausted: map['retryExhausted'] == true,
  43. );
  44. }
  45. }