Java Code Examples for com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue#handleNewBgReading()

The following examples show how to use com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue#handleNewBgReading() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BgReading.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public static void create(Context context, double raw_data, double age_adjusted_raw_value, double filtered_data, Long timestamp,
        double calculated_bg,  double calculated_current_slope, boolean hide_slope, double xdrip_filtered_calculated_value) {
    
    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.w(TAG, "No sensor, ignoring this bg reading");
        return ;
    }

    Calibration calibration = Calibration.last();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
    } else {
        Log.d(TAG,"Calibrations, so doing everything bgReading = " + bgReading);
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
    }
    
    bgReading.sensor = sensor;
    bgReading.sensor_uuid = sensor.uuid;
    bgReading.raw_data = (raw_data/1000);
    bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
    bgReading.filtered_data = (filtered_data/1000);
    bgReading.timestamp = timestamp;
    bgReading.uuid = UUID.randomUUID().toString();
    bgReading.calculated_value = calculated_bg;
    bgReading.calculated_value_slope = calculated_current_slope;
    bgReading.hide_slope = hide_slope;
    bgReading.filtered_calculated_value = xdrip_filtered_calculated_value;
    bgReading.save();

    BgSendQueue.handleNewBgReading(bgReading, "create", context);

    Log.i("BG GSON: ",bgReading.toS());
}
 
Example 2
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void notifyAndSync(final BgReading bgr) {
    final boolean recent = bgr.isCurrent();
    if (recent) {
        Notifications.start(); // may not be needed as this is duplicated in handleNewBgReading
        // probably not wanted for G5 internal values?
        //bgr.injectNoise(true); // Add noise parameter for nightscout
        //bgr.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    }
    BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), Home.get_follower(), !recent); // pebble and widget and follower
}
 
Example 3
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void notifyAndSync(final BgReading bgr) {
    final boolean recent = bgr.isCurrent();
    if (recent) {
        Notifications.start(); // may not be needed as this is duplicated in handleNewBgReading
        // probably not wanted for G5 internal values?
        //bgr.injectNoise(true); // Add noise parameter for nightscout
        //bgr.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    }
    BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), Home.get_follower(), !recent); // pebble and widget and follower
}
 
Example 4
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void create(EGVRecord egvRecord, long addativeOffset, Context context) {
    BgReading bgReading = BgReading.getForTimestamp(egvRecord.getSystemTime().getTime() + addativeOffset);
    Log.i(TAG, "create: Looking for BG reading to tag this thing to: " + egvRecord.getBGValue());
    if (bgReading != null) {
        bgReading.calculated_value = egvRecord.getBGValue();
        if (egvRecord.getBGValue() <= 13) {
            Calibration calibration = bgReading.calibration;
            double firstAdjSlope = calibration.first_slope + (calibration.first_decay * (Math.ceil(new Date().getTime() - calibration.timestamp) / (1000 * 60 * 10)));
            double calSlope = (calibration.first_scale / firstAdjSlope) * 1000;
            double calIntercept = ((calibration.first_scale * calibration.first_intercept) / firstAdjSlope) * -1;
            bgReading.raw_calculated = (((calSlope * bgReading.raw_data) + calIntercept) - 5);
        }
        Log.i(TAG, "create: NEW VALUE CALCULATED AT: " + bgReading.calculated_value);
        bgReading.calculated_value_slope = bgReading.slopefromName(egvRecord.getTrend().friendlyTrendName());
        bgReading.noise = egvRecord.noiseValue();
        String friendlyName = egvRecord.getTrend().friendlyTrendName();
        if (friendlyName.compareTo("NONE") == 0 ||
                friendlyName.compareTo("NOT_COMPUTABLE") == 0 ||
                friendlyName.compareTo("NOT COMPUTABLE") == 0 ||
                friendlyName.compareTo("OUT OF RANGE") == 0 ||
                friendlyName.compareTo("OUT_OF_RANGE") == 0) {
            bgReading.hide_slope = true;
        }
        bgReading.save();
        bgReading.find_new_curve();
        bgReading.find_new_raw_curve();
        //context.startService(new Intent(context, Notifications.class));
        Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading
        BgSendQueue.handleNewBgReading(bgReading, "create", context);
    }
}
 
Example 5
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static void notifyAndSync(final BgReading bgr) {
    final boolean recent = bgr.isCurrent();
    if (recent) {
        Notifications.start(); // may not be needed as this is duplicated in handleNewBgReading
        // probably not wanted for G5 internal values?
        //bgr.injectNoise(true); // Add noise parameter for nightscout
        //bgr.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    }
    BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), Home.get_follower(), !recent); // pebble and widget and follower
}
 
Example 6
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void create(EGVRecord egvRecord, long addativeOffset, Context context) {
    BgReading bgReading = BgReading.getForTimestamp(egvRecord.getSystemTime().getTime() + addativeOffset);
    Log.i(TAG, "create: Looking for BG reading to tag this thing to: " + egvRecord.getBGValue());
    if (bgReading != null) {
        bgReading.calculated_value = egvRecord.getBGValue();
        if (egvRecord.getBGValue() <= 13) {
            Calibration calibration = bgReading.calibration;
            double firstAdjSlope = calibration.first_slope + (calibration.first_decay * (Math.ceil(new Date().getTime() - calibration.timestamp) / (1000 * 60 * 10)));
            double calSlope = (calibration.first_scale / firstAdjSlope) * 1000;
            double calIntercept = ((calibration.first_scale * calibration.first_intercept) / firstAdjSlope) * -1;
            bgReading.raw_calculated = (((calSlope * bgReading.raw_data) + calIntercept) - 5);
        }
        Log.i(TAG, "create: NEW VALUE CALCULATED AT: " + bgReading.calculated_value);
        bgReading.calculated_value_slope = bgReading.slopefromName(egvRecord.getTrend().friendlyTrendName());
        bgReading.noise = egvRecord.noiseValue();
        String friendlyName = egvRecord.getTrend().friendlyTrendName();
        if (friendlyName.compareTo("NONE") == 0 ||
                friendlyName.compareTo("NOT_COMPUTABLE") == 0 ||
                friendlyName.compareTo("NOT COMPUTABLE") == 0 ||
                friendlyName.compareTo("OUT OF RANGE") == 0 ||
                friendlyName.compareTo("OUT_OF_RANGE") == 0) {
            bgReading.hide_slope = true;
        }
        bgReading.save();
        bgReading.find_new_curve();
        bgReading.find_new_raw_curve();
        //context.startService(new Intent(context, Notifications.class));
        Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading
        BgSendQueue.handleNewBgReading(bgReading, "create", context);
    }
}
 
Example 7
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void notifyAndSync(final BgReading bgr) {
    final boolean recent = bgr.isCurrent();
    if (recent) {
        Notifications.start(); // may not be needed as this is duplicated in handleNewBgReading
        // probably not wanted for G5 internal values?
        //bgr.injectNoise(true); // Add noise parameter for nightscout
        //bgr.injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    }
    BgSendQueue.handleNewBgReading(bgr, "create", xdrip.getAppContext(), Home.get_follower(), !recent); // pebble and widget and follower
}
 
Example 8
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static void create(EGVRecord egvRecord, long addativeOffset, Context context) {
    BgReading bgReading = BgReading.getForTimestamp(egvRecord.getSystemTime().getTime() + addativeOffset);
    Log.i(TAG, "create: Looking for BG reading to tag this thing to: " + egvRecord.getBGValue());
    if (bgReading != null) {
        bgReading.calculated_value = egvRecord.getBGValue();
        if (egvRecord.getBGValue() <= 13) {
            Calibration calibration = bgReading.calibration;
            double firstAdjSlope = calibration.first_slope + (calibration.first_decay * (Math.ceil(new Date().getTime() - calibration.timestamp) / (1000 * 60 * 10)));
            double calSlope = (calibration.first_scale / firstAdjSlope) * 1000;
            double calIntercept = ((calibration.first_scale * calibration.first_intercept) / firstAdjSlope) * -1;
            bgReading.raw_calculated = (((calSlope * bgReading.raw_data) + calIntercept) - 5);
        }
        Log.i(TAG, "create: NEW VALUE CALCULATED AT: " + bgReading.calculated_value);
        bgReading.calculated_value_slope = bgReading.slopefromName(egvRecord.getTrend().friendlyTrendName());
        bgReading.noise = egvRecord.noiseValue();
        String friendlyName = egvRecord.getTrend().friendlyTrendName();
        if (friendlyName.compareTo("NONE") == 0 ||
                friendlyName.compareTo("NOT_COMPUTABLE") == 0 ||
                friendlyName.compareTo("NOT COMPUTABLE") == 0 ||
                friendlyName.compareTo("OUT OF RANGE") == 0 ||
                friendlyName.compareTo("OUT_OF_RANGE") == 0) {
            bgReading.hide_slope = true;
        }
        bgReading.save();
        bgReading.find_new_curve();
        bgReading.find_new_raw_curve();
        //context.startService(new Intent(context, Notifications.class));
        Notifications.start(); // this may not be needed as it is duplicated in handleNewBgReading
        BgSendQueue.handleNewBgReading(bgReading, "create", context);
    }
}
 
Example 9
Source File: WatchUpdaterService.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
private synchronized void syncBgReadingsData(DataMap dataMap) {
    Log.d(TAG, "sync-precalculated-bg-readings-Data");

    final int calibration_state = dataMap.getInt("native_calibration_state", 0);
    Ob1G5CollectionService.processCalibrationState(CalibrationState.parse(calibration_state));
    Ob1G5StateMachine.injectDexTime(dataMap.getString("dextime", null));

    final boolean queue_drained = dataMap.getBoolean(PREF_QUEUE_DRAINED);
    if (queue_drained) {
        Ob1G5StateMachine.emptyQueue();
    }


    final ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        final Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .registerTypeAdapter(Date.class, new DateTypeAdapter())
                .serializeSpecialFloatingPointValues()
                .create();


        final int count = entries.size();

        if (count > 0) {

            final Sensor current_sensor = Sensor.currentSensor();
            if (current_sensor == null) {
                UserError.Log.e(TAG, "Cannot sync wear BG readings because sensor is marked stopped on phone");
                return;
            }

            Log.d(TAG, "syncTransmitterData add BgReading Table entries count=" + count);
            int idx = 0;
            long timeOfLastBG = 0;
            for (DataMap entry : entries) {
                if (entry != null) {
                    idx++;
                    final String bgrecord = entry.getString("bgs");
                    if (bgrecord != null) {

                        final BgReading bgData = gson.fromJson(bgrecord, BgReading.class);

                        final BgReading uuidexists = BgReading.findByUuid(bgData.uuid);
                        if (uuidexists == null) {

                            final BgReading exists = BgReading.getForTimestamp(bgData.timestamp);
                            if (exists == null) {
                                Log.d(TAG, "Saving new synced pre-calculated bg-reading: " + JoH.dateTimeText(bgData.timestamp) + " last entry: " + (idx == count) + " " + BgGraphBuilder.unitized_string_static(bgData.calculated_value));
                                bgData.sensor = current_sensor;
                                bgData.save();
                                BgSendQueue.handleNewBgReading(bgData, "create", xdrip.getAppContext(), Home.get_follower(), idx != count);
                            } else {
                                Log.d(TAG, "BgReading for timestamp already exists: " + JoH.dateTimeText(bgData.timestamp));
                            }
                        } else {
                            Log.d(TAG, "BgReading with uuid: " + bgData.uuid + " already exists: " + JoH.dateTimeText(bgData.timestamp));
                        }

                        timeOfLastBG = Math.max(bgData.timestamp + 1, timeOfLastBG);
                    }
                }
            }
            sendDataReceived(DATA_ITEM_RECEIVED_PATH, "DATA_RECEIVED_BGS count=" + entries.size(), timeOfLastBG, "BG", -1);

        } else {
            UserError.Log.e(TAG, "Not acknowledging wear BG readings as count was 0");
        }
    } else {
        UserError.Log.d(TAG, "Null entries list - should only happen with native status update only");
    }
}
 
Example 10
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, long timeoffset, Context context, boolean note_only, long estimatedInterstitialLagSeconds) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final String unit = prefs.getString("units", "mgdl");
    final boolean adjustPast = prefs.getBoolean("rewrite_history", true);

    if (unit.compareTo("mgdl") != 0) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    if ((bg < 40) || (bg > 400)) {
        Log.wtf(TAG, "Invalid out of range calibration glucose mg/dl value of: " + bg);
        JoH.static_toast_long("Calibration out of range: " + bg + " mg/dl");
        return null;
    }

    if (!note_only) CalibrationRequest.clearAll();
    final Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    boolean is_follower = prefs.getString("dex_collection_method", "").equals("Follower");
    if ((sensor == null)
            && (is_follower)) {
        Sensor.create(Math.round(JoH.ts())); // no sensor? no problem, create virtual one for follower
        sensor = Sensor.currentSensor();
    }

    if (sensor != null) {
        BgReading bgReading = null;
        if (timeoffset == 0) {
            bgReading = BgReading.last(is_follower);
        } else {
            // get closest bg reading we can find with a cut off at 15 minutes max time
            bgReading = BgReading.getForPreciseTimestamp(new Date().getTime() - ((timeoffset - estimatedInterstitialLagSeconds) * 1000 ), (15 * 60 * 1000));
        }
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime() - (timeoffset * 1000); //  potential historical bg readings
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000)) / 4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            if (!note_only) {
                calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            } else {
                calibration.sensor_confidence = 0; // exclude from calibrations but show on graph
                calibration.slope = 0;
                calibration.intercept = 0;
            }
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            if (!note_only) {
                bgReading.calibration = calibration;
                bgReading.calibration_flag = true;
                bgReading.save();
            }

            if ((!is_follower) && (!note_only)) {
                BgSendQueue.handleNewBgReading(bgReading, "update", context);
                // TODO probably should add a more fine grained prefs option in future
                calculate_w_l_s(prefs.getBoolean("infrequent_calibration", false));
                adjustRecentBgReadings(adjustPast ? 30 : 2);
                CalibrationSendQueue.addToQueue(calibration, context);
                context.startService(new Intent(context, Notifications.class));
                Calibration.requestCalibrationIfRangeTooNarrow();
                //KS newFingerStickData();
            } else {
                Log.d(TAG, "Follower mode or note so not processing calibration deeply");
            }
        } else {
            // we couldn't get a reading close enough to the calibration timestamp
            if (!is_follower) {
                JoH.static_toast(context, "No close enough reading for Calib (15 min)", Toast.LENGTH_LONG);
            }
        }
    } else {
        Log.d("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}
 
Example 11
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static void create(Context context, double raw_data, double age_adjusted_raw_value, double filtered_data, Long timestamp,
                          double calculated_bg, double calculated_current_slope, boolean hide_slope) {

    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.w(TAG, "No sensor, ignoring this bg reading");
        return;
    }

    Calibration calibration = Calibration.lastValid();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
        bgReading.perform_calculations();
    } else {
        Log.d(TAG, "Calibrations, so doing everything bgReading = " + bgReading);
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
    }
    BgSendQueue.handleNewBgReading(bgReading, "create", context);

    Log.i("BG GSON: ", bgReading.toS());
}
 
Example 12
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void postProcess(final boolean quick) {
    injectNoise(true); // Add noise parameter for nightscout
    injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    BgSendQueue.handleNewBgReading(this, "create", xdrip.getAppContext(), Home.get_follower(), quick);
}
 
Example 13
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static void create(Context context, double raw_data, double age_adjusted_raw_value, double filtered_data, Long timestamp,
                          double calculated_bg, double calculated_current_slope, boolean hide_slope) {

    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.w(TAG, "No sensor, ignoring this bg reading");
        return;
    }

    Calibration calibration = Calibration.lastValid();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
        bgReading.perform_calculations();
    } else {
        Log.d(TAG, "Calibrations, so doing everything bgReading = " + bgReading);
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
    }
    BgSendQueue.handleNewBgReading(bgReading, "create", context);

    Log.i("BG GSON: ", bgReading.toS());
}
 
Example 14
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public void postProcess(final boolean quick) {
    injectNoise(true); // Add noise parameter for nightscout
    injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    BgSendQueue.handleNewBgReading(this, "create", xdrip.getAppContext(), Home.get_follower(), quick);
}
 
Example 15
Source File: BgReading.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public static BgReading create(double raw_data, double filtered_data, Context context, Long timestamp) {
    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i("BG GSON: ",bgReading.toS());

        return bgReading;
    }

    Calibration calibration = Calibration.last();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.time_since_sensor_started = bgReading.timestamp - sensor.started_at;
        bgReading.calibration_flag = false;

        bgReading.calculateAgeAdjustedRawValue(context);

        bgReading.save();
        bgReading.perform_calculations();
    } else {
        Log.d(TAG,"Calibrations, so doing everything");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
        bgReading.raw_data = (raw_data/1000);
        bgReading.filtered_data = (filtered_data/1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.time_since_sensor_started = bgReading.timestamp - sensor.started_at;

        bgReading.calculateAgeAdjustedRawValue(context);

        if(calibration.check_in) {
            double firstAdjSlope = calibration.first_slope + (calibration.first_decay * (Math.ceil(new Date().getTime() - calibration.timestamp)/(1000 * 60 * 10)));
            double calSlope = (calibration.first_scale / firstAdjSlope)*1000;
            double calIntercept = ((calibration.first_scale * calibration.first_intercept) / firstAdjSlope)*-1;
            bgReading.calculated_value = (((calSlope * bgReading.raw_data) + calIntercept) - 5);
            bgReading.filtered_calculated_value = (((calSlope * bgReading.ageAdjustedFiltered()) + calIntercept) -5);

        } else {
            BgReading lastBgReading = BgReading.last();
            if (lastBgReading != null && lastBgReading.calibration != null) {
                if (lastBgReading.calibration_flag == true && ((lastBgReading.timestamp + (60000 * 20)) > bgReading.timestamp) && ((lastBgReading.calibration.timestamp + (60000 * 20)) > bgReading.timestamp)) {
                    lastBgReading.calibration.rawValueOverride(BgReading.weightedAverageRaw(lastBgReading.timestamp, bgReading.timestamp, lastBgReading.calibration.timestamp, lastBgReading.age_adjusted_raw_value, bgReading.age_adjusted_raw_value), context);
                }
            }
            bgReading.calculated_value = ((calibration.slope * bgReading.age_adjusted_raw_value) + calibration.intercept);
            bgReading.filtered_calculated_value = ((calibration.slope * bgReading.ageAdjustedFiltered()) + calibration.intercept);
        }
        updateCalculatedValue(bgReading);

        bgReading.save();
        bgReading.perform_calculations();
        context.startService(new Intent(context, Notifications.class));
        BgSendQueue.handleNewBgReading(bgReading, "create", context);
    }

    Log.i("BG GSON: ",bgReading.toS());

    return bgReading;
}
 
Example 16
Source File: WatchUpdaterService.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
private synchronized void syncBgReadingsData(DataMap dataMap) {
    Log.d(TAG, "sync-precalculated-bg-readings-Data");

    final int calibration_state = dataMap.getInt("native_calibration_state", 0);
    Ob1G5CollectionService.processCalibrationState(CalibrationState.parse(calibration_state));
    Ob1G5StateMachine.injectDexTime(dataMap.getString("dextime", null));

    final boolean queue_drained = dataMap.getBoolean(PREF_QUEUE_DRAINED);
    if (queue_drained) {
        Ob1G5StateMachine.emptyQueue();
    }


    final ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        final Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .registerTypeAdapter(Date.class, new DateTypeAdapter())
                .serializeSpecialFloatingPointValues()
                .create();


        final int count = entries.size();

        if (count > 0) {

            final Sensor current_sensor = Sensor.currentSensor();
            if (current_sensor == null) {
                UserError.Log.e(TAG, "Cannot sync wear BG readings because sensor is marked stopped on phone");
                return;
            }

            Log.d(TAG, "syncTransmitterData add BgReading Table entries count=" + count);
            int idx = 0;
            long timeOfLastBG = 0;
            for (DataMap entry : entries) {
                if (entry != null) {
                    idx++;
                    final String bgrecord = entry.getString("bgs");
                    if (bgrecord != null) {

                        final BgReading bgData = gson.fromJson(bgrecord, BgReading.class);

                        final BgReading uuidexists = BgReading.findByUuid(bgData.uuid);
                        if (uuidexists == null) {

                            final BgReading exists = BgReading.getForTimestamp(bgData.timestamp);
                            if (exists == null) {
                                Log.d(TAG, "Saving new synced pre-calculated bg-reading: " + JoH.dateTimeText(bgData.timestamp) + " last entry: " + (idx == count) + " " + BgGraphBuilder.unitized_string_static(bgData.calculated_value));
                                bgData.sensor = current_sensor;
                                bgData.save();
                                BgSendQueue.handleNewBgReading(bgData, "create", xdrip.getAppContext(), Home.get_follower(), idx != count);
                            } else {
                                Log.d(TAG, "BgReading for timestamp already exists: " + JoH.dateTimeText(bgData.timestamp));
                            }
                        } else {
                            Log.d(TAG, "BgReading with uuid: " + bgData.uuid + " already exists: " + JoH.dateTimeText(bgData.timestamp));
                        }

                        timeOfLastBG = Math.max(bgData.timestamp + 1, timeOfLastBG);
                    }
                }
            }
            sendDataReceived(DATA_ITEM_RECEIVED_PATH, "DATA_RECEIVED_BGS count=" + entries.size(), timeOfLastBG, "BG", -1);

        } else {
            UserError.Log.e(TAG, "Not acknowledging wear BG readings as count was 0");
        }
    } else {
        UserError.Log.d(TAG, "Null entries list - should only happen with native status update only");
    }
}
 
Example 17
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static void create(Context context, double raw_data, double age_adjusted_raw_value, double filtered_data, Long timestamp,
                          double calculated_bg, double calculated_current_slope, boolean hide_slope) {

    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.w(TAG, "No sensor, ignoring this bg reading");
        return;
    }

    Calibration calibration = Calibration.lastValid();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
        bgReading.perform_calculations();
    } else {
        Log.d(TAG, "Calibrations, so doing everything bgReading = " + bgReading);
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
    }
    BgSendQueue.handleNewBgReading(bgReading, "create", context);

    Log.i("BG GSON: ", bgReading.toS());
}
 
Example 18
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public void postProcess(final boolean quick) {
    injectNoise(true); // Add noise parameter for nightscout
    injectDisplayGlucose(BestGlucose.getDisplayGlucose()); // Add display glucose for nightscout
    BgSendQueue.handleNewBgReading(this, "create", xdrip.getAppContext(), Home.get_follower(), quick);
}
 
Example 19
Source File: BgReading.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static void create(Context context, double raw_data, double age_adjusted_raw_value, double filtered_data, Long timestamp,
                          double calculated_bg, double calculated_current_slope, boolean hide_slope) {

    BgReading bgReading = new BgReading();
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.w(TAG, "No sensor, ignoring this bg reading");
        return;
    }

    Calibration calibration = Calibration.lastValid();
    if (calibration == null) {
        Log.d(TAG, "create: No calibration yet");
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
        bgReading.perform_calculations();
    } else {
        Log.d(TAG, "Calibrations, so doing everything bgReading = " + bgReading);
        bgReading.sensor = sensor;
        bgReading.sensor_uuid = sensor.uuid;
        bgReading.calibration = calibration;
        bgReading.calibration_uuid = calibration.uuid;
        bgReading.raw_data = (raw_data / 1000);
        bgReading.age_adjusted_raw_value = age_adjusted_raw_value;
        bgReading.filtered_data = (filtered_data / 1000);
        bgReading.timestamp = timestamp;
        bgReading.uuid = UUID.randomUUID().toString();
        bgReading.calculated_value = calculated_bg;
        bgReading.calculated_value_slope = calculated_current_slope;
        bgReading.hide_slope = hide_slope;

        bgReading.save();
    }
    BgSendQueue.handleNewBgReading(bgReading, "create", context);

    Log.i("BG GSON: ", bgReading.toS());
}
 
Example 20
Source File: Calibration.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public static Calibration create(double bg, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String unit = prefs.getString("units", "mgdl");
    boolean adjustPast = prefs.getBoolean("adjust_past", false);

    if(unit.compareTo("mgdl") != 0 ) {
        bg = bg * Constants.MMOLL_TO_MGDL;
    }

    CalibrationRequest.clearAll();
    Calibration calibration = new Calibration();
    Sensor sensor = Sensor.currentSensor();

    if (sensor != null) {
        BgReading bgReading = BgReading.last();
        if (bgReading != null) {
            calibration.sensor = sensor;
            calibration.bg = bg;
            calibration.check_in = false;
            calibration.timestamp = new Date().getTime();
            calibration.raw_value = bgReading.raw_data;
            calibration.adjusted_raw_value = bgReading.age_adjusted_raw_value;
            calibration.sensor_uuid = sensor.uuid;
            calibration.slope_confidence = Math.min(Math.max(((4 - Math.abs((bgReading.calculated_value_slope) * 60000))/4), 0), 1);

            double estimated_raw_bg = BgReading.estimated_raw_bg(new Date().getTime());
            calibration.raw_timestamp = bgReading.timestamp;
            if (Math.abs(estimated_raw_bg - bgReading.age_adjusted_raw_value) > 20) {
                calibration.estimate_raw_at_time_of_calibration = bgReading.age_adjusted_raw_value;
            } else {
                calibration.estimate_raw_at_time_of_calibration = estimated_raw_bg;
            }
            calibration.distance_from_estimate = Math.abs(calibration.bg - bgReading.calculated_value);
            calibration.sensor_confidence = Math.max(((-0.0018 * bg * bg) + (0.6657 * bg) + 36.7505) / 100, 0);
            calibration.sensor_age_at_time_of_estimation = calibration.timestamp - sensor.started_at;
            calibration.uuid = UUID.randomUUID().toString();
            calibration.save();

            bgReading.calibration = calibration;
            bgReading.calibration_flag = true;
            bgReading.save();
            BgSendQueue.handleNewBgReading(bgReading, "update", context);

            calculate_w_l_s(context);

            adjustRecentBgReadings(adjustPast?30:1);
            CalibrationSendQueue.addToQueue(calibration, context);
            context.startService(new Intent(context, Notifications.class));
            Calibration.requestCalibrationIfRangeTooNarrow();
        }
    } else {
        Log.d("CALIBRATION", "No sensor, cant save!");
    }
    return Calibration.last();
}