write method
Writes a JSON encoded dataPoint
to the file.
Implementation
Future<void> write(DataPoint dataPoint) async {
// check if the sink is ready for writing
if (!_initialized) {
info('File sink not ready -- delaying for 2 sec...');
sink.then((_) {});
return Future.delayed(const Duration(seconds: 2), () => write(dataPoint));
// sink.then((_) => write(dataPoint));
}
final json = jsonEncode(dataPoint);
await sink.then((activeSink) async {
try {
// always add a comma directly after json
activeSink.write('$json\n,\n');
debug(
'Writing data point to file - type: ${dataPoint.carpHeader.dataFormat}');
await file.then((activeFile) async {
await activeFile.length().then((len) async {
if (len > fileDataEndPoint.bufferSize) {
await flush(activeFile, activeSink);
}
});
});
} catch (err) {
warning('Error writing to file - $err');
_initialized = false;
write(dataPoint);
}
});
}