Search in sources :

Example 1 with BgReading

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

the class WatchUpdaterService method sendWearCalibrationData.

private static boolean sendWearCalibrationData(Integer count, long startTime, List<Calibration> list) {
    try {
        if (googleApiClient != null && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) {
            googleApiClient.connect();
        }
        // if ((googleApiClient != null) && (googleApiClient.isConnected())) {
        if (googleApiClient != null) {
            Log.d(TAG, "sendWearCalibrationData");
            final Sensor sensor = Sensor.currentSensor();
            final Calibration last = list != null && list.size() > 0 ? list.get(0) : Calibration.last();
            List<Calibration> latest;
            BgReading lastBgReading = BgReading.last();
            // From BgReading:     lastBgReading.calibration.rawValueOverride()
            if (list != null)
                latest = list;
            else if (startTime != 0)
                latest = Calibration.latestForGraphSensor(count, startTime, Long.MAX_VALUE);
            else if (lastBgReading != null && lastBgReading.calibration != null && lastBgReading.calibration_flag == true) {
                Log.d(TAG, "sendWearCalibrationData lastBgReading.calibration_flag=" + lastBgReading.calibration_flag + " lastBgReading.timestamp: " + lastBgReading.timestamp + " lastBgReading.calibration.timestamp: " + lastBgReading.calibration.timestamp);
                latest = Calibration.allForSensor();
            } else {
                latest = Calibration.latest(count);
            }
            if ((sensor != null) && (last != null) && (latest != null && !latest.isEmpty())) {
                Log.d(TAG, "sendWearCalibrationData latest count = " + latest.size());
                final DataMap entries = dataMap(last);
                final ArrayList<DataMap> dataMaps = new ArrayList<>(latest.size());
                if (sensor.uuid != null) {
                    for (Calibration bg : latest) {
                        if ((bg != null) && (bg.sensor_uuid != null) && (bg.sensor_uuid.equals(sensor.uuid))) {
                            dataMaps.add(dataMap(bg));
                        }
                    }
                }
                // MOST IMPORTANT LINE FOR TIMESTAMP
                entries.putLong("time", new Date().getTime());
                entries.putDataMapArrayList("entries", dataMaps);
                new SendToDataLayerThread(WEARABLE_CALIBRATION_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
            } else
                Log.d(TAG, "sendWearCalibrationData latest count = 0");
        } else {
            Log.e(TAG, "sendWearCalibrationData No connection to wearable available for send treatment!");
            return false;
        }
    } catch (NullPointerException e) {
        Log.e(TAG, "Nullpointer exception in sendWearCalibrationData: " + e);
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Calibration(com.eveningoutpost.dexdrip.Models.Calibration) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor) DataMap(com.google.android.gms.wearable.DataMap)

Example 2 with BgReading

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

the class WatchUpdaterService method sendWearBgData.

private static boolean sendWearBgData(Integer count, long startTime, List<BgReading> list) {
    try {
        if (googleApiClient != null && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) {
            // googleApiConnect();
            googleApiClient.connect();
        }
        if (googleApiClient != null) {
            Log.d(TAG, "sendWearBgData");
            final BgReading last = BgReading.last();
            List<BgReading> latest;
            if (list != null)
                latest = list;
            else if (startTime != 0)
                latest = BgReading.latestForGraphSensor(count, startTime, Long.MAX_VALUE);
            else
                latest = BgReading.latest(count);
            if ((last != null) && (latest != null && !latest.isEmpty())) {
                final int battery = PowerStateReceiver.getBatteryLevel(xdrip.getAppContext());
                Log.d(TAG, "sendWearBgData latest count = " + latest.size() + " battery=" + battery);
                final DataMap entries = dataMap(last);
                final ArrayList<DataMap> dataMaps = new ArrayList<>(latest.size());
                final Sensor sensor = Sensor.currentSensor();
                if ((sensor != null) && (sensor.uuid != null)) {
                    for (BgReading bg : latest) {
                        if ((bg != null) && (bg.sensor_uuid != null) && (bg.sensor_uuid.equals(sensor.uuid) && (bg.calibration_uuid != null))) {
                            dataMaps.add(dataMap(bg));
                        }
                    }
                }
                // MOST IMPORTANT LINE FOR TIMESTAMP
                entries.putLong("time", new Date().getTime());
                entries.putInt("battery", battery);
                entries.putDataMapArrayList("entries", dataMaps);
                new SendToDataLayerThread(WEARABLE_BG_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
            } else
                Log.d(TAG, "sendWearBgData lastest count = 0");
        } else {
            Log.e(TAG, "sendWearBgData No connection to wearable available for send BG!");
            return false;
        }
    } catch (NullPointerException e) {
        Log.e(TAG, "Nullpointer exception in sendWearBgData: " + e);
        return false;
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 3 with BgReading

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

the class DexShareCollectionService method setRetryTimer.

public void setRetryTimer() {
    if (CollectionServiceStarter.isBTShare(getApplicationContext())) {
        BgReading bgReading = BgReading.last();
        long retry_in;
        if (bgReading != null) {
            retry_in = Math.min(Math.max((1000 * 30), (1000 * 60 * 5) - (new Date().getTime() - bgReading.timestamp) + (1000 * 5)), (1000 * 60 * 5));
        } else {
            retry_in = (1000 * 20);
        }
        Log.d(TAG, "Restarting in: " + (retry_in / (60 * 1000)) + " minutes");
        Calendar calendar = Calendar.getInstance();
        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
        if (pendingIntent != null)
            alarm.cancel(pendingIntent);
        long wakeTime = calendar.getTimeInMillis() + retry_in;
        pendingIntent = PendingIntent.getService(this, 0, new Intent(this, this.getClass()), 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarm.setExact(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
        } else
            alarm.set(AlarmManager.RTC_WAKEUP, wakeTime, pendingIntent);
    }
}
Also used : Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date)

Example 4 with BgReading

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

the class BgGraphBuilder method oldunitizedDeltaString.

public String oldunitizedDeltaString(boolean showUnit, boolean highGranularity) {
    List<BgReading> last2 = BgReading.latest(2);
    if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000) {
        // don't show delta if there are not enough values or the values are more than 20 mintes apart
        return "???";
    }
    double value = BgReading.currentSlope() * 5 * 60 * 1000;
    if (Math.abs(value) > 100) {
        // a delta > 100 will not happen with real BG values -> problematic sensor data
        return "ERR";
    }
    // TODO: allow localization from os settings once pebble doesn't require english locale
    DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
    String delta_sign = "";
    if (value > 0) {
        delta_sign = "+";
    }
    if (doMgdl) {
        if (highGranularity) {
            df.setMaximumFractionDigits(1);
        } else {
            df.setMaximumFractionDigits(0);
        }
        return delta_sign + df.format(unitized(value)) + (showUnit ? " mg/dl" : "");
    } else {
        if (highGranularity) {
            df.setMaximumFractionDigits(2);
        } else {
            df.setMaximumFractionDigits(1);
        }
        df.setMinimumFractionDigits(1);
        df.setMinimumIntegerDigits(1);
        return delta_sign + df.format(unitized(value)) + (showUnit ? " mmol/l" : "");
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) BgReading(com.eveningoutpost.dexdrip.Models.BgReading)

Example 5 with BgReading

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

the class LibreTrendGraph method getLatestBg.

private static ArrayList<Float> getLatestBg(LibreBlock libreBlock) {
    ReadingData readingData = NFCReaderX.getTrend(libreBlock);
    if (readingData == null) {
        Log.e(TAG, "NFCReaderX.getTrend returned null");
        return null;
    }
    ArrayList<Float> ret = new ArrayList<Float>();
    if (readingData.trend.size() == 0 || readingData.trend.get(0).glucoseLevelRaw == 0) {
        Log.e(TAG, "libreBlock exists but no trend data exists, or first value is zero ");
        return null;
    }
    double factor = libreBlock.calculated_bg / readingData.trend.get(0).glucoseLevelRaw;
    if (factor == 0) {
        // We don't have the calculated value, but we do have the raw value. (No calibration exists)
        // I want to show raw data.
        Log.w(TAG, "Bg data was not calculated, working on raw data");
        List<BgReading> latestReading = BgReading.latestForGraph(1, libreBlock.timestamp - 1000, libreBlock.timestamp + 1000);
        if (latestReading == null || latestReading.size() == 0) {
            Log.e(TAG, "libreBlock exists but no matching bg record exists");
            return null;
        }
        factor = latestReading.get(0).raw_data / readingData.trend.get(0).glucoseLevelRaw;
    }
    for (GlucoseData data : readingData.trend) {
        ret.add(new Float(factor * data.glucoseLevelRaw));
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) ReadingData(com.eveningoutpost.dexdrip.Models.ReadingData) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) GlucoseData(com.eveningoutpost.dexdrip.Models.GlucoseData)

Aggregations

BgReading (com.eveningoutpost.dexdrip.Models.BgReading)66 Date (java.util.Date)26 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)20 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)16 ArrayList (java.util.ArrayList)14 DataMap (com.google.android.gms.wearable.DataMap)12 Intent (android.content.Intent)8 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)8 IOException (java.io.IOException)8 JSONException (org.json.JSONException)8 PendingIntent (android.app.PendingIntent)6 Paint (android.graphics.Paint)6 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)6 TransmitterData (com.eveningoutpost.dexdrip.Models.TransmitterData)6 BgGraphBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)6 CalibrationAbstract (com.eveningoutpost.dexdrip.calibrations.CalibrationAbstract)6 DecimalFormat (java.text.DecimalFormat)6 JSONArray (org.json.JSONArray)6 Point (android.graphics.Point)4 SpannableString (android.text.SpannableString)4