tryDeployment method

Future<StudyStatus> tryDeployment()

Tries to deploy the study if it's ready to be deployed by registering the client device using deviceRegistration and verifying the study is supported on this device.

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,
      'Cannot deploy without a valid study deployment id and device role name. '
      "Call 'configure()' first.");

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

  // check the status of this deployment.
  if (await getStudyDeploymentStatus() == null) return status;

  status = StudyStatus.Deploying;

  // register the primary device for the given study deployment
  try {
    deploymentStatus = await deploymentService.registerDevice(
      study!.studyDeploymentId,
      study!.deviceRoleName,
      deviceRegistration!,
    );
  } catch (error) {
    // we only print a warning - this device may already be registered
    print(
        "$runtimeType - Error registering '${study!.deviceRoleName}' as primary device.\n$error");
  }

  // get the deployment from the deployment service
  deployment = await deploymentService.getDeviceDeploymentFor(
    study!.studyDeploymentId,
    study!.deviceRoleName,
  );
  status = StudyStatus.DeviceDeploymentReceived;

  if (deploymentStatus != null) {
    for (var deviceStatus in deploymentStatus!.deviceStatusList) {
      if (deviceStatus.status == DeviceDeploymentStatusTypes.Unregistered) {
        _remainingDevicesToRegister.add(deviceStatus.device);
      }
    }
  }

  // mark this deployment as successful
  try {
    await deploymentService.deviceDeployed(
      study!.studyDeploymentId,
      study!.deviceRoleName,
      deployment?.lastUpdatedOn ?? DateTime.now(),
    );
  } catch (error) {
    // we only print a warning
    // see issue #50 - there is a bug in CAWS
    print(
        "$runtimeType - Error marking deployment '${study!.studyDeploymentId}' as deployed.\n$error");
  }
  print(
      "$runtimeType - Study deployment '${study!.studyDeploymentId}' successfully deployed.");

  return status = StudyStatus.Deployed;
}