uploadData method
Called every time a JSON CARP data
object is to be uploaded.
Implementation
Future<bool> uploadData(DataPoint dataPoint) async {
assert(dataPoint.data is Datum);
final datum = dataPoint.data as Datum?;
User? authenticatedUser = await user;
if (authenticatedUser != null) {
final String deviceId = DeviceInfo().deviceID.toString();
final String dataType = dataPoint.carpHeader.dataFormat.toString();
final jsonDataString = json.encode(dataPoint);
Map<String, dynamic> jsonData =
json.decode(jsonDataString) as Map<String, dynamic>;
// add json data
FirebaseFirestore.instance
.collection(firebaseDatabaseDataEndPoint!.collection)
.doc(studyDeploymentId) // study deployment id
.collection(deviceId) // device id
.doc('upload') // the default upload document is called 'upload'
.collection(dataType) // data/measure type
.doc(datum!.id) // data id
.set(jsonData);
return true;
} else {
warning(
'Could not upload data in $runtimeType - no user is authenticated.');
}
return false;
}