tryDeployment method

Future<StudyStatus> tryDeployment()

Verifies whether the master device is ready for deployment and in case it is, deploy the study previously added. Deployment entails trying to retrieve the deployment from the deploymentService, based on the studyDeploymentId.

In case already deployed, nothing happens.

Implementation

Future<StudyStatus> tryDeployment() async {
  assert(
      study != null && device != null,
      'Cannot deploy without a valid study deployment id and device role name. '
      "Call 'initialize()' first.");

  // early out if already deployed.
  if (status.index >= StudyStatus.Deployed.index) return status;

  _status = StudyStatus.Deploying;
  deploymentStatus = await deploymentService
      .getStudyDeploymentStatus(study!.studyDeploymentId);

  // get the deployment from the deployment service
  deployment = await deploymentService.getDeviceDeploymentFor(
    study!.studyDeploymentId,
    device!.roleName,
  );
  _status = StudyStatus.DeploymentReceived;

  // TODO - set _remainingDevicesToRegister

  // mark this deployment as successful
  try {
    await deploymentService.deploymentSuccessfulFor(
      study!.studyDeploymentId,
      device!.roleName,
      deployment!.lastUpdateDate,
    );
  } catch (error) {
    // we only print a warning
    // see issue #50 - there is a bug in CARP
    print(
        "$runtimeType - Error marking deployment '${study!.studyDeploymentId}' as successful.\n$error");
  }
  print(
      "$runtimeType - Study deployment '${study!.studyDeploymentId}' successfully deployed.");

  _status = StudyStatus.Deployed;

  return _status;
}