-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundcoll...task.txt
More file actions
175 lines (144 loc) · 4.4 KB
/
backgroundcoll...task.txt
File metadata and controls
175 lines (144 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:scoped_model/scoped_model.dart';
class _Message {
int whom;
String text;
_Message(this.whom, this.text);
}
class DataSample {
double temperature1;
double temperature2;
double waterpHlevel;
DateTime timestamp;
DataSample({
required this.temperature1,
required this.temperature2,
required this.waterpHlevel,
required this.timestamp,
});
}
class BackgroundCollectingTask extends Model {
static BackgroundCollectingTask of(
BuildContext context, {
bool rebuildOnChange = false,
}) =>
ScopedModel.of<BackgroundCollectingTask>(
context,
rebuildOnChange: rebuildOnChange,
);
final BluetoothConnection _connection;
List<int> _buffer = List<int>.empty(growable: true);
// @TODO , Such sample collection in real code should be delegated
// (via `Stream<DataSample>` preferably) and then saved for later
// displaying on chart (or even stright prepare for displaying).
// @TODO ? should be shrinked at some point, endless colleting data would cause memory shortage.
List<DataSample> samples = List<DataSample>.empty(growable: true);
bool inProgress = false;
List<_Message> messages = [];
String _messageBuffer = '';
BackgroundCollectingTask._fromConnection(this._connection) {
_connection.input!.listen((data) {_onDataReceived(data);
}).onDone(() {
inProgress = false;
notifyListeners();
});
}
void _onDataReceived(Uint8List data) {
// Allocate buffer for parsed data
int backspacesCounter = 0;
data.forEach((byte) {
if (byte == 8 || byte == 127) {
backspacesCounter++;
}
});
Uint8List buffer = Uint8List(data.length - backspacesCounter);
int bufferIndex = buffer.length;
// Apply backspace control character
backspacesCounter = 0;
for (int i = data.length - 1; i >= 0; i--) {
if (data[i] == 8 || data[i] == 127) {
backspacesCounter++;
} else {
if (backspacesCounter > 0) {
backspacesCounter--;
} else {
buffer[--bufferIndex] = data[i];
}
}
}
// Create message if there is new line character
String dataString = String.fromCharCodes(buffer);
int index = buffer.indexOf(13);
if (~index != 0) {
messages.add(
_Message(
1,
backspacesCounter > 0
?
_messageBuffer.substring(
0, _messageBuffer.length - backspacesCounter)
: _messageBuffer + dataString.substring(0, index),
),
);
_messageBuffer = dataString.substring(index);
print(messages.length-1);
print ("Weight");
print (_messageBuffer);
print ("Weigt");
} else {
_messageBuffer = (backspacesCounter > 0
? _messageBuffer.substring(
0, _messageBuffer.length - backspacesCounter)
: _messageBuffer + dataString);
}
}
static Future<BackgroundCollectingTask> connect(
BluetoothDevice server) async {
final BluetoothConnection connection =
await BluetoothConnection.toAddress(server.address);
return BackgroundCollectingTask._fromConnection(connection);
}
void dispose() {
_connection.dispose();
}
Future<void> start() async {
inProgress = true;
_buffer.clear();
samples.clear();
notifyListeners();
_connection.output.add(ascii.encode('start'));
await _connection.output.allSent;
}
Future<void> cancel() async {
inProgress = false;
notifyListeners();
_connection.output.add(ascii.encode('stop'));
await _connection.finish();
}
Future<void> pause() async {
inProgress = false;
notifyListeners();
_connection.output.add(ascii.encode('stop'));
await _connection.output.allSent;
}
Future<void> reasume() async {
inProgress = true;
notifyListeners();
_connection.output.add(ascii.encode('start'));
await _connection.output.allSent;
}
Iterable<DataSample> getLastOf(Duration duration) {
DateTime startingTime = DateTime.now().subtract(duration);
int i = samples.length;
do {
i -= 1;
if (i <= 0) {
break;
}
} while (samples[i].timestamp.isAfter(startingTime));
return samples.getRange(i, samples.length);
}
}