data_header.dart 1.96 KB
Newer Older
1
import 'package:collection/collection.dart';
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
2
import 'package:flutter/cupertino.dart';
3

4 5
/// @ToDeprecate Describes the contents of the data packets
/// @ReplaceBy Includes the device name and a config_file URL
6
class DataHeader {
7
  /// Singleton factory
8 9 10 11
  factory DataHeader() => _singleton;

  DataHeader._internal() {
    _headerData = [];
12
    _headerSet = false;
13 14 15 16
  }
  
  static final DataHeader _singleton = new DataHeader._internal();

17
  /// Data contained within the Header packet
18
  List<String> _headerData;
19 20 21

  bool _headerSet;

Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
22 23 24 25
  String _deviceId;

  String _url;

26 27
  void setHeader(List<int> header) {
    this._headerData = String.fromCharCodes(header.sublist(1, header.length - 1)).split(',');
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
28 29
    this._deviceId = this._headerData[0];
    this._url = this._headerData[1];
30 31 32
    this._headerSet = true;
  }

Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
33 34 35 36
  void setHeaderFromExisting({@required String deviceId, @required String url}) {
    this._headerData = [deviceId, url];
    this._deviceId = deviceId;
    this._url = url;
37 38 39
    this._headerSet = true;
  }

40
  /// Retrieve the deviceID from the header
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
41 42 43
  String get deviceId => (this._headerSet && this._deviceId != null) ? this._deviceId : '';
  String get url => (this._headerSet && this._url != null) ? this._url : '';

44

45
  /// @Getter for _headerSet;
46
  bool get headerSet => this._headerSet;
47

48 49 50 51 52 53 54
  Stream<bool> getWhenHeaderSet() async* {
    while (true) {
      yield this._headerSet;
      await Future.delayed(Duration(seconds: 1));
    }
  }

55 56
  bool isEqual(List<int> that) => ListEquality().equals(_headerData, that);

57
  // TODO
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
58
  Map<String, dynamic> jsonify() => { 'device_id' : this.deviceId, 'url': this.url };
59 60 61 62 63

  void dispose() {
    this._headerSet = false;
    this._headerData = []; 
  }
64 65 66

  Stream<String> deviceIdStream() async* {
    while(true) {
Nicolas Richard Walter Boeckh's avatar
Nicolas Richard Walter Boeckh committed
67
      yield this._deviceId;
68 69 70 71 72 73 74 75 76 77
      await Future.delayed(Duration(seconds: 1));
    }
  }

  Stream<int> deviceBatteryLevel() async* {
    while(true) {
      yield -1;
      await Future.delayed(Duration(seconds: 1));
    }
  }
78
}