tryRegisterConnectedDevice method

Future<void> tryRegisterConnectedDevice(
  1. DeviceConfiguration<DeviceRegistration> device
)

Tries to register the connected device with the deployment service. The device must be available in the device controller.

Implementation

Future<void> tryRegisterConnectedDevice(DeviceConfiguration device) async {
  final deviceType = device.type;
  final deviceRoleName = device.roleName;

  info(
    "$runtimeType - Trying to register device with role name "
    "'$deviceRoleName' for study deployment '${study.studyDeploymentId}'.",
  );

  // Check if this phone has this type of device.
  if (_deviceController.hasDevice(deviceType)) {
    DeviceManager deviceManager = _deviceController.getDeviceManager(
      deviceType,
    )!;

    DeviceRegistration registration = deviceManager.createRegistration();

    try {
      final deploymentStatus = await _deploymentService.registerDevice(
        study.studyDeploymentId,
        deviceRoleName,
        registration,
      );

      // Also update local deployment information about the connected device registrations,
      // so that it is in sync with the deployment service.
      if (device is! PrimaryDeviceConfiguration) {
        deployment?.connectedDeviceRegistrations[deviceRoleName] =
            registration;
      }
      study.deploymentStatusReceived(deploymentStatus);
    } catch (error) {
      warning(
        "$runtimeType - Failed to register device with role name "
        "'$deviceRoleName' for study deployment '${study.studyDeploymentId}' "
        "at deployment service '$_deploymentService'.\n"
        "Error: $error\n"
        "Continuing without registration.",
      );
    }
  } else {
    warning(
      "$runtimeType - Trying to register device of type '$deviceType' playing the role "
      "'$deviceRoleName' for study deployment '${study.studyDeploymentId}'. "
      "But this smartphone does not have such a type of device."
      "Continuing without registration.",
    );
  }
}