private void addMockBgReading(int raw_data, int minutes, Sensor sensor) { BgReading mockReading = new BgReading(); mockReading.raw_data = raw_data; mockReading.timestamp = System.currentTimeMillis() - (1000 * 60 * minutes); mockReading.sensor = sensor; mockReading.age_adjusted_raw_value = raw_data + 0.1; mockReading.save(); } }
public static BgReading bgReadingInsertFromG5(double calculated_value, long timestamp) { return bgReadingInsertFromG5(calculated_value, timestamp, null); }
public static double estimated_bg(double timestamp) { timestamp = timestamp + BESTOFFSET; BgReading latest = BgReading.last(); if (latest == null) { return 0; } else { return (latest.a * timestamp * timestamp) + (latest.b * timestamp) + latest.c; } }
public void find_slope() { List<BgReading> last_2 = BgReading.latest(2); // FYI: By default, assertions are disabled at runtime. Add "-ea" to commandline to enable. // https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html assert last_2.get(0).uuid.equals(this.uuid) : "Invariant condition not fulfilled: calculating slope and current reading wasn't saved before"; if ((last_2 != null) && (last_2.size() == 2)) { calculated_value_slope = calculateSlope(this, last_2.get(1)); save(); } else if ((last_2 != null) && (last_2.size() == 1)) { calculated_value_slope = 0; save(); } else { if (JoH.ratelimit("no-bg-couldnt-find-slope", 15)) { Log.w(TAG, "NO BG? COULDNT FIND SLOPE!"); } } }
public static void speakNow(long grace) { final BgReading bgReading = BgReading.last(); if (bgReading != null) { final BestGlucose.DisplayGlucose dg = BestGlucose.getDisplayGlucose(); if (dg != null) { BgToSpeech.realSpeakNow(dg.mgdl, dg.timestamp + grace, dg.delta_name); } else { BgToSpeech.realSpeakNow(bgReading.calculated_value, bgReading.timestamp + grace, bgReading.slopeName()); } } }
private void populateV1APIBGEntry(JSONArray array, BgReading record) throws Exception { JSONObject json = new JSONObject(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US); format.setTimeZone(TimeZone.getDefault()); json.put("device", getDeviceString(record)); if (record != null) {//KS json.put("date", record.timestamp); json.put("dateString", format.format(record.timestamp)); if(prefs.getBoolean("cloud_storage_api_use_best_glucose", false)){ json.put("sgv", (int) record.getDg_mgdl()); json.put("delta", new BigDecimal(record.getDg_slope() * 5 * 60 * 1000).setScale(3, BigDecimal.ROUND_HALF_UP)); json.put("direction", record.getDg_deltaName()); } else { json.put("sgv", (int) record.calculated_value); json.put("delta", new BigDecimal(record.currentSlope() * 5 * 60 * 1000).setScale(3, BigDecimal.ROUND_HALF_UP)); // jamorham for automation json.put("direction", record.slopeName()); } json.put("type", "sgv"); json.put("filtered", record.ageAdjustedFiltered() * 1000); json.put("unfiltered", record.usedRaw() * 1000); json.put("rssi", 100); json.put("noise", record.noiseValue()); json.put("sysTime", format.format(record.timestamp)); array.put(json); } else Log.e(TAG, "doRESTUploadTo BG record is null."); }
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_slope = bgReading.slopefromName(egvRecord.getTrend().friendlyTrendName()); bgReading.noise = egvRecord.noiseValue(); String friendlyName = egvRecord.getTrend().friendlyTrendName(); bgReading.hide_slope = true; bgReading.save(); bgReading.find_new_curve(); bgReading.find_new_raw_curve();
bundle.putString(Intents.EXTRA_NS_NOISE_LEVEL, bgReading.noise); if ((Pref.getBoolean("broadcast_data_use_best_glucose", false)) && !bgReading.isBackfilled() && ((dg = BestGlucose.getDisplayGlucose()) != null)) { bundle.putDouble(Intents.EXTRA_BG_SLOPE, BgReading.currentSlope()); if (bgReading.hide_slope) { bundle.putString(Intents.EXTRA_BG_SLOPE_NAME, "9"); // not sure if this is right but has been this way for a long time } else { bundle.putString(Intents.EXTRA_BG_SLOPE_NAME, bgReading.slopeName()); scale = 1; unfiltered = bgReading.usedRaw() * 1000; filtered = bgReading.ageAdjustedFiltered() * 1000;
public static void create(SensorRecord sensorRecord, long addativeOffset, Context context) { Log.i(TAG, "create: gonna make some sensor records: " + sensorRecord.getUnfiltered()); if (BgReading.is_new(sensorRecord, addativeOffset)) { BgReading bgReading = new BgReading(); Sensor sensor = Sensor.currentSensor(); Calibration calibration = Calibration.getForTimestamp(sensorRecord.getSystemTime().getTime() + addativeOffset); if (sensor != null && calibration != null) { bgReading.sensor = sensor; bgReading.sensor_uuid = sensor.uuid; bgReading.calibration = calibration; bgReading.calibration_uuid = calibration.uuid; bgReading.raw_data = (sensorRecord.getUnfiltered() / 1000); bgReading.filtered_data = (sensorRecord.getFiltered() / 1000); bgReading.timestamp = sensorRecord.getSystemTime().getTime() + addativeOffset; if (bgReading.timestamp > new Date().getTime()) { return; } bgReading.uuid = UUID.randomUUID().toString(); bgReading.time_since_sensor_started = bgReading.timestamp - sensor.started_at; bgReading.calculateAgeAdjustedRawValue(); bgReading.save(); } } }
private Point createGlucosePoint(BgReading record) { // TODO DisplayGlucose option final BigDecimal delta = new BigDecimal(record.calculated_value_slope * 5 * 60 * 1000) .setScale(3, BigDecimal.ROUND_HALF_UP); return Point.measurement("glucose") .time(record.getEpochTimestamp(), TimeUnit.MILLISECONDS) .addField("value_mmol", record.calculated_value_mmol()) .addField("value_mgdl", record.getMgdlValue()) .addField("direction", record.slopeName()) .addField("filtered", record.ageAdjustedFiltered() * 1000) .addField("unfiltered", record.usedRaw() * 1000) .addField("rssi", 100) .addField("noise", record.noiseValue()) .addField("delta", delta) .build(); }
final BgReading existing = getForPreciseTimestamp(timestamp, Constants.MINUTE_IN_MS); if (existing == null) { final BgReading bgr = new BgReading(); bgr.sensor = sensor; bgr.sensor_uuid = sensor.uuid; bgr.calculated_value = calculated_value; bgr.appendSourceInfo("G5 Native"); if (sourceInfoAppend != null && sourceInfoAppend.length() > 0) { bgr.appendSourceInfo(sourceInfoAppend); bgr.save(); if (JoH.ratelimit("sync wakelock", 15)) { final PowerManager.WakeLock linger = JoH.getWakeLock("G5 Insert", 4000); Inevitable.stackableTask("NotifySyncBgr", 3000, () -> notifyAndSync(bgr)); return bgr; } else {
return null; final BgReading bgr = fromJSON(json); if (bgr != null) { try { if (readingNearTimeStamp(bgr.timestamp) == null) { FixCalibration(bgr); if (force_sensor) { final Sensor forced_sensor = Sensor.currentSensor(); UserError.Log.wtf(TAG, "Received a bg reading that appears to be in the future: " + JoH.dateTimeText(bgr.timestamp) + " vs " + JoH.dateTimeText(now)); bgr.save(); if (do_notification) {
private static void processFromMessage(BgReadingMessage btm) { if ((btm != null) && (btm.uuid != null) && (btm.uuid.length() == 36)) { BgReading bg = byUUID(btm.uuid); if (bg != null) { // we already have this uuid and we don't have a circumstance to update the record, so quick return here return; } if (bg == null) { bg = getForPreciseTimestamp(Wire.get(btm.timestamp, BgReadingMessage.DEFAULT_TIMESTAMP), CLOSEST_READING_MS, false); if (bg != null) { UserError.Log.wtf(TAG, "Error matches a different uuid with the same timestamp: " + bg.uuid + " vs " + btm.uuid + " skipping!"); return; } bg = new BgReading(); } bg.timestamp = Wire.get(btm.timestamp, BgReadingMessage.DEFAULT_TIMESTAMP); bg.calculated_value = Wire.get(btm.calculated_value, BgReadingMessage.DEFAULT_CALCULATED_VALUE); bg.filtered_calculated_value = Wire.get(btm.filtered_calculated_value, BgReadingMessage.DEFAULT_FILTERED_CALCULATED_VALUE); bg.calibration_flag = Wire.get(btm.calibration_flag, BgReadingMessage.DEFAULT_CALIBRATION_FLAG); bg.raw_calculated = Wire.get(btm.raw_calculated, BgReadingMessage.DEFAULT_RAW_CALCULATED); bg.raw_data = Wire.get(btm.raw_data, BgReadingMessage.DEFAULT_RAW_DATA); bg.calculated_value_slope = Wire.get(btm.calculated_value_slope, BgReadingMessage.DEFAULT_CALCULATED_VALUE_SLOPE); bg.calibration_uuid = btm.calibration_uuid; bg.uuid = btm.uuid; bg.save(); } else { UserError.Log.wtf(TAG, "processFromMessage uuid is null or invalid"); } }
if (glucose.usable() || (glucose.insufficient() && Pref.getBoolean("ob1_g5_use_insufficiently_calibrated", true))) { UserError.Log.d(TAG, "Got usable glucose data from G5!!"); final BgReading bgReading = BgReading.bgReadingInsertFromG5(glucose.glucose, JoH.tsl()); if (bgReading != null) { try { bgReading.appendSourceInfo("Insufficient").save();
public void onClick(View v) { if (futureReadings != null && futureReadings.size() > 0) { for (BgReading bgReading : futureReadings) { bgReading.calculated_value = 0; bgReading.raw_data = 0; bgReading.timestamp = 0; bgReading.save(); } } if (futureCalibrations != null && futureCalibrations.size() > 0) { for (Calibration calibration : futureCalibrations) { calibration.slope_confidence = 0; calibration.sensor_confidence = 0; calibration.timestamp = 0; calibration.save(); } } } });
public static Accuracy create(BloodTest bloodTest, BestGlucose.DisplayGlucose dg) { if (dg == null) return null; final BgReading from_dg = new BgReading(); from_dg.timestamp = dg.timestamp; from_dg.calculated_value = dg.mgdl; return create(bloodTest, from_dg, dg.plugin_name); }
@Test public void bgReadingInsertFromDataTest() throws JSONException { // test data courtesy of philipgo - thanks final JSONObject json_object = new JSONObject(); json_object.put("date", 1526717691000.0); json_object.put("sgv", 148); json_object.put("direction", "SingleUp"); final BgReading result = NSEmulatorReceiver.bgReadingInsertFromData(json_object.getLong("date"), json_object.getDouble("sgv"), BgReading.slopefromName(json_object.getString("direction")), true); assertWithMessage("result not null").that(result).isNotNull(); assertWithMessage("direction matches").that(result.slopeName()).isEqualTo("SingleUp"); assertWithMessage("timestamp matches").that(result.timestamp).isEqualTo(1526717691000L); assertWithMessage("sgv matches 1").that(result.calculated_value).isEqualTo(148d); assertWithMessage("sgv matches 2").that(result.filtered_calculated_value).isEqualTo(148d); assertWithMessage("sgv matches 3").that(result.raw_data).isEqualTo(148d); assertWithMessage("sgv matches 4").that(result.filtered_data).isEqualTo(148d); //System.out.println(result.toJSON(true)); //System.out.println(result.slopeName()); } }
private static void processBacksies(List<BackFillStream.Backsie> backsies) { boolean changed = false; for (BackFillStream.Backsie backsie : backsies) { final long time = DexTimeKeeper.fromDexTime(getTransmitterID(), backsie.getDextime()); final long since = JoH.msSince(time); if ((since > HOUR_IN_MS * 6) || (since < 0)) { UserError.Log.wtf(TAG, "Backfill timestamp unrealistic: " + JoH.dateTimeText(time) + " (ignored)"); } else { if (BgReading.getForPreciseTimestamp(time, Constants.MINUTE_IN_MS * 4) == null) { final BgReading bgr = BgReading.bgReadingInsertFromG5(backsie.getGlucose(), time, "Backfill"); lastGlucoseBgReading = bgr; UserError.Log.d(TAG, "Adding backfilled reading: " + JoH.dateTimeText(time) + " " + BgGraphBuilder.unitized_string_static(backsie.getGlucose())); changed = true; } UserError.Log.d(TAG, "Backsie: " + JoH.dateTimeText(time) + " " + BgGraphBuilder.unitized_string_static(backsie.getGlucose())); } } if (changed) { updateStreamedTillTimeForBackfill(); } }
public String getDg_deltaName(){ if(dg_mgdl != 0 && dg_delta_name != null) return dg_delta_name; return slopeName(); }
Double slope = BgReading.slopefromName(sgv_map.get("direction").toString()); jsonObject.put("calculated_value_slope", slope); if (BgReading.isSlopeNameInvalid(sgv_map.get("direction").toString())) { jsonObject.put("hide_slope", true);