Search in sources :

Example 61 with BgReading

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

the class WatchUpdaterService method sendData.

private void sendData() {
    BgReading bg = BgReading.last();
    if (bg != null) {
        forceGoogleApiConnect();
        if (wear_integration) {
            final int battery = PowerStateReceiver.getBatteryLevel(getApplicationContext());
            new SendToDataLayerThread(WEARABLE_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, dataMap(bg, mPrefs, new BgGraphBuilder(getApplicationContext()), battery));
        }
    }
}
Also used : BgReading(com.eveningoutpost.dexdrip.Models.BgReading) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 62 with BgReading

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

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 63 with BgReading

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

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)

Example 64 with BgReading

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

the class WebServiceSgv method request.

// process the request and produce a response object
public WebResponse request(String query) {
    // result code for any steps cgi parameters, 200 = good
    int steps_result_code = 0;
    // result code for any heart cgi parameters, 200 = good
    int heart_result_code = 0;
    // result code for any heart cgi parameters, 200 = good
    int tasker_result_code = 0;
    // show the units we are using
    int units_indicator = 1;
    // whether to cut out portions of the data
    boolean brief = false;
    final Map<String, String> cgi = getQueryParameters(query);
    int count = 24;
    if (cgi.containsKey("count")) {
        try {
            count = Integer.valueOf(cgi.get("count"));
            count = Math.min(count, 100);
            count = Math.max(count, 1);
            UserError.Log.d(TAG, "SGV count request for: " + count + " entries");
        } catch (Exception e) {
        // meh
        }
    }
    if (cgi.containsKey("steps")) {
        UserError.Log.d(TAG, "Received steps request: " + cgi.get("steps"));
        // forward steps request to steps route
        final WebResponse steps_reply_wr = ((RouteFinder) Singleton.get("RouteFinder")).handleRoute("steps/set/" + cgi.get("steps"));
        steps_result_code = steps_reply_wr.resultCode;
    }
    if (cgi.containsKey("heart")) {
        UserError.Log.d(TAG, "Received heart request: " + cgi.get("heart"));
        // forward steps request to heart route
        // accuracy currently ignored (always 1) - TODO review
        final WebResponse heart_reply_wr = ((RouteFinder) Singleton.get("RouteFinder")).handleRoute("heart/set/" + cgi.get("heart") + "/1");
        heart_result_code = heart_reply_wr.resultCode;
    }
    if (cgi.containsKey("tasker")) {
        UserError.Log.d(TAG, "Received tasker request: " + cgi.get("tasker"));
        // forward steps request to heart route
        // send single word command to tasker, eg snooze or osnooze
        final WebResponse tasker_reply_wr = ((RouteFinder) Singleton.get("RouteFinder")).handleRoute("tasker/" + cgi.get("tasker"));
        tasker_result_code = tasker_reply_wr.resultCode;
    }
    if (cgi.containsKey("brief_mode")) {
        brief = true;
    }
    final JSONArray reply = new JSONArray();
    final List<BgReading> readings = BgReading.latest(count);
    if (readings != null) {
        // populate json structures
        try {
            final String collector_device = DexCollectionType.getBestCollectorHardwareName();
            String external_status_line = getLastStatusLine();
            // for each reading produce a json record
            for (BgReading reading : readings) {
                final JSONObject item = new JSONObject();
                if (!brief) {
                    item.put("_id", reading.uuid);
                    item.put("device", collector_device);
                    item.put("dateString", DateUtil.toNightscoutFormat(reading.timestamp));
                    item.put("sysTime", DateUtil.toNightscoutFormat(reading.timestamp));
                }
                item.put("date", reading.timestamp);
                item.put("sgv", (int) reading.getDg_mgdl());
                item.put("delta", new BigDecimal(reading.getDg_slope() * 5 * 60 * 1000).setScale(3, BigDecimal.ROUND_HALF_UP));
                item.put("direction", reading.getDg_deltaName());
                item.put("noise", reading.noiseValue());
                if (!brief) {
                    item.put("filtered", (long) (reading.filtered_data * 1000));
                    item.put("unfiltered", (long) (reading.raw_data * 1000));
                    item.put("rssi", 100);
                    item.put("type", "sgv");
                }
                if (units_indicator > 0) {
                    item.put("units_hint", Pref.getString("units", "mgdl").equals("mgdl") ? "mgdl" : "mmol");
                    units_indicator = 0;
                }
                // emit the external status line once if present
                if (external_status_line.length() > 0) {
                    item.put("aaps", external_status_line);
                    item.put("aaps-ts", getLastStatusLineTime());
                    external_status_line = "";
                }
                // emit result code from steps if present
                if (steps_result_code > 0) {
                    item.put("steps_result", steps_result_code);
                    steps_result_code = 0;
                }
                // emit result code from heart if present
                if (heart_result_code > 0) {
                    item.put("heart_result", heart_result_code);
                    heart_result_code = 0;
                }
                // emit result code from tasker if present
                if (tasker_result_code > 0) {
                    item.put("tasker_result", tasker_result_code);
                    tasker_result_code = 0;
                }
                reply.put(item);
            }
            Log.d(TAG, "Output: " + reply.toString());
        } catch (JSONException e) {
            UserError.Log.wtf(TAG, "Got json exception: " + e);
        }
    }
    return new WebResponse(reply.toString());
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException) BigDecimal(java.math.BigDecimal) JSONObject(org.json.JSONObject) BgReading(com.eveningoutpost.dexdrip.Models.BgReading)

Example 65 with BgReading

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

the class CalibrationAbstract method getBgReadingFromBgReading.

public BgReading getBgReadingFromBgReading(BgReading bgReading, CalibrationData data) {
    if (data == null)
        return null;
    if (bgReading == null)
        return null;
    // do we need deep clone?
    final BgReading new_bg = (BgReading) JoH.cloneObject(bgReading);
    if (new_bg == null)
        return null;
    // algorithm can override to decide whether or not to be using age_adjusted_raw
    new_bg.calculated_value = getGlucoseFromBgReading(bgReading, data);
    new_bg.filtered_calculated_value = getGlucoseFromFilteredBgReading(bgReading, data);
    return new_bg;
}
Also used : BgReading(com.eveningoutpost.dexdrip.Models.BgReading)

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