uploadData method

Future<void> uploadData(
  1. DataPoint dataPoint
)

Handle upload of data depending on the specified CarpUploadMethod.

Implementation

Future<void> uploadData(DataPoint dataPoint) async {
  // Check if CARP authentication is ready before writing...
  if (!_initialized) {
    warning("Waiting for CARP to be initialized -- delaying for 10 sec...");
    return Future.delayed(
        const Duration(seconds: 10), () => uploadData(dataPoint));
  }

  CarpUser? _user = await user;
  if (_user == null) {
    warning('User is not authenticated - username: ${carpEndPoint.email}');
    return;
  } else {
    // first check if this is a [FileDatum] that has a separate file to be uploaded
    if (dataPoint.data is FileDatum) {
      FileDatum fileDatum = dataPoint.data as FileDatum;
      if (fileDatum.upload) _uploadFileToCarp(fileDatum);
    }

    // then upload the datum as specified in the upload method.
    switch (carpEndPoint.uploadMethod) {
      case CarpUploadMethod.DATA_POINT:
        info(
            'Uploading data point to CARP - ${dataPoint.carpHeader.dataFormat}');
        await CarpService().getDataPointReference().postDataPoint(dataPoint);
        break;
      case CarpUploadMethod.BATCH_DATA_POINT:
      case CarpUploadMethod.FILE:
        // In both cases, forward to [FileDataManager], which collects data
        // in a file before upload.
        await fileDataManager.write(dataPoint);
        break;
      case CarpUploadMethod.DOCUMENT:
        info(
            'Uploading data point document to CARP - ${dataPoint.carpHeader.dataFormat}');
        await CarpService()
            .collection('/${carpEndPoint.collection}')
            .document()
            .setData(
                json.decode(json.encode(dataPoint)) as Map<String, dynamic>);
        return;
    }
  }
}