onStart method

  1. @override
Future<bool> onStart()
override

Callback when this executor is started. Returns true if successfully started, false otherwise.

Implementation

@override
Future<bool> onStart() async {
  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 to the stream.
      for (var data in data) {
        _ctrl.add(Measurement(
            sensorStartTime: data.dateFrom.microsecondsSinceEpoch,
            sensorEndTime: data.dateTo.microsecondsSinceEpoch,
            data: HealthData.fromHealthDataPoint(data)));
      }
    } catch (exception) {
      warning("$runtimeType - Error collecting health data. $exception");
      _ctrl.addError(exception);
      return false;
    }
  }
  return true;
}