From 2a2589f5eb076786a63cd43cb57144d17160dfde Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 21 Jun 2016 23:24:54 +0200 Subject: [PATCH] some db and incomming data optimalisation --- .../info/nightscout/androidaps/Constants.java | 1 + .../nightscout/androidaps/MainActivity.java | 1 + .../info/nightscout/androidaps/MainApp.java | 10 +++ .../androidaps/Services/DataService.java | 61 +++++++++++++------ .../androidaps/db/DatabaseHelper.java | 20 +++++- .../androidaps/receivers/xDripReceiver.java | 16 ++--- 6 files changed, 79 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index bb947f94a0..32ffb61818 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -10,4 +10,5 @@ public class Constants { public static final double MMOLL_TO_MGDL = 18.0182; public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL; + public static final int hoursToKeepInDatabase = 24; } diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index f95f396499..2aba223d03 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -84,6 +84,7 @@ public class MainActivity extends AppCompatActivity { registerBus(); configBuilderFragment.initialize(); + MainApp.setConfigBuilder(configBuilderFragment); } setUpTabs(false); } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 5b15182e92..0edc28acfc 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -10,6 +10,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.db.DatabaseHelper; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; public class MainApp extends Application { @@ -19,6 +20,7 @@ public class MainApp extends Application { private static MainApp sInstance; private static DatabaseHelper databaseHelper = null; + private static ConfigBuilderFragment configBuilder = null; @Override public void onCreate() { @@ -49,6 +51,14 @@ public class MainApp extends Application { } } + public static void setConfigBuilder(ConfigBuilderFragment cb) { + configBuilder = cb; + } + + public static ConfigBuilderFragment getConfigBuilder() { + return configBuilder; + } + @Override public void onTerminate() { super.onTerminate(); 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 e37dfed275..768f3f5a8d 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,7 @@ import java.sql.SQLException; import java.util.Date; import java.util.List; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.BgReading; @@ -32,7 +33,11 @@ import info.nightscout.androidaps.events.EventNewBasalProfile; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientFragment; +import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripFragment; +import info.nightscout.androidaps.receivers.NSClientDataReceiver; +import info.nightscout.androidaps.receivers.xDripReceiver; import info.nightscout.client.data.NSProfile; import info.nightscout.client.data.NSSgv; @@ -40,8 +45,8 @@ import info.nightscout.client.data.NSSgv; public class DataService extends IntentService { private static Logger log = LoggerFactory.getLogger(DataService.class); - Handler mHandler; - private HandlerThread mHandlerThread; + boolean xDripEnabled = true; + boolean nsClientEnabled = true; public DataService() { super("DataService"); @@ -52,10 +57,23 @@ public class DataService extends IntentService { if (Config.logFunctionCalls) log.debug("onHandleIntent"); + if (MainApp.getConfigBuilder() != null) { + if (MainApp.getConfigBuilder().getActiveBgSource().getClass().equals(SourceXdripFragment.class)) { + xDripEnabled = true; + nsClientEnabled = false; + } + if (MainApp.getConfigBuilder().getActiveBgSource().getClass().equals(SourceNSClientFragment.class)) { + xDripEnabled = false; + nsClientEnabled = true; + } + } + if (intent != null) { final String action = intent.getAction(); if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { - handleNewDataFromXDrip(intent); + if (xDripEnabled) + handleNewDataFromXDrip(intent); + xDripReceiver.completeWakefulIntent(intent); } else if (Intents.ACTION_NEW_PROFILE.equals(action) || Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) || @@ -63,6 +81,7 @@ public class DataService extends IntentService { Intents.ACTION_NEW_SGV.equals(action) ) { handleNewDataFromNSClient(intent); + NSClientDataReceiver.completeWakefulIntent(intent); } } } @@ -74,17 +93,7 @@ public class DataService extends IntentService { if (Config.logFunctionCalls) log.debug("onStartCommand"); - if (mHandlerThread == null) { - if (Config.detailedLog) - log.debug("Creating handler thread"); - - this.mHandlerThread = new HandlerThread(DataService.class.getSimpleName() + "Handler"); - mHandlerThread.start(); - - this.mHandler = new Handler(mHandlerThread.getLooper()); - - registerBus(); - } + registerBus(); return START_STICKY; } @@ -114,6 +123,12 @@ public class DataService extends IntentService { bgReading.timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); + if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (Config.logIncommingBG) + log.debug("Ignoring old XDRIPREC BG " + bgReading.toString()); + return; + } + if (Config.logIncommingBG) log.debug("XDRIPREC BG " + bgReading.toString()); @@ -298,13 +313,18 @@ public class DataService extends IntentService { } if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) { - if (MainActivity.getConfigBuilder().getActiveBgSource().getClass().equals(SourceNSClientFragment.class)) { + if (nsClientEnabled) { try { if (bundles.containsKey("sgv")) { String sgvstring = bundles.getString("sgv"); JSONObject sgvJson = new JSONObject(sgvstring); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); + if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (Config.logIncommingData) + log.debug("Ignoring old BG: " + bgReading.toString()); + return; + } MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading); if (Config.logIncommingData) log.debug("ADD: Stored new BG: " + bgReading.toString()); @@ -317,9 +337,14 @@ public class DataService extends IntentService { JSONObject sgvJson = jsonArray.getJSONObject(i); NSSgv nsSgv = new NSSgv(sgvJson); BgReading bgReading = new BgReading(nsSgv); - MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading); - if (Config.logIncommingData) - log.debug("ADD: Stored new BG: " + bgReading.toString()); + if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) { + if (Config.logIncommingData) + log.debug("Ignoring old BG: " + bgReading.toString()); + } else { + MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading); + if (Config.logIncommingData) + log.debug("ADD: Stored new BG: " + bgReading.toString()); + } } } MainApp.bus().post(new EventTreatmentChange()); 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 47d230bb02..30ef491185 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -7,6 +7,7 @@ import java.util.Date; import java.util.List; import android.content.Context; +import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.os.Parcel; import android.os.Parcelable; @@ -24,6 +25,7 @@ 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.events.EventNewBG; @@ -50,7 +52,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { TableUtils.createTableIfNotExists(connectionSource, TempBasal.class); TableUtils.createTableIfNotExists(connectionSource, Treatment.class); TableUtils.createTableIfNotExists(connectionSource, BgReading.class); - // TODO: add bg support } catch (SQLException e) { log.error(DatabaseHelper.class.getName(), "Can't create database", e); throw new RuntimeException(e); @@ -79,6 +80,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { super.close(); } + public void cleanUpDatabases() { + // TODO: call it somewhere + log.debug("Before BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "BgReadings")); + getWritableDatabase().delete("BgReadings", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + log.debug("After BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "BgReadings")); + + log.debug("Before TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "TempBasals")); + getWritableDatabase().delete("TempBasals", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + log.debug("After TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "TempBasals")); + + log.debug("Before Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "Treatments")); + getWritableDatabase().delete("Treatments", "timeIndex" + " < '" + Math.ceil((new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) / 60000d) + "'", null); + log.debug("After Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "Treatments")); + } + public void resetDatabases() { try { TableUtils.dropTable(connectionSource, TempBasal.class, true); @@ -114,6 +130,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { } public Dao getDaoBgReadings() throws SQLException { + //SQLiteDatabase db = getReadableDatabase(); + //log.debug("BgReadings size: " + DatabaseUtils.queryNumEntries(db, "BgReadings")); return getDao(BgReading.class); } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java index ef006b7400..2c6a9b9237 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/xDripReceiver.java @@ -18,16 +18,10 @@ public class xDripReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (MainActivity.getConfigBuilder().getActiveBgSource() == null) { - log.debug("getActiveBgSource is still null"); - return; - } - if (MainActivity.getConfigBuilder().getActiveBgSource().getClass().equals(SourceXdripFragment.class)) { - if (Config.logFunctionCalls) - log.debug("onReceive " + intent); - startWakefulService(context, new Intent(context, DataService.class) - .setAction(intent.getAction()) - .putExtras(intent)); - } + if (Config.logFunctionCalls) + log.debug("onReceive " + intent); + startWakefulService(context, new Intent(context, DataService.class) + .setAction(intent.getAction()) + .putExtras(intent)); } }