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 a $runtimeType, 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)!;
    final targetDevice = configuration!.getDeviceFromRoleName(
      taskControl.destinationDeviceRoleName!,
    )!;

    // Only create an executor for "real" tasks
    if (task is! MonitoringTask) {
      TaskControlExecutor executor;

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

      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 [CompletedAppTask] measurement
  AppTaskController().userTaskEvents
      .where((userTask) => userTask.state == UserTaskState.done)
      .listen(
        (userTask) => addMeasurement(
          Measurement.fromData(CompletedAppTask.fromUserTask(userTask)),
        ),
      );

  return true;
}