Merge pull request #770 from jotomo/new-bg-processing

Trigger loop only for new and current BGs from the active BG source
This commit is contained in:
Milos Kozak 2018-03-18 20:47:28 +01:00 committed by GitHub
commit 7f0918d147
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 27 deletions

View file

@ -192,7 +192,7 @@ public class DataService extends IntentService {
bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW);
MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP");
MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP", xDripEnabled);
}
private void handleNewDataFromGlimp(Intent intent) {
@ -206,7 +206,7 @@ public class DataService extends IntentService {
bgReading.date = bundle.getLong("myTimestamp");
bgReading.raw = 0;
MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP");
MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP", glimpEnabled);
}
private void handleNewDataFromDexcomG5(Intent intent) {
@ -229,7 +229,7 @@ public class DataService extends IntentService {
bgReading.direction = json.getString("m_trend");
bgReading.date = json.getLong("m_time") * 1000L;
bgReading.raw = 0;
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5");
boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5", dexcomG5Enabled);
if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) {
NSUpload.uploadBg(bgReading);
}
@ -268,7 +268,7 @@ public class DataService extends IntentService {
bgReading.date = json_object.getLong("date");
bgReading.raw = json_object.getDouble("sgv");
MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g");
MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g", mm640gEnabled);
break;
default:
log.debug("Unknown entries type: " + type);
@ -425,7 +425,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
MainApp.getDbHelper().createIfNotExists(bgReading, "NS", nsClientEnabled);
}
if (bundles.containsKey("sgvs")) {
@ -435,7 +435,7 @@ public class DataService extends IntentService {
JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().createIfNotExists(bgReading, "NS");
MainApp.getDbHelper().createIfNotExists(bgReading, "NS", nsClientEnabled);
}
}
} catch (Exception e) {

View file

@ -8,7 +8,6 @@ import android.support.annotation.Nullable;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
@ -241,7 +240,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
log.error("Unhandled exception", e);
}
VirtualPumpPlugin.setFakingStatus(true);
scheduleBgChange(null); // trigger refresh
scheduleBgChange(null, false, false); // trigger refresh
scheduleTemporaryBasalChange();
scheduleTreatmentChange(null);
scheduleExtendedBolusChange();
@ -367,14 +366,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
}
// ------------------- BgReading handling -----------------------
public boolean createIfNotExists(BgReading bgReading, String from) {
public boolean createIfNotExists(BgReading bgReading, String from, boolean isFromActiveBgSource) {
try {
bgReading.date = roundDateToSec(bgReading.date);
BgReading old = getDaoBgReadings().queryForId(bgReading.date);
if (old == null) {
getDaoBgReadings().create(bgReading);
log.debug("BG: New record from: " + from + " " + bgReading.toString());
scheduleBgChange(bgReading);
scheduleBgChange(bgReading, true, isFromActiveBgSource);
return true;
}
if (!old.isEqual(bgReading)) {
@ -382,7 +381,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
old.copyFrom(bgReading);
getDaoBgReadings().update(old);
log.debug("BG: Updating record from: " + from + " New data: " + old.toString());
scheduleBgChange(bgReading);
scheduleBgChange(bgReading, false, isFromActiveBgSource);
return false;
}
} catch (SQLException e) {
@ -400,11 +399,11 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
}
}
private static void scheduleBgChange(@Nullable final BgReading bgReading) {
private static void scheduleBgChange(@Nullable final BgReading bgReading, boolean isNew, boolean isFromActiveBgSource) {
class PostRunnable implements Runnable {
public void run() {
log.debug("Firing EventNewBg");
MainApp.bus().post(new EventNewBG(bgReading));
MainApp.bus().post(new EventNewBG(bgReading, isNew, isFromActiveBgSource));
scheduledBgPost = null;
}
}

View file

@ -10,8 +10,17 @@ import info.nightscout.androidaps.db.BgReading;
public class EventNewBG extends EventLoop {
@Nullable
public final BgReading bgReading;
public final boolean isNew;
public final boolean isFromActiveBgSource;
public EventNewBG(BgReading bgReading) {
/** Whether the BgReading is current (enough to use for treatment decisions). */
public boolean isCurrent() {
return bgReading != null && bgReading.date + 9 * 60 * 1000 > System.currentTimeMillis();
}
public EventNewBG(@Nullable BgReading bgReading, boolean isNew, boolean isFromActiveBgSource) {
this.bgReading = bgReading;
this.isNew = isNew;
this.isFromActiveBgSource = isFromActiveBgSource;
}
}

View file

@ -531,6 +531,8 @@ public class IobCobCalculatorPlugin implements PluginBase {
@Subscribe
public void onEventNewBG(EventNewBG ev) {
if (!ev.isFromActiveBgSource)
return;
if (this != getPlugin()) {
log.debug("Ignoring event for non default instance");
return;

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.interfaces.APSInterface;
@ -159,8 +160,12 @@ public class LoopPlugin implements PluginBase {
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
if (ev.cause instanceof EventNewBG) {
invoke(ev.getClass().getSimpleName() + "(" + ev.cause.getClass().getSimpleName() + ")", true);
if (!(ev.cause instanceof EventNewBG))
return;
EventNewBG bgEv = (EventNewBG) ev.cause;
if (bgEv.isNew && bgEv.isFromActiveBgSource && bgEv.isCurrent()) {
invoke("New BG", true);
}
}

View file

@ -142,14 +142,16 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com
@Subscribe
public void onStatusEvent(final EventNewBG e) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
calculateInsulin();
}
});
if (e.isFromActiveBgSource && e.isNew && e.isCurrent()) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
calculateInsulin();
}
});
}
}
@Subscribe

View file

@ -255,7 +255,8 @@ public class PersistentNotificationPlugin implements PluginBase {
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
updateNotification();
if (ev.isFromActiveBgSource && ev.isNew && ev.isCurrent())
updateNotification();
}
@Subscribe

View file

@ -182,7 +182,8 @@ public class WearPlugin implements PluginBase {
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
sendDataToWatch(true, true, true);
if (ev.isFromActiveBgSource)
sendDataToWatch(true, true, true);
}
@Subscribe

View file

@ -235,7 +235,8 @@ public class StatuslinePlugin implements PluginBase {
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
sendStatus();
if (ev.isFromActiveBgSource && ev.isNew && ev.isCurrent())
sendStatus();
}
@Subscribe