onInitialize method

  1. @override
bool onInitialize()
override

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

Note that this is a non-async method and should hence be 'light-weight' and not block execution for a long duration.

Implementation

@override
bool onInitialize() {
  if (configuration == null) {
    warning(
        'Trying to initialize StudyDeploymentExecutor but the deployment configuration is null. Cannot initialize study deployment.');
    return false;
  }

  _group.add(_manualMeasurementController.stream);

  for (var taskControl in configuration!.taskControls) {
    // get the trigger and task based on the trigger id and task name
    final trigger = configuration!.triggers['${taskControl.triggerId}']!;
    final task = configuration!.getTaskByName(taskControl.taskName)!;

    TaskControlExecutor executor;

    // a TriggeredAppTaskExecutor need BOTH a [Schedulable] trigger and an [AppTask]
    // to schedule
    if (trigger is Schedulable && task is AppTask) {
      executor = AppTaskControlExecutor(this, taskControl, trigger, task);
    } else {
      // all other cases we use the normal background triggering relying on the app
      // running in the background
      executor = TaskControlExecutor(this, taskControl, trigger, task);
    }

    executor.initialize(taskControl, deployment!);
    addExecutor(executor);

    // let the device manger know about this executor
    getDeviceManagerFromRoleName(
            executor.taskControl.destinationDeviceRoleName)
        ?.executors
        .add(executor);
  }

  // listen for "done" tasks and add them as a [CompletedTask] measurement
  AppTaskController()
      .userTaskEvents
      .where((userTask) => userTask.state == UserTaskState.done)
      .listen((userTask) {
    addMeasurement(Measurement.fromData(CompletedTask(
      taskName: userTask.name,
      taskData: userTask.result,
    )));
  });

  return true;
}