obd_state.dart 1.2 KB

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