com.google.gson.internal.bind.DateTypeAdapter#com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue源码实例Demo

下面列出了com.google.gson.internal.bind.DateTypeAdapter#com.eveningoutpost.dexdrip.UtilityModels.BgSendQueue 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: xDrip   文件: BgReading.java
public synchronized static void updateDB() {
    final String[] updates = new String[]{"ALTER TABLE BgReadings ADD COLUMN dg_mgdl REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_slope REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_delta_name TEXT;",
            "ALTER TABLE BgReadings ADD COLUMN source_info TEXT;"};
    for (String patch : updates) {
        try {
            SQLiteUtils.execSql(patch);
        } catch (Exception e) {
        }
    }

    // needs different handling on wear
    if (JoH.areWeRunningOnAndroidWear()) {
        BgSendQueue.emptyQueue();
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT FAIL", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT IGNORE", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgSendQueue", "BgReadings_temp", "BgReadings");
    }

}
 
源代码2 项目: xDrip   文件: BgReading.java
public synchronized static void updateDB() {
    final String[] updates = new String[]{"ALTER TABLE BgReadings ADD COLUMN dg_mgdl REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_slope REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_delta_name TEXT;",
            "ALTER TABLE BgReadings ADD COLUMN source_info TEXT;"};
    for (String patch : updates) {
        try {
            SQLiteUtils.execSql(patch);
        } catch (Exception e) {
        }
    }

    // needs different handling on wear
    if (JoH.areWeRunningOnAndroidWear()) {
        BgSendQueue.emptyQueue();
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT FAIL", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT IGNORE", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgSendQueue", "BgReadings_temp", "BgReadings");
    }

}
 
源代码3 项目: xDrip-plus   文件: BgReading.java
public synchronized static void updateDB() {
    final String[] updates = new String[]{"ALTER TABLE BgReadings ADD COLUMN dg_mgdl REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_slope REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_delta_name TEXT;",
            "ALTER TABLE BgReadings ADD COLUMN source_info TEXT;"};
    for (String patch : updates) {
        try {
            SQLiteUtils.execSql(patch);
        } catch (Exception e) {
        }
    }

    // needs different handling on wear
    if (JoH.areWeRunningOnAndroidWear()) {
        BgSendQueue.emptyQueue();
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT FAIL", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT IGNORE", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgSendQueue", "BgReadings_temp", "BgReadings");
    }

}
 
源代码4 项目: xDrip-plus   文件: BgReading.java
public synchronized static void updateDB() {
    final String[] updates = new String[]{"ALTER TABLE BgReadings ADD COLUMN dg_mgdl REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_slope REAL;",
            "ALTER TABLE BgReadings ADD COLUMN dg_delta_name TEXT;",
            "ALTER TABLE BgReadings ADD COLUMN source_info TEXT;"};
    for (String patch : updates) {
        try {
            SQLiteUtils.execSql(patch);
        } catch (Exception e) {
        }
    }

    // needs different handling on wear
    if (JoH.areWeRunningOnAndroidWear()) {
        BgSendQueue.emptyQueue();
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT FAIL", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgReadings", "uuid TEXT UNIQUE ON CONFLICT IGNORE", "uuid TEXT UNIQUE ON CONFLICT REPLACE");
        SqliteRejigger.rejigSchema("BgSendQueue", "BgReadings_temp", "BgReadings");
    }

}
 
源代码5 项目: xDrip-Experimental   文件: BgReading.java
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);
        Pair<Double, Boolean> slopePair = BgReading.slopefromName(egvRecord.getTrend().friendlyTrendName());
        bgReading.calculated_value_slope = slopePair.first;
        bgReading.hide_slope = slopePair.second;
        bgReading.noise = egvRecord.noiseValue();
        bgReading.save();
        bgReading.find_new_curve();
        bgReading.find_new_raw_curve();
        context.startService(new Intent(context, Notifications.class));
        BgSendQueue.handleNewBgReading(bgReading, "create", context);
    }
}
 
源代码6 项目: xDrip   文件: BgReading.java
public static void create(EGVRecord egvRecord, long addativeOffset, Context context) {
    BgReading bgReading = BgReading.getForTimestamp(egvRecord.getSystemTime().getTime() + addativeOffset);
    Log.w(TAG, "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);
            bgReading.noise = egvRecord.noiseValue();
        }
        Log.w(TAG, "NEW VALUE CALCULATED AT: " + bgReading.calculated_value);
        bgReading.calculated_value_slope = bgReading.slopefromName(egvRecord.getTrend().friendlyTrendName());
        if(egvRecord.getTrend().friendlyTrendName().compareTo("NOT_COMPUTABLE") == 0 || egvRecord.getTrend().friendlyTrendName().compareTo("OUT_OF_RANGE") == 0) {
            bgReading.hide_slope = true;
        }
        bgReading.save();
        bgReading.find_new_curve();
        bgReading.find_new_raw_curve();
        Notifications.notificationSetter(context);
        BgSendQueue.addToQueue(bgReading, "create", context);
    }
}
 
源代码7 项目: xDrip   文件: BgReading.java
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);
    }
}
 
源代码8 项目: xDrip   文件: BgReading.java
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
}
 
源代码9 项目: xDrip   文件: BgReading.java
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);
    }
}
 
源代码10 项目: xDrip   文件: BgReading.java
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
}
 
源代码11 项目: xDrip-plus   文件: BgReading.java
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);
    }
}
 
源代码12 项目: xDrip-plus   文件: BgReading.java
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
}
 
源代码13 项目: xDrip-plus   文件: BgReading.java
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);
    }
}
 
源代码14 项目: xDrip-plus   文件: BgReading.java
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
}
 
源代码15 项目: xDrip-Experimental   文件: BgReading.java
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());
}
 
源代码16 项目: xDrip   文件: Calibration.java
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();
}
 
源代码17 项目: xDrip   文件: BgReading.java
public static BgReading create(double raw_data, double filtered_data, Context context, Long timestamp, boolean quick) {
    if (context == null) context = xdrip.getAppContext();
    BgReading bgReading = new BgReading();
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i("BG GSON: ", bgReading.toS());
        return bgReading;
    }

    if (raw_data == 0) {
        Log.e(TAG,"Warning: raw_data is 0 in BgReading.create()");
    }

    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.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();

        bgReading.save();
        bgReading.perform_calculations();
        BgSendQueue.sendToPhone(context);
    } else {
        Log.d(TAG, "Calibrations, so doing everything: " + calibration.uuid);
        bgReading = createFromRawNoSave(sensor, calibration, raw_data, filtered_data, timestamp);

        bgReading.save();

        // used when we are not fast inserting data
        if (!quick) {
            bgReading.perform_calculations();

            if (JoH.ratelimit("opportunistic-calibration", 60)) {
                BloodTest.opportunisticCalibration();
            }

            //context.startService(new Intent(context, Notifications.class));
            // allow this instead to be fired inside handleNewBgReading when noise will have been injected already
        }

        bgReading.postProcess(quick);

    }

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

    return bgReading;
}
 
源代码18 项目: xDrip   文件: BgReading.java
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);
}
 
源代码19 项目: xDrip   文件: BgReading.java
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());
}
 
源代码20 项目: xDrip   文件: CircleWatchface.java
@Override
public void onCreate() {
    super.onCreate();

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CreateWakelock");
    wakeLock.acquire(30000);
    try {

        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        display.getSize(displaySize);

        specW = View.MeasureSpec.makeMeasureSpec(displaySize.x,
                View.MeasureSpec.EXACTLY);
        specH = View.MeasureSpec.makeMeasureSpec(displaySize.y,
                View.MeasureSpec.EXACTLY);

        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        sharedPrefs.registerOnSharedPreferenceChangeListener(this);

        //register Message Receiver
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, new IntentFilter(Intent.ACTION_SEND));

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myLayout = inflater.inflate(R.layout.modern_layout, null);
        prepareLayout();
        prepareDrawTime();

        Context context = xdrip.getAppContext();
        if (Home.get_forced_wear()) {
            if (d) Log.d(TAG, "performViewSetup FORCE WEAR init BGs for graph");
            BgSendQueue.resendData(context);
        }
        Log.d(TAG, "performViewSetup requestData");
        ListenerService.requestData(this); //usually connection is not set up yet  //KS uncomment
    } finally {
        wakeLock.release();
    }
}
 
源代码21 项目: xDrip   文件: BgReading.java
public static BgReading create(double raw_data, double filtered_data, Context context, Long timestamp, boolean quick) {
    if (context == null) context = xdrip.getAppContext();
    BgReading bgReading = new BgReading();
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i("BG GSON: ", bgReading.toS());
        return bgReading;
    }

    if (raw_data == 0) {
        Log.e(TAG,"Warning: raw_data is 0 in BgReading.create()");
    }

    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.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();

        bgReading.save();
        bgReading.perform_calculations();
        BgSendQueue.sendToPhone(context);
    } else {
        Log.d(TAG, "Calibrations, so doing everything: " + calibration.uuid);
        bgReading = createFromRawNoSave(sensor, calibration, raw_data, filtered_data, timestamp);

        bgReading.save();

        // used when we are not fast inserting data
        if (!quick) {
            bgReading.perform_calculations();

            if (JoH.ratelimit("opportunistic-calibration", 60)) {
                BloodTest.opportunisticCalibration();
            }

            //context.startService(new Intent(context, Notifications.class));
            // allow this instead to be fired inside handleNewBgReading when noise will have been injected already
        }

        bgReading.postProcess(quick);

    }

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

    return bgReading;
}
 
源代码22 项目: xDrip   文件: BgReading.java
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);
}
 
源代码23 项目: xDrip   文件: BgReading.java
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());
}
 
源代码24 项目: xDrip   文件: WatchUpdaterService.java
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");
    }
}
 
源代码25 项目: xDrip-plus   文件: Calibration.java
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();
}
 
源代码26 项目: xDrip-plus   文件: BgReading.java
public static BgReading create(double raw_data, double filtered_data, Context context, Long timestamp, boolean quick) {
    if (context == null) context = xdrip.getAppContext();
    BgReading bgReading = new BgReading();
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i("BG GSON: ", bgReading.toS());
        return bgReading;
    }

    if (raw_data == 0) {
        Log.e(TAG,"Warning: raw_data is 0 in BgReading.create()");
    }

    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.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();

        bgReading.save();
        bgReading.perform_calculations();
        BgSendQueue.sendToPhone(context);
    } else {
        Log.d(TAG, "Calibrations, so doing everything: " + calibration.uuid);
        bgReading = createFromRawNoSave(sensor, calibration, raw_data, filtered_data, timestamp);

        bgReading.save();

        // used when we are not fast inserting data
        if (!quick) {
            bgReading.perform_calculations();

            if (JoH.ratelimit("opportunistic-calibration", 60)) {
                BloodTest.opportunisticCalibration();
            }

            //context.startService(new Intent(context, Notifications.class));
            // allow this instead to be fired inside handleNewBgReading when noise will have been injected already
        }

        bgReading.postProcess(quick);

    }

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

    return bgReading;
}
 
源代码27 项目: xDrip-plus   文件: BgReading.java
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);
}
 
源代码28 项目: xDrip-plus   文件: BgReading.java
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());
}
 
源代码29 项目: xDrip-plus   文件: CircleWatchface.java
@Override
public void onCreate() {
    super.onCreate();

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CreateWakelock");
    wakeLock.acquire(30000);
    try {

        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
        display.getSize(displaySize);

        specW = View.MeasureSpec.makeMeasureSpec(displaySize.x,
                View.MeasureSpec.EXACTLY);
        specH = View.MeasureSpec.makeMeasureSpec(displaySize.y,
                View.MeasureSpec.EXACTLY);

        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        sharedPrefs.registerOnSharedPreferenceChangeListener(this);

        //register Message Receiver
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, new IntentFilter(Intent.ACTION_SEND));

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myLayout = inflater.inflate(R.layout.modern_layout, null);
        prepareLayout();
        prepareDrawTime();

        Context context = xdrip.getAppContext();
        if (Home.get_forced_wear()) {
            if (d) Log.d(TAG, "performViewSetup FORCE WEAR init BGs for graph");
            BgSendQueue.resendData(context);
        }
        Log.d(TAG, "performViewSetup requestData");
        ListenerService.requestData(this); //usually connection is not set up yet  //KS uncomment
    } finally {
        wakeLock.release();
    }
}
 
源代码30 项目: xDrip-plus   文件: BgReading.java
public static BgReading create(double raw_data, double filtered_data, Context context, Long timestamp, boolean quick) {
    if (context == null) context = xdrip.getAppContext();
    BgReading bgReading = new BgReading();
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        Log.i("BG GSON: ", bgReading.toS());
        return bgReading;
    }

    if (raw_data == 0) {
        Log.e(TAG,"Warning: raw_data is 0 in BgReading.create()");
    }

    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.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();

        bgReading.save();
        bgReading.perform_calculations();
        BgSendQueue.sendToPhone(context);
    } else {
        Log.d(TAG, "Calibrations, so doing everything: " + calibration.uuid);
        bgReading = createFromRawNoSave(sensor, calibration, raw_data, filtered_data, timestamp);

        bgReading.save();

        // used when we are not fast inserting data
        if (!quick) {
            bgReading.perform_calculations();

            if (JoH.ratelimit("opportunistic-calibration", 60)) {
                BloodTest.opportunisticCalibration();
            }

            //context.startService(new Intent(context, Notifications.class));
            // allow this instead to be fired inside handleNewBgReading when noise will have been injected already
        }

        bgReading.postProcess(quick);

    }

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

    return bgReading;
}