onStart method
Callback when this executor is started. Returns true if successfully started, false otherwise.
Implementation
@override
Future<bool> onStart() async {
// Check if we have permissions to access health data and fast out if not.
if (!await hasPermissions()) return false;
if (await super.onStart()) {
DateTime start = samplingConfiguration.lastTime ??
DateTime.now().subtract(samplingConfiguration.past);
DateTime end = DateTime.now();
List<HealthDataType> healthDataTypes =
samplingConfiguration.healthDataTypes;
if (healthDataTypes.isEmpty) {
warning(
"$runtimeType - Trying to collect health data but the list of health data type to collect is empty. "
"Did you add any types to the protocol which are available on this platform (iOS or Android)?");
} else {
debug(
'$runtimeType - Collecting health data, types: $healthDataTypes, start: ${start.toUtc()}, end: ${end.toUtc()}');
try {
List<HealthDataPoint>? data =
await deviceManager.service?.getHealthDataFromTypes(
startTime: start,
endTime: end,
types: healthDataTypes,
) ??
[];
debug(
'$runtimeType - Retrieved ${data.length} health data points of types: $healthDataTypes');
// Convert HealthDataPoint to measurements and add them the measurements stream.
for (var data in data) {
addMeasurement(Measurement(
sensorStartTime: data.dateFrom.microsecondsSinceEpoch,
sensorEndTime: data.dateTo.microsecondsSinceEpoch,
data: HealthData.fromHealthDataPoint(data)));
}
// Automatically stop this probe after it is done adding the measurements.
Future.delayed(const Duration(seconds: 5), () => stop());
} catch (exception) {
warning("$runtimeType - Error collecting health data. $exception");
_ctrl.addError(exception);
return false;
}
}
return true;
}
return false;
}