start method

Future<void> start()

Start data collection using this controller.

Will attempt to deploy this study if not already done.

Will resume data collection if the study's samplingStatus is Resumed. If not, sampling can be started later by calling the resume method.

Implementation

Future<void> start() async {
  if (study.status == StudyStatus.Stopped) {
    warning(
      '$runtimeType - Study has been stopped. Will not start data sampling.',
    );
    return;
  }

  info(
    '$runtimeType - Starting data sampling for study deployment: ${study.studyDeploymentId}',
  );

  // If this study has not yet been deployed, do this first.
  if (!study.isDeployed) {
    debug('$runtimeType - Study not yet deployed - trying to deploy...');
    await SmartPhoneClientManager().tryDeployment(
      study.studyDeploymentId,
      study.deviceRoleName,
    );
  }

  // Ask for permissions for all measures in this deployment
  if (SmartPhoneClientManager().askForPermissions) {
    await _askForAllPermissions();
  }

  // TODO - why this model? The AppTaskController should be responsible for restoring itself... - just like sampling state
  // Restore the app task controller state for this study.
  await AppTaskController()._restoreQueue(study);

  // Finally, resume/pause data sampling based on the current sampling state of this study.
  if (study.samplingState?.state == ExecutorState.Resumed) {
    debug('$runtimeType - Restarting sampling in 15 seconds...');
    Future.delayed(Duration(seconds: 15), () => resume());
  } else if (study.samplingState?.state == ExecutorState.Paused) {
    pause();
  }
}