Search in sources :

Example 1 with FitProActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample in project Gadgetbridge by Freeyourgadget.

the class FitProDeviceSupport method handleStepData.

public void handleStepData(byte[] value) {
    debugPrintArray(value, "step data value");
    // step packet consists of: date + list of 8bytes of (always?) 5minutes intervals
    // these intervals contain seconds offset from the date, type of activity, calories,
    // steps, distance, duration
    byte[] dateArray = new byte[2];
    System.arraycopy(value, 8, dateArray, 0, 2);
    Calendar date = decodeDateTime(dateArray);
    List<FitProActivitySample> samples = new ArrayList<>();
    for (int i = 12; i < value.length - 7; i = i + 8) {
        byte[] packet = new byte[8];
        System.arraycopy(value, i, packet, 0, 8);
        long data = ByteBuffer.wrap(packet).getLong();
        int steps = (int) Math.abs((data >> 52));
        int calories = (int) (data & 0x7ffff);
        int activity_kind = (int) ((data >> 19) & 0x1);
        int duration = (int) ((data >> 48) & 0xf);
        int distance = (int) ((data >> 32) & 0xffff);
        int encodedTime = (int) ((data >> 21) & 0x7ff);
        int seconds = getSecondsOfDay(encodedTime);
        // do not modify the caller's argument
        Calendar now = (Calendar) date.clone();
        now.add(Calendar.SECOND, seconds);
        int timestamp = (int) (now.getTimeInMillis() / 1000L);
        debugPrintArray(packet, "processing steps packet");
        LOG.debug("FitPro adding new steps: " + steps);
        FitProActivitySample sample = new FitProActivitySample();
        sample.setTimestamp(timestamp);
        sample.setHeartRate(ActivitySample.NOT_MEASURED);
        sample.setSteps(steps);
        sample.setDistanceMeters(distance);
        sample.setCaloriesBurnt(calories);
        sample.setActiveTimeMinutes(duration);
        sample.setRawKind(rawActivityKindToUniqueKind(activity_kind));
        samples.add(sample);
    }
    if (addGBActivitySamples(samples)) {
        sendAck(value[3], value[1], value[2], value[5]);
    }
}
Also used : FitProActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList)

Example 2 with FitProActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample in project Gadgetbridge by Freeyourgadget.

the class FitProDeviceSupport method getStepsOnDay.

private int getStepsOnDay(Calendar dayStart, Calendar dayEnd) {
    // this is for day data values, not used in Gb, handleStepData uses 5min data which is better
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        FitProSampleProvider provider = new FitProSampleProvider(this.getDevice(), dbHandler.getDaoSession());
        List<FitProActivitySample> samples = provider.getActivitySamples((int) (dayStart.getTimeInMillis() / 1000L), (int) (dayEnd.getTimeInMillis() / 1000L));
        int totalSteps = 0;
        for (FitProActivitySample sample : samples) {
            totalSteps += sample.getSteps();
        }
        return totalSteps;
    } catch (Exception ex) {
        LOG.error(ex.getMessage());
        return 0;
    }
}
Also used : FitProActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) FitProSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.fitpro.FitProSampleProvider) IOException(java.io.IOException) ParseException(java.text.ParseException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 3 with FitProActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample in project Gadgetbridge by Freeyourgadget.

the class FitProDeviceSupport method handleHR.

public void handleHR(int seconds, int heartRate, int pressureLow, int pressureHigh, int spo2) {
    LOG.debug("FitPro handle heart rate measurement");
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.add(Calendar.SECOND, seconds);
    LOG.debug("date: " + date);
    FitProActivitySample sample = new FitProActivitySample();
    sample.setHeartRate(heartRate);
    sample.setPressureLowMmHg(pressureLow);
    sample.setPressureHighMmHg(pressureHigh);
    sample.setSpo2Percent(spo2);
    sample.setTimestamp((int) (date.getTimeInMillis() / 1000));
    sample.setRawKind(ActivityKind.TYPE_ACTIVITY);
    addGBActivitySample(sample);
    broadcastSample(sample);
    GB.signalActivityDataFinish();
}
Also used : FitProActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar)

Example 4 with FitProActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample in project Gadgetbridge by Freeyourgadget.

the class FitProDeviceSupport method handleSleepData.

public void handleSleepData(byte[] value) {
    debugPrintArray(value, "sleep data value");
    // sleep packet consists of: date + list of 4bytes of 15minutes intervals
    // these intervals contain seconds offset from the date and type of sleep
    byte[] dateArray = new byte[2];
    System.arraycopy(value, 8, dateArray, 0, 2);
    Calendar date = decodeDateTime(dateArray);
    List<FitProActivitySample> samples = new ArrayList<>();
    for (int i = 12; i < value.length - 3; i = i + 4) {
        byte[] packet = new byte[4];
        System.arraycopy(value, i, packet, 0, 4);
        int data = ByteBuffer.wrap(packet).getInt();
        int activity_kind = (int) (data & 0xff);
        int encodedTime = (int) (data >> 16);
        int seconds = getSleepSecondsOfDay(encodedTime);
        // do not modify the caller's argument
        Calendar now = (Calendar) date.clone();
        now.add(Calendar.SECOND, seconds);
        int timestamp = (int) (now.getTimeInMillis() / 1000L);
        debugPrintArray(packet, "processing sleep packet");
        LOG.debug("FitPro new sleep: " + activity_kind + " seconds: " + seconds + " ts: " + timestamp + " date: " + DateTimeUtils.formatDateTime(new Date(timestamp * 1000L)));
        FitProActivitySample sample = new FitProActivitySample();
        sample.setTimestamp(timestamp);
        sample.setHeartRate(ActivitySample.NOT_MEASURED);
        sample.setActiveTimeMinutes(15);
        sample.setRawKind(rawSleepKindToUniqueKind(activity_kind));
        samples.add(sample);
    }
    if (addGBActivitySamples(samples)) {
        sendAck(value[3], value[1], value[2], value[5]);
    }
}
Also used : FitProActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 5 with FitProActivitySample

use of nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample in project Gadgetbridge by Freeyourgadget.

the class FitProDeviceSupport method addGBActivitySamples.

private boolean addGBActivitySamples(List<FitProActivitySample> samples) {
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        User user = DBHelper.getUser(dbHandler.getDaoSession());
        Device device = DBHelper.getDevice(this.getDevice(), dbHandler.getDaoSession());
        FitProSampleProvider provider = new FitProSampleProvider(this.getDevice(), dbHandler.getDaoSession());
        for (FitProActivitySample sample : samples) {
            sample.setDevice(device);
            sample.setUser(user);
            sample.setProvider(provider);
            provider.addGBActivitySample(sample);
        }
    } catch (Exception ex) {
        LOG.error("Error saving samples: " + ex);
        GB.updateTransferNotification(null, "Data transfer failed", false, 0, getContext());
        return false;
    }
    return true;
}
Also used : FitProActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) User(nodomain.freeyourgadget.gadgetbridge.entities.User) ActivityUser(nodomain.freeyourgadget.gadgetbridge.model.ActivityUser) FitProSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.fitpro.FitProSampleProvider) Device(nodomain.freeyourgadget.gadgetbridge.entities.Device) GBDevice(nodomain.freeyourgadget.gadgetbridge.impl.GBDevice) IOException(java.io.IOException) ParseException(java.text.ParseException) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Aggregations

FitProActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.FitProActivitySample)5 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)2 DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)2 FitProSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.fitpro.FitProSampleProvider)2 Date (java.util.Date)1 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)1 User (nodomain.freeyourgadget.gadgetbridge.entities.User)1 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)1 ActivityUser (nodomain.freeyourgadget.gadgetbridge.model.ActivityUser)1