data_handler.dart 3.8 KB
Newer Older
1
import 'package:logair_application/logic/handlers/bluetooth_le_handler.dart';
2 3
import 'package:logair_application/logic/data_header.dart';
import 'package:logair_application/logic/data_packet.dart';
4
import 'package:logair_application/logic/handlers/main_database_handler.dart';
5
import 'package:logair_application/logic/handlers/network_handler.dart';
6

7 8 9
class DataHandler {
  factory DataHandler() => _singleton;

10 11 12 13
  DataHandler._internal() { 
    NetworkHandler();
  }
  
14
  static const HEADER_SIZE = 11;
15

16
  static final DataHandler _singleton = new DataHandler._internal();  
17

18
  /// [List] containing all of the data received by this.
19 20
  List<int> _data = [];

21
  /// [List] containing separate packet instances received by this.
22
  List<DataPacket> _sortedData = [];
23

24 25
  /// Allows the [BTLEHandler] to add acquired information to this, and effects
  /// a primary triage.
26
  void addData(List<int> data) {
27
    _data.addAll(data);
28

29
    /// Get a start index for the data, either '{' (DataHeader) or '[' (DataPacket)
30 31 32 33
    int index = _data.indexWhere((e) => e == 91 || e == 123);
    if (index == -1)
      return;

34
    /// Remove noise if exists
35 36
    _data.removeRange(0, index);

37
    /// Get the termination index '$'
38 39 40 41
    int termIndex = _data.indexWhere((e) => e == 36);
    if (termIndex == -1)
      return;

42
    /// If both exist, then extract the packet.
43 44 45
    List<int> packet = _data.sublist(index, termIndex + 1);
    _data.removeRange(0, termIndex);

46 47
    /// If a packet is ingrained within this packet (possibly because of BLE transmission issues),
    /// this will attempt to fix it.
48 49 50 51 52 53
    if (packet.sublist(1).contains((e) => e == 91 || e == 123)) {
      print('True ${packet.sublist(1).indexWhere((e) => e == 91 || e == 123)}');
      int startIndex = packet.sublist(1).indexWhere((e) => e == 91 || e == 123);
      packet.removeRange(0, startIndex);
    }

54
    /// If it's a [DataHeader] instance.
55 56
    if (packet[0] == 123) {
      print('HEADER ${String.fromCharCodes(packet)}');
57
      bool wasSet = DataHeader().headerSet;
58
      DataHeader().setHeader(packet);
59
      /// Retroactively add all previous packets
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
60 61 62 63 64 65 66
      if (!wasSet && DataHeader().headerSet) {
        _sortedData.forEach((dataPacket) {
          dataPacket.deviceId = DataHeader().deviceId;
          dataPacket.url = DataHeader().url;
          MainDatabaseHandler().insertData(dataPacket);
        });
      }
67
      
68
    /// Or if it is a [DataPacket] instance.
69 70
    } else if (packet[0] == 91) {
      print('PACKET ${String.fromCharCodes(packet)}');
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
71 72 73 74 75 76 77
      DataPacket dataPacket = DataPacket(packet);
      if (DataHeader().headerSet) {
        dataPacket.deviceId = DataHeader().deviceId;
        dataPacket.url = DataHeader().url;
        MainDatabaseHandler().insertData(dataPacket);
      } else 
        _sortedData.add(dataPacket);
78 79 80
    }
  }

81
  /// Get the entirety of the [DataPacket]s.
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
82
  @deprecated
83 84
  List<DataPacket> getData() => _sortedData;

85
  /// Remove the entirety of the acquired data.
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
86
  @deprecated
87 88 89 90
  void clearData() {
    _sortedData.removeWhere((element) => true);
  }

91
  /// Remove the first element of the [DataPacket]s. Used for surgical removal.
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
92
  @deprecated
93 94 95
  void pop(int timestamp) {
    if (_sortedData.isNotEmpty) 
      _sortedData.removeWhere((DataPacket d) => d.timestamp == timestamp);
96 97
  }

98
  /// Get the latest [DataPacket]
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
99
  @deprecated
100 101 102
  DataPacket getLatestData() => (_sortedData.length > 0) ? _sortedData.last : null;

  /// Stream the latest confirmed [DataPacket] received by the device.
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
103
  @deprecated
104
  Stream<DataPacket> getDataStream() async* {
105
    while(true) {
106
      yield (_sortedData.length > 0) ? _sortedData.last : null;
107
      await Future.delayed(Duration(seconds: 1));
108 109
    }
  }
110

111
  /// This destroys the elements it depends on to avoid duplication or wrong information
112 113 114
  void dispose() {
    DataHeader().dispose();
    this._data.removeWhere((element) => true);
115
  }
116
}