data_packet.dart 5.77 KB
Newer Older
1
import 'package:geolocator/geolocator.dart';
2 3
import 'package:logair_application/logic/handlers/position_handler.dart';
import 'package:logair_application/utils/utils.dart';
4

5 6 7 8
/// Represents 1 second of data streamed from LogAir's devices, 
/// and contains methods for: 
///   quick lookup, 
///   conversion to POST format,
9
// TODO Firmware@v2 : Extra data
10
class DataPacket {
11 12
  List<int> _data;
  int _timestamp;
13
  String _deviceId;
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
14
  String _url;
15 16 17 18 19

  /* Location variables */
  double _latitude;
  double _longitude;
  int _altitude;
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
20
  int _heading;
21 22 23 24 25 26 27 28
  double _speed;

  /* Ambient metrics */
  double _temperature;
  double _pressure;
  double _relativeHumidity;
  
  /* Environment metrics */
29 30 31 32
  double _pm1;
  double _pm2_5;
  double _pm4;
  double _pm10;
33

34
  // TODO Firmware v2
35
  // ignore: unused_field
36 37
  String _extraData;

Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  int get timestamp => this._timestamp;
  String get deviceId => this._deviceId ?? '';
  String get url => this._url ?? '';
  double get latitude => this._latitude;
  double get longitude => this._longitude;
  int get altitude => this._altitude;
  int get heading => this._heading;
  double get speed => this._speed;
  double get temperature => this._temperature;
  double get pressure => this._pressure;
  double get relativeHumidity => this._relativeHumidity;
  double get pm1 => this._pm1;
  double get pm2_5 => this._pm2_5;
  double get pm4 => this._pm4;
  double get pm10 => this._pm10;

  set deviceId(String newId) => this._deviceId = newId;
  set url(String newUrl) => this._url = newUrl;
56

57 58 59
  DataPacket(this._data) {
    _timestamp = DateTime.now().millisecondsSinceEpoch;
    List<String> acquiredData = String.fromCharCodes(this._data.sublist(1, this._data.length - 1)).split(',');
Nicolas Richard Walter Boeckh's avatar
LOI  
Nicolas Richard Walter Boeckh committed
60
    
61 62 63
    this._latitude = (acquiredData[0] != '') ? parseDoubleWrapper(acquiredData[0]) : null;
    this._longitude = (acquiredData[1] != '') ? parseDoubleWrapper(acquiredData[1]) : null;
    this._altitude = (acquiredData[2] != '') ? int.tryParse(acquiredData[2]) : null;
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
64
    this._heading = (acquiredData[3] != '') ? int.tryParse(acquiredData[3]) : null;
65 66 67
    this._speed = (acquiredData[4] != '') ? parseDoubleWrapper(acquiredData[4]) : null;

    this._temperature = (acquiredData[5] != '') ? parseDoubleWrapper(acquiredData[5]) : null;
68 69
    this._relativeHumidity = (acquiredData[6] != '') ? parseDoubleWrapper(acquiredData[6]) : null;
    this._pressure = (acquiredData[7] != '') ? parseDoubleWrapper(acquiredData[7]) : null;
70 71 72 73 74 75 76 77 78 79 80 81
    
    this._pm1 = (acquiredData[8] != '') ? parseDoubleWrapper(acquiredData[8]) : null;
    this._pm2_5 = (acquiredData[9] != '') ? parseDoubleWrapper(acquiredData[9]) : null;
    this._pm4 = (acquiredData[10] != '') ? parseDoubleWrapper(acquiredData[10]) : null;
    this._pm10 = (acquiredData[11] != '') ? parseDoubleWrapper(acquiredData[11]) : null;

    if (this._latitude == null || this._longitude == null) {
      Position position = PositionHandler().getLastPosition();
      if (position != null) {
        this._latitude = position.latitude;
        this._longitude = position.longitude;
        this._altitude = position.altitude.toInt();
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
82
        this._heading = position.heading.round() % 360;
83 84 85
        this._speed = position.speed;
      }
    }
86
  }
87
  
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
88
  DataPacket.fromData(this._timestamp, this._latitude, this._longitude, this._altitude, this._speed, this._heading, this._temperature, this._pressure, this._relativeHumidity, this._pm1, this._pm2_5, this._pm4, this._pm10, this._extraData);
89

90
  DataPacket.fromMappedData(Map<String, dynamic> data) {
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
91
    List<String> guardSet = ['device_id', 'url', 'timestamp_nix', 'latitude', 'longitude', 'altitude', 'speed', 'heading', 'temperature', 'pressure', 'relative_humidity', 'pm_1', 'pm_2_5', 'pm_4', 'pm_10', 'extra'];
Nicolas Richard Walter Boeckh's avatar
LOI  
Nicolas Richard Walter Boeckh committed
92 93 94 95 96

    if(data.keys.toList().where((String key) => guardSet.contains(key)).length < guardSet.length) {
      throw Exception('Mapped Data Error');
    }

97
    this._deviceId = data['device_id'];
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
98
    this._url = data['url'];
99 100 101 102 103 104 105 106
    this._timestamp = data['timestamp_nix'];
    this._latitude = data['latitude'];
    this._longitude = data['longitude'];
    this._altitude = data['altitude'];
    this._speed = data['speed'];
    this._heading = data['heading'];
    this._temperature = data['temperature'];
    this._pressure = data['pressure'];
Nicolas Richard Walter Boeckh's avatar
LOI  
Nicolas Richard Walter Boeckh committed
107
    this._relativeHumidity = data['relative_humidity'];
108 109 110 111 112 113 114
    this._pm1 = data['pm_1'];
    this._pm2_5 = data['pm_2_5'];
    this._pm4 = data['pm_4'];
    this._pm10 = data['pm_10'];
    this._extraData = data['extra'];
  }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  String stringify() => '${this._latitude}, ${this._longitude}, ${this._temperature}, ${this._pressure}, ${this._relativeHumidity}, ${this._pm2_5}, ${this._pm10}';
  
  List<dynamic> jsonify() => [
    this._timestamp,
    this._latitude,
    this._longitude,
    this._altitude,
    this._speed,
    this._heading,
    this._temperature,
    this._pressure,
    this._relativeHumidity,
    this._pm1,
    this._pm2_5,
    this._pm4,
    this._pm10
131
  ];
132

Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
133
  /// Retrieves the DB insertable reference to this packet.
134 135 136 137 138 139 140 141 142
  Map<String, dynamic> toMap() {
    return {
      'timestamp_nix': this._timestamp,
      'latitude': this._latitude,
      'longitude': this._longitude,
      'altitude': this._altitude,
      'speed': this._speed,
      'heading': this._heading,
      'temperature': this._temperature,
Nicolas Richard Walter Boeckh's avatar
LOI  
Nicolas Richard Walter Boeckh committed
143
      'relative_humidity': this.relativeHumidity,
144 145 146 147 148
      'pressure': this._pressure,
      'pm_1': this._pm1,
      'pm_2_5': this._pm2_5,
      'pm_4': this._pm4,
      'pm_10': this._pm10,
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
149 150 151 152
      'extra': '',
      'device_id': this._deviceId,
      'url': this._url,

153 154 155 156 157
    };
  }

  @override
  String toString() {
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
158
    return 'DataPacket{time: $_timestamp, lat: $_latitude, long: $_longitude, pm1: $_pm1, pm2.5: $_pm2_5, pm4: $_pm4, pm10: $_pm10}';
159
  }
160
}