From 98c933af5772c863164f6f2687f96fa8bdcade2a Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:10:10 +0200 Subject: [PATCH 01/20] BgReading: add sourcePlugin and filtered fields. --- .../nightscout/androidaps/db/BgReading.java | 18 ++++++++---------- .../androidaps/db/DatabaseHelper.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 5ddb7a80b2..79b5236162 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -38,6 +38,10 @@ public class BgReading implements DataPointWithLabelInterface { public String direction; @DatabaseField public double raw; + @DatabaseField + public boolean filtered; + @DatabaseField + public String sourcePlugin; @DatabaseField public int source = Source.NONE; @@ -120,19 +124,11 @@ public class BgReading implements DataPointWithLabelInterface { ", value=" + value + ", direction=" + direction + ", raw=" + raw + + ", filtered=" + filtered + + ", sourcePlugin=" + sourcePlugin + '}'; } - public boolean isDataChanging(BgReading other) { - if (date != other.date) { - log.error("Comparing different"); - return false; - } - if (value != other.value) - return true; - return false; - } - public boolean isEqual(BgReading other) { if (date != other.date) { log.error("Comparing different"); @@ -158,6 +154,8 @@ public class BgReading implements DataPointWithLabelInterface { raw = other.raw; direction = other.direction; _id = other._id; + sourcePlugin = other.sourcePlugin; + filtered = other.filtered; } // ------------------ DataPointWithLabelInterface ------------------ diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 1c6cfddaf5..12b8b46a85 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -117,12 +117,27 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); TableUtils.createTableIfNotExists(connectionSource, TDD.class); + + // soft migration without changing DB version + createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, + "filtered", "integer"); + createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, + "sourcePlugin", "integer"); + } catch (SQLException e) { log.error("Can't create database", e); throw new RuntimeException(e); } } + private void createRowIfNotExists(Dao dao, String table, String name, String type) { + try { + dao.executeRaw("ALTER TABLE `" + table + "` ADD CoLUMN `" + name + " " + type); + } catch (SQLException e) { + // row already exists + } + } + @Override public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { From 6abad56b1f814377b7024831b808df4e8477a6ea Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:11:57 +0200 Subject: [PATCH 02/20] Replace BgSourceInterface.advancedFilteringSupported. Replaced by 'filtered' property on BgReading. --- .../androidaps/Services/DataService.java | 139 +++++++++--------- .../androidaps/data/ConstraintChecker.java | 19 +-- .../interfaces/BgSourceInterface.java | 4 +- .../interfaces/ConstraintsInterface.java | 4 - .../interfaces/PluginDescription.java | 6 + .../ConfigBuilder/ConfigBuilderPlugin.java | 8 +- .../ConstraintsSafety/SafetyPlugin.java | 11 -- .../plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 11 +- .../plugins/Source/SourceDexcomG5Plugin.java | 4 - .../plugins/Source/SourceGlimpPlugin.java | 4 - .../plugins/Source/SourceMM640gPlugin.java | 4 - .../plugins/Source/SourceNSClientPlugin.java | 17 --- .../plugins/Source/SourcePoctechPlugin.java | 5 - .../plugins/Source/SourceXdripPlugin.java | 11 -- .../interfaces/ConstraintsCheckerTest.java | 18 +-- .../ConstraintsSafety/SafetyPluginTest.java | 19 +-- .../Source/SourceDexcomG5PluginTest.java | 8 +- .../plugins/Source/SourceGlimpPluginTest.java | 8 +- .../Source/SourceMM640gPluginTest.java | 8 +- .../Source/SourceNSClientPluginTest.java | 8 +- .../plugins/Source/SourceXdripPluginTest.java | 8 +- 21 files changed, 124 insertions(+), 200 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index 9f3873cb93..e7dd87ab44 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -12,6 +12,8 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Objects; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; @@ -60,56 +62,58 @@ public class DataService extends IntentService { public DataService() { super("DataService"); - registerBus(); + MainApp.subscribe(this); } @Override protected void onHandleIntent(final Intent intent) { + if (intent == null) + return; if (Config.logFunctionCalls) log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); - if (ConfigBuilderPlugin.getPlugin().getActiveBgSource() == null) { + if (ConfigBuilderPlugin.getActiveBgSource() == null) { xDripEnabled = true; nsClientEnabled = false; mm640gEnabled = false; glimpEnabled = false; dexcomG5Enabled = false; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceXdripPlugin.class)) { xDripEnabled = true; nsClientEnabled = false; mm640gEnabled = false; glimpEnabled = false; dexcomG5Enabled = false; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceNSClientPlugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceNSClientPlugin.class)) { xDripEnabled = false; nsClientEnabled = true; mm640gEnabled = false; glimpEnabled = false; dexcomG5Enabled = false; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceMM640gPlugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceMM640gPlugin.class)) { xDripEnabled = false; nsClientEnabled = false; mm640gEnabled = true; glimpEnabled = false; dexcomG5Enabled = false; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceGlimpPlugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceGlimpPlugin.class)) { xDripEnabled = false; nsClientEnabled = false; mm640gEnabled = false; glimpEnabled = true; dexcomG5Enabled = false; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourceDexcomG5Plugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourceDexcomG5Plugin.class)) { xDripEnabled = false; nsClientEnabled = false; mm640gEnabled = false; glimpEnabled = false; dexcomG5Enabled = true; poctechEnabled = false; - } else if (ConfigBuilderPlugin.getPlugin().getActiveBgSource().getClass().equals(SourcePoctechPlugin.class)) { + } else if (ConfigBuilderPlugin.getActiveBgSource().getClass().equals(SourcePoctechPlugin.class)) { xDripEnabled = false; nsClientEnabled = false; mm640gEnabled = false; @@ -126,55 +130,53 @@ public class DataService extends IntentService { acceptNSData = acceptNSData || bundles.getBoolean("islocal"); } - - if (intent != null) { - final String action = intent.getAction(); - if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { - if (xDripEnabled) { - handleNewDataFromXDrip(intent); - } - } else if (Intents.NS_EMULATOR.equals(action)) { - if (mm640gEnabled) { - handleNewDataFromMM640g(intent); - } - } else if (Intents.GLIMP_BG.equals(action)) { - if (glimpEnabled) { - handleNewDataFromGlimp(intent); - } - } else if (Intents.DEXCOMG5_BG.equals(action)) { - if (dexcomG5Enabled) { - handleNewDataFromDexcomG5(intent); - } - } else if (Intents.POCTECH_BG.equals(action)) { - if (poctechEnabled) { - handleNewDataFromPoctech(intent); - } - } else if (Intents.ACTION_NEW_SGV.equals(action)) { - if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true)) - handleNewDataFromNSClient(intent); - // Objectives 0 - ObjectivesPlugin.bgIsAvailableInNS = true; - ObjectivesPlugin.saveProgress(); - } else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_DEVICESTATUS.equals(action)) { - // always handle Profile if NSProfile is enabled without looking at nsUploadOnly - handleNewDataFromNSClient(intent); - } else if (acceptNSData && - (Intents.ACTION_NEW_TREATMENT.equals(action) || - Intents.ACTION_CHANGED_TREATMENT.equals(action) || - Intents.ACTION_REMOVED_TREATMENT.equals(action) || - Intents.ACTION_NEW_STATUS.equals(action) || - Intents.ACTION_NEW_DEVICESTATUS.equals(action) || - Intents.ACTION_NEW_FOOD.equals(action) || - Intents.ACTION_CHANGED_FOOD.equals(action) || - Intents.ACTION_REMOVED_FOOD.equals(action) || - Intents.ACTION_NEW_CAL.equals(action) || - Intents.ACTION_NEW_MBG.equals(action)) - ) { - handleNewDataFromNSClient(intent); - } else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) { - handleNewSMS(intent); + final String action = intent.getAction(); + if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { + if (xDripEnabled) { + handleNewDataFromXDrip(intent); } + } else if (Intents.NS_EMULATOR.equals(action)) { + if (mm640gEnabled) { + handleNewDataFromMM640g(intent); + } + } else if (Intents.GLIMP_BG.equals(action)) { + if (glimpEnabled) { + handleNewDataFromGlimp(intent); + } + } else if (Intents.DEXCOMG5_BG.equals(action)) { + if (dexcomG5Enabled) { + handleNewDataFromDexcomG5(intent); + } + } else if (Intents.POCTECH_BG.equals(action)) { + if (poctechEnabled) { + handleNewDataFromPoctech(intent); + } + } else if (Intents.ACTION_NEW_SGV.equals(action)) { + if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true)) + handleNewDataFromNSClient(intent); + // Objectives 0 + ObjectivesPlugin.bgIsAvailableInNS = true; + ObjectivesPlugin.saveProgress(); + } else if (isNSProfile && Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_DEVICESTATUS.equals(action)) { + // always handle Profile if NSProfile is enabled without looking at nsUploadOnly + handleNewDataFromNSClient(intent); + } else if (acceptNSData && + (Intents.ACTION_NEW_TREATMENT.equals(action) || + Intents.ACTION_CHANGED_TREATMENT.equals(action) || + Intents.ACTION_REMOVED_TREATMENT.equals(action) || + Intents.ACTION_NEW_STATUS.equals(action) || + Intents.ACTION_NEW_DEVICESTATUS.equals(action) || + Intents.ACTION_NEW_FOOD.equals(action) || + Intents.ACTION_CHANGED_FOOD.equals(action) || + Intents.ACTION_REMOVED_FOOD.equals(action) || + Intents.ACTION_NEW_CAL.equals(action) || + Intents.ACTION_NEW_MBG.equals(action)) + ) { + handleNewDataFromNSClient(intent); + } else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) { + handleNewSMS(intent); } + if (Config.logFunctionCalls) log.debug("onHandleIntent exit " + intent); DataReceiver.completeWakefulIntent(intent); @@ -195,16 +197,7 @@ public class DataService extends IntentService { @Override public void onDestroy() { super.onDestroy(); - MainApp.bus().unregister(this); - } - - private void registerBus() { - try { - MainApp.bus().unregister(this); - } catch (RuntimeException x) { - // Ignore - } - MainApp.bus().register(this); + MainApp.unsubscribe(this); } private void handleNewDataFromXDrip(Intent intent) { @@ -217,8 +210,9 @@ public class DataService extends IntentService { bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - String source = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "no Source specified"); - SourceXdripPlugin.getPlugin().setSource(source); + bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().pluginDescription.getUserfriendlyName(); + bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); + MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); } @@ -232,6 +226,8 @@ public class DataService extends IntentService { bgReading.direction = bundle.getString("myTrend"); bgReading.date = bundle.getLong("myTimestamp"); bgReading.raw = 0; + bgReading.filtered = false; + bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().pluginDescription.getUserfriendlyName(); MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP"); } @@ -256,6 +252,8 @@ public class DataService extends IntentService { bgReading.direction = json.getString("m_trend"); bgReading.date = json.getLong("m_time") * 1000L; bgReading.raw = 0; + bgReading.filtered = true; + bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().pluginDescription.getUserfriendlyName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { NSUpload.uploadBg(bgReading); @@ -264,9 +262,8 @@ public class DataService extends IntentService { NSUpload.sendToXdrip(bgReading); } } - } catch (JSONException e) { - e.printStackTrace(); + log.error("Unhandled exception", e); } } @@ -329,6 +326,8 @@ public class DataService extends IntentService { bgReading.direction = json_object.getString("direction"); bgReading.date = json_object.getLong("date"); bgReading.raw = json_object.getDouble("sgv"); + bgReading.filtered = true; + bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().pluginDescription.getUserfriendlyName(); MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); break; @@ -545,7 +544,7 @@ public class DataService extends IntentService { MainApp.getDbHelper().deleteProfileSwitchById(_id); } - private void handleTreatmentFromNS(JSONObject json, Intent intent) throws JSONException { + private void handleTreatmentFromNS(JSONObject json, Intent intent) { // new DB model int mode = Intents.ACTION_NEW_TREATMENT.equals(intent.getAction()) ? EventNsTreatment.ADD : EventNsTreatment.UPDATE; double insulin = JsonHelper.safeGetDouble(json, "insulin"); diff --git a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java index bfbbfedbd1..272158f2ed 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java +++ b/app/src/main/java/info/nightscout/androidaps/data/ConstraintChecker.java @@ -42,10 +42,6 @@ public class ConstraintChecker implements ConstraintsInterface { return isSMBModeEnabled(new Constraint<>(true)); } - public Constraint isAdvancedFilteringEnabled() { - return isAdvancedFilteringEnabled(new Constraint<>(true)); - } - public Constraint getMaxBasalAllowed(Profile profile) { return applyBasalConstraints(new Constraint<>(Constants.REALLYHIGHBASALRATE), profile); } @@ -69,7 +65,7 @@ public class ConstraintChecker implements ConstraintsInterface { @Override public Constraint isLoopInvocationAllowed(Constraint value) { - ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginType.CONSTRAINTS)) continue; @@ -81,7 +77,7 @@ public class ConstraintChecker implements ConstraintsInterface { @Override public Constraint isClosedLoopAllowed(Constraint value) { - ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); + ArrayList constraintsPlugins = MainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); for (PluginBase p : constraintsPlugins) { ConstraintsInterface constraint = (ConstraintsInterface) p; if (!p.isEnabled(PluginType.CONSTRAINTS)) continue; @@ -126,17 +122,6 @@ public class ConstraintChecker implements ConstraintsInterface { return value; } - @Override - public Constraint isAdvancedFilteringEnabled(Constraint value) { - ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); - for (PluginBase p : constraintsPlugins) { - ConstraintsInterface constraint = (ConstraintsInterface) p; - if (!p.isEnabled(PluginType.CONSTRAINTS)) continue; - constraint.isAdvancedFilteringEnabled(value); - } - return value; - } - @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { ArrayList constraintsPlugins = mainApp.getSpecificPluginsListByInterface(ConstraintsInterface.class); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java index a45ab083e7..429bfff8fe 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java @@ -3,6 +3,4 @@ package info.nightscout.androidaps.interfaces; /** * Created by mike on 20.06.2016. */ -public interface BgSourceInterface { - boolean advancedFilteringSupported(); -} +public interface BgSourceInterface {} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java index 820170dc0f..4404379196 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/ConstraintsInterface.java @@ -27,10 +27,6 @@ public interface ConstraintsInterface { return value; } - default Constraint isAdvancedFilteringEnabled(Constraint value) { - return value; - } - default Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { return absoluteRate; } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java index 1634fc672d..4b073443bb 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.interfaces; +import info.nightscout.androidaps.MainApp; + public class PluginDescription { PluginType mainType = PluginType.GENERAL; String fragmentClass = null; @@ -87,4 +89,8 @@ public class PluginDescription { public PluginType getType() { return mainType; } + + public String getUserfriendlyName() { + return MainApp.gs(pluginName); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 5bb810274e..e28f0782da 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -65,12 +65,11 @@ public class ConfigBuilderPlugin extends PluginBase { return configBuilderPlugin; } - private BgSourceInterface activeBgSource; + private static BgSourceInterface activeBgSource; private static PumpInterface activePump; private static ProfileInterface activeProfile; private static TreatmentsInterface activeTreatments; private static APSInterface activeAPS; - private static LoopPlugin activeLoop; private static InsulinInterface activeInsulin; private static SensitivityInterface activeSensitivity; @@ -249,7 +248,7 @@ public class ConfigBuilderPlugin extends PluginBase { return commandQueue; } - public BgSourceInterface getActiveBgSource() { + public static BgSourceInterface getActiveBgSource() { return activeBgSource; } @@ -329,9 +328,6 @@ public class ConfigBuilderPlugin extends PluginBase { } this.setFragmentVisiblities(((PluginBase) activePump).getName(), pluginsInCategory, PluginType.PUMP); - // PluginType.LOOP - activeLoop = this.determineActivePlugin(PluginType.LOOP); - // PluginType.TREATMENT activeTreatments = this.determineActivePlugin(PluginType.TREATMENT); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java index 3d17970edc..d68d4a2edc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPlugin.java @@ -92,17 +92,6 @@ public class SafetyPlugin extends PluginBase implements ConstraintsInterface { return value; } - @Override - public Constraint isAdvancedFilteringEnabled(Constraint value) { - BgSourceInterface bgSource = MainApp.getConfigBuilder().getActiveBgSource(); - - if (bgSource != null) { - if (!bgSource.advancedFilteringSupported()) - value.set(false, MainApp.gs(R.string.smbalwaysdisabled), this); - } - return value; - } - @Override public Constraint applyBasalConstraints(Constraint absoluteRate, Profile profile) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index f28a6eff8e..54bfd9d780 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -14,6 +14,8 @@ import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.Profile; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.interfaces.APSInterface; @@ -104,6 +106,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { } GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); + BgReading bgReading = DatabaseHelper.actualBg(); Profile profile = MainApp.getConfigBuilder().getProfile(); PumpInterface pump = ConfigBuilderPlugin.getActivePump(); @@ -121,7 +124,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { return; } - if (glucoseStatus == null) { + if (glucoseStatus == null || bgReading == null) { MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata))); if (Config.logAPSResult) log.debug(MainApp.gs(R.string.openapsma_noglucosedata)); @@ -189,10 +192,6 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); inputConstraints.copyReasons(smbAllowed); - Constraint advancedFiltering = new Constraint<>(!tempBasalFallback); - MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering); - inputConstraints.copyReasons(advancedFiltering); - Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); Profiler.log(log, "SMB data gathering", start); @@ -202,7 +201,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { lastAutosensResult.ratio, //autosensDataRatio isTempTarget, smbAllowed.value(), - advancedFiltering.value() + bgReading.filtered ); } catch (JSONException e) { log.error(e.getMessage()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index af040c8c90..64fa05fca8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -33,8 +33,4 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac ); } - @Override - public boolean advancedFilteringSupported() { - return true; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index 38b891ff65..bc3bb0dcd3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -28,8 +28,4 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { ); } - @Override - public boolean advancedFilteringSupported() { - return false; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index 041b084efd..e2a73c46fc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -27,8 +27,4 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface ); } - @Override - public boolean advancedFilteringSupported() { - return false; - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index c99fbcb9bb..d5f30b1a37 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -20,9 +20,6 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac return plugin; } - private long lastBGTimeStamp = 0; - private boolean isAdvancedFilteringEnabled = false; - private SourceNSClientPlugin() { super(new PluginDescription() .mainType(PluginType.BGSOURCE) @@ -34,18 +31,4 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac ); } - @Override - public boolean advancedFilteringSupported() { - return isAdvancedFilteringEnabled; - } - - public void detectSource(String source, long timeStamp) { - if (timeStamp > lastBGTimeStamp) { - if (source.contains("G5 Native") || source.contains("AndroidAPS-DexcomG5")) - isAdvancedFilteringEnabled = true; - else - isAdvancedFilteringEnabled = false; - lastBGTimeStamp = timeStamp; - } - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index 13cb99d392..9f15bd9b29 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -31,9 +31,4 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface ); } - @Override - public boolean advancedFilteringSupported() { - return false; - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index fa61c9ea61..a69ff84c2d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -13,8 +13,6 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { private static SourceXdripPlugin plugin = null; - boolean advancedFiltering; - public static SourceXdripPlugin getPlugin() { if (plugin == null) plugin = new SourceXdripPlugin(); @@ -29,13 +27,4 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { .description(R.string.description_source_xdrip) ); } - - @Override - public boolean advancedFilteringSupported() { - return advancedFiltering; - } - - public void setSource(String source) { - this.advancedFiltering = source.contains("G5 Native"); - } } diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java index 655cc23282..6b584a365d 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -111,15 +111,15 @@ public class ConstraintsCheckerTest { Assert.assertEquals(Boolean.FALSE, c.value()); } - @Test - public void isAdvancedFilteringEnabledTest() throws Exception { - when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); - - Constraint c = constraintChecker.isAdvancedFilteringEnabled(); - Assert.assertEquals(true, c.getReasonList().size() == 1); // Safety - Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 1); // Safety - Assert.assertEquals(Boolean.FALSE, c.value()); - } +// @Test +// public void isAdvancedFilteringEnabledTest() throws Exception { +// when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); +// +// Constraint c = constraintChecker.isAdvancedFilteringEnabled(); +// Assert.assertEquals(true, c.getReasonList().size() == 1); // Safety +// Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 1); // Safety +// Assert.assertEquals(Boolean.FALSE, c.value()); +// } @Test public void isSMBModeEnabledTest() throws Exception { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java index 77a91046ab..ee3359c84e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java @@ -89,15 +89,16 @@ public class SafetyPluginTest { Assert.assertEquals(Boolean.FALSE, c.value()); } - @Test - public void bgsourceShouldPreventSMBAlways() throws Exception { - when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); - - Constraint c = new Constraint<>(true); - c = safetyPlugin.isAdvancedFilteringEnabled(c); - Assert.assertEquals("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering", c.getReasons()); - Assert.assertEquals(Boolean.FALSE, c.value()); - } + // TODO +// @Test +// public void bgsourceShouldPreventSMBAlways() throws Exception { +// when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); +// +// Constraint c = new Constraint<>(true); +// c = safetyPlugin.isAdvancedFilteringEnabled(c); +// Assert.assertEquals("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering", c.getReasons()); +// Assert.assertEquals(Boolean.FALSE, c.value()); +// } @Test public void basalRateShouldBeLimited() throws Exception { diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java index 042aaeadbf..7f160ce1d2 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java @@ -15,8 +15,8 @@ public class SourceDexcomG5PluginTest { Assert.assertNotEquals(null, SourceDexcomG5Plugin.getPlugin()); } - @Test - public void advancedFilteringSupported() { - Assert.assertEquals(true, SourceDexcomG5Plugin.getPlugin().advancedFilteringSupported()); - } +// @Test +// public void advancedFilteringSupported() { +// Assert.assertEquals(true, SourceDexcomG5Plugin.getPlugin().advancedFilteringSupported()); +// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java index 045552c8da..0910442be8 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java @@ -15,8 +15,8 @@ public class SourceGlimpPluginTest { Assert.assertNotEquals(null, SourceGlimpPlugin.getPlugin()); } - @Test - public void advancedFilteringSupported() { - Assert.assertEquals(false, SourceGlimpPlugin.getPlugin().advancedFilteringSupported()); - } +// @Test +// public void advancedFilteringSupported() { +// Assert.assertEquals(false, SourceGlimpPlugin.getPlugin().advancedFilteringSupported()); +// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java index 9bbe86da40..e40f9bb492 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java @@ -15,8 +15,8 @@ public class SourceMM640gPluginTest { Assert.assertNotEquals(null, SourceMM640gPlugin.getPlugin()); } - @Test - public void advancedFilteringSupported() { - Assert.assertEquals(false, SourceMM640gPlugin.getPlugin().advancedFilteringSupported()); - } +// @Test +// public void advancedFilteringSupported() { +// Assert.assertEquals(false, SourceMM640gPlugin.getPlugin().advancedFilteringSupported()); +// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java index 3ca70a900b..fecf0712d8 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java @@ -13,8 +13,8 @@ public class SourceNSClientPluginTest { Assert.assertNotEquals(null, SourceNSClientPlugin.getPlugin()); } - @Test - public void advancedFilteringSupported() { - Assert.assertEquals(false, SourceNSClientPlugin.getPlugin().advancedFilteringSupported()); - } +// @Test +// public void advancedFilteringSupported() { +// Assert.assertEquals(false, SourceNSClientPlugin.getPlugin().advancedFilteringSupported()); +// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java index f85719dc47..33d9bc45b1 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java @@ -15,8 +15,8 @@ public class SourceXdripPluginTest { Assert.assertNotEquals(null, SourceXdripPlugin.getPlugin()); } - @Test - public void advancedFilteringSupported() { - Assert.assertEquals(false, SourceXdripPlugin.getPlugin().advancedFilteringSupported()); - } +// @Test +// public void advancedFilteringSupported() { +// Assert.assertEquals(false, SourceXdripPlugin.getPlugin().advancedFilteringSupported()); +// } } \ No newline at end of file From 7a331e0c944ad238deda40a35e10b57adae9d5b1 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:13:00 +0200 Subject: [PATCH 03/20] Don't loop with readings coming from non-active BG source. --- .../androidaps/plugins/Loop/LoopPlugin.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index 480646ccfc..84c0d8b54d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -20,6 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; +import java.util.Objects; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; @@ -32,7 +33,6 @@ import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.events.Event; import info.nightscout.androidaps.events.EventNewBG; -import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; @@ -57,11 +57,9 @@ import info.nightscout.utils.SP; public class LoopPlugin extends PluginBase { private static Logger log = LoggerFactory.getLogger(LoopPlugin.class); - public static final String CHANNEL_ID = "AndroidAPS-Openloop"; - - long lastBgTriggeredRun = 0; - - protected static LoopPlugin loopPlugin; + private static final String CHANNEL_ID = "AndroidAPS-Openloop"; + private long lastBgTriggeredRun = 0; + private static LoopPlugin loopPlugin; @NonNull public static LoopPlugin getPlugin() { @@ -157,15 +155,20 @@ public class LoopPlugin extends PluginBase { // already looped with that value return; } + PluginBase bgSource = (PluginBase) ConfigBuilderPlugin.getActiveBgSource(); + if (bgSource == null) { + // no BG source active + return; + } + if (!Objects.equals(bgReading.sourcePlugin, bgSource.pluginDescription.getUserfriendlyName())) { + // reading not from active BG source (likely coming in from NS) + return; + } lastBgTriggeredRun = bgReading.date; invoke("AutosenseCalculation for " + bgReading, true); } - public long suspendedTo() { - return loopSuspendedTill; - } - public void suspendTo(long endTime) { loopSuspendedTill = endTime; isSuperBolus = false; From 922f9ff83d080e604e540e776d9d3b02e72c59c5 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:28:05 +0200 Subject: [PATCH 04/20] Refactor DataService to delegate BG processing to BgSources. --- .../androidaps/Services/DataService.java | 157 ++---------------- .../interfaces/BgSourceInterface.java | 6 +- .../interfaces/PluginDescription.java | 6 - .../androidaps/plugins/Loop/LoopPlugin.java | 2 +- .../plugins/Source/SourceDexcomG5Plugin.java | 44 +++++ .../plugins/Source/SourceGlimpPlugin.java | 17 ++ .../plugins/Source/SourceMM640gPlugin.java | 49 ++++++ .../plugins/Source/SourceNSClientPlugin.java | 7 + .../plugins/Source/SourcePoctechPlugin.java | 46 +++++ .../plugins/Source/SourceXdripPlugin.java | 21 +++ 10 files changed, 202 insertions(+), 153 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index e7dd87ab44..2b6d1c1604 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -23,6 +23,8 @@ import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.events.EventNsTreatment; +import info.nightscout.androidaps.interfaces.BgSourceInterface; +import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; @@ -133,23 +135,23 @@ public class DataService extends IntentService { final String action = intent.getAction(); if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { if (xDripEnabled) { - handleNewDataFromXDrip(intent); + processNewBgIntent(SourceXdripPlugin.getPlugin(), intent); } } else if (Intents.NS_EMULATOR.equals(action)) { if (mm640gEnabled) { - handleNewDataFromMM640g(intent); + processNewBgIntent(SourceMM640gPlugin.getPlugin(), intent); } } else if (Intents.GLIMP_BG.equals(action)) { if (glimpEnabled) { - handleNewDataFromGlimp(intent); + processNewBgIntent(SourceGlimpPlugin.getPlugin(), intent); } } else if (Intents.DEXCOMG5_BG.equals(action)) { if (dexcomG5Enabled) { - handleNewDataFromDexcomG5(intent); + processNewBgIntent(SourceDexcomG5Plugin.getPlugin(), intent); } } else if (Intents.POCTECH_BG.equals(action)) { if (poctechEnabled) { - handleNewDataFromPoctech(intent); + processNewBgIntent(SourcePoctechPlugin.getPlugin(), intent); } } else if (Intents.ACTION_NEW_SGV.equals(action)) { if (nsClientEnabled || SP.getBoolean(R.string.key_ns_autobackfill, true)) @@ -200,153 +202,18 @@ public class DataService extends IntentService { MainApp.unsubscribe(this); } - private void handleNewDataFromXDrip(Intent intent) { + private void processNewBgIntent(BgSourceInterface bgSource, Intent intent) { + if (Config.logIncommingData) + log.debug("Got intent: " + intent.getAction()); Bundle bundle = intent.getExtras(); if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); - bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); - bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); - bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); - - MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); - } - - private void handleNewDataFromGlimp(Intent intent) { - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - bgReading.value = bundle.getDouble("mySGV"); - bgReading.direction = bundle.getString("myTrend"); - bgReading.date = bundle.getLong("myTimestamp"); - bgReading.raw = 0; - bgReading.filtered = false; - bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - - MainApp.getDbHelper().createIfNotExists(bgReading, "GLIMP"); - } - - private void handleNewDataFromDexcomG5(Intent intent) { - // onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle - - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - String data = bundle.getString("data"); - log.debug("Received Dexcom Data", data); - - try { - JSONArray jsonArray = new JSONArray(data); - log.debug("Received Dexcom Data size:" + jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject json = jsonArray.getJSONObject(i); - bgReading.value = json.getInt("m_value"); - bgReading.direction = json.getString("m_trend"); - bgReading.date = json.getLong("m_time") * 1000L; - bgReading.raw = 0; - bgReading.filtered = true; - bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().pluginDescription.getUserfriendlyName(); - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); - } - } - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - } - - private void handleNewDataFromPoctech(Intent intent) { - - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - BgReading bgReading = new BgReading(); - - String data = bundle.getString("data"); - log.debug("Received Poctech Data", data); - - try { - JSONArray jsonArray = new JSONArray(data); - log.debug("Received Poctech Data size:" + jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject json = jsonArray.getJSONObject(i); - bgReading.value = json.getDouble("current"); - bgReading.direction = json.getString("direction"); - bgReading.date = json.getLong("date"); - bgReading.raw = json.getDouble("raw"); - if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) - bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "Poctech"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); - } - } - - } catch (JSONException e) { - e.printStackTrace(); - } - } - - private void handleNewDataFromMM640g(Intent intent) { - Bundle bundle = intent.getExtras(); - if (bundle == null) return; - - final String collection = bundle.getString("collection"); - if (collection == null) return; - - if (collection.equals("entries")) { - final String data = bundle.getString("data"); - - if ((data != null) && (data.length() > 0)) { - try { - final JSONArray json_array = new JSONArray(data); - for (int i = 0; i < json_array.length(); i++) { - final JSONObject json_object = json_array.getJSONObject(i); - final String type = json_object.getString("type"); - switch (type) { - case "sgv": - BgReading bgReading = new BgReading(); - - bgReading.value = json_object.getDouble("sgv"); - bgReading.direction = json_object.getString("direction"); - bgReading.date = json_object.getLong("date"); - bgReading.raw = json_object.getDouble("sgv"); - bgReading.filtered = true; - bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().pluginDescription.getUserfriendlyName(); - - MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); - break; - default: - log.debug("Unknown entries type: " + type); - } - } - } catch (JSONException e) { - log.error("Got JSON exception: " + e); - } - } - } + bgSource.processNewData(bundle); } private void handleNewDataFromNSClient(Intent intent) { Bundle bundles = intent.getExtras(); if (bundles == null) return; - if (Config.logIncommingData) - log.debug("Got intent: " + intent.getAction()); + if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java index 429bfff8fe..c6cb7c1b8e 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java @@ -1,6 +1,10 @@ package info.nightscout.androidaps.interfaces; +import android.os.Bundle; + /** * Created by mike on 20.06.2016. */ -public interface BgSourceInterface {} +public interface BgSourceInterface { + void processNewData(Bundle bundle); +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java index 4b073443bb..1634fc672d 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginDescription.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.interfaces; -import info.nightscout.androidaps.MainApp; - public class PluginDescription { PluginType mainType = PluginType.GENERAL; String fragmentClass = null; @@ -89,8 +87,4 @@ public class PluginDescription { public PluginType getType() { return mainType; } - - public String getUserfriendlyName() { - return MainApp.gs(pluginName); - } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index 84c0d8b54d..e4a5be19b2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -160,7 +160,7 @@ public class LoopPlugin extends PluginBase { // no BG source active return; } - if (!Objects.equals(bgReading.sourcePlugin, bgSource.pluginDescription.getUserfriendlyName())) { + if (!Objects.equals(bgReading.sourcePlugin, bgSource.getName())) { // reading not from active BG source (likely coming in from NS) return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index 64fa05fca8..9327b31754 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -1,17 +1,30 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; /** * Created by mike on 28.11.2017. */ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceDexcomG5Plugin.class); private static SourceDexcomG5Plugin plugin = null; @@ -33,4 +46,35 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + String data = bundle.getString("data"); + // onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle + log.debug("Received Dexcom Data", data); + + try { + JSONArray jsonArray = new JSONArray(data); + log.debug("Received Dexcom Data size:" + jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject json = jsonArray.getJSONObject(i); + bgReading.value = json.getInt("m_value"); + bgReading.direction = json.getString("m_trend"); + bgReading.date = json.getLong("m_time") * 1000L; + bgReading.raw = 0; + bgReading.filtered = true; + bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().getName(); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); + if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } + } + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index bc3bb0dcd3..a94caa0155 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -1,6 +1,10 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -28,4 +32,17 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + bgReading.value = bundle.getDouble("mySGV"); + bgReading.direction = bundle.getString("myTrend"); + bgReading.date = bundle.getLong("myTimestamp"); + bgReading.raw = 0; + bgReading.filtered = false; + bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().getName(); + + MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index e2a73c46fc..8b7b08e9fb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -1,6 +1,16 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -10,6 +20,8 @@ import info.nightscout.androidaps.interfaces.PluginType; * Created by mike on 05.08.2016. */ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceMM640gPlugin.class); + private static SourceMM640gPlugin plugin = null; public static SourceMM640gPlugin getPlugin() { @@ -27,4 +39,41 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface ); } + @Override + public void processNewData(Bundle bundle) { + final String collection = bundle.getString("collection"); + if (collection == null) return; + + if (collection.equals("entries")) { + final String data = bundle.getString("data"); + + if ((data != null) && (data.length() > 0)) { + try { + final JSONArray json_array = new JSONArray(data); + for (int i = 0; i < json_array.length(); i++) { + final JSONObject json_object = json_array.getJSONObject(i); + final String type = json_object.getString("type"); + switch (type) { + case "sgv": + BgReading bgReading = new BgReading(); + + bgReading.value = json_object.getDouble("sgv"); + bgReading.direction = json_object.getString("direction"); + bgReading.date = json_object.getLong("date"); + bgReading.raw = json_object.getDouble("sgv"); + bgReading.filtered = true; + bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().getName(); + + MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); + break; + default: + log.debug("Unknown entries type: " + type); + } + } + } catch (JSONException e) { + log.error("Got JSON exception: " + e); + } + } + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index d5f30b1a37..1bd2c3ae7b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.BgSourceInterface; @@ -31,4 +33,9 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac ); } + @Override + public void processNewData(Bundle bundle) { + // TODO + throw new IllegalStateException("Nope"); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index 9f15bd9b29..fc900f38c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -1,16 +1,31 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.utils.JsonHelper; +import info.nightscout.utils.NSUpload; +import info.nightscout.utils.SP; /** * Created by mike on 05.08.2016. */ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourcePoctechPlugin.class); private static SourcePoctechPlugin plugin = null; @@ -31,4 +46,35 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface ); } + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + String data = bundle.getString("data"); + log.debug("Received Poctech Data", data); + + try { + JSONArray jsonArray = new JSONArray(data); + log.debug("Received Poctech Data size:" + jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject json = jsonArray.getJSONObject(i); + bgReading.value = json.getDouble("current"); + bgReading.direction = json.getString("direction"); + bgReading.date = json.getLong("date"); + bgReading.raw = json.getDouble("raw"); + if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) + bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "Poctech"); + if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } + } + + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index a69ff84c2d..786edb2b49 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -1,6 +1,13 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; + +import java.util.Objects; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; @@ -27,4 +34,18 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { .description(R.string.description_source_xdrip) ); } + + @Override + public void processNewData(Bundle bundle) { + BgReading bgReading = new BgReading(); + + bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); + bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); + bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); + bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); + bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().getName(); + bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); + + MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); + } } From aab171932c6250c01cec4bc7ee7371ce5204096c Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Fri, 22 Jun 2018 18:57:02 +0200 Subject: [PATCH 05/20] Have Bgsource.processNewData return added BGs. --- .../androidaps/Services/DataService.java | 90 +++++++------------ .../interfaces/BgSourceInterface.java | 6 +- .../plugins/Source/SourceDexcomG5Plugin.java | 27 ++++-- .../plugins/Source/SourceGlimpPlugin.java | 10 ++- .../plugins/Source/SourceMM640gPlugin.java | 18 ++-- .../plugins/Source/SourceNSClientPlugin.java | 45 +++++++++- .../plugins/Source/SourcePoctechPlugin.java | 25 ++++-- .../plugins/Source/SourceXdripPlugin.java | 9 +- 8 files changed, 142 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index 2b6d1c1604..7cdd287316 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -71,6 +71,8 @@ public class DataService extends IntentService { protected void onHandleIntent(final Intent intent) { if (intent == null) return; + if (Config.logIncommingData) + log.debug("Got intent: " + intent.getAction()); if (Config.logFunctionCalls) log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); if (ConfigBuilderPlugin.getActiveBgSource() == null) { @@ -203,25 +205,21 @@ public class DataService extends IntentService { } private void processNewBgIntent(BgSourceInterface bgSource, Intent intent) { - if (Config.logIncommingData) - log.debug("Got intent: " + intent.getAction()); Bundle bundle = intent.getExtras(); if (bundle == null) return; bgSource.processNewData(bundle); } private void handleNewDataFromNSClient(Intent intent) { - Bundle bundles = intent.getExtras(); - if (bundles == null) return; - - + Bundle bundle = intent.getExtras(); + if (bundle == null) return; if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { - if (bundles.containsKey("nsclientversioncode")) { - ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203 - ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname"); - ConfigBuilderPlugin.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117 - ConfigBuilderPlugin.nsClientVersionName = bundles.getString("nsclientversionname"); + if (bundle.containsKey("nsclientversioncode")) { + ConfigBuilderPlugin.nightscoutVersionCode = bundle.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203 + ConfigBuilderPlugin.nightscoutVersionName = bundle.getString("nightscoutversionname"); + ConfigBuilderPlugin.nsClientVersionCode = bundle.getInt("nsclientversioncode"); // for ver 1.17 contains 117 + ConfigBuilderPlugin.nsClientVersionName = bundle.getString("nsclientversionname"); log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName); try { if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) { @@ -243,9 +241,9 @@ public class DataService extends IntentService { Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); MainApp.bus().post(new EventNewNotification(notification)); } - if (bundles.containsKey("status")) { + if (bundle.containsKey("status")) { try { - JSONObject statusJson = new JSONObject(bundles.getString("status")); + JSONObject statusJson = new JSONObject(bundle.getString("status")); NSSettingsStatus.getInstance().setData(statusJson); if (Config.logIncommingData) log.debug("Received status: " + statusJson.toString()); @@ -262,8 +260,8 @@ public class DataService extends IntentService { } if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) { try { - if (bundles.containsKey("devicestatus")) { - JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus")); + if (bundle.containsKey("devicestatus")) { + JSONObject devicestatusJson = new JSONObject(bundle.getString("devicestatus")); NSDeviceStatus.getInstance().setData(devicestatusJson); if (devicestatusJson.has("pump")) { // Objectives 0 @@ -271,8 +269,8 @@ public class DataService extends IntentService { ObjectivesPlugin.saveProgress(); } } - if (bundles.containsKey("devicestatuses")) { - String devicestatusesstring = bundles.getString("devicestatuses"); + if (bundle.containsKey("devicestatuses")) { + String devicestatusesstring = bundle.getString("devicestatuses"); JSONArray jsonArray = new JSONArray(devicestatusesstring); for (int i = 0; i < jsonArray.length(); i++) { JSONObject devicestatusJson = jsonArray.getJSONObject(i); @@ -291,8 +289,8 @@ public class DataService extends IntentService { // Handle profile if (intent.getAction().equals(Intents.ACTION_NEW_PROFILE)) { try { - String activeProfile = bundles.getString("activeprofile"); - String profile = bundles.getString("profile"); + String activeProfile = bundle.getString("activeprofile"); + String profile = bundle.getString("profile"); ProfileStore profileStore = new ProfileStore(new JSONObject(profile)); NSProfilePlugin.getPlugin().storeNewProfile(profileStore); MainApp.bus().post(new EventNSProfileUpdateGUI()); @@ -305,12 +303,12 @@ public class DataService extends IntentService { if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT) || intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) { try { - if (bundles.containsKey("treatment")) { - JSONObject json = new JSONObject(bundles.getString("treatment")); + if (bundle.containsKey("treatment")) { + JSONObject json = new JSONObject(bundle.getString("treatment")); handleTreatmentFromNS(json, intent); } - if (bundles.containsKey("treatments")) { - String trstring = bundles.getString("treatments"); + if (bundle.containsKey("treatments")) { + String trstring = bundle.getString("treatments"); JSONArray jsonArray = new JSONArray(trstring); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); @@ -324,14 +322,14 @@ public class DataService extends IntentService { if (intent.getAction().equals(Intents.ACTION_REMOVED_TREATMENT)) { try { - if (bundles.containsKey("treatment")) { - String trstring = bundles.getString("treatment"); + if (bundle.containsKey("treatment")) { + String trstring = bundle.getString("treatment"); JSONObject json = new JSONObject(trstring); handleTreatmentFromNS(json); } - if (bundles.containsKey("treatments")) { - String trstring = bundles.getString("treatments"); + if (bundle.containsKey("treatments")) { + String trstring = bundle.getString("treatments"); JSONArray jsonArray = new JSONArray(trstring); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); @@ -344,36 +342,19 @@ public class DataService extends IntentService { } if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) { - try { - if (bundles.containsKey("sgv")) { - String sgvstring = bundles.getString("sgv"); - JSONObject sgvJson = new JSONObject(sgvstring); - storeSgv(sgvJson); - } - - if (bundles.containsKey("sgvs")) { - String sgvstring = bundles.getString("sgvs"); - JSONArray jsonArray = new JSONArray(sgvstring); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject sgvJson = jsonArray.getJSONObject(i); - storeSgv(sgvJson); - } - } - } catch (Exception e) { - log.error("Unhandled exception", e); - } + SourceNSClientPlugin.getPlugin().processNewData(bundle); } if (intent.getAction().equals(Intents.ACTION_NEW_MBG)) { try { - if (bundles.containsKey("mbg")) { - String mbgstring = bundles.getString("mbg"); + if (bundle.containsKey("mbg")) { + String mbgstring = bundle.getString("mbg"); JSONObject mbgJson = new JSONObject(mbgstring); storeMbg(mbgJson); } - if (bundles.containsKey("mbgs")) { - String sgvstring = bundles.getString("mbgs"); + if (bundle.containsKey("mbgs")) { + String sgvstring = bundle.getString("mbgs"); JSONArray jsonArray = new JSONArray(sgvstring); for (int i = 0; i < jsonArray.length(); i++) { JSONObject mbgJson = jsonArray.getJSONObject(i); @@ -388,12 +369,12 @@ public class DataService extends IntentService { if (intent.getAction().equals(Intents.ACTION_NEW_FOOD) || intent.getAction().equals(Intents.ACTION_CHANGED_FOOD)) { int mode = Intents.ACTION_NEW_FOOD.equals(intent.getAction()) ? EventNsFood.ADD : EventNsFood.UPDATE; - EventNsFood evt = new EventNsFood(mode, bundles); + EventNsFood evt = new EventNsFood(mode, bundle); MainApp.bus().post(evt); } if (intent.getAction().equals(Intents.ACTION_REMOVED_FOOD)) { - EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles); + EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundle); MainApp.bus().post(evt); } } @@ -466,13 +447,6 @@ public class DataService extends IntentService { log.debug("Adding/Updating new MBG: " + careportalEvent.log()); } - private void storeSgv(JSONObject sgvJson) { - NSSgv nsSgv = new NSSgv(sgvJson); - BgReading bgReading = new BgReading(nsSgv); - MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); - SourceNSClientPlugin.getPlugin().detectSource(JsonHelper.safeGetString(sgvJson, "device"), JsonHelper.safeGetLong(sgvJson, "mills")); - } - private void handleNewSMS(Intent intent) { Bundle bundle = intent.getExtras(); if (bundle == null) return; diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java index c6cb7c1b8e..d3d9f1e640 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/BgSourceInterface.java @@ -2,9 +2,13 @@ package info.nightscout.androidaps.interfaces; import android.os.Bundle; +import java.util.List; + +import info.nightscout.androidaps.db.BgReading; + /** * Created by mike on 20.06.2016. */ public interface BgSourceInterface { - void processNewData(Bundle bundle); + List processNewData(Bundle bundle); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index 9327b31754..896782f720 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -2,12 +2,18 @@ package info.nightscout.androidaps.plugins.Source; import android.os.Bundle; +import com.google.common.collect.Lists; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -47,8 +53,8 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac } @Override - public void processNewData(Bundle bundle) { - BgReading bgReading = new BgReading(); + public List processNewData(Bundle bundle) { + List bgReadings = new ArrayList<>(); String data = bundle.getString("data"); // onHandleIntent Bundle{ data => [{"m_time":1511939180,"m_trend":"NotComputable","m_value":335}]; android.support.content.wakelockid => 95; }Bundle @@ -59,22 +65,27 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac log.debug("Received Dexcom Data size:" + jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); + BgReading bgReading = new BgReading(); bgReading.value = json.getInt("m_value"); bgReading.direction = json.getString("m_trend"); bgReading.date = json.getLong("m_time") * 1000L; bgReading.raw = 0; bgReading.filtered = true; bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().getName(); - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "DexcomG5"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + if (isNew) { + bgReadings.add(bgReading); + if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } } } } catch (JSONException e) { log.error("Unhandled exception", e); } + return bgReadings; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index a94caa0155..416d51f095 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -2,6 +2,11 @@ package info.nightscout.androidaps.plugins.Source; import android.os.Bundle; +import com.google.common.collect.Lists; + +import java.util.Collections; +import java.util.List; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; @@ -33,7 +38,7 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { } @Override - public void processNewData(Bundle bundle) { + public List processNewData(Bundle bundle) { BgReading bgReading = new BgReading(); bgReading.value = bundle.getDouble("mySGV"); @@ -43,6 +48,7 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { bgReading.filtered = false; bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().getName(); - MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList(); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index 8b7b08e9fb..62bf95bc7c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -8,6 +8,10 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; @@ -40,11 +44,10 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface } @Override - public void processNewData(Bundle bundle) { - final String collection = bundle.getString("collection"); - if (collection == null) return; + public List processNewData(Bundle bundle) { + List bgReadings = new ArrayList<>(); - if (collection.equals("entries")) { + if (Objects.equals(bundle.getString("collection"), "entries")) { final String data = bundle.getString("data"); if ((data != null) && (data.length() > 0)) { @@ -64,7 +67,10 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface bgReading.filtered = true; bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().getName(); - MainApp.getDbHelper().createIfNotExists(bgReading, "MM640g"); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + if (isNew) { + bgReadings.add(bgReading); + } break; default: log.debug("Unknown entries type: " + type); @@ -75,5 +81,7 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface } } } + + return bgReadings; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index 1bd2c3ae7b..1a1081ef6c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -2,17 +2,29 @@ package info.nightscout.androidaps.plugins.Source; import android.os.Bundle; +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.BgSourceInterface; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; /** * Created by mike on 05.08.2016. */ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceNSClientPlugin.class); private static SourceNSClientPlugin plugin = null; @@ -34,8 +46,35 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac } @Override - public void processNewData(Bundle bundle) { - // TODO - throw new IllegalStateException("Nope"); + public List processNewData(Bundle bundle) { + List sgvs = new ArrayList<>(); + try { + if (bundle.containsKey("sgv")) { + String sgvstring = bundle.getString("sgv"); + JSONObject sgvJson = new JSONObject(sgvstring); + BgReading bgReading = new BgReading(new NSSgv(sgvJson)); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + if (isNew) { + sgvs.add(bgReading); + } + } + + if (bundle.containsKey("sgvs")) { + String sgvstring = bundle.getString("sgvs"); + JSONArray jsonArray = new JSONArray(sgvstring); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject sgvJson = jsonArray.getJSONObject(i); + BgReading bgReading = new BgReading(new NSSgv(sgvJson)); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); + if (isNew) { + sgvs.add(bgReading); + } + } + } + } catch (Exception e) { + log.error("Unhandled exception", e); + } + return sgvs; } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index fc900f38c5..793e367a34 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -8,6 +8,9 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; @@ -47,8 +50,8 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface } @Override - public void processNewData(Bundle bundle) { - BgReading bgReading = new BgReading(); + public List processNewData(Bundle bundle) { + List bgReadings = new ArrayList<>(); String data = bundle.getString("data"); log.debug("Received Poctech Data", data); @@ -58,23 +61,27 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface log.debug("Received Poctech Data size:" + jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); + BgReading bgReading = new BgReading(); bgReading.value = json.getDouble("current"); bgReading.direction = json.getString("direction"); bgReading.date = json.getLong("date"); bgReading.raw = json.getDouble("raw"); if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "Poctech"); - if (isNew && SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { - NSUpload.uploadBg(bgReading); - } - if (isNew && SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { - NSUpload.sendToXdrip(bgReading); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + if (isNew) { + bgReadings.add(bgReading); + if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { + NSUpload.uploadBg(bgReading); + } + if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { + NSUpload.sendToXdrip(bgReading); + } } } - } catch (JSONException e) { log.error("Unhandled exception", e); } + return bgReadings; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 786edb2b49..9dfcf85dd2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -2,6 +2,10 @@ package info.nightscout.androidaps.plugins.Source; import android.os.Bundle; +import com.google.common.collect.Lists; + +import java.util.Collections; +import java.util.List; import java.util.Objects; import info.nightscout.androidaps.MainApp; @@ -36,7 +40,7 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { } @Override - public void processNewData(Bundle bundle) { + public List processNewData(Bundle bundle) { BgReading bgReading = new BgReading(); bgReading.value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE); @@ -46,6 +50,7 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().getName(); bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); - MainApp.getDbHelper().createIfNotExists(bgReading, "XDRIP"); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); + return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList(); } } From 53b9d5cae3587d01eff35c0e0451770b07800653 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 00:28:34 +0200 Subject: [PATCH 06/20] Fix column type. --- .../java/info/nightscout/androidaps/db/DatabaseHelper.java | 4 ++-- .../java/info/nightscout/androidaps/queue/CommandQueue.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 12b8b46a85..49b2a1e2f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -122,7 +122,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "filtered", "integer"); createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, - "sourcePlugin", "integer"); + "sourcePlugin", "text"); } catch (SQLException e) { log.error("Can't create database", e); @@ -132,7 +132,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private void createRowIfNotExists(Dao dao, String table, String name, String type) { try { - dao.executeRaw("ALTER TABLE `" + table + "` ADD CoLUMN `" + name + " " + type); + dao.executeRaw("ALTER TABLE `" + table + "` ADD COLUMN `" + name + " " + type); } catch (SQLException e) { // row already exists } diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index 47fe4acd6a..c3188db587 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -180,7 +180,7 @@ public class CommandQueue { if (type == Command.CommandType.SMB_BOLUS) { if (isRunning(Command.CommandType.BOLUS) || bolusInQueue()) { - log.debug("Rejecting SMB since a bolus is queue/running"); + log.debug("Rejecting SMB since a bolus is queued/running"); return false; } if (detailedBolusInfo.lastKnownBolusTime < TreatmentsPlugin.getPlugin().getLastBolusTime()) { From 940d58fc79e1598afd0dfebf9deea2515c9ba7b2 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 00:50:53 +0200 Subject: [PATCH 07/20] Tests. --- .../interfaces/ConstraintsCheckerTest.java | 10 --- .../ConstraintsSafety/SafetyPluginTest.java | 11 ---- .../Source/SourceDexcomG5PluginTest.java | 5 -- .../plugins/Source/SourceGlimpPluginTest.java | 5 -- .../Source/SourceMM640gPluginTest.java | 5 -- .../Source/SourceNSClientPluginTest.java | 5 -- .../plugins/Source/SourceXdripPluginTest.java | 65 +++++++++++++++++-- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java index 6b584a365d..4eb8291bdc 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -111,16 +111,6 @@ public class ConstraintsCheckerTest { Assert.assertEquals(Boolean.FALSE, c.value()); } -// @Test -// public void isAdvancedFilteringEnabledTest() throws Exception { -// when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); -// -// Constraint c = constraintChecker.isAdvancedFilteringEnabled(); -// Assert.assertEquals(true, c.getReasonList().size() == 1); // Safety -// Assert.assertEquals(true, c.getMostLimitedReasonList().size() == 1); // Safety -// Assert.assertEquals(Boolean.FALSE, c.value()); -// } - @Test public void isSMBModeEnabledTest() throws Exception { objectivesPlugin.objectives.get(7).setStartedOn(null); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java index ee3359c84e..4e3ab763fa 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/ConstraintsSafety/SafetyPluginTest.java @@ -89,17 +89,6 @@ public class SafetyPluginTest { Assert.assertEquals(Boolean.FALSE, c.value()); } - // TODO -// @Test -// public void bgsourceShouldPreventSMBAlways() throws Exception { -// when(MainApp.getConfigBuilder().getActiveBgSource()).thenReturn(SourceGlimpPlugin.getPlugin()); -// -// Constraint c = new Constraint<>(true); -// c = safetyPlugin.isAdvancedFilteringEnabled(c); -// Assert.assertEquals("Safety: SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering", c.getReasons()); -// Assert.assertEquals(Boolean.FALSE, c.value()); -// } - @Test public void basalRateShouldBeLimited() throws Exception { when(SP.getDouble(R.string.key_openapsma_max_basal, 1d)).thenReturn(1d); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java index 7f160ce1d2..fcf83cd745 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5PluginTest.java @@ -14,9 +14,4 @@ public class SourceDexcomG5PluginTest { public void getPlugin() { Assert.assertNotEquals(null, SourceDexcomG5Plugin.getPlugin()); } - -// @Test -// public void advancedFilteringSupported() { -// Assert.assertEquals(true, SourceDexcomG5Plugin.getPlugin().advancedFilteringSupported()); -// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java index 0910442be8..1729a1ef5e 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPluginTest.java @@ -14,9 +14,4 @@ public class SourceGlimpPluginTest { public void getPlugin() { Assert.assertNotEquals(null, SourceGlimpPlugin.getPlugin()); } - -// @Test -// public void advancedFilteringSupported() { -// Assert.assertEquals(false, SourceGlimpPlugin.getPlugin().advancedFilteringSupported()); -// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java index e40f9bb492..7cd7f36d6f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPluginTest.java @@ -14,9 +14,4 @@ public class SourceMM640gPluginTest { public void getPlugin() { Assert.assertNotEquals(null, SourceMM640gPlugin.getPlugin()); } - -// @Test -// public void advancedFilteringSupported() { -// Assert.assertEquals(false, SourceMM640gPlugin.getPlugin().advancedFilteringSupported()); -// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java index fecf0712d8..787eb65351 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPluginTest.java @@ -12,9 +12,4 @@ public class SourceNSClientPluginTest { public void getPlugin() { Assert.assertNotEquals(null, SourceNSClientPlugin.getPlugin()); } - -// @Test -// public void advancedFilteringSupported() { -// Assert.assertEquals(false, SourceNSClientPlugin.getPlugin().advancedFilteringSupported()); -// } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java index 33d9bc45b1..458feb2d34 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java @@ -1,22 +1,75 @@ package info.nightscout.androidaps.plugins.Source; +import android.os.Bundle; +import android.support.annotation.NonNull; + import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import info.nightscout.androidaps.plugins.Source.SourceXdripPlugin; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.Services.Intents; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.DatabaseHelper; + +import static junit.framework.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @RunWith(PowerMockRunner.class) +@PrepareForTest({MainApp.class, DatabaseHelper.class}) public class SourceXdripPluginTest { + private SourceXdripPlugin plugin = SourceXdripPlugin.getPlugin(); + + @Before + public void prepareTest() { + PowerMockito.mockStatic(MainApp.class); + when(MainApp.gs(0)).thenReturn(""); + DatabaseHelper databaseHelper = mock(DatabaseHelper.class); + when(MainApp.getDbHelper()).thenReturn(databaseHelper); + when(databaseHelper.createIfNotExists(any(), any())).thenReturn(true); + } + @Test - public void getPlugin() { + public void pluginInitializes() { Assert.assertNotEquals(null, SourceXdripPlugin.getPlugin()); } -// @Test -// public void advancedFilteringSupported() { -// Assert.assertEquals(false, SourceXdripPlugin.getPlugin().advancedFilteringSupported()); -// } + // TODO + @Ignore("Bundle needs to be properly mocked") + @Test + public void bgWithUnknownSourceIsMarkedUnfiltered() { + Bundle bundle = createBroadcastBundle(); + BgReading bgReadings = plugin.processNewData(bundle).get(0); + assertFalse(bgReadings.filtered); + } + + // TODO + @Ignore("Bundle needs to be properly mocked") + @Test + public void bgWithSourceG5NativeIsMarkedFiltered() { + Bundle bundle = createBroadcastBundle(); + bundle.putString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "G5 Native"); + + BgReading bgReadings = plugin.processNewData(bundle).get(0); + assertTrue(bgReadings.filtered); + } + + @NonNull + private Bundle createBroadcastBundle() { + Bundle bundle = new Bundle(); + bundle.putDouble(Intents.EXTRA_BG_ESTIMATE, 100.0); + bundle.putString(Intents.EXTRA_BG_SLOPE_NAME, "DoubleDown"); + bundle.putLong(Intents.EXTRA_TIMESTAMP, 0L); + bundle.putDouble(Intents.EXTRA_RAW, 430.0); + return bundle; + } } \ No newline at end of file From 906cc8a76aceaab1eac6f3e820c8388ab5e1d531 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 01:05:53 +0200 Subject: [PATCH 08/20] Clean up assigning sourcePlugin, filtered properties. --- .../androidaps/plugins/Source/SourceDexcomG5Plugin.java | 2 +- .../nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java | 2 +- .../androidaps/plugins/Source/SourceMM640gPlugin.java | 2 +- .../androidaps/plugins/Source/SourceNSClientPlugin.java | 2 ++ .../androidaps/plugins/Source/SourcePoctechPlugin.java | 2 ++ .../nightscout/androidaps/plugins/Source/SourceXdripPlugin.java | 2 +- 6 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index 896782f720..80785ec7fe 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -71,7 +71,7 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac bgReading.date = json.getLong("m_time") * 1000L; bgReading.raw = 0; bgReading.filtered = true; - bgReading.sourcePlugin = SourceDexcomG5Plugin.getPlugin().getName(); + bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); if (isNew) { bgReadings.add(bgReading); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index 416d51f095..ee9ff1424f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -46,7 +46,7 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { bgReading.date = bundle.getLong("myTimestamp"); bgReading.raw = 0; bgReading.filtered = false; - bgReading.sourcePlugin = SourceGlimpPlugin.getPlugin().getName(); + bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index 62bf95bc7c..84a3cd5427 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -65,7 +65,7 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface bgReading.date = json_object.getLong("date"); bgReading.raw = json_object.getDouble("sgv"); bgReading.filtered = true; - bgReading.sourcePlugin = SourceMM640gPlugin.getPlugin().getName(); + bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); if (isNew) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index 1a1081ef6c..b082fc95b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -65,6 +65,8 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac for (int i = 0; i < jsonArray.length(); i++) { JSONObject sgvJson = jsonArray.getJSONObject(i); BgReading bgReading = new BgReading(new NSSgv(sgvJson)); + bgReading.filtered = false; + bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); if (isNew) { sgvs.add(bgReading); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index 793e367a34..a2953eb2cd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -66,6 +66,8 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface bgReading.direction = json.getString("direction"); bgReading.date = json.getLong("date"); bgReading.raw = json.getDouble("raw"); + bgReading.filtered = false; + bgReading.sourcePlugin = getName(); if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 9dfcf85dd2..6349d9050b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -47,8 +47,8 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - bgReading.sourcePlugin = SourceXdripPlugin.getPlugin().getName(); bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); + bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); return isNew ? Lists.newArrayList(bgReading) : Collections.emptyList(); From 986d9fb01c77c3185ab24b82a7339a2bed5fcac4 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 01:28:49 +0200 Subject: [PATCH 09/20] Detect source from NSClient BG. --- .../androidaps/plugins/Source/SourceNSClientPlugin.java | 5 ++++- .../androidaps/plugins/Source/SourceXdripPlugin.java | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index b082fc95b0..5a54541aa9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -19,6 +19,7 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; +import info.nightscout.utils.JsonHelper; /** * Created by mike on 05.08.2016. @@ -65,7 +66,9 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac for (int i = 0; i < jsonArray.length(); i++) { JSONObject sgvJson = jsonArray.getJSONObject(i); BgReading bgReading = new BgReading(new NSSgv(sgvJson)); - bgReading.filtered = false; + String sourceDescription = JsonHelper.safeGetString(sgvJson, "device"); + bgReading.filtered = sourceDescription != null + && (sourceDescription.equals("G5 Native") || sourceDescription.equals("AndroidAPS-DexcomG5")); bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); if (isNew) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 6349d9050b..6fe5564c91 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -47,7 +47,8 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - bgReading.filtered = Objects.equals(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION), "G5 Native"); + String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); + bgReading.filtered = sourceDescription.equals("G5 Native") || sourceDescription.equals("AndroidAPS-DexcomG5"); bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); From 79dec2e8715c4d594c91c83b1c4530def1b7c5e9 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 02:48:36 +0200 Subject: [PATCH 10/20] Fixes. --- .../java/info/nightscout/androidaps/db/DatabaseHelper.java | 3 ++- .../androidaps/plugins/Source/SourceNSClientPlugin.java | 2 +- .../androidaps/plugins/Source/SourceXdripPlugin.java | 5 ++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 49b2a1e2f6..19fbc7b10d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -132,7 +132,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private void createRowIfNotExists(Dao dao, String table, String name, String type) { try { - dao.executeRaw("ALTER TABLE `" + table + "` ADD COLUMN `" + name + " " + type); + final String statement = "ALTER TABLE `" + table + "` ADD COLUMN `" + name + "` " + type; + dao.executeRaw(statement); } catch (SQLException e) { // row already exists } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index 5a54541aa9..b435fd9a60 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -70,7 +70,7 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac bgReading.filtered = sourceDescription != null && (sourceDescription.equals("G5 Native") || sourceDescription.equals("AndroidAPS-DexcomG5")); bgReading.sourcePlugin = getName(); - boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, "NS"); + boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); if (isNew) { sgvs.add(bgReading); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 6fe5564c91..07dda4ca6e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -6,7 +6,6 @@ import com.google.common.collect.Lists; import java.util.Collections; import java.util.List; -import java.util.Objects; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -23,7 +22,7 @@ import info.nightscout.androidaps.interfaces.PluginType; public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { private static SourceXdripPlugin plugin = null; - + public static SourceXdripPlugin getPlugin() { if (plugin == null) plugin = new SourceXdripPlugin(); @@ -48,7 +47,7 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); - bgReading.filtered = sourceDescription.equals("G5 Native") || sourceDescription.equals("AndroidAPS-DexcomG5"); + bgReading.filtered = sourceDescription.equals("G5 Native"); bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); From 34ee125666e9ecac371c6c64fdc7b79655d36884 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:07:16 +0200 Subject: [PATCH 11/20] Add message to constraints if BG is not filtered. --- .../androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index 54bfd9d780..9281be6e6f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -192,6 +192,12 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); inputConstraints.copyReasons(smbAllowed); + Constraint bgFiltered = new Constraint<>(bgReading.filtered); + if (!bgReading.filtered) { + bgFiltered.set(false, MainApp.gs(R.string.smbalwaysdisabled), this); + } + inputConstraints.copyReasons(bgFiltered); + Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); Profiler.log(log, "SMB data gathering", start); From b4db98fa1d82c4f6ef139c872b4758414b2222c6 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:21:05 +0200 Subject: [PATCH 12/20] Process xDrip noise data if provided (engineering mode only). --- .../info/nightscout/androidaps/Services/Intents.java | 1 + .../java/info/nightscout/androidaps/db/BgReading.java | 5 +++-- .../info/nightscout/androidaps/db/DatabaseHelper.java | 2 ++ .../androidaps/plugins/Source/SourceXdripPlugin.java | 9 +++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java index 25485fb34d..1c2753b847 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java @@ -38,6 +38,7 @@ public interface Intents { String EXTRA_TIMESTAMP = "com.eveningoutpost.dexdrip.Extras.Time"; String EXTRA_RAW = "com.eveningoutpost.dexdrip.Extras.Raw"; String XDRIP_DATA_SOURCE_DESCRIPTION = "com.eveningoutpost.dexdrip.Extras.SourceDesc"; + String EXTRA_NOISE = "com.eveningoutpost.dexdrip.Extras.Noise"; String ACTION_NEW_BG_ESTIMATE_NO_DATA = "com.eveningoutpost.dexdrip.BgEstimateNoData"; diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 79b5236162..322f46100d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -1,7 +1,5 @@ package info.nightscout.androidaps.db; -import android.content.res.Resources; - import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; @@ -39,6 +37,8 @@ public class BgReading implements DataPointWithLabelInterface { @DatabaseField public double raw; @DatabaseField + public double noise = -999; // xDrip sends -999 to indicate lack of a noise reading (due to missed readings or calibration) + @DatabaseField public boolean filtered; @DatabaseField public String sourcePlugin; @@ -154,6 +154,7 @@ public class BgReading implements DataPointWithLabelInterface { raw = other.raw; direction = other.direction; _id = other._id; + noise = other.noise; sourcePlugin = other.sourcePlugin; filtered = other.filtered; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 19fbc7b10d..8dba0a7919 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -123,6 +123,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { "filtered", "integer"); createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "sourcePlugin", "text"); + createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, + "noise", "real"); } catch (SQLException e) { log.error("Can't create database", e); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 07dda4ca6e..647e5cf877 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -4,6 +4,9 @@ import android.os.Bundle; import com.google.common.collect.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Collections; import java.util.List; @@ -20,6 +23,7 @@ import info.nightscout.androidaps.interfaces.PluginType; * Created by mike on 05.08.2016. */ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { + private static final Logger log = LoggerFactory.getLogger(SourceXdripPlugin.class); private static SourceXdripPlugin plugin = null; @@ -46,8 +50,13 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); + bgReading.noise = bundle.getDouble(Intents.EXTRA_NOISE, -999); String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); bgReading.filtered = sourceDescription.equals("G5 Native"); + if (MainApp.engineeringMode && !bgReading.filtered && bgReading.noise >= 0 && bgReading.noise <= 4) { + log.debug("Setting filtered=true, since noise is provided and passed check: " + bgReading.noise); + bgReading.filtered = true; + } bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); From c18effd8932a4f5ec0a4c676f42cbae45c6de5d8 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:27:56 +0200 Subject: [PATCH 13/20] Fix detecting BG source from NS entry. --- .../androidaps/plugins/Source/SourceNSClientPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index b435fd9a60..43fff2eae5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -68,7 +68,7 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac BgReading bgReading = new BgReading(new NSSgv(sgvJson)); String sourceDescription = JsonHelper.safeGetString(sgvJson, "device"); bgReading.filtered = sourceDescription != null - && (sourceDescription.equals("G5 Native") || sourceDescription.equals("AndroidAPS-DexcomG5")); + && (sourceDescription.contains("G5 Native") || sourceDescription.contains("AndroidAPS-DexcomG5")); bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); if (isNew) { From 6d437b4afa056fc8222ccede38effb228d64fba8 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:35:01 +0200 Subject: [PATCH 14/20] Test stubs. --- .../plugins/Source/SourceXdripPlugin.java | 2 ++ .../plugins/Source/SourceXdripPluginTest.java | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 647e5cf877..9c90ae8249 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -54,6 +54,8 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); bgReading.filtered = sourceDescription.equals("G5 Native"); if (MainApp.engineeringMode && !bgReading.filtered && bgReading.noise >= 0 && bgReading.noise <= 4) { + // TODO syncing noice with NS is neither implemented nor tested + // * NSUpload.uploadBg log.debug("Setting filtered=true, since noise is provided and passed check: " + bgReading.noise); bgReading.filtered = true; } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java index 458feb2d34..17e0544904 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java @@ -44,7 +44,7 @@ public class SourceXdripPluginTest { } // TODO - @Ignore("Bundle needs to be properly mocked") + @Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved") @Test public void bgWithUnknownSourceIsMarkedUnfiltered() { Bundle bundle = createBroadcastBundle(); @@ -53,7 +53,7 @@ public class SourceXdripPluginTest { } // TODO - @Ignore("Bundle needs to be properly mocked") + @Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved") @Test public void bgWithSourceG5NativeIsMarkedFiltered() { Bundle bundle = createBroadcastBundle(); @@ -63,6 +63,28 @@ public class SourceXdripPluginTest { assertTrue(bgReadings.filtered); } + // TODO + @Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved") + @Test + public void bgWithWithGoodNoiseIsMarkedFiltered() { + Bundle bundle = createBroadcastBundle(); + bundle.putString(Intents.EXTRA_NOISE, "1.0"); + + BgReading bgReadings = plugin.processNewData(bundle).get(0); + assertTrue(bgReadings.filtered); + } + + // TODO + @Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved") + @Test + public void bgWithWithExcessiveNoiseDataIsMarkedFiltered() { + Bundle bundle = createBroadcastBundle(); + bundle.putString(Intents.EXTRA_NOISE, "80.0"); + + BgReading bgReadings = plugin.processNewData(bundle).get(0); + assertTrue(bgReadings.filtered); + } + @NonNull private Bundle createBroadcastBundle() { Bundle bundle = new Bundle(); From 73e8a0c8763949957d20c502ae12f8402aa27ace Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:36:23 +0200 Subject: [PATCH 15/20] Rename BgReading.filtered to BgReading.isFiltered and add to NS. Avoids collision with 'filtered' value in NS data. --- .../java/info/nightscout/androidaps/db/BgReading.java | 6 +++--- .../info/nightscout/androidaps/db/DatabaseHelper.java | 2 +- .../androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java | 6 +++--- .../androidaps/plugins/Source/SourceDexcomG5Plugin.java | 5 +---- .../androidaps/plugins/Source/SourceGlimpPlugin.java | 2 +- .../androidaps/plugins/Source/SourceMM640gPlugin.java | 2 +- .../androidaps/plugins/Source/SourceNSClientPlugin.java | 2 +- .../androidaps/plugins/Source/SourcePoctechPlugin.java | 2 +- .../androidaps/plugins/Source/SourceXdripPlugin.java | 6 +++--- app/src/main/java/info/nightscout/utils/NSUpload.java | 2 ++ .../androidaps/plugins/Source/SourceXdripPluginTest.java | 8 ++++---- 11 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 322f46100d..3ecc969fc6 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -39,7 +39,7 @@ public class BgReading implements DataPointWithLabelInterface { @DatabaseField public double noise = -999; // xDrip sends -999 to indicate lack of a noise reading (due to missed readings or calibration) @DatabaseField - public boolean filtered; + public boolean isFiltered; @DatabaseField public String sourcePlugin; @@ -124,7 +124,7 @@ public class BgReading implements DataPointWithLabelInterface { ", value=" + value + ", direction=" + direction + ", raw=" + raw + - ", filtered=" + filtered + + ", filtered=" + isFiltered + ", sourcePlugin=" + sourcePlugin + '}'; } @@ -156,7 +156,7 @@ public class BgReading implements DataPointWithLabelInterface { _id = other._id; noise = other.noise; sourcePlugin = other.sourcePlugin; - filtered = other.filtered; + isFiltered = other.isFiltered; } // ------------------ DataPointWithLabelInterface ------------------ diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 8dba0a7919..868e44b65e 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -120,7 +120,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { // soft migration without changing DB version createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, - "filtered", "integer"); + "isFiltered", "integer"); createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "sourcePlugin", "text"); createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index 9281be6e6f..0f66501e58 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -192,8 +192,8 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { MainApp.getConstraintChecker().isSMBModeEnabled(smbAllowed); inputConstraints.copyReasons(smbAllowed); - Constraint bgFiltered = new Constraint<>(bgReading.filtered); - if (!bgReading.filtered) { + Constraint bgFiltered = new Constraint<>(bgReading.isFiltered); + if (!bgReading.isFiltered) { bgFiltered.set(false, MainApp.gs(R.string.smbalwaysdisabled), this); } inputConstraints.copyReasons(bgFiltered); @@ -207,7 +207,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { lastAutosensResult.ratio, //autosensDataRatio isTempTarget, smbAllowed.value(), - bgReading.filtered + bgReading.isFiltered ); } catch (JSONException e) { log.error(e.getMessage()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java index 80785ec7fe..8d442d4d70 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceDexcomG5Plugin.java @@ -2,8 +2,6 @@ package info.nightscout.androidaps.plugins.Source; import android.os.Bundle; -import com.google.common.collect.Lists; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -11,7 +9,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import info.nightscout.androidaps.Config; @@ -70,7 +67,7 @@ public class SourceDexcomG5Plugin extends PluginBase implements BgSourceInterfac bgReading.direction = json.getString("m_trend"); bgReading.date = json.getLong("m_time") * 1000L; bgReading.raw = 0; - bgReading.filtered = true; + bgReading.isFiltered = true; bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); if (isNew) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java index ee9ff1424f..b5fb2ca294 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceGlimpPlugin.java @@ -45,7 +45,7 @@ public class SourceGlimpPlugin extends PluginBase implements BgSourceInterface { bgReading.direction = bundle.getString("myTrend"); bgReading.date = bundle.getLong("myTimestamp"); bgReading.raw = 0; - bgReading.filtered = false; + bgReading.isFiltered = false; bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java index 84a3cd5427..196529f1d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceMM640gPlugin.java @@ -64,7 +64,7 @@ public class SourceMM640gPlugin extends PluginBase implements BgSourceInterface bgReading.direction = json_object.getString("direction"); bgReading.date = json_object.getLong("date"); bgReading.raw = json_object.getDouble("sgv"); - bgReading.filtered = true; + bgReading.isFiltered = true; bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java index 43fff2eae5..c40fbb4806 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceNSClientPlugin.java @@ -67,7 +67,7 @@ public class SourceNSClientPlugin extends PluginBase implements BgSourceInterfac JSONObject sgvJson = jsonArray.getJSONObject(i); BgReading bgReading = new BgReading(new NSSgv(sgvJson)); String sourceDescription = JsonHelper.safeGetString(sgvJson, "device"); - bgReading.filtered = sourceDescription != null + bgReading.isFiltered = sourceDescription != null && (sourceDescription.contains("G5 Native") || sourceDescription.contains("AndroidAPS-DexcomG5")); bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java index a2953eb2cd..2ed3ca0cf7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourcePoctechPlugin.java @@ -66,7 +66,7 @@ public class SourcePoctechPlugin extends PluginBase implements BgSourceInterface bgReading.direction = json.getString("direction"); bgReading.date = json.getLong("date"); bgReading.raw = json.getDouble("raw"); - bgReading.filtered = false; + bgReading.isFiltered = false; bgReading.sourcePlugin = getName(); if (JsonHelper.safeGetString(json, "utils", Constants.MGDL).equals("mmol/L")) bgReading.value = bgReading.value * Constants.MMOLL_TO_MGDL; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index 9c90ae8249..c97a01e516 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -52,12 +52,12 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); bgReading.noise = bundle.getDouble(Intents.EXTRA_NOISE, -999); String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); - bgReading.filtered = sourceDescription.equals("G5 Native"); - if (MainApp.engineeringMode && !bgReading.filtered && bgReading.noise >= 0 && bgReading.noise <= 4) { + bgReading.isFiltered = sourceDescription.equals("G5 Native"); + if (MainApp.engineeringMode && !bgReading.isFiltered && bgReading.noise >= 0 && bgReading.noise <= 4) { // TODO syncing noice with NS is neither implemented nor tested // * NSUpload.uploadBg log.debug("Setting filtered=true, since noise is provided and passed check: " + bgReading.noise); - bgReading.filtered = true; + bgReading.isFiltered = true; } bgReading.sourcePlugin = getName(); diff --git a/app/src/main/java/info/nightscout/utils/NSUpload.java b/app/src/main/java/info/nightscout/utils/NSUpload.java index 00d5e39821..a425cc5d52 100644 --- a/app/src/main/java/info/nightscout/utils/NSUpload.java +++ b/app/src/main/java/info/nightscout/utils/NSUpload.java @@ -469,6 +469,8 @@ public class NSUpload { data.put("sgv", reading.value); data.put("direction", reading.direction); data.put("type", "sgv"); + data.put("isFiltered", reading.isFiltered); + data.put("sourcePlugin", reading.sourcePlugin); } catch (JSONException e) { log.error("Unhandled exception", e); } diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java index 17e0544904..a21a8c0a9b 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java @@ -49,7 +49,7 @@ public class SourceXdripPluginTest { public void bgWithUnknownSourceIsMarkedUnfiltered() { Bundle bundle = createBroadcastBundle(); BgReading bgReadings = plugin.processNewData(bundle).get(0); - assertFalse(bgReadings.filtered); + assertFalse(bgReadings.isFiltered); } // TODO @@ -60,7 +60,7 @@ public class SourceXdripPluginTest { bundle.putString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, "G5 Native"); BgReading bgReadings = plugin.processNewData(bundle).get(0); - assertTrue(bgReadings.filtered); + assertTrue(bgReadings.isFiltered); } // TODO @@ -71,7 +71,7 @@ public class SourceXdripPluginTest { bundle.putString(Intents.EXTRA_NOISE, "1.0"); BgReading bgReadings = plugin.processNewData(bundle).get(0); - assertTrue(bgReadings.filtered); + assertTrue(bgReadings.isFiltered); } // TODO @@ -82,7 +82,7 @@ public class SourceXdripPluginTest { bundle.putString(Intents.EXTRA_NOISE, "80.0"); BgReading bgReadings = plugin.processNewData(bundle).get(0); - assertTrue(bgReadings.filtered); + assertTrue(bgReadings.isFiltered); } @NonNull From 6cb09d4fcb7f5e8b8b83966dc00b4b34895b9103 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 11:41:03 +0200 Subject: [PATCH 16/20] Remove BgReading.noise for now, clashes with existing NS data. I believe NS uses 'noise' to store the noise level (1 through 3?). --- .../java/info/nightscout/androidaps/Services/Intents.java | 1 - .../main/java/info/nightscout/androidaps/db/BgReading.java | 3 --- .../java/info/nightscout/androidaps/db/DatabaseHelper.java | 2 -- .../androidaps/plugins/Source/SourceXdripPlugin.java | 7 ------- 4 files changed, 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java index 1c2753b847..25485fb34d 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/Intents.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/Intents.java @@ -38,7 +38,6 @@ public interface Intents { String EXTRA_TIMESTAMP = "com.eveningoutpost.dexdrip.Extras.Time"; String EXTRA_RAW = "com.eveningoutpost.dexdrip.Extras.Raw"; String XDRIP_DATA_SOURCE_DESCRIPTION = "com.eveningoutpost.dexdrip.Extras.SourceDesc"; - String EXTRA_NOISE = "com.eveningoutpost.dexdrip.Extras.Noise"; String ACTION_NEW_BG_ESTIMATE_NO_DATA = "com.eveningoutpost.dexdrip.BgEstimateNoData"; diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 3ecc969fc6..0a936f23d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -37,8 +37,6 @@ public class BgReading implements DataPointWithLabelInterface { @DatabaseField public double raw; @DatabaseField - public double noise = -999; // xDrip sends -999 to indicate lack of a noise reading (due to missed readings or calibration) - @DatabaseField public boolean isFiltered; @DatabaseField public String sourcePlugin; @@ -154,7 +152,6 @@ public class BgReading implements DataPointWithLabelInterface { raw = other.raw; direction = other.direction; _id = other._id; - noise = other.noise; sourcePlugin = other.sourcePlugin; isFiltered = other.isFiltered; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 868e44b65e..c954d8c06a 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -123,8 +123,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { "isFiltered", "integer"); createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "sourcePlugin", "text"); - createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, - "noise", "real"); } catch (SQLException e) { log.error("Can't create database", e); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java index c97a01e516..2c7f399beb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Source/SourceXdripPlugin.java @@ -50,15 +50,8 @@ public class SourceXdripPlugin extends PluginBase implements BgSourceInterface { bgReading.direction = bundle.getString(Intents.EXTRA_BG_SLOPE_NAME); bgReading.date = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); - bgReading.noise = bundle.getDouble(Intents.EXTRA_NOISE, -999); String sourceDescription = bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION, ""); bgReading.isFiltered = sourceDescription.equals("G5 Native"); - if (MainApp.engineeringMode && !bgReading.isFiltered && bgReading.noise >= 0 && bgReading.noise <= 4) { - // TODO syncing noice with NS is neither implemented nor tested - // * NSUpload.uploadBg - log.debug("Setting filtered=true, since noise is provided and passed check: " + bgReading.noise); - bgReading.isFiltered = true; - } bgReading.sourcePlugin = getName(); boolean isNew = MainApp.getDbHelper().createIfNotExists(bgReading, getName()); From b2a60e2face75248dba43e0c0de1a304b7c17259 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 12:52:07 +0200 Subject: [PATCH 17/20] Disable unfinished noise tests. --- .../androidaps/plugins/Source/SourceXdripPluginTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java index a21a8c0a9b..9e0925b332 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/Source/SourceXdripPluginTest.java @@ -63,6 +63,7 @@ public class SourceXdripPluginTest { assertTrue(bgReadings.isFiltered); } + /* // TODO @Ignore("Bundle needs to be properly mocked or Robolectrics issues with SQLite resolved") @Test @@ -84,6 +85,7 @@ public class SourceXdripPluginTest { BgReading bgReadings = plugin.processNewData(bundle).get(0); assertTrue(bgReadings.isFiltered); } + */ @NonNull private Bundle createBroadcastBundle() { From eac1d5ffdb9a67d9a340a6f3411af16d4d36e1a1 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 23 Jun 2018 13:12:29 +0200 Subject: [PATCH 18/20] (Hopefully) fix iob(AMA|SMB)shouldBeLimited tests. By making sure there is exactly one APS plugin enabled. --- .../androidaps/interfaces/ConstraintsCheckerTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java index 4eb8291bdc..8d893c3af8 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.java @@ -223,13 +223,14 @@ public class ConstraintsCheckerTest { // No limit by default when(SP.getDouble(R.string.key_openapsma_max_iob, 1.5d)).thenReturn(1.5d); when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); - OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); + OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false); + OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, false); // Apply all limits Constraint d = constraintChecker.getMaxIOBAllowed(); Assert.assertEquals(1.5d, d.value()); - Assert.assertEquals(3, d.getReasonList().size()); + Assert.assertEquals(d.getReasonList().toString(),2, d.getReasonList().size()); Assert.assertEquals("Safety: Limiting IOB to 1.5 U because of max value in preferences", d.getMostLimitedReasons()); } @@ -240,11 +241,13 @@ public class ConstraintsCheckerTest { when(SP.getDouble(R.string.key_openapssmb_max_iob, 3d)).thenReturn(3d); when(SP.getString(R.string.key_age, "")).thenReturn("teenage"); OpenAPSSMBPlugin.getPlugin().setPluginEnabled(PluginType.APS, true); + OpenAPSAMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false); + OpenAPSMAPlugin.getPlugin().setPluginEnabled(PluginType.APS, false); // Apply all limits Constraint d = constraintChecker.getMaxIOBAllowed(); Assert.assertEquals(3d, d.value()); - Assert.assertEquals(4, d.getReasonList().size()); + Assert.assertEquals(d.getReasonList().toString(), 2, d.getReasonList().size()); Assert.assertEquals("Safety: Limiting IOB to 3.0 U because of max value in preferences", d.getMostLimitedReasons()); } From 069f0fb7b1c6c7ee5b6042152b2a87dc8fc7f82e Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Mon, 25 Jun 2018 22:51:59 +0200 Subject: [PATCH 19/20] Fix method name. --- .../java/info/nightscout/androidaps/db/DatabaseHelper.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index c954d8c06a..e13f1b1e0c 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -28,7 +28,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.ProfileStore; @@ -119,9 +118,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, TDD.class); // soft migration without changing DB version - createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, + createColumnIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "isFiltered", "integer"); - createRowIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, + createColumnIfNotExists(getDaoBgReadings(), DatabaseHelper.DATABASE_BGREADINGS, "sourcePlugin", "text"); } catch (SQLException e) { @@ -130,7 +129,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } } - private void createRowIfNotExists(Dao dao, String table, String name, String type) { + private void createColumnIfNotExists(Dao dao, String table, String name, String type) { try { final String statement = "ALTER TABLE `" + table + "` ADD COLUMN `" + name + "` " + type; dao.executeRaw(statement); From 50ffebc679ded320e0aeeea7745362189028d192 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Mon, 25 Jun 2018 23:02:58 +0200 Subject: [PATCH 20/20] BgReading.copyFrom: don't update local data. Like the 'valid' flag, which is not copied and is not synced with NS, don't sync sourcePlugin or isFiltered. Otherwise there might be a race condition if the NS-roundtrip of a locally received BG updates the sourcePlugin field to null, thus leaving no values for the loop to loop with. --- app/src/main/java/info/nightscout/androidaps/db/BgReading.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 0a936f23d8..d7ebfdc677 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -152,8 +152,6 @@ public class BgReading implements DataPointWithLabelInterface { raw = other.raw; direction = other.direction; _id = other._id; - sourcePlugin = other.sourcePlugin; - isFiltered = other.isFiltered; } // ------------------ DataPointWithLabelInterface ------------------