Skip to content

Commit

Permalink
Merge pull request #1955 from Navid200/Navid_2022_01_25
Browse files Browse the repository at this point in the history
G6 backfill prior to start AAPS fix copied to wear
  • Loading branch information
jamorham authored Jan 28, 2022
2 parents 8c67a14 + 8cde6f9 commit 984ac39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,12 @@ private static void disconnectNow(Ob1G5CollectionService parent, RxBleConnection
}

private static void backFillIfNeeded(Ob1G5CollectionService parent, RxBleConnection connection) {
final Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
UserError.Log.e(TAG, "Cannot process backfill evaluation as no active sensor");
return;
}

final int check_readings = nextBackFillCheckSize;
UserError.Log.d(TAG, "Checking " + check_readings + " for backfill requirement");
final List<BgReading> lastReadings = BgReading.latest_by_size(check_readings);
Expand Down Expand Up @@ -794,8 +800,14 @@ private static void backFillIfNeeded(Ob1G5CollectionService parent, RxBleConnect
if (ask_for_backfill) {
nextBackFillCheckSize = BACKFILL_CHECK_LARGE;
monitorBackFill(parent, connection);
final long startTime = earliest_timestamp - (Constants.MINUTE_IN_MS * 5);
final long startTime = Math.max(earliest_timestamp - (Constants.MINUTE_IN_MS * 5), sensor.started_at);
final long endTime = latest_timestamp + (Constants.MINUTE_IN_MS * 5);

if (startTime >= endTime) {
UserError.Log.e(TAG, "Cannot process backfill request where start time would be after end time");
return;
}

UserError.Log.d(TAG, "Requesting backfill between: " + JoH.dateTimeText(startTime) + " " + JoH.dateTimeText(endTime));
enqueueUniqueCommand(
BackFillTxMessage.get(getTransmitterID(), startTime, endTime),
Expand Down Expand Up @@ -1175,7 +1187,9 @@ private static void processGlucoseRxMessage(Ob1G5CollectionService parent, final
DexTimeKeeper.updateAge(getTransmitterID(), glucose.timestamp);
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, tsl());
final long rxtimestamp = tsl();

final BgReading bgReading = BgReading.bgReadingInsertFromG5(glucose.glucose, rxtimestamp);
if (bgReading != null) {
try {
bgReading.calculated_value_slope = glucose.getTrend() / Constants.MINUTE_IN_MS; // note this is different to the typical calculated slope, (normally delta)
Expand Down Expand Up @@ -1204,8 +1218,8 @@ private static void processGlucoseRxMessage(Ob1G5CollectionService parent, final
}
}

if (android_wear && wear_broadcast && bgReading != null) {
// emit local broadcast
if (android_wear && wear_broadcast && bgReading != null && bgReading.timestamp == rxtimestamp) {
// emit local broadcast on wear if reading was newly created
BroadcastGlucose.sendLocalBroadcast(bgReading);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ public static BgReading bgReadingInsertFromG5(double calculated_value, long time

// TODO can these methods be unified to reduce duplication
// TODO remember to sync this with wear code base
public static synchronized BgReading bgReadingInsertFromG5(double calculated_value, long timestamp, String sourceInfoAppend) {
public static synchronized BgReading bgReadingInsertFromG5(double calculated_value, final long timestamp, String sourceInfoAppend) {

final Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.eveningoutpost.dexdrip.UtilityModels;

import static com.eveningoutpost.dexdrip.Models.JoH.dateTimeText;
import static com.eveningoutpost.dexdrip.Models.JoH.msSince;
import static com.eveningoutpost.dexdrip.UtilityModels.Constants.MINUTE_IN_MS;

import android.content.Intent;
import android.os.Bundle;

Expand All @@ -8,15 +12,19 @@
import com.eveningoutpost.dexdrip.Models.Calibration;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.Noise;
import com.eveningoutpost.dexdrip.Models.Sensor;
import com.eveningoutpost.dexdrip.Models.UserError;
import com.eveningoutpost.dexdrip.utils.DexCollectionType;
import com.eveningoutpost.dexdrip.utils.PowerStateReceiver;

import lombok.val;

// created by jamorham

public class BroadcastGlucose {

private static final String TAG = "BroadcastGlucose";
private static long lastTimestamp = 0;

public static void sendLocalBroadcast(final BgReading bgReading) {
if (SendXdripBroadcast.enabled()) {
Expand All @@ -27,6 +35,28 @@ public static void sendLocalBroadcast(final BgReading bgReading) {
return;
}

if (Math.abs(bgReading.timestamp - lastTimestamp) < MINUTE_IN_MS) {
val msg = String.format("Refusing to broadcast a reading with close timestamp to last broadcast: %s (%d) vs %s (%d) ", dateTimeText(lastTimestamp), lastTimestamp, dateTimeText(bgReading.timestamp), bgReading.timestamp);
if (bgReading.timestamp == lastTimestamp) {
UserError.Log.d(TAG, msg);
} else {
UserError.Log.wtf(TAG, msg);
}
return;
}

val sensor = Sensor.currentSensor();
if (sensor == null) {
UserError.Log.wtf(TAG, "Refusing to broadcast a reading as no sensor is active");
return;
}

if (bgReading.timestamp <= sensor.started_at) {
UserError.Log.wtf(TAG, "Refusing to broadcast a reading before sensor start: " + dateTimeText(sensor.started_at) + " vs " + dateTimeText(bgReading.timestamp));
return;
}

lastTimestamp = bgReading.timestamp;

BestGlucose.DisplayGlucose dg;

Expand All @@ -50,6 +80,7 @@ public static void sendLocalBroadcast(final BgReading bgReading) {
bundle.putString(Intents.EXTRA_NS_NOISE_LEVEL, bgReading.noise);
if ((Pref.getBoolean("broadcast_data_use_best_glucose", false))
&& !bgReading.isBackfilled()
&& (msSince(bgReading.timestamp) < MINUTE_IN_MS * 5)
&& ((dg = BestGlucose.getDisplayGlucose()) != null)) {

bundle.putDouble(Intents.EXTRA_NOISE, dg.noise);
Expand Down

0 comments on commit 984ac39

Please sign in to comment.