Search in sources :

Example 1 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.

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 2 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.

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 3 with HeartRate

use of com.eveningoutpost.dexdrip.Models.HeartRate in project xDrip by NightscoutFoundation.

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)

Example 4 with HeartRate

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

the class NightscoutUploader method postHeartRate.

private void postHeartRate(NightscoutService nightscoutService, String apiSecret) throws Exception {
    Log.d(TAG, "Processing heartrate for RESTAPI");
    if (apiSecret != null) {
        final String STORE_COUNTER = "nightscout-rest-heartrate-synced-time";
        final long syncedTillTime = Math.max(PersistentStore.getLong(STORE_COUNTER), JoH.tsl() - Constants.DAY_IN_MS * 7);
        final List<HeartRate> readings = HeartRate.latestForGraph((MAX_ACTIVITY_RECORDS / Math.min(1, Math.max(activityErrorCount, MAX_ACTIVITY_RECORDS / 10))), syncedTillTime);
        final JSONArray data = new JSONArray();
        // final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
        // format.setTimeZone(TimeZone.getDefault());
        long highest_timestamp = 0;
        if (readings.size() > 0) {
            for (HeartRate reading : readings) {
                final JSONObject json = new JSONObject();
                json.put("type", "hr-bpm");
                json.put("timeStamp", reading.timestamp);
                // json.put("dateString", format.format(reading.timestamp));
                json.put("created_at", DateUtil.toISOString(reading.timestamp));
                json.put("bpm", reading.bpm);
                if (reading.accuracy != 1)
                    json.put("accuracy", reading.accuracy);
                data.put(json);
                highest_timestamp = Math.max(highest_timestamp, reading.timestamp);
            }
            // send to nightscout - update counter
            final RequestBody body = RequestBody.create(MediaType.parse("application/json"), data.toString());
            Response<ResponseBody> r;
            r = nightscoutService.uploadActivity(apiSecret, body).execute();
            if (!r.isSuccess()) {
                activityErrorCount++;
                if (JoH.ratelimit("heartrate-unable-upload", 3600)) {
                    UserError.Log.e(TAG, "Unable to upload heart-rate data to Nightscout - check nightscout version");
                }
                throw new UploaderException(r.message(), r.code());
            } else {
                PersistentStore.setLong(STORE_COUNTER, highest_timestamp);
                UserError.Log.d(TAG, "Updating heartrate synced record count (success) " + JoH.dateTimeText(highest_timestamp) + " Processed: " + readings.size() + " records");
                checkGzipSupport(r);
            }
        }
    } else {
        UserError.Log.e(TAG, "Api secret is null");
    }
}
Also used : HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RequestBody(com.squareup.okhttp.RequestBody) ResponseBody(com.squareup.okhttp.ResponseBody)

Example 5 with HeartRate

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

the class HeartRateService method getWearHeartSensorData.

public static synchronized DataMap getWearHeartSensorData(int count, long last_send_time, int min_count) {
    UserError.Log.d(TAG, "getWearHeartSensorData last_send_time:" + JoH.dateTimeText(last_send_time));
    if ((count != 0) || (JoH.ratelimit("heartrate-datamap", 5))) {
        HeartRate last_log = HeartRate.last();
        if (last_log != null) {
            UserError.Log.d(TAG, "getWearHeartSensorData last_log.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
        } else {
            UserError.Log.d(TAG, "getWearHeartSensorData HeartRate.last() = null:");
            return null;
        }
        if (last_log != null && last_send_time <= last_log.timestamp) {
            // startTime
            long last_send_success = last_send_time;
            UserError.Log.d(TAG, "getWearHeartSensorData last_send_time < last_bg.timestamp:" + JoH.dateTimeText((long) last_log.timestamp));
            List<HeartRate> logs = HeartRate.latestForGraph(count, last_send_time);
            if (!logs.isEmpty() && logs.size() > min_count) {
                DataMap entries = dataMap(last_log);
                final ArrayList<DataMap> dataMaps = new ArrayList<>(logs.size());
                for (HeartRate log : logs) {
                    dataMaps.add(dataMap(log));
                    last_send_success = (long) log.timestamp;
                }
                // MOST IMPORTANT LINE FOR TIMESTAMP
                entries.putLong("time", JoH.tsl());
                entries.putDataMapArrayList("entries", dataMaps);
                UserError.Log.i(TAG, "getWearHeartSensorData SYNCED up to " + JoH.dateTimeText(last_send_success) + " count = " + logs.size());
                return entries;
            } else
                UserError.Log.i(TAG, "getWearHeartSensorData SYNCED up to " + JoH.dateTimeText(last_send_success) + " count = 0");
        }
        return null;
    } else {
        UserError.Log.d(TAG, "Ratelimitted getWearHeartSensorData");
        return null;
    }
}
Also used : HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) ArrayList(java.util.ArrayList) DataMap(com.google.android.gms.wearable.DataMap)

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