Search in sources :

Example 6 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class HPlusHandlerThread method processDaySummary.

/**
     * Process a day summary message
     * This message includes aggregates regarding an entire day
     *
     * @param data the message from the device
     * @return boolean indicating success or fail
     */
public boolean processDaySummary(byte[] data) {
    HPlusDataRecordDaySummary record;
    try {
        record = new HPlusDataRecordDaySummary(data);
    } catch (IllegalArgumentException e) {
        LOG.debug((e.getMessage()));
        return false;
    }
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
        HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
        sample.setRawKind(record.type);
        sample.setSteps(record.steps);
        sample.setDistance(record.distance);
        sample.setCalories(record.calories);
        sample.setDistance(record.distance);
        //TODO: Find an alternative approach for Day Summary Heart Rate
        sample.setHeartRate((record.maxHeartRate - record.minHeartRate) / 2);
        sample.setRawHPlusHealthData(record.getRawData());
        sample.setProvider(provider);
        provider.addGBActivitySample(sample);
    } catch (GBException ex) {
        LOG.debug((ex.getMessage()));
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
    mGetDaySummaryTime = GregorianCalendar.getInstance();
    mGetDaySummaryTime.add(Calendar.SECOND, DAY_SUMMARY_SYNC_PERIOD);
    return true;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) HPlusHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider) GBException(nodomain.freeyourgadget.gadgetbridge.GBException)

Example 7 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class HPlusHandlerThread method processIncomingSleepData.

/**
     * Process sleep data from the device
     * Devices send a single sleep message for each sleep period
     * This message contains the duration of the sub-intervals (rem, deep, etc...)
     *
     * @param data the message from the device
     * @return boolean indicating success or fail
     */
public boolean processIncomingSleepData(byte[] data) {
    HPlusDataRecordSleep record;
    try {
        record = new HPlusDataRecordSleep(data);
    } catch (IllegalArgumentException e) {
        LOG.debug((e.getMessage()));
        return false;
    }
    mLastSleepDayReceived.setTimeInMillis(record.bedTimeStart * 1000L);
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Long userId = DBHelper.getUser(session).getId();
        Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
        HPlusHealthActivityOverlayDao overlayDao = session.getHPlusHealthActivityOverlayDao();
        HPlusHealthSampleProvider provider = new HPlusHealthSampleProvider(getDevice(), dbHandler.getDaoSession());
        //Get the individual Sleep overlays and insert them
        List<HPlusHealthActivityOverlay> overlayList = new ArrayList<>();
        List<HPlusDataRecord.RecordInterval> intervals = record.getIntervals();
        for (HPlusDataRecord.RecordInterval interval : intervals) {
            overlayList.add(new HPlusHealthActivityOverlay(interval.timestampFrom, interval.timestampTo, interval.activityKind, deviceId, userId, null));
        }
        overlayDao.insertOrReplaceInTx(overlayList);
        //Store the data
        HPlusHealthActivitySample sample = createSample(dbHandler, record.timestamp);
        sample.setRawHPlusHealthData(record.getRawData());
        sample.setRawKind(record.activityKind);
        sample.setProvider(provider);
        provider.addGBActivitySample(sample);
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
    mGetSleepTime = GregorianCalendar.getInstance();
    mGetSleepTime.add(GregorianCalendar.SECOND, SLEEP_SYNC_PERIOD);
    return true;
}
Also used : HPlusHealthActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample) ArrayList(java.util.ArrayList) HPlusHealthSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider) HPlusHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlay) GBException(nodomain.freeyourgadget.gadgetbridge.GBException) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) HPlusHealthActivityOverlayDao(nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivityOverlayDao) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Example 8 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class DbManagementActivity method exportDB.

private void exportDB() {
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DBHelper helper = new DBHelper(this);
        File dir = FileUtils.getExternalFilesDir();
        File destFile = helper.exportDB(dbHandler, dir);
        GB.toast(this, getString(R.string.dbmanagementactivity_exported_to, destFile.getAbsolutePath()), Toast.LENGTH_LONG, GB.INFO);
    } catch (Exception ex) {
        GB.toast(this, getString(R.string.dbmanagementactivity_error_exporting_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) DBHelper(nodomain.freeyourgadget.gadgetbridge.database.DBHelper) File(java.io.File)

Example 9 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class AppMessageHandlerMorpheuz method handleMessage.

@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
    int ctrl_message = 0;
    GBDeviceEventSleepMonitorResult sleepMonitorResult = null;
    for (Pair<Integer, Object> pair : pairs) {
        if (Objects.equals(pair.first, keyTransmit)) {
            sleepMonitorResult = new GBDeviceEventSleepMonitorResult();
            sleepMonitorResult.smartalarm_from = smartalarm_from;
            sleepMonitorResult.smartalarm_to = smartalarm_to;
            sleepMonitorResult.alarm_gone_off = alarm_gone_off;
            sleepMonitorResult.recording_base_timestamp = recording_base_timestamp;
            ctrl_message |= CTRL_TRANSMIT_DONE;
        } else if (pair.first.equals(keyGoneoff)) {
            alarm_gone_off = (int) pair.second;
            LOG.info("got gone off: " + alarm_gone_off / 60 + ":" + alarm_gone_off % 60);
            ctrl_message |= CTRL_DO_NEXT | CTRL_GONEOFF_DONE;
        } else if (pair.first.equals(keyPoint)) {
            if (recording_base_timestamp == -1) {
                // we have no base timestamp but received points, stop this
                ctrl_message = CTRL_VERSION_DONE | CTRL_GONEOFF_DONE | CTRL_TRANSMIT_DONE | CTRL_SET_LAST_SENT;
            } else {
                int index = ((int) pair.second >> 16);
                int intensity = ((int) pair.second & 0xffff);
                LOG.info("got point:" + index + " " + intensity);
                if (index >= 0) {
                    try (DBHandler db = GBApplication.acquireDB()) {
                        Long userId = DBHelper.getUser(db.getDaoSession()).getId();
                        Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();
                        PebbleMorpheuzSampleProvider sampleProvider = new PebbleMorpheuzSampleProvider(getDevice(), db.getDaoSession());
                        PebbleMorpheuzSample sample = new PebbleMorpheuzSample(recording_base_timestamp + index * 600, deviceId, userId, intensity);
                        sample.setProvider(sampleProvider);
                        sampleProvider.addGBActivitySample(sample);
                    } catch (Exception e) {
                        LOG.error("Error acquiring database", e);
                    }
                }
                ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
            }
        } else if (pair.first.equals(keyFrom)) {
            smartalarm_from = (int) pair.second;
            LOG.info("got from: " + smartalarm_from / 60 + ":" + smartalarm_from % 60);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyTo)) {
            smartalarm_to = (int) pair.second;
            LOG.info("got to: " + smartalarm_to / 60 + ":" + smartalarm_to % 60);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyVersion)) {
            int version = (int) pair.second;
            LOG.info("got version: " + ((float) version / 10.0f));
            ctrl_message |= CTRL_VERSION_DONE;
        } else if (pair.first.equals(keyBase)) {
            // fix timestamp
            TimeZone tz = SimpleTimeZone.getDefault();
            recording_base_timestamp = (int) pair.second - (tz.getOffset(System.currentTimeMillis())) / 1000;
            LOG.info("got base: " + recording_base_timestamp);
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyAutoReset)) {
            ctrl_message |= CTRL_SET_LAST_SENT | CTRL_DO_NEXT;
        } else if (pair.first.equals(keySnoozes)) {
            ctrl_message |= CTRL_SNOOZES_DONE | CTRL_DO_NEXT;
        } else if (pair.first.equals(keyFault)) {
            LOG.info("fault code: " + (int) pair.second);
            ctrl_message |= CTRL_DO_NEXT;
        } else {
            LOG.info("unhandled key: " + pair.first);
        }
    }
    // always ack
    GBDeviceEventSendBytes sendBytesAck = new GBDeviceEventSendBytes();
    sendBytesAck.encodedBytes = mPebbleProtocol.encodeApplicationMessageAck(mUUID, mPebbleProtocol.last_id);
    // sometimes send control message
    GBDeviceEventSendBytes sendBytesCtrl = null;
    if (ctrl_message > 0) {
        sendBytesCtrl = new GBDeviceEventSendBytes();
        sendBytesCtrl.encodedBytes = encodeMorpheuzMessage(keyCtrl, ctrl_message);
    }
    // ctrl and sleep monitor might be null, thats okay
    return new GBDeviceEvent[] { sendBytesAck, sendBytesCtrl, sleepMonitorResult };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) PebbleMorpheuzSample(nodomain.freeyourgadget.gadgetbridge.entities.PebbleMorpheuzSample) IOException(java.io.IOException) JSONException(org.json.JSONException) PebbleMorpheuzSampleProvider(nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleMorpheuzSampleProvider) DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) TimeZone(java.util.TimeZone) SimpleTimeZone(java.util.SimpleTimeZone) JSONObject(org.json.JSONObject) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) GBDeviceEventSleepMonitorResult(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepMonitorResult)

Example 10 with DBHandler

use of nodomain.freeyourgadget.gadgetbridge.database.DBHandler in project Gadgetbridge by Freeyourgadget.

the class DatalogSessionHealthSleep method store.

private void store(SleepRecord[] sleepRecords) {
    try (DBHandler dbHandler = GBApplication.acquireDB()) {
        DaoSession session = dbHandler.getDaoSession();
        Long userId = DBHelper.getUser(session).getId();
        Long deviceId = DBHelper.getDevice(getDevice(), session).getId();
        PebbleHealthActivityOverlayDao overlayDao = session.getPebbleHealthActivityOverlayDao();
        List<PebbleHealthActivityOverlay> overlayList = new ArrayList<>();
        for (SleepRecord sleepRecord : sleepRecords) {
            //TODO: check the firmware version and don't use the sleep record if overlay is available?
            overlayList.add(new PebbleHealthActivityOverlay(sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd, sleepRecord.type, deviceId, userId, sleepRecord.getRawData()));
        }
        overlayDao.insertOrReplaceInTx(overlayList);
    } catch (Exception ex) {
        LOG.debug(ex.getMessage());
    }
}
Also used : DBHandler(nodomain.freeyourgadget.gadgetbridge.database.DBHandler) ArrayList(java.util.ArrayList) PebbleHealthActivityOverlayDao(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao) PebbleHealthActivityOverlay(nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay) DaoSession(nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)

Aggregations

DBHandler (nodomain.freeyourgadget.gadgetbridge.database.DBHandler)14 DaoSession (nodomain.freeyourgadget.gadgetbridge.entities.DaoSession)6 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)5 ArrayList (java.util.ArrayList)4 HPlusHealthSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.hplus.HPlusHealthSampleProvider)4 Device (nodomain.freeyourgadget.gadgetbridge.entities.Device)4 HPlusHealthActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.HPlusHealthActivitySample)4 IOException (java.io.IOException)3 GBDevice (nodomain.freeyourgadget.gadgetbridge.impl.GBDevice)3 BluetoothDevice (android.bluetooth.BluetoothDevice)2 GregorianCalendar (java.util.GregorianCalendar)2 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)2 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)2 MiBandSampleProvider (nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider)2 MiBandActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample)2 PebbleHealthActivityOverlay (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlay)2 PebbleHealthActivityOverlayDao (nodomain.freeyourgadget.gadgetbridge.entities.PebbleHealthActivityOverlayDao)2 User (nodomain.freeyourgadget.gadgetbridge.entities.User)2 Intent (android.content.Intent)1 File (java.io.File)1