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 {
  // Early out if no probes.
  if (probes.isEmpty) return true;

  // Early out if already running (this is a background task)
  if (state == ExecutorState.started) {
    warning(
        'Trying to start $this but it is already started. Ignoring this.');
    return false;
  }

  // Listen to stop this background executor when all of its underlying
  // probes have stopped - Issue #384
  _subscription =
      states.where((event) => event == ExecutorState.stopped).listen((_) {
    if (haveAllProbesStopped) {
      debug(
          '$runtimeType - all probes have stopped - stopping this $this too.');
      stop();
    }
  });

  // Check if the devices for this task is connected.
  await connectAllConnectableDevices();

  if (configuration?.duration != null) {
    // If the task has a duration (optional), stop it again after this duration has passed.
    Timer(Duration(seconds: configuration!.duration!.inSeconds.truncate()),
        () => stop());
  }

  // Now - finally - we can start the probes.
  return await super.onStart();
}