data_header.dart 1.8 KB
Newer Older
1
2
import 'package:collection/collection.dart';

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

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

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

  bool _headerSet;

21
22
23
24
25
  void setHeader(List<int> header) {
    this._headerData = String.fromCharCodes(header.sublist(1, header.length - 1)).split(',');
    this._headerSet = true;
  }

26
27
28
29
30
  void setHeaderFromExisting({String deviceId = '', String url = ''}) {
    this._headerData = [deviceID, url];
    this._headerSet = true;
  }

31
32
  /// Retrieve the deviceID from the header
  String get deviceID => (this._headerData != null && this._headerData.length > 0) ? this._headerData[0] : '';
33
  String get url => (this._headerData != null && this._headerData.length > 0) ? this._headerData[1] : '';
34

35
  /// @Getter for _headerSet;
36
  bool get headerSet => this._headerSet;
37

38
39
40
41
42
43
44
  Stream<bool> getWhenHeaderSet() async* {
    while (true) {
      yield this._headerSet;
      await Future.delayed(Duration(seconds: 1));
    }
  }

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

47
48
  // TODO
  Map<String, dynamic> jsonify() => { 'device_id' : this._headerData[0], 'url': '000000000000' };
49
50
51
52
53

  void dispose() {
    this._headerSet = false;
    this._headerData = []; 
  }
54
55
56
57
58
59
60
61
62
63
64
65
66
67

  Stream<String> deviceIdStream() async* {
    while(true) {
      yield this.deviceID;
      await Future.delayed(Duration(seconds: 1));
    }
  }

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