Search in sources :

Example 11 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip-plus by jamorham.

the class BgGraphBuilder method heartLines.

// line illustrating result from heartrate monitor
private List<Line> heartLines() {
    final boolean d = false;
    final List<Line> heartLines = new ArrayList<>();
    if ((prefs.getBoolean("use_pebble_health", true) && prefs.getBoolean("show_pebble_movement_line", true))) {
        final List<HeartRate> heartRates = HeartRate.latestForGraph(2000, loaded_start, loaded_end);
        final long condenseCutoffMs = Pref.getBooleanDefaultFalse("smooth_heartrate") ? (10 * Constants.MINUTE_IN_MS) : FUZZER;
        final List<HeartRate> condensedHeartRateList = new ArrayList<>();
        for (HeartRate thisHeartRateRecord : heartRates) {
            final int condensedListSize = condensedHeartRateList.size();
            if (condensedListSize > 0) {
                final HeartRate tailOfList = condensedHeartRateList.get(condensedListSize - 1);
                // if its close enough to merge then average with previous
                if ((thisHeartRateRecord.timestamp - tailOfList.timestamp) < condenseCutoffMs) {
                    tailOfList.bpm = (tailOfList.bpm += thisHeartRateRecord.bpm) / 2;
                } else {
                    // not close enough to merge
                    condensedHeartRateList.add(thisHeartRateRecord);
                }
            } else {
                // first record
                condensedHeartRateList.add(thisHeartRateRecord);
            }
        }
        if (d)
            Log.d(TAG, "heartrate before size: " + heartRates.size());
        if (d)
            Log.d(TAG, "heartrate after c size: " + condensedHeartRateList.size());
        // final float yscale = doMgdl ? (float) Constants.MMOLL_TO_MGDL : 1f;
        final float yscale = doMgdl ? 10f : 1f;
        // 
        float ypos;
        final List<PointValue> new_points = new ArrayList<>();
        if (d)
            UserError.Log.d("HEARTRATE", "Size " + condensedHeartRateList.size());
        for (HeartRate pm : condensedHeartRateList) {
            if (d)
                UserError.Log.d("HEARTRATE: ", JoH.dateTimeText(pm.timestamp) + " \tHR: " + pm.bpm);
            ypos = (pm.bpm * yscale) / 10;
            final PointValue this_point = new PointValue((float) pm.timestamp / FUZZER, ypos);
            new_points.add(this_point);
        }
        final Line macroHeartRateLine = new Line(new_points);
        for (Line this_line : autoSplitLine(macroHeartRateLine, 30)) {
            this_line.setColor(getCol(X.color_heart_rate1));
            this_line.setStrokeWidth(6);
            this_line.setHasPoints(false);
            this_line.setHasLines(true);
            this_line.setCubic(true);
            heartLines.add(this_line);
        }
    }
    return heartLines;
}
Also used : TrendLine(com.eveningoutpost.dexdrip.Models.Forecast.TrendLine) PolyTrendLine(com.eveningoutpost.dexdrip.Models.Forecast.PolyTrendLine) Line(lecho.lib.hellocharts.model.Line) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) PointValue(lecho.lib.hellocharts.model.PointValue) ArrayList(java.util.ArrayList)

Example 12 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip-plus by jamorham.

the class PebbleWatchSync method init.

protected void init() {
    Log.i(TAG, "Initialising...");
    Log.i(TAG, "configuring PebbleDataReceiver for: " + currentWatchFaceUUID.toString());
    PebbleKit.registerReceivedDataHandler(context, new PebbleKit.PebbleDataReceiver(currentWatchFaceUUID) {

        @Override
        public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
            getActivePebbleDisplay().receiveData(transactionId, data);
        }
    });
    PebbleKit.registerReceivedAckHandler(context, new PebbleKit.PebbleAckReceiver(currentWatchFaceUUID) {

        @Override
        public void receiveAck(Context context, int transactionId) {
            getActivePebbleDisplay().receiveAck(transactionId);
        }
    });
    PebbleKit.registerReceivedNackHandler(context, new PebbleKit.PebbleNackReceiver(currentWatchFaceUUID) {

        @Override
        public void receiveNack(Context context, int transactionId) {
            getActivePebbleDisplay().receiveNack(transactionId);
        }
    });
    PebbleKit.registerDataLogReceiver(context, new PebbleKit.PebbleDataLogReceiver(currentWatchFaceUUID) {

        @Override
        public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, int data) {
            if (d)
                Log.d(TAG, "receiveLogData: uuid:" + logUuid + " " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " data: " + data);
        }

        @Override
        public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, Long data) {
            Log.d(TAG, "receiveLogData: uuid:" + logUuid + " started: " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " data: " + data);
            if (Pref.getBoolean("use_pebble_health", true)) {
                if ((tag != null) && (data != null)) {
                    // alternator
                    final int s = ((int) (long) tag) & 0xfffffff7;
                    switch(s) {
                        case HEARTRATE_LOG:
                            if (data > sanity_timestamp) {
                                if (last_heartrate_timestamp > 0) {
                                    Log.e(TAG, "Out of sequence heartrate timestamp received!");
                                }
                                last_heartrate_timestamp = data;
                            } else {
                                if (data > 0) {
                                    if (last_heartrate_timestamp > 0) {
                                        final HeartRate hr = new HeartRate();
                                        hr.timestamp = last_heartrate_timestamp * 1000;
                                        hr.bpm = (int) (long) data;
                                        Log.d(TAG, "Saving HeartRate: " + hr.toS());
                                        hr.saveit();
                                        // reset state
                                        last_heartrate_timestamp = 0;
                                    } else {
                                        Log.e(TAG, "Out of sequence heartrate value received!");
                                    }
                                }
                            }
                            break;
                        case MOVEMENT_LOG:
                            if (data > sanity_timestamp) {
                                if (last_movement_timestamp > 0) {
                                    Log.e(TAG, "Out of sequence movement timestamp received!");
                                }
                                last_movement_timestamp = data;
                            } else {
                                if (data > 0) {
                                    if (last_movement_timestamp > 0) {
                                        final StepCounter pm = StepCounter.createEfficientRecord(last_movement_timestamp * 1000, (int) (long) data);
                                        Log.d(TAG, "Saving Movement: " + pm.toS());
                                        // reset state
                                        last_movement_timestamp = 0;
                                    } else {
                                        Log.e(TAG, "Out of sequence movement value received!");
                                    }
                                }
                            }
                            break;
                        default:
                            Log.e(TAG, "Unknown pebble data log type received: " + s);
                            break;
                    }
                } else {
                    Log.e(TAG, "Got null Long in receive data");
                }
            }
        }

        @Override
        public void receiveData(Context context, UUID logUuid, Long timestamp, Long tag, byte[] data) {
            if (d)
                Log.d(TAG, "receiveLogData: uuid:" + logUuid + " " + JoH.dateTimeText(timestamp * 1000) + " tag:" + tag + " hexdata: " + JoH.bytesToHex(data));
        }

        @Override
        public void onFinishSession(Context context, UUID logUuid, Long timestamp, Long tag) {
            if (d)
                Log.i(TAG, "Session " + tag + " finished!");
        }
    });
    // control app
    PebbleKit.registerReceivedDataHandler(context, new PebbleKit.PebbleDataReceiver(PEBBLE_CONTROL_APP_UUID) {

        @Override
        public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
            getActivePebbleDisplay().receiveAppData(transactionId, data);
        }
    });
}
Also used : Context(android.content.Context) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StepCounter(com.eveningoutpost.dexdrip.Models.StepCounter) UUID(java.util.UUID) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) PebbleKit(com.getpebble.android.kit.PebbleKit)

Example 13 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip-plus by jamorham.

the class WatchUpdaterService method syncHeartSensorData.

private synchronized void syncHeartSensorData(DataMap dataMap, boolean bBenchmark) {
    Log.d(TAG, "syncHeartSensorData");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    long timeOfLastEntry = 0;
    Log.d(TAG, "syncHeartSensorData add to Table");
    if (entries != null) {
        final Gson gson = JoH.defaultGsonInstance();
        // final HeartRate pm = HeartRate.last();
        Log.d(TAG, "syncHeartSensorData add Table entries count=" + entries.size());
        for (DataMap entry : entries) {
            if (entry != null) {
                Log.d(TAG, "syncHeartSensorData add Table entry=" + entry);
                String record = entry.getString("entry");
                if (record != null) {
                    Log.d(TAG, "syncHeartSensorData add Table record=" + record);
                    final HeartRate data = gson.fromJson(record, HeartRate.class);
                    if (data != null) {
                        timeOfLastEntry = (long) data.timestamp + 1;
                        Log.d(TAG, "syncHeartSensorData add Entry Wear=" + data.toString() + " " + record);
                        Log.d(TAG, "syncHeartSensorData WATCH data.metric=" + data.bpm + " timestamp=" + JoH.dateTimeText((long) data.timestamp));
                        if (!bBenchmark)
                            data.saveit();
                    }
                }
            }
        }
        sendDataReceived(DATA_ITEM_RECEIVED_PATH, "DATA_RECEIVED_LOGS count=" + entries.size(), timeOfLastEntry, bBenchmark ? "BM" : "HEART", -1);
    }
}
Also used : HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

Example 14 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip-plus by jamorham.

the class Home method updateHealthInfo.

private void updateHealthInfo(String caller) {
    final StepCounter pm = StepCounter.last();
    final boolean use_pebble_health = Pref.getBoolean("use_pebble_health", true);
    if ((use_pebble_health) && (pm != null)) {
        stepsButton.setText(Integer.toString(pm.metric));
        stepsButton.setVisibility(View.VISIBLE);
        // TODO this can be done with PrefsView binding
        stepsButton.setAlpha(Pref.getBoolean("show_pebble_movement_line", true) ? 1.0f : 0.3f);
    } else {
        stepsButton.setVisibility(View.INVISIBLE);
    }
    final HeartRate hr = HeartRate.last();
    if ((use_pebble_health) && (hr != null)) {
        bpmButton.setText(Integer.toString(hr.bpm));
        bpmButton.setVisibility(View.VISIBLE);
        // TODO this can be done with PrefsView binding
        bpmButton.setAlpha(Pref.getBoolean("show_pebble_movement_line", true) ? 1.0f : 0.3f);
    } else {
        bpmButton.setVisibility(View.INVISIBLE);
    }
}
Also used : StepCounter(com.eveningoutpost.dexdrip.Models.StepCounter) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate)

Aggregations

HeartRate (com.eveningoutpost.dexdrip.Models.HeartRate)14 DataMap (com.google.android.gms.wearable.DataMap)6 StepCounter (com.eveningoutpost.dexdrip.Models.StepCounter)4 ArrayList (java.util.ArrayList)4 Context (android.content.Context)2 PolyTrendLine (com.eveningoutpost.dexdrip.Models.Forecast.PolyTrendLine)2 TrendLine (com.eveningoutpost.dexdrip.Models.Forecast.TrendLine)2 PebbleMovement (com.eveningoutpost.dexdrip.Models.PebbleMovement)2 PebbleKit (com.getpebble.android.kit.PebbleKit)2 PebbleDictionary (com.getpebble.android.kit.util.PebbleDictionary)2 Gson (com.google.gson.Gson)2 RequestBody (com.squareup.okhttp.RequestBody)2 ResponseBody (com.squareup.okhttp.ResponseBody)2 UUID (java.util.UUID)2 Line (lecho.lib.hellocharts.model.Line)2 PointValue (lecho.lib.hellocharts.model.PointValue)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2