some db and incomming data optimalisation

This commit is contained in:
Milos Kozak 2016-06-21 23:24:54 +02:00
parent edddfed004
commit 2a2589f5eb
6 changed files with 79 additions and 30 deletions

View file

@ -10,4 +10,5 @@ public class Constants {
public static final double MMOLL_TO_MGDL = 18.0182; public static final double MMOLL_TO_MGDL = 18.0182;
public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL; public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL;
public static final int hoursToKeepInDatabase = 24;
} }

View file

@ -84,6 +84,7 @@ public class MainActivity extends AppCompatActivity {
registerBus(); registerBus();
configBuilderFragment.initialize(); configBuilderFragment.initialize();
MainApp.setConfigBuilder(configBuilderFragment);
} }
setUpTabs(false); setUpTabs(false);
} }

View file

@ -10,6 +10,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment;
public class MainApp extends Application { public class MainApp extends Application {
@ -19,6 +20,7 @@ public class MainApp extends Application {
private static MainApp sInstance; private static MainApp sInstance;
private static DatabaseHelper databaseHelper = null; private static DatabaseHelper databaseHelper = null;
private static ConfigBuilderFragment configBuilder = null;
@Override @Override
public void onCreate() { 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 @Override
public void onTerminate() { public void onTerminate() {
super.onTerminate(); super.onTerminate();

View file

@ -23,6 +23,7 @@ import java.sql.SQLException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.db.BgReading; 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.events.EventTreatmentChange;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.interfaces.PumpInterface; 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.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.NSProfile;
import info.nightscout.client.data.NSSgv; import info.nightscout.client.data.NSSgv;
@ -40,8 +45,8 @@ import info.nightscout.client.data.NSSgv;
public class DataService extends IntentService { public class DataService extends IntentService {
private static Logger log = LoggerFactory.getLogger(DataService.class); private static Logger log = LoggerFactory.getLogger(DataService.class);
Handler mHandler; boolean xDripEnabled = true;
private HandlerThread mHandlerThread; boolean nsClientEnabled = true;
public DataService() { public DataService() {
super("DataService"); super("DataService");
@ -52,10 +57,23 @@ public class DataService extends IntentService {
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onHandleIntent"); 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) { if (intent != null) {
final String action = intent.getAction(); final String action = intent.getAction();
if (Intents.ACTION_NEW_BG_ESTIMATE.equals(action)) { 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) || } else if (Intents.ACTION_NEW_PROFILE.equals(action) ||
Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_NEW_TREATMENT.equals(action) ||
Intents.ACTION_CHANGED_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) ||
@ -63,6 +81,7 @@ public class DataService extends IntentService {
Intents.ACTION_NEW_SGV.equals(action) Intents.ACTION_NEW_SGV.equals(action)
) { ) {
handleNewDataFromNSClient(intent); handleNewDataFromNSClient(intent);
NSClientDataReceiver.completeWakefulIntent(intent);
} }
} }
} }
@ -74,17 +93,7 @@ public class DataService extends IntentService {
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onStartCommand"); log.debug("onStartCommand");
if (mHandlerThread == null) { registerBus();
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();
}
return START_STICKY; return START_STICKY;
} }
@ -114,6 +123,12 @@ public class DataService extends IntentService {
bgReading.timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP); bgReading.timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP);
bgReading.raw = bundle.getDouble(Intents.EXTRA_RAW); 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) if (Config.logIncommingBG)
log.debug("XDRIPREC BG " + bgReading.toString()); log.debug("XDRIPREC BG " + bgReading.toString());
@ -298,13 +313,18 @@ public class DataService extends IntentService {
} }
if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) { if (intent.getAction().equals(Intents.ACTION_NEW_SGV)) {
if (MainActivity.getConfigBuilder().getActiveBgSource().getClass().equals(SourceNSClientFragment.class)) { if (nsClientEnabled) {
try { try {
if (bundles.containsKey("sgv")) { if (bundles.containsKey("sgv")) {
String sgvstring = bundles.getString("sgv"); String sgvstring = bundles.getString("sgv");
JSONObject sgvJson = new JSONObject(sgvstring); JSONObject sgvJson = new JSONObject(sgvstring);
NSSgv nsSgv = new NSSgv(sgvJson); NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv); 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); MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("ADD: Stored new BG: " + bgReading.toString()); log.debug("ADD: Stored new BG: " + bgReading.toString());
@ -317,9 +337,14 @@ public class DataService extends IntentService {
JSONObject sgvJson = jsonArray.getJSONObject(i); JSONObject sgvJson = jsonArray.getJSONObject(i);
NSSgv nsSgv = new NSSgv(sgvJson); NSSgv nsSgv = new NSSgv(sgvJson);
BgReading bgReading = new BgReading(nsSgv); BgReading bgReading = new BgReading(nsSgv);
MainApp.getDbHelper().getDaoBgReadings().createIfNotExists(bgReading); if (bgReading.timestamp < new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000l) {
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("ADD: Stored new BG: " + bgReading.toString()); 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()); MainApp.bus().post(new EventTreatmentChange());

View file

@ -7,6 +7,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import android.content.Context; import android.content.Context;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@ -24,6 +25,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventNewBG;
@ -50,7 +52,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class); TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class); TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class); TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
// TODO: add bg support
} catch (SQLException e) { } catch (SQLException e) {
log.error(DatabaseHelper.class.getName(), "Can't create database", e); log.error(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e); throw new RuntimeException(e);
@ -79,6 +80,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
super.close(); 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() { public void resetDatabases() {
try { try {
TableUtils.dropTable(connectionSource, TempBasal.class, true); TableUtils.dropTable(connectionSource, TempBasal.class, true);
@ -114,6 +130,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
} }
public Dao<BgReading, Long> getDaoBgReadings() throws SQLException { public Dao<BgReading, Long> getDaoBgReadings() throws SQLException {
//SQLiteDatabase db = getReadableDatabase();
//log.debug("BgReadings size: " + DatabaseUtils.queryNumEntries(db, "BgReadings"));
return getDao(BgReading.class); return getDao(BgReading.class);
} }

View file

@ -18,16 +18,10 @@ public class xDripReceiver extends WakefulBroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (MainActivity.getConfigBuilder().getActiveBgSource() == null) { if (Config.logFunctionCalls)
log.debug("getActiveBgSource is still null"); log.debug("onReceive " + intent);
return; startWakefulService(context, new Intent(context, DataService.class)
} .setAction(intent.getAction())
if (MainActivity.getConfigBuilder().getActiveBgSource().getClass().equals(SourceXdripFragment.class)) { .putExtras(intent));
if (Config.logFunctionCalls)
log.debug("onReceive " + intent);
startWakefulService(context, new Intent(context, DataService.class)
.setAction(intent.getAction())
.putExtras(intent));
}
} }
} }