diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java index 701b812bef..803e3d1b47 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java @@ -23,10 +23,14 @@ import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import de.greenrobot.dao.AbstractDao; import de.greenrobot.dao.Property; import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.WhereCondition; + import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java index 3af113b312..01954bb8c8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java @@ -21,7 +21,6 @@ import android.support.annotation.Nullable; import java.util.List; - import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; /** diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java index 927af487f5..63712334c9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java @@ -27,6 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.util.ArrayList; @@ -57,6 +58,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband2.AbstractMiBand2Operation; import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils; import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils; +import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; +import nodomain.freeyourgadget.gadgetbridge.util.CSVExport; import nodomain.freeyourgadget.gadgetbridge.util.GB; /** @@ -207,6 +210,14 @@ private GregorianCalendar saveSamples() { timestamp.add(Calendar.MINUTE, 1); } + + // export csv on fetching + File myPath = null; + myPath = FileUtils.getExternalFilesDir(); + File myFile = new File(myPath, startTimestamp.getTimeInMillis() / 1000 + ".csv"); + CSVExport.exportToCSV(samples.toArray(new MiBandActivitySample[0]), myFile); + + // store samples on fetching sampleProvider.addGBActivitySamples(samples.toArray(new MiBandActivitySample[0])); saveLastSyncTimestamp(timestamp); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CSVExport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CSVExport.java new file mode 100644 index 0000000000..c7b4420ea5 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/CSVExport.java @@ -0,0 +1,42 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; +import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; + +/** + * Created by Vebryn on 29/05/17. + */ + +public class CSVExport { + private static final Logger LOG = LoggerFactory.getLogger(SampleProvider.class); + + public static void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) { + String separator = ","; + + LOG.info("Exporting samples into csv file: " + outFile.getName()); + try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) { + bw.write("TIMESTAMP" + separator + "DEVICE_ID" + separator + "USER_ID" + separator + "RAW_INTENSITY" + separator + "STEPS" + separator + "RAW_KIND" + separator + "HEART_RATE"); + bw.newLine(); + + for (AbstractActivitySample sample : activitySamples){ + String line = sample.getTimestamp() + separator + sample.getDeviceId() + separator + sample.getUserId() + separator + sample.getRawIntensity() + separator + sample.getSteps() + separator + sample.getRawKind() + separator + sample.getHeartRate(); + + //LOG.debug("Adding line into buffer: " + line); + bw.write(line); + bw.newLine(); + } + + bw.flush(); + } catch (IOException e) { + LOG.error("Error related to " + outFile.getName() + " file: " + e.getMessage(), e); + } + } +}