carp_firebase_backend 0.3.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 49

CARP Firebase Data Backend #

A Flutter plugin for uploading data from the CARP Mobile Sensing Framework to a Google Firebase data backend.

pub package

For Flutter plugins for other CARP products, see CARP Mobile Sensing in Flutter.

This package can upload sending data to Goggle Firebase in two ways.

Google Firebase Storage #

By using the Google Firebase Storage endpoint, CARP sensing data are uploaded as raw or zipped JSON file, generated using the FileDataManager in carp_mobile_sensing. In Firebase, files with sensed data is stored in the path specified in the FirebaseStorageDataEndPoint plus subfolders for each study and device. The path on Firebase hence follow this pattern:

/<path>/<study_id>/<device_id>/

Google Firebase Database #

By using the Google Firebase Database endpoint, CARP sensing data are uploaded as raw JSON data points, using Firebase as a DataManager in carp_mobile_sensing. In Firebase, data json objects are stores in the collection specified in the FirebaseDatabaseDataManager. JSON objects will be stored in collections named:

/<collection>/<study_id>/<device_id>/upload/<data_type>

relative to this path. For example, if collection is carp_data, study_id is 1234 and device_id is R16NW, location data will be stored as documents in this collection:

carp_data/1234/R16NW/upload/location.

Setting up support for Google Firebase #

For Firebase to work with your Flutter app, configuration of both Firebase and your Flutter app has to be done. Please follow the step below in details, since the level of debugging/error messages are quite limited when setting this up. If you are new to Firebase, then please start by reading the extensive Firebase documentation first.

In Google Firebase #

  1. Create a Firebase project with a cloud storage

  2. Add Firebase to your Flutter project by following the description. Note that you need to add support for both the Android and iOS version of the Flutter app.

  3. Download the configuration file named google-services.json and make sure to put in in your Android app module root directory, i.e a folder like <appname>/android/app/

  4. Add support for the Google Services Gradle plugin to read the google-services.json file that was generated by Firebase.

    • in your IDE, open android/app/build.gradle, and add the following line as the last line in the file:

    apply plugin: 'com.google.gms.google-services'

    • In android/build.gradle, inside the buildscript tag, add a new dependency:
      dependencies {
              // ...
              classpath 'com.google.gms:google-services:4.0.1'   // new
          }
    
  5. Configure authentication

  6. Add users that can upload data e.g. via the console

  7. Set up your storage security rules as shown below.

# Default access rule
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}
  • for testing/debugging purposes, you may remove authentication by the authentication rule below (but should be removed in production)
# No authentication rule
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

Your Flutter app should now be connected to Firebase.

In your Flutter App #

In Flutter, the carp_firebase_backend plugin is used to upload data from carp_mobile_sensing to a Firebase endpoint.

Add the carp_firebase_backend plugin to the pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  carp_firebase_backend: ^0.2.0     # support for uploading CARP data to Firebase

Run flutter packages get. For more information on managing packages and plugins, see Using Packages.

Using the Plugin #

Upload of files to Firebase Storage uses the FirebaseStorageDataManager and upload of json objects to Firebase Database uses FirebaseDatabaseDataManager. Using the library takes three steps.

1. Register the Data Managers #

First you should register the data manager you want to use (or both) in the DataManagerRegistry.

DataManagerRegistry.register(DataEndPointType.FIREBASE_STORAGE, new FirebaseStorageDataManager());
DataManagerRegistry.register(DataEndPointType.FIREBASE_DATABASE, new FirebaseDatabaseDataManager());

2. Specify Access Details to the Firebase App #

Both data managers uses a FirebaseEndPoint object to handle access to Firebase. In this object, Firebase endpoint configuration keys are stored as well as authentication details. All of the Firebase configuration keys can be found in the Projects Settings in the Firebase Console. Remember to register your app in Firebase, as described above. The firebaseAuthenticationMethod key specify the authentication method. Currently, only email/password and Google Sign-In is implemented (even though FireBaseAuthenticationMethods lists them all (for future use)).

Using email/password as authentication

final FirebaseEndPoint firebaseEndPoint = new FirebaseEndPoint(
    name: "Flutter Sensing Sandbox",
    uri: 'gs://flutter-sensing-sandbox.appspot.com',
    projectID: 'flutter-sensing-sandbox',
    webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
    gcmSenderID: '201621881872',
    androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
    iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
    firebaseAuthenticationMethod: FireBaseAuthenticationMethods.PASSWORD,
    email: "some_email@dtu.dk",
    password: "some_password");

Using Google Sign-In as authentication

final FirebaseEndPoint firebaseEndPoint = new FirebaseEndPoint(
    name: "Flutter Sensing Sandbox",
    uri: 'gs://flutter-sensing-sandbox.appspot.com',
    projectID: 'flutter-sensing-sandbox',
    webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
    gcmSenderID: '201621881872',
    androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
    iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
    firebaseAuthenticationMethod: FireBaseAuthenticationMethods.GOOGLE);

3. Create the Data Endpoint #

Finally, you create the data endpoint (FirebaseStorageDataEndPoint or FirebaseDatabaseDataEndPoint) providing it with a FirebaseEndPoint and add it as your Study data endpoint.

Firebase Storage Endpoint

final FirebaseStorageDataEndPoint storageEndPoint = new FirebaseStorageDataEndPoint(
   firebaseEndPoint,
   path: 'sensing/data', 
   bufferSize: 500 * 1000, 
   zip: true, 
   encrypt: false);

Study study_1 = new Study("1234", "user_1@dtu.dk", name: "Test study #1");
study_1.dataEndPoint = storageEndPoint;

Note that a FirebaseStorageDataEndPoint extends the FileDataEndPoint class and parameters related to how to create the files can be specified, including bufferSize, zip, and encrypt. In the example above, the file buffer size is set to 1 MB, which is zipped before upload.

Firebase Database Endpoint

final FirebaseDatabaseDataEndPoint databaseEndPoint =
    new FirebaseDatabaseDataEndPoint(firebaseEndPoint, collection: 'carp_data');

Study study_2 = new Study("5678", "user_2@dtu.dk", name: "Test study #2");
study_2.dataEndPoint = databaseEndPoint;

Features and bugs #

Please file feature requests and bug reports at the issue tracker.

License #

This software is copyright (c) 2018 Copenhagen Center for Health Technology (CACHET) at the Technical University of Denmark (DTU). This software is made available 'as-is' in a MIT license.

0.3.0 #

  • Adjustment to refactoring of carp_mobile_sensing library to version 0.3.0

0.2.0 #

  • Support for uploading to the Google Firebase Database also.

0.1.1 #

  • Bug fixes
  • Uploading zip files (instead of json)
  • using carp_core as domain model

0.1.0 #

  • Version tested and ready for use
  • Detailed documentation (see readme.md file)
  • Improved cooperation with FileDataManager

0.0.1 #

  • Initial version of dedicated package bringing together the old implementation.

example/example.dart

import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
import 'package:carp_firebase_backend/carp_firebase_backend.dart';

void main() {
  // Using email/password as authentication
  final FirebaseEndPoint firebaseEndPoint_1 = new FirebaseEndPoint(
      name: "Flutter Sensing Sandbox",
      uri: 'gs://flutter-sensing-sandbox.appspot.com',
      projectID: 'flutter-sensing-sandbox',
      webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
      gcmSenderID: '201621881872',
      androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
      iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
      firebaseAuthenticationMethod: FireBaseAuthenticationMethods.PASSWORD,
      email: "some_email@dtu.dk",
      password: "some_password");

  final FirebaseStorageDataEndPoint storageEndPoint = new FirebaseStorageDataEndPoint(firebaseEndPoint_1,
      path: 'sensing/data', bufferSize: 500 * 1000, zip: true, encrypt: false);

  Study study_1 = new Study("1234", "user_1@dtu.dk", name: "Test study #1");
  study_1.dataEndPoint = storageEndPoint;

  // Using Google Sign-In as authentication
  final FirebaseEndPoint firebaseEndPoint_2 = new FirebaseEndPoint(
      name: "Flutter Sensing Sandbox",
      uri: 'gs://flutter-sensing-sandbox.appspot.com',
      projectID: 'flutter-sensing-sandbox',
      webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
      gcmSenderID: '201621881872',
      androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
      iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
      firebaseAuthenticationMethod: FireBaseAuthenticationMethods.GOOGLE);

  final FirebaseDatabaseDataEndPoint databaseEndPoint =
      new FirebaseDatabaseDataEndPoint(firebaseEndPoint_2, collection: 'carp_data');

  Study study_2 = new Study("5678", "user_2@dtu.dk", name: "Test study #2");
  study_2.dataEndPoint = databaseEndPoint;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  carp_firebase_backend: ^0.3.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:carp_firebase_backend/carp_firebase_backend.dart';
  
Version Uploaded Documentation Archive
0.3.0 Jan 21, 2019 Go to the documentation of carp_firebase_backend 0.3.0 Download carp_firebase_backend 0.3.0 archive
0.2.0 Dec 5, 2018 Go to the documentation of carp_firebase_backend 0.2.0 Download carp_firebase_backend 0.2.0 archive
0.1.1 Nov 15, 2018 Go to the documentation of carp_firebase_backend 0.1.1 Download carp_firebase_backend 0.1.1 archive
0.1.0 Oct 21, 2018 Go to the documentation of carp_firebase_backend 0.1.0 Download carp_firebase_backend 0.1.0 archive
0.0.1 Oct 21, 2018 Go to the documentation of carp_firebase_backend 0.0.1 Download carp_firebase_backend 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

We analyzed this package on Apr 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.4.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/firebase/firebase_data_managers.dart. (-3.45 points)

Analysis of lib/firebase/firebase_data_managers.dart reported 7 hints, including:

line 17 col 25: Use ; instead of {} for empty constructor bodies.

line 197 col 33: Use ; instead of {} for empty constructor bodies.

line 226 col 11: Name non-constant identifiers using lowerCamelCase.

line 230 col 20: Name non-constant identifiers using lowerCamelCase.

line 231 col 20: Name non-constant identifiers using lowerCamelCase.

Format lib/firebase/firebase_data_endpoint.dart.

Run flutter format to format lib/firebase/firebase_data_endpoint.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
carp_mobile_sensing ^0.3.0 0.3.10
cloud_firestore ^0.8.2+3 0.8.2+3 0.9.13+1
firebase_auth ^0.5.20 0.5.20 0.8.4+5
firebase_storage ^1.0.2 1.1.0+1 2.1.0+1
flutter 0.0.0
google_sign_in ^3.0.5 3.3.0+1 4.0.1+3
json_annotation ^1.0.0 1.2.0 2.2.0
Transitive dependencies
app_usage 0.1.0
archive 2.0.8
args 1.5.1
async 2.2.0
battery 0.2.3 0.3.0+3
charcode 1.1.2
collection 1.14.11
connectivity 0.3.2 0.4.3
convert 2.1.1
crypto 2.0.6
device_apps 1.0.7
device_info 0.2.1 0.4.0+1
file_utils 0.1.1
firebase_core 0.2.5+1 0.3.4
fixnum 0.10.9
flutter_blue 0.4.2 0.5.0
globbing 0.2.0
light 0.1.1
meta 1.1.6 1.1.7
path 1.6.2
path_provider 0.4.1 0.5.0+1
pedometer 0.0.5
protobuf 0.10.8 0.13.11
screen_state 0.1.1
sensors 0.3.5 0.4.0+1
simple_permissions 0.1.9
sky_engine 0.0.99
stats 0.2.0+2
system_info 0.1.1
typed_data 1.1.6
uuid 1.0.3 2.0.1
vector_math 2.0.8
Dev dependencies
build_runner any
flutter_test
json_serializable ^1.0.0