Skip to content

Commit 3ac7127

Browse files
committed
correct logic
1 parent 9a0c56a commit 3ac7127

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

lib/providers/gas_sensor_state_provider.dart

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class GasSensorStateProvider extends ChangeNotifier {
1818

1919
double _currentValue = 0.0;
2020
String _activeMode = "Raw";
21+
int _updatePeriod = 1000;
2122

2223
Timer? _readTimer;
2324

@@ -119,14 +120,32 @@ class GasSensorStateProvider extends ChangeNotifier {
119120
final prefs = await SharedPreferences.getInstance();
120121
final jsonString = prefs.getString('gas_sensor_config');
121122
if (jsonString != null) {
122-
String newMode = json.decode(jsonString)['activeGas'] ?? 'Raw';
123+
final configData = json.decode(jsonString);
124+
String newMode = configData['activeGas'] ?? 'Raw';
125+
int newPeriod = configData['updatePeriod'] ?? 1000;
126+
127+
bool modeChanged = false;
128+
bool periodChanged = false;
123129

124130
if (newMode != _activeMode) {
125131
_activeMode = newMode;
126132
clearData();
127133
_startTime = DateTime.now().millisecondsSinceEpoch / 1000.0;
134+
modeChanged = true;
135+
}
136+
137+
if (newPeriod != _updatePeriod) {
138+
_updatePeriod = newPeriod;
139+
periodChanged = true;
140+
}
141+
142+
if (modeChanged || periodChanged) {
128143
notifyListeners();
129144
}
145+
146+
if (periodChanged && _readTimer != null && _readTimer!.isActive) {
147+
_startReadingGasData();
148+
}
130149
}
131150
} catch (e) {
132151
logger.e("Error reading config: $e");
@@ -186,7 +205,7 @@ class GasSensorStateProvider extends ChangeNotifier {
186205
void _startReadingGasData() {
187206
_readTimer?.cancel();
188207
_readTimer =
189-
Timer.periodic(const Duration(milliseconds: 1000), (timer) async {
208+
Timer.periodic(Duration(milliseconds: _updatePeriod), (timer) async {
190209
if (_isPlayingRecordedData) return;
191210
await fetchConfig();
192211
if (!_isSensorAvailable ||
@@ -255,9 +274,9 @@ class GasSensorStateProvider extends ChangeNotifier {
255274
'Timestamp',
256275
'DateTime',
257276
'Readings',
277+
'Active Gas',
258278
'Latitude',
259-
'Longitude',
260-
'Active Gas'
279+
'Longitude'
261280
]
262281
];
263282
notifyListeners();
@@ -278,7 +297,8 @@ class GasSensorStateProvider extends ChangeNotifier {
278297
_isPlayingRecordedData = true;
279298
_isPlaybackPaused = false;
280299
_playbackData = data;
281-
_playbackIndex = 1;
300+
301+
_playbackIndex = 2;
282302

283303
_readTimer?.cancel();
284304

@@ -308,8 +328,8 @@ class GasSensorStateProvider extends ChangeNotifier {
308328
_currentTime = (timestamp - _playbackStartTimestamp!) / 1000.0;
309329
}
310330

311-
if (currentRow.length > 5) {
312-
_activeMode = currentRow[5].toString();
331+
if (currentRow.length > 3) {
332+
_activeMode = currentRow[3].toString();
313333
}
314334

315335
_updateData();
@@ -386,9 +406,9 @@ class GasSensorStateProvider extends ChangeNotifier {
386406
now.millisecondsSinceEpoch.toString(),
387407
dateFormat.format(now),
388408
_currentValue.toStringAsFixed(2),
409+
_activeMode,
389410
currentPosition?.latitude.toString() ?? 0,
390-
currentPosition?.longitude.toString() ?? 0,
391-
_activeMode
411+
currentPosition?.longitude.toString() ?? 0
392412
]);
393413
}
394414

0 commit comments

Comments
 (0)