|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:pslab/communication/peripherals/i2c.dart'; |
| 4 | +import 'package:pslab/communication/science_lab.dart'; |
| 5 | +import 'package:pslab/others/logger_service.dart'; |
| 6 | +import '../communication/sensors/ccs811.dart'; |
| 7 | +import '../l10n/app_localizations.dart'; |
| 8 | +import '../models/chart_data_points.dart'; |
| 9 | +import 'locator.dart'; |
| 10 | + |
| 11 | +class CCS811Provider extends ChangeNotifier { |
| 12 | + AppLocalizations get appLocalizations => getIt.get<AppLocalizations>(); |
| 13 | + |
| 14 | + CCS811? _ccs811; |
| 15 | + Timer? _dataTimer; |
| 16 | + |
| 17 | + int _eCO2 = 0; |
| 18 | + int _tvoc = 0; |
| 19 | + |
| 20 | + final List<ChartDataPoint> _eCO2Data = []; |
| 21 | + final List<ChartDataPoint> _tvocData = []; |
| 22 | + |
| 23 | + bool _isRunning = false; |
| 24 | + bool _isLooping = false; |
| 25 | + int _timegapMs = 1000; |
| 26 | + int _numberOfReadings = 100; |
| 27 | + int _collectedReadings = 0; |
| 28 | + |
| 29 | + double _currentTime = 0.0; |
| 30 | + static const int maxDataPoints = 1000; |
| 31 | + |
| 32 | + int get eCO2 => _eCO2; |
| 33 | + int get tvoc => _tvoc; |
| 34 | + |
| 35 | + List<ChartDataPoint> get eCO2Data => List.unmodifiable(_eCO2Data); |
| 36 | + List<ChartDataPoint> get tvocData => List.unmodifiable(_tvocData); |
| 37 | + |
| 38 | + bool get isRunning => _isRunning; |
| 39 | + bool get isLooping => _isLooping; |
| 40 | + int get timegapMs => _timegapMs; |
| 41 | + int get numberOfReadings => _numberOfReadings; |
| 42 | + int get collectedReadings => _collectedReadings; |
| 43 | + |
| 44 | + CCS811Provider(); |
| 45 | + |
| 46 | + Future<void> initializeSensors({ |
| 47 | + required Function(String) onError, |
| 48 | + required I2C? i2c, |
| 49 | + required ScienceLab? scienceLab, |
| 50 | + }) async { |
| 51 | + try { |
| 52 | + if (i2c == null || scienceLab == null) { |
| 53 | + onError(appLocalizations.pslabNotConnected); |
| 54 | + logger.w(appLocalizations.notConnected); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!scienceLab.isConnected()) { |
| 59 | + onError(appLocalizations.pslabNotConnected); |
| 60 | + logger.w(appLocalizations.notConnected); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + _ccs811 = await CCS811.create(i2c, scienceLab); |
| 65 | + notifyListeners(); |
| 66 | + } catch (e) { |
| 67 | + logger.e('Error initializing CCS811: $e'); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void toggleDataCollection() { |
| 72 | + if (_isRunning) { |
| 73 | + _stopDataCollection(); |
| 74 | + } else { |
| 75 | + _startDataCollection(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void _startDataCollection() { |
| 80 | + if (_ccs811 == null) return; |
| 81 | + |
| 82 | + _isRunning = true; |
| 83 | + _collectedReadings = 0; |
| 84 | + |
| 85 | + _dataTimer = |
| 86 | + Timer.periodic(Duration(milliseconds: _timegapMs), (timer) async { |
| 87 | + try { |
| 88 | + await _fetchSensorData(); |
| 89 | + _collectedReadings++; |
| 90 | + |
| 91 | + if (!_isLooping && _collectedReadings >= _numberOfReadings) { |
| 92 | + _stopDataCollection(); |
| 93 | + } |
| 94 | + |
| 95 | + if (_isLooping && _eCO2Data.length >= maxDataPoints) { |
| 96 | + _removeOldestDataPoints(); |
| 97 | + } |
| 98 | + } catch (e) { |
| 99 | + logger.e('Error fetching sensor data: $e'); |
| 100 | + } |
| 101 | + }); |
| 102 | + notifyListeners(); |
| 103 | + } |
| 104 | + |
| 105 | + void _stopDataCollection() { |
| 106 | + _isRunning = false; |
| 107 | + _dataTimer?.cancel(); |
| 108 | + _dataTimer = null; |
| 109 | + notifyListeners(); |
| 110 | + } |
| 111 | + |
| 112 | + Future<void> _fetchSensorData() async { |
| 113 | + if (_ccs811 == null) return; |
| 114 | + |
| 115 | + try { |
| 116 | + final rawData = await _ccs811!.getRawData(); |
| 117 | + |
| 118 | + _eCO2 = rawData['eCO2'] ?? 0; |
| 119 | + _tvoc = rawData['TVOC'] ?? 0; |
| 120 | + |
| 121 | + _currentTime += _timegapMs / 1000.0; |
| 122 | + |
| 123 | + _addDataPoint(_eCO2Data, _eCO2.toDouble()); |
| 124 | + _addDataPoint(_tvocData, _tvoc.toDouble()); |
| 125 | + |
| 126 | + notifyListeners(); |
| 127 | + } catch (e) { |
| 128 | + logger.e('Error in _fetchSensorData: $e'); |
| 129 | + rethrow; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + void _addDataPoint(List<ChartDataPoint> dataList, double value) { |
| 134 | + dataList.add(ChartDataPoint(_currentTime, value)); |
| 135 | + if (dataList.length > 50) { |
| 136 | + dataList.removeAt(0); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + void _removeOldestDataPoints() { |
| 141 | + const keepPoints = 800; |
| 142 | + |
| 143 | + if (_eCO2Data.length > keepPoints) { |
| 144 | + final removeCount = _eCO2Data.length - keepPoints; |
| 145 | + _eCO2Data.removeRange(0, removeCount); |
| 146 | + _tvocData.removeRange(0, removeCount); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + void toggleLooping() { |
| 151 | + _isLooping = !_isLooping; |
| 152 | + notifyListeners(); |
| 153 | + } |
| 154 | + |
| 155 | + void setTimegap(int timegapMs) { |
| 156 | + _timegapMs = timegapMs; |
| 157 | + |
| 158 | + if (_isRunning) { |
| 159 | + _stopDataCollection(); |
| 160 | + _startDataCollection(); |
| 161 | + } |
| 162 | + |
| 163 | + notifyListeners(); |
| 164 | + } |
| 165 | + |
| 166 | + void setNumberOfReadings(int numberOfReadings) { |
| 167 | + _numberOfReadings = numberOfReadings; |
| 168 | + notifyListeners(); |
| 169 | + } |
| 170 | + |
| 171 | + void clearData() { |
| 172 | + _eCO2Data.clear(); |
| 173 | + _tvocData.clear(); |
| 174 | + _eCO2 = 0; |
| 175 | + _tvoc = 0; |
| 176 | + _currentTime = 0.0; |
| 177 | + _collectedReadings = 0; |
| 178 | + notifyListeners(); |
| 179 | + } |
| 180 | + |
| 181 | + bool get isCollectionComplete { |
| 182 | + return !_isLooping && _collectedReadings >= _numberOfReadings; |
| 183 | + } |
| 184 | + |
| 185 | + @override |
| 186 | + void dispose() { |
| 187 | + _stopDataCollection(); |
| 188 | + super.dispose(); |
| 189 | + } |
| 190 | +} |
0 commit comments