obd_state.dart 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. });
  13. final bool connected;
  14. final bool connecting;
  15. final double? speedKmh;
  16. final double? throttlePercent;
  17. final double? mafGramsPerSecond;
  18. final double? ambientCelsius;
  19. final String? error;
  20. final int updatedAt;
  21. bool get hasVehicleData =>
  22. connected && (speedKmh != null || throttlePercent != null);
  23. bool get hasTemperature => connected && ambientCelsius != null;
  24. factory ObdState.fromMap(Map<dynamic, dynamic> map) {
  25. double? number(String key) => (map[key] as num?)?.toDouble();
  26. return ObdState(
  27. connected: map['connected'] == true,
  28. connecting: map['connecting'] == true,
  29. speedKmh: number('speedKmh'),
  30. throttlePercent: number('throttle'),
  31. mafGramsPerSecond: number('maf'),
  32. ambientCelsius: number('ambient'),
  33. error: map['error'] as String?,
  34. updatedAt: (map['updatedAt'] as num?)?.toInt() ?? 0,
  35. );
  36. }
  37. }