obd_state.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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.ambientCelsius,
  10. this.error,
  11. this.updatedAt = 0,
  12. this.retryExhausted = false,
  13. });
  14. final bool connected;
  15. final bool connecting;
  16. final double? speedKmh;
  17. final double? throttlePercent;
  18. final double? mafGramsPerSecond;
  19. final double? ambientCelsius;
  20. final String? error;
  21. final int updatedAt;
  22. /// The native worker stopped retrying after repeated connection failures.
  23. final bool retryExhausted;
  24. bool get hasVehicleData =>
  25. connected && (speedKmh != null || throttlePercent != null);
  26. bool get hasTemperature => connected && ambientCelsius != null;
  27. factory ObdState.fromMap(Map<dynamic, dynamic> map) {
  28. double? number(String key) => (map[key] as num?)?.toDouble();
  29. return ObdState(
  30. connected: map['connected'] == true,
  31. connecting: map['connecting'] == true,
  32. speedKmh: number('speedKmh'),
  33. throttlePercent: number('throttle'),
  34. mafGramsPerSecond: number('maf'),
  35. ambientCelsius: number('ambient'),
  36. error: map['error'] as String?,
  37. updatedAt: (map['updatedAt'] as num?)?.toInt() ?? 0,
  38. retryExhausted: map['retryExhausted'] == true,
  39. );
  40. }
  41. }