carp_backend 0.21.2 copy "carp_backend: ^0.21.2" to clipboard
carp_backend: ^0.21.2 copied to clipboard

outdated

CARP data backend for CARP mobile sensing. Supports uploading json to CARP as either zipped filed or as plain json in collections.

example/lib/example.dart

import 'package:carp_backend/carp_backend.dart';
import 'package:carp_webservices/carp_auth/carp_auth.dart';
import 'package:carp_webservices/carp_services/carp_services.dart';
import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
import 'package:research_package/model.dart';

void main() async {
  // -----------------------------------------------
  // EXAMPLE OF CONFIGURING THE CARP APP
  // -----------------------------------------------

  final String uri = "https://cans.cachet.dk:443";

  // configure an app that points to the CARP web service
  CarpApp app = CarpApp(
    name: 'any_display_friendly_name_is_fine',
    uri: Uri.parse(uri),
    oauth: OAuthEndPoint(
      clientID: 'the_client_id',
      clientSecret: 'the_client_secret',
    ),
  );

  // configure the CARP service
  CarpService().configure(app);

  // authenticate at CARP
  await CarpService()
      .authenticate(username: 'the_username', password: 'the_password');

  // configure the other services needed
  CarpParticipationService().configureFrom(CarpService());
  CarpDeploymentService().configureFrom(CarpService());

  // get the invitations to studies from CARP for this user
  List<ActiveParticipationInvitation> invitations =
      await CarpParticipationService().getActiveParticipationInvitations();

  // use the first (i.e. latest) invitation
  String studyDeploymentId = invitations[0].studyDeploymentId;

  // -----------------------------------------------
  // EXAMPLE OF GETTING A STUDY PROTOCOL FROM CARP
  // -----------------------------------------------

  // create a CARP study manager and initialize it
  CarpStudyProtocolManager manager = CarpStudyProtocolManager();
  await manager.initialize();

  // get the study from CARP
  StudyProtocol study = await manager.getStudyProtocol('protocol_id');
  print(study);

  // -----------------------------------------------
  // EXAMPLE OF GETTING A STUDY DEPLOYMENT FROM CARP
  // -----------------------------------------------

  // get the status of the deployment
  StudyDeploymentStatus status = await CustomProtocolDeploymentService()
      .getStudyDeploymentStatus(studyDeploymentId);

  // create and configure a client manager for this phone
  SmartPhoneClientManager client = SmartPhoneClientManager(
    deploymentService: CustomProtocolDeploymentService(),
    deviceRegistry: DeviceController(),
  );
  await client.configure();

  String deviceRolename = status?.masterDeviceStatus?.device?.roleName;

  // add and deploy this deployment using its rolename
  StudyDeploymentController controller =
      await client.addStudy(studyDeploymentId, deviceRolename);

  // configure the controller with the default privacy schema
  await controller.configure();
  // controller.resume();

  // listening on the data stream and print them as json to the debug console
  controller.data.listen((data) => print(toJsonString(data)));

  // -----------------------------------------------
  // DIFFERENT WAYS TO UPLOAD DATA TO CARP
  // -----------------------------------------------

  // first register the CARP data manager
  DataManagerRegistry().register(CarpDataManager());

  // create a CARP data endpoint that upload using the DATA_POINT method
  CarpDataEndPoint cdep = CarpDataEndPoint(
      uploadMethod: CarpUploadMethod.DATA_POINT,
      name: 'CARP Staging Server',
      uri: 'http://staging.carp.cachet.dk:8080',
      clientId: 'carp',
      clientSecret: 'a_secret',
      email: 'username@cachet.dk',
      password: 'password');

  // using the file method would also take information on file size whether to zip it
  CarpDataEndPoint cdep_2 = CarpDataEndPoint(
      uploadMethod: CarpUploadMethod.FILE,
      name: 'CARP Staging Server',
      uri: 'http://staging.carp.cachet.dk:8080',
      clientId: 'carp',
      clientSecret: 'a_secret',
      email: 'username@cachet.dk',
      password: 'password',
      bufferSize: 500 * 1000,
      zip: true);
  print('$cdep_2');

  // using the batch upload method could also take information on file size,
  // but the file must NOT be zipped
  CarpDataEndPoint cdep_3 = CarpDataEndPoint(
    uploadMethod: CarpUploadMethod.BATCH_DATA_POINT,
    name: 'CARP Staging Server',
    uri: 'http://staging.carp.cachet.dk:8080',
    clientId: 'carp',
    clientSecret: 'a_secret',
    email: 'username@cachet.dk',
    password: 'password',
    bufferSize: 500 * 1000,
    deleteWhenUploaded: true,
  );
  print('$cdep_3');

  StudyDeploymentStatus status_2 =
      await CarpDeploymentService().getStudyDeploymentStatus(studyDeploymentId);
  print(status_2);

  // get the master device deployment
  MasterDeviceDeployment deployment =
      await CarpDeploymentService().getDeviceDeploymentFor(
    status.studyDeploymentId,
    status.masterDeviceStatus.device.roleName,
  );

  /// change a master device deployment to use another data endpoint before
  /// it is being executed
  deployment.dataEndPoint = cdep;

  // ... or configure the controller with this data endpoint
  await controller.configure(dataEndPoint: cdep);

  // --------------------------------------------------
  // EXAMPLE OF GETTING AN INFORMED CONSENT FROM CARP
  // --------------------------------------------------

  // create and initialize the informed consent manager
  CarpResourceManager icManager = CarpResourceManager();
  icManager.initialize();

  // get the informed consent as a RP ordered task
  RPOrderedTask informedConsent = await icManager.getInformedConsent();

  print(informedConsent);

  // upload another informed consent to CARP
  RPOrderedTask anotherInformedConsent = RPOrderedTask('12', [
    RPInstructionStep(
      "1",
      title: "Welcome!",
    )..text = "Welcome to this study! ",
    RPCompletionStep("2")
      ..title = "Thank You!"
      ..text = "We saved your consent document.",
  ]);
  await icManager.setInformedConsent(anotherInformedConsent);
}
3
likes
0
pub points
75%
popularity

Publisher

verified publishercachet.dk

CARP data backend for CARP mobile sensing. Supports uploading json to CARP as either zipped filed or as plain json in collections.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

carp_mobile_sensing, carp_webservices, flutter, json_annotation, meta, research_package

More

Packages that depend on carp_backend