onMeasurement method
On each measurement collected, the onMeasurement handler is called.
Implementations of this interface should handle how to save
or upload the measurement.
Implementation
@override
Future<void> onMeasurement(Measurement measurement) async {
// If the database hasn't been created yet, wait for 3 secs
if (database == null) {
return Future.delayed(
const Duration(seconds: 3),
() => onMeasurement(measurement),
);
}
final Map<String, dynamic> map = {
UPLOADED_COLUMN: 0,
DEPLOYMENT_ID_COLUMN: deployment.studyDeploymentId,
TRIGGER_ID_COLUMN: measurement.taskControl?.triggerId ?? 0,
DEVICE_ROLE_NAME_COLUMN:
measurement.taskControl?.destinationDeviceRoleName ??
deployment.deviceConfiguration.roleName,
DATATYPE_COLUMN: measurement.dataType.toString(),
RECORD_ID_COLUMN: measurement.data.recordId,
MEASUREMENT_COLUMN: jsonEncode(measurement),
};
// Fast out if DB has been closed.
// This may happen when the data manager is closed while some probes are still
// running and sampling measurements.
if (!database!.isOpen) return;
// One transaction per burst: a transaction per row caps out at a few
// hundred inserts/s, which is too slow for e.g. health data catch-up.
_rows.add(map);
_flushTimer ??= Timer(const Duration(milliseconds: 500), _flush);
}