2016-06-05 01:40:35 +02:00
|
|
|
package info.nightscout.androidaps.db;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-06-21 23:24:54 +02:00
|
|
|
import android.database.DatabaseUtils;
|
2016-06-05 01:40:35 +02:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2016-06-12 14:18:21 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
|
|
|
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
|
2017-05-22 12:52:19 +02:00
|
|
|
import com.j256.ormlite.dao.CloseableIterator;
|
2016-06-05 01:40:35 +02:00
|
|
|
import com.j256.ormlite.dao.Dao;
|
2016-06-07 21:48:17 +02:00
|
|
|
import com.j256.ormlite.stmt.PreparedQuery;
|
2016-06-05 01:40:35 +02:00
|
|
|
import com.j256.ormlite.stmt.QueryBuilder;
|
2016-06-09 00:01:28 +02:00
|
|
|
import com.j256.ormlite.stmt.Where;
|
2016-06-05 01:40:35 +02:00
|
|
|
import com.j256.ormlite.support.ConnectionSource;
|
|
|
|
import com.j256.ormlite.table.TableUtils;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2016-06-05 01:40:35 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-07-11 18:07:54 +02:00
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2017-01-30 20:00:46 +01:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-07-11 18:07:54 +02:00
|
|
|
|
2017-01-30 20:00:46 +01:00
|
|
|
import info.nightscout.androidaps.Config;
|
2016-06-21 23:24:54 +02:00
|
|
|
import info.nightscout.androidaps.Constants;
|
2016-06-07 21:48:17 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2017-06-02 12:27:21 +02:00
|
|
|
import info.nightscout.androidaps.data.Profile;
|
2017-05-25 20:18:29 +02:00
|
|
|
import info.nightscout.androidaps.events.EventCareportalEventChange;
|
2017-05-22 20:58:05 +02:00
|
|
|
import info.nightscout.androidaps.events.EventExtendedBolusChange;
|
2017-05-22 12:52:19 +02:00
|
|
|
import info.nightscout.androidaps.events.EventNewBG;
|
2017-06-02 12:27:21 +02:00
|
|
|
import info.nightscout.androidaps.events.EventProfileSwitchChange;
|
2017-05-30 22:44:26 +02:00
|
|
|
import info.nightscout.androidaps.events.EventRefreshGui;
|
2017-05-29 11:39:12 +02:00
|
|
|
import info.nightscout.androidaps.events.EventReloadTempBasalData;
|
|
|
|
import info.nightscout.androidaps.events.EventReloadTreatmentData;
|
2017-05-22 20:58:05 +02:00
|
|
|
import info.nightscout.androidaps.events.EventTempBasalChange;
|
2017-05-23 23:56:53 +02:00
|
|
|
import info.nightscout.androidaps.events.EventTempTargetChange;
|
2017-01-30 20:00:46 +01:00
|
|
|
import info.nightscout.androidaps.events.EventTreatmentChange;
|
2017-04-30 10:23:26 +02:00
|
|
|
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
|
2017-05-22 12:52:19 +02:00
|
|
|
import info.nightscout.androidaps.plugins.PumpDanaR.History.DanaRNSHistorySync;
|
2016-06-07 21:48:17 +02:00
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
2016-06-07 21:48:17 +02:00
|
|
|
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
public static final String DATABASE_NAME = "AndroidAPSDb";
|
2016-07-21 15:10:42 +02:00
|
|
|
public static final String DATABASE_BGREADINGS = "BgReadings";
|
2017-05-21 22:05:03 +02:00
|
|
|
public static final String DATABASE_TEMPORARYBASALS = "TemporaryBasals";
|
|
|
|
public static final String DATABASE_EXTENDEDBOLUSES = "ExtendedBoluses";
|
2017-01-13 22:36:47 +01:00
|
|
|
public static final String DATABASE_TEMPTARGETS = "TempTargets";
|
2016-07-21 15:10:42 +02:00
|
|
|
public static final String DATABASE_TREATMENTS = "Treatments";
|
|
|
|
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
|
2017-02-24 13:02:44 +01:00
|
|
|
public static final String DATABASE_DBREQUESTS = "DBRequests";
|
2017-05-25 20:18:29 +02:00
|
|
|
public static final String DATABASE_CAREPORTALEVENTS = "CareportalEvents";
|
2017-06-02 10:25:49 +02:00
|
|
|
public static final String DATABASE_PROFILESWITCHES = "ProfileSwitches";
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-05-21 22:05:03 +02:00
|
|
|
private static final int DATABASE_VERSION = 7;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-06-01 20:52:38 +02:00
|
|
|
private static Long earliestDataChange = null;
|
2017-01-30 20:00:46 +01:00
|
|
|
|
2017-05-23 20:15:14 +02:00
|
|
|
private static final ScheduledExecutorService bgWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledBgPost = null;
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
private static final ScheduledExecutorService treatmentsWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledTratmentPost = null;
|
|
|
|
|
|
|
|
private static final ScheduledExecutorService tempBasalsWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledTemBasalsPost = null;
|
|
|
|
|
2017-05-23 23:56:53 +02:00
|
|
|
private static final ScheduledExecutorService tempTargetWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledTemTargetPost = null;
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
private static final ScheduledExecutorService extendedBolusWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledExtendedBolusPost = null;
|
2017-01-30 20:00:46 +01:00
|
|
|
|
2017-05-25 20:18:29 +02:00
|
|
|
private static final ScheduledExecutorService careportalEventWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledCareportalEventPost = null;
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static final ScheduledExecutorService profileSwitchEventWorker = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
private static ScheduledFuture<?> scheduledProfileSwitchEventPost = null;
|
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
public DatabaseHelper(Context context) {
|
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
2017-01-14 00:37:35 +01:00
|
|
|
onCreate(getWritableDatabase(), getConnectionSource());
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
|
2016-06-07 21:48:17 +02:00
|
|
|
try {
|
|
|
|
log.info("onCreate");
|
2017-01-13 22:36:47 +01:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
|
2016-06-07 21:48:17 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
2016-07-21 15:10:42 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
2017-02-24 13:02:44 +01:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
|
2017-05-22 12:52:19 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
|
2017-05-25 20:18:29 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
|
2017-06-02 10:25:49 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
|
2016-06-07 21:48:17 +02:00
|
|
|
} catch (SQLException e) {
|
2017-01-14 00:37:35 +01:00
|
|
|
log.error("Can't create database", e);
|
2016-06-07 21:48:17 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
|
2016-06-07 21:48:17 +02:00
|
|
|
try {
|
|
|
|
log.info(DatabaseHelper.class.getName(), "onUpgrade");
|
2017-01-13 22:36:47 +01:00
|
|
|
TableUtils.dropTable(connectionSource, TempTarget.class, true);
|
2016-06-07 21:48:17 +02:00
|
|
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
|
|
|
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
2016-07-21 15:10:42 +02:00
|
|
|
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
2017-02-24 13:02:44 +01:00
|
|
|
TableUtils.dropTable(connectionSource, DbRequest.class, true);
|
2017-05-22 12:52:19 +02:00
|
|
|
TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
|
|
|
|
TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
|
2017-05-25 20:18:29 +02:00
|
|
|
TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
|
2017-06-02 10:25:49 +02:00
|
|
|
TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
|
2016-06-07 21:48:17 +02:00
|
|
|
onCreate(database, connectionSource);
|
|
|
|
} catch (SQLException e) {
|
2017-01-14 00:37:35 +01:00
|
|
|
log.error("Can't drop databases", e);
|
2016-06-07 21:48:17 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-07 21:48:17 +02:00
|
|
|
* Close the database connections and clear any cached DAOs.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
super.close();
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:24:54 +02:00
|
|
|
public void cleanUpDatabases() {
|
|
|
|
// TODO: call it somewhere
|
2016-07-21 15:10:42 +02:00
|
|
|
log.debug("Before BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
|
2017-05-21 22:05:03 +02:00
|
|
|
getWritableDatabase().delete(DATABASE_BGREADINGS, "date" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
|
2016-07-21 15:10:42 +02:00
|
|
|
log.debug("After BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
|
2016-06-21 23:24:54 +02:00
|
|
|
|
2017-01-13 22:36:47 +01:00
|
|
|
log.debug("Before TempTargets size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPTARGETS));
|
2017-05-21 22:05:03 +02:00
|
|
|
getWritableDatabase().delete(DATABASE_TEMPTARGETS, "date" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
|
2017-01-13 22:36:47 +01:00
|
|
|
log.debug("After TempTargets size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPTARGETS));
|
|
|
|
|
2016-07-21 15:10:42 +02:00
|
|
|
log.debug("Before Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
|
2017-05-21 22:05:03 +02:00
|
|
|
getWritableDatabase().delete(DATABASE_TREATMENTS, "date" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
|
2016-07-21 15:10:42 +02:00
|
|
|
log.debug("After Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
|
2016-07-08 00:17:02 +02:00
|
|
|
|
2017-01-13 22:36:47 +01:00
|
|
|
log.debug("Before History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_DANARHISTORY));
|
|
|
|
getWritableDatabase().delete(DATABASE_DANARHISTORY, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
|
|
|
|
log.debug("After History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_DANARHISTORY));
|
2017-05-22 12:52:19 +02:00
|
|
|
|
|
|
|
log.debug("Before TemporaryBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPORARYBASALS));
|
|
|
|
getWritableDatabase().delete(DATABASE_TEMPORARYBASALS, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
|
|
|
|
log.debug("After TemporaryBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPORARYBASALS));
|
|
|
|
|
|
|
|
log.debug("Before ExtendedBoluses size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_EXTENDEDBOLUSES));
|
|
|
|
getWritableDatabase().delete(DATABASE_EXTENDEDBOLUSES, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
|
|
|
|
log.debug("After ExtendedBoluses size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_EXTENDEDBOLUSES));
|
2017-05-25 20:18:29 +02:00
|
|
|
|
|
|
|
log.debug("Before CareportalEvent size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_CAREPORTALEVENTS));
|
|
|
|
getWritableDatabase().delete(DATABASE_CAREPORTALEVENTS, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
|
|
|
|
log.debug("After CareportalEvent size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_CAREPORTALEVENTS));
|
2017-06-02 10:25:49 +02:00
|
|
|
|
|
|
|
log.debug("Before ProfileSwitch size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_PROFILESWITCHES));
|
|
|
|
getWritableDatabase().delete(DATABASE_PROFILESWITCHES, "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
|
|
|
|
log.debug("After ProfileSwitch size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_PROFILESWITCHES));
|
2016-06-21 23:24:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
public long size(String database) {
|
|
|
|
return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------- DB resets ---------------------
|
|
|
|
|
2016-06-07 21:48:17 +02:00
|
|
|
public void resetDatabases() {
|
|
|
|
try {
|
2017-01-13 22:36:47 +01:00
|
|
|
TableUtils.dropTable(connectionSource, TempTarget.class, true);
|
2016-06-07 21:48:17 +02:00
|
|
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
|
|
|
TableUtils.dropTable(connectionSource, BgReading.class, true);
|
2016-07-21 15:10:42 +02:00
|
|
|
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
|
2017-05-22 12:52:19 +02:00
|
|
|
TableUtils.dropTable(connectionSource, DbRequest.class, true);
|
|
|
|
TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
|
|
|
|
TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
|
2017-05-25 20:18:29 +02:00
|
|
|
TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
|
2017-06-02 10:25:49 +02:00
|
|
|
TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
|
2017-01-13 22:36:47 +01:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
|
2016-06-07 21:48:17 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
2016-07-21 15:10:42 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
2017-05-22 12:52:19 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, DbRequest.class);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
|
2017-05-25 20:18:29 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
|
2017-06-02 10:25:49 +02:00
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(0);
|
2016-06-07 21:48:17 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-23 20:15:14 +02:00
|
|
|
scheduleBgChange(); // trigger refresh
|
2017-05-23 22:59:06 +02:00
|
|
|
scheduleTemporaryBasalChange();
|
|
|
|
scheduleTreatmentChange();
|
|
|
|
scheduleExtendedBolusChange();
|
2017-05-23 23:56:53 +02:00
|
|
|
scheduleTemporaryTargetChange();
|
2017-05-25 20:18:29 +02:00
|
|
|
scheduleCareportalEventChange();
|
2017-06-02 10:25:49 +02:00
|
|
|
scheduleProfileSwitchChange();
|
2017-05-30 22:44:26 +02:00
|
|
|
new java.util.Timer().schedule(
|
|
|
|
new java.util.TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
MainApp.bus().post(new EventRefreshGui(false));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
3000
|
|
|
|
);
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void resetTreatments() {
|
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(0);
|
2016-06-07 21:48:17 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduleTreatmentChange();
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-01-14 16:36:42 +01:00
|
|
|
public void resetTempTargets() {
|
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, TempTarget.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
2017-05-23 23:56:53 +02:00
|
|
|
scheduleTemporaryTargetChange();
|
2016-06-07 21:48:17 +02:00
|
|
|
}
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
public void resetTemporaryBasals() {
|
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(0);
|
2017-05-22 12:52:19 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduleTemporaryBasalChange();
|
2017-05-22 12:52:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void resetExtededBoluses() {
|
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(0);
|
2017-05-22 12:52:19 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduleExtendedBolusChange();
|
2017-05-22 12:52:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 15:26:25 +02:00
|
|
|
public void resetCareportalEvents() {
|
2017-05-25 20:18:29 +02:00
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleCareportalEventChange();
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:27:21 +02:00
|
|
|
public void resetProfileSwitch() {
|
2017-06-02 10:25:49 +02:00
|
|
|
try {
|
|
|
|
TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
|
|
|
|
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleProfileSwitchChange();
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
// ------------------ getDao -------------------------------------------
|
|
|
|
|
|
|
|
private Dao<TempTarget, Long> getDaoTempTargets() throws SQLException {
|
2017-01-13 22:36:47 +01:00
|
|
|
return getDao(TempTarget.class);
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 20:00:46 +01:00
|
|
|
private Dao<Treatment, Long> getDaoTreatments() throws SQLException {
|
2016-06-07 21:48:17 +02:00
|
|
|
return getDao(Treatment.class);
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
private Dao<BgReading, Long> getDaoBgReadings() throws SQLException {
|
2016-06-07 21:48:17 +02:00
|
|
|
return getDao(BgReading.class);
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
private Dao<DanaRHistoryRecord, String> getDaoDanaRHistory() throws SQLException {
|
2016-07-21 15:10:42 +02:00
|
|
|
return getDao(DanaRHistoryRecord.class);
|
2016-07-08 00:17:02 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
private Dao<DbRequest, String> getDaoDbRequest() throws SQLException {
|
2017-02-24 13:02:44 +01:00
|
|
|
return getDao(DbRequest.class);
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
private Dao<TemporaryBasal, Long> getDaoTemporaryBasal() throws SQLException {
|
2017-05-22 12:52:19 +02:00
|
|
|
return getDao(TemporaryBasal.class);
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
private Dao<ExtendedBolus, Long> getDaoExtendedBolus() throws SQLException {
|
2017-05-22 12:52:19 +02:00
|
|
|
return getDao(ExtendedBolus.class);
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:18:29 +02:00
|
|
|
private Dao<CareportalEvent, Long> getDaoCareportalEvents() throws SQLException {
|
|
|
|
return getDao(CareportalEvent.class);
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private Dao<ProfileSwitch, Long> getDaoProfileSwitch() throws SQLException {
|
|
|
|
return getDao(ProfileSwitch.class);
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
// ------------------- BgReading handling -----------------------
|
|
|
|
|
|
|
|
public void createIfNotExists(BgReading bgReading) {
|
2017-05-23 22:59:06 +02:00
|
|
|
bgReading.date = bgReading.date - bgReading.date % 1000;
|
2017-05-22 12:52:19 +02:00
|
|
|
try {
|
|
|
|
getDaoBgReadings().createIfNotExists(bgReading);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-23 20:15:14 +02:00
|
|
|
scheduleBgChange();
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleBgChange() {
|
2017-05-23 20:15:14 +02:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing EventNewBg");
|
2017-05-23 20:15:14 +02:00
|
|
|
MainApp.bus().post(new EventNewBG());
|
|
|
|
scheduledBgPost = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// prepare task for execution in 1 sec
|
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledBgPost != null)
|
|
|
|
scheduledBgPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
|
|
|
final int sec = 1;
|
|
|
|
scheduledBgPost = bgWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
2017-02-24 13:02:44 +01:00
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
/*
|
|
|
|
* Return last BgReading from database or null if db is empty
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public static BgReading lastBg() {
|
|
|
|
List<BgReading> bgList = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Dao<BgReading, Long> daoBgReadings = MainApp.getDbHelper().getDaoBgReadings();
|
|
|
|
QueryBuilder<BgReading, Long> queryBuilder = daoBgReadings.queryBuilder();
|
|
|
|
queryBuilder.orderBy("date", false);
|
|
|
|
queryBuilder.limit(1L);
|
|
|
|
queryBuilder.where().gt("value", 38);
|
|
|
|
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
|
|
|
|
bgList = daoBgReadings.query(preparedQuery);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
log.debug(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
if (bgList != null && bgList.size() > 0)
|
|
|
|
return bgList.get(0);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return bg reading if not old ( <9 min )
|
|
|
|
* or null if older
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public static BgReading actualBg() {
|
|
|
|
BgReading lastBg = lastBg();
|
|
|
|
|
|
|
|
if (lastBg == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if (lastBg.date > new Date().getTime() - 9 * 60 * 1000)
|
|
|
|
return lastBg;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-09 10:57:15 +01:00
|
|
|
public List<BgReading> getBgreadingsDataFromTime(long mills, boolean ascending) {
|
2016-06-09 00:01:28 +02:00
|
|
|
try {
|
|
|
|
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
|
|
|
|
List<BgReading> bgReadings;
|
|
|
|
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
|
2017-05-21 22:05:03 +02:00
|
|
|
queryBuilder.orderBy("date", ascending);
|
2016-06-09 00:01:28 +02:00
|
|
|
Where where = queryBuilder.where();
|
2017-05-21 22:05:03 +02:00
|
|
|
where.ge("date", mills).and().gt("value", 38);
|
2016-06-09 00:01:28 +02:00
|
|
|
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
|
|
|
|
bgReadings = daoBgreadings.query(preparedQuery);
|
|
|
|
return bgReadings;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<BgReading>();
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
// ------------- DbRequests handling -------------------
|
2017-02-24 13:02:44 +01:00
|
|
|
|
|
|
|
public void create(DbRequest dbr) {
|
|
|
|
try {
|
|
|
|
getDaoDbRequest().create(dbr);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 10:23:26 +02:00
|
|
|
public int delete(DbRequest dbr) {
|
2017-02-24 13:02:44 +01:00
|
|
|
try {
|
|
|
|
return getDaoDbRequest().delete(dbr);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int deleteDbRequest(String nsClientId) {
|
|
|
|
try {
|
|
|
|
return getDaoDbRequest().deleteById(nsClientId);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int deleteDbRequestbyMongoId(String action, String id) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<DbRequest, String> queryBuilder = getDaoDbRequest().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", id).and().eq("action", action);
|
|
|
|
queryBuilder.limit(10L);
|
|
|
|
PreparedQuery<DbRequest> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<DbRequest> dbList = getDaoDbRequest().query(preparedQuery);
|
|
|
|
if (dbList.size() != 1) {
|
|
|
|
log.error("deleteDbRequestbyMongoId query size: " + dbList.size());
|
|
|
|
} else {
|
|
|
|
//log.debug("Treatment findTreatmentById found: " + trList.get(0).log());
|
|
|
|
return delete(dbList.get(0));
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteAllDbRequests() {
|
|
|
|
try {
|
|
|
|
TableUtils.clearTable(connectionSource, DbRequest.class);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
public CloseableIterator getDbRequestInterator() {
|
|
|
|
try {
|
|
|
|
return getDaoDbRequest().closeableIterator();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------- TREATMENT HANDLING -------------------
|
2017-01-30 20:00:46 +01:00
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private boolean changeAffectingIobCob(Treatment t) {
|
2017-05-30 22:44:26 +02:00
|
|
|
Treatment existing = findTreatmentByTime(t.date);
|
2017-04-30 10:23:26 +02:00
|
|
|
if (existing == null)
|
|
|
|
return true;
|
|
|
|
if (existing.insulin == t.insulin && existing.carbs == t.carbs)
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-01-30 20:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
|
2017-05-22 22:10:56 +02:00
|
|
|
treatment.date = treatment.date - treatment.date % 1000;
|
2017-01-30 20:00:46 +01:00
|
|
|
Dao.CreateOrUpdateStatus status = null;
|
|
|
|
try {
|
2017-05-22 22:10:56 +02:00
|
|
|
boolean historyChange = changeAffectingIobCob(treatment);
|
2017-01-30 20:00:46 +01:00
|
|
|
status = getDaoTreatments().createOrUpdate(treatment);
|
2017-04-30 10:23:26 +02:00
|
|
|
if (historyChange)
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(treatment.date);
|
2017-01-30 20:00:46 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleTreatmentChange();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:13:13 +01:00
|
|
|
public void delete(Treatment treatment) {
|
|
|
|
try {
|
|
|
|
getDaoTreatments().delete(treatment);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(treatment.date);
|
2017-02-14 21:13:13 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleTreatmentChange();
|
|
|
|
}
|
|
|
|
|
2017-05-29 15:26:25 +02:00
|
|
|
public void deleteTreatmentById(String _id) {
|
|
|
|
Treatment stored = findTreatmentById(_id);
|
|
|
|
if (stored != null) {
|
|
|
|
log.debug("Removing TempTarget record from database: " + stored.log());
|
|
|
|
delete(stored);
|
|
|
|
} else {
|
|
|
|
log.debug("Treatment not found database: " + _id);
|
2017-01-30 20:00:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public Treatment findTreatmentById(String _id) {
|
|
|
|
try {
|
|
|
|
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
|
|
|
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
2017-02-11 12:35:08 +01:00
|
|
|
queryBuilder.limit(10L);
|
2017-01-30 20:00:46 +01:00
|
|
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
|
|
|
if (trList.size() != 1) {
|
|
|
|
//log.debug("Treatment findTreatmentById query size: " + trList.size());
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
//log.debug("Treatment findTreatmentById found: " + trList.get(0).log());
|
|
|
|
return trList.get(0);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2017-05-30 22:44:26 +02:00
|
|
|
public Treatment findTreatmentByTime(Long timeIndex) {
|
2017-01-30 20:00:46 +01:00
|
|
|
try {
|
|
|
|
QueryBuilder<Treatment, String> qb = null;
|
|
|
|
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
|
|
|
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
2017-05-21 22:05:03 +02:00
|
|
|
where.eq("date", timeIndex);
|
2017-02-11 12:35:08 +01:00
|
|
|
queryBuilder.limit(10L);
|
2017-01-30 20:00:46 +01:00
|
|
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
|
|
|
if (trList.size() != 1) {
|
2017-05-30 22:44:26 +02:00
|
|
|
//log.debug("Treatment findTreatmentByTime query size: " + trList.size());
|
2017-01-30 20:00:46 +01:00
|
|
|
return null;
|
|
|
|
} else {
|
2017-05-30 22:44:26 +02:00
|
|
|
//log.debug("Treatment findTreatmentByTime found: " + trList.get(0).log());
|
2017-01-30 20:00:46 +01:00
|
|
|
return trList.get(0);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private void updateEarliestDataChange(long newDate) {
|
2017-06-01 20:52:38 +02:00
|
|
|
if (earliestDataChange == null) {
|
|
|
|
earliestDataChange = newDate;
|
2017-05-29 15:26:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-01 20:52:38 +02:00
|
|
|
if (newDate < earliestDataChange) {
|
|
|
|
earliestDataChange = newDate;
|
2017-05-29 15:26:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleTreatmentChange() {
|
2017-01-30 20:00:46 +01:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing EventTreatmentChange");
|
2017-05-29 11:39:12 +02:00
|
|
|
MainApp.bus().post(new EventReloadTreatmentData());
|
2017-01-30 20:00:46 +01:00
|
|
|
MainApp.bus().post(new EventTreatmentChange());
|
2017-06-01 20:52:38 +02:00
|
|
|
if (earliestDataChange != null)
|
|
|
|
MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
|
|
|
|
earliestDataChange = null;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledTratmentPost = null;
|
2017-01-30 20:00:46 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 22:10:56 +02:00
|
|
|
// prepare task for execution in 1 sec
|
2017-01-30 20:00:46 +01:00
|
|
|
// cancel waiting task to prevent sending multiple posts
|
2017-05-22 20:58:05 +02:00
|
|
|
if (scheduledTratmentPost != null)
|
|
|
|
scheduledTratmentPost.cancel(false);
|
2017-01-30 20:00:46 +01:00
|
|
|
Runnable task = new PostRunnable();
|
2017-05-22 22:10:56 +02:00
|
|
|
final int sec = 1;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledTratmentPost = treatmentsWorker.schedule(task, sec, TimeUnit.SECONDS);
|
2017-01-30 20:00:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-09 10:57:15 +01:00
|
|
|
public List<Treatment> getTreatmentDataFromTime(long mills, boolean ascending) {
|
|
|
|
try {
|
|
|
|
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
|
|
|
List<Treatment> treatments;
|
|
|
|
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
2017-05-21 22:05:03 +02:00
|
|
|
queryBuilder.orderBy("date", ascending);
|
2017-01-09 10:57:15 +01:00
|
|
|
Where where = queryBuilder.where();
|
2017-05-21 22:05:03 +02:00
|
|
|
where.ge("date", mills);
|
2017-01-09 10:57:15 +01:00
|
|
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
|
|
treatments = daoTreatments.query(preparedQuery);
|
|
|
|
return treatments;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<Treatment>();
|
|
|
|
}
|
|
|
|
|
2017-05-29 11:39:12 +02:00
|
|
|
public void createTreatmentFromJsonIfNotExists(JSONObject trJson) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<Treatment, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoTreatments().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<Treatment> list = getDaoTreatments().query(preparedQuery);
|
|
|
|
Treatment treatment;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
treatment = new Treatment();
|
|
|
|
treatment.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding Treatment record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
treatment = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating Treatment record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
treatment.date = trJson.getLong("mills");
|
|
|
|
treatment.carbs = trJson.has("carbs") ? trJson.getDouble("carbs") : 0;
|
|
|
|
treatment.insulin = trJson.has("insulin") ? trJson.getDouble("insulin") : 0d;
|
|
|
|
treatment._id = trJson.getString("_id");
|
|
|
|
if (trJson.has("eventType")) {
|
2017-06-02 10:25:49 +02:00
|
|
|
treatment.mealBolus = !trJson.get("eventType").equals("Correction Bolus");
|
2017-05-29 11:39:12 +02:00
|
|
|
double carbs = treatment.carbs;
|
|
|
|
if (trJson.has("boluscalc")) {
|
|
|
|
JSONObject boluscalc = trJson.getJSONObject("boluscalc");
|
|
|
|
if (boluscalc.has("carbs")) {
|
|
|
|
carbs = Math.max(boluscalc.getDouble("carbs"), carbs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (carbs <= 0)
|
|
|
|
treatment.mealBolus = false;
|
|
|
|
}
|
|
|
|
createOrUpdate(treatment);
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-29 11:39:12 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
// ---------------- TempTargets handling ---------------
|
|
|
|
|
2017-01-13 23:43:17 +01:00
|
|
|
public List<TempTarget> getTemptargetsDataFromTime(long mills, boolean ascending) {
|
|
|
|
try {
|
|
|
|
Dao<TempTarget, Long> daoTempTargets = getDaoTempTargets();
|
|
|
|
List<TempTarget> tempTargets;
|
|
|
|
QueryBuilder<TempTarget, Long> queryBuilder = daoTempTargets.queryBuilder();
|
2017-05-21 22:05:03 +02:00
|
|
|
queryBuilder.orderBy("date", ascending);
|
2017-01-13 23:43:17 +01:00
|
|
|
Where where = queryBuilder.where();
|
2017-05-21 22:05:03 +02:00
|
|
|
where.ge("date", mills);
|
2017-01-13 23:43:17 +01:00
|
|
|
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
|
|
|
|
tempTargets = daoTempTargets.query(preparedQuery);
|
|
|
|
return tempTargets;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<TempTarget>();
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
public void createOrUpdate(TempTarget tempTarget) {
|
|
|
|
tempTarget.date = tempTarget.date - tempTarget.date % 1000;
|
2017-05-22 12:52:19 +02:00
|
|
|
try {
|
2017-05-23 22:59:06 +02:00
|
|
|
getDaoTempTargets().createOrUpdate(tempTarget);
|
2017-05-23 23:56:53 +02:00
|
|
|
scheduleTemporaryTargetChange();
|
2017-05-22 12:52:19 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 14:42:02 +02:00
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
public void delete(TempTarget tempTarget) {
|
2017-01-09 10:57:15 +01:00
|
|
|
try {
|
2017-05-22 12:52:19 +02:00
|
|
|
getDaoTempTargets().delete(tempTarget);
|
2017-05-23 23:56:53 +02:00
|
|
|
scheduleTemporaryTargetChange();
|
2017-05-22 12:52:19 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleTemporaryTargetChange() {
|
2017-05-23 23:56:53 +02:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing EventTempTargetChange");
|
2017-05-23 23:56:53 +02:00
|
|
|
MainApp.bus().post(new EventTempTargetChange());
|
|
|
|
scheduledTemTargetPost = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// prepare task for execution in 1 sec
|
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledTemTargetPost != null)
|
|
|
|
scheduledTemTargetPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
|
|
|
final int sec = 1;
|
|
|
|
scheduledTemTargetPost = tempTargetWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
/*
|
|
|
|
{
|
|
|
|
"_id": "58795998aa86647ba4d68ce7",
|
|
|
|
"enteredBy": "",
|
|
|
|
"eventType": "Temporary Target",
|
|
|
|
"reason": "Eating Soon",
|
|
|
|
"targetTop": 80,
|
|
|
|
"targetBottom": 80,
|
|
|
|
"duration": 120,
|
|
|
|
"created_at": "2017-01-13T22:50:00.782Z",
|
|
|
|
"carbs": null,
|
|
|
|
"insulin": null
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-05-23 20:15:14 +02:00
|
|
|
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
|
2017-05-22 12:52:19 +02:00
|
|
|
try {
|
|
|
|
QueryBuilder<TempTarget, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoTempTargets().queryBuilder();
|
2017-01-09 10:57:15 +01:00
|
|
|
Where where = queryBuilder.where();
|
2017-05-22 12:52:19 +02:00
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
2017-06-02 10:25:49 +02:00
|
|
|
Profile profile = MainApp.getConfigBuilder().getProfile();
|
2017-05-22 12:52:19 +02:00
|
|
|
String units = profile.getUnits();
|
|
|
|
TempTarget tempTarget;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
tempTarget = new TempTarget();
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding TempTarget record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
tempTarget = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating TempTarget record in database: " + trJson.toString());
|
|
|
|
} else {
|
2017-05-23 22:59:06 +02:00
|
|
|
log.error("Something went wrong");
|
2017-05-22 12:52:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tempTarget.date = trJson.getLong("mills");
|
|
|
|
tempTarget.durationInMinutes = trJson.getInt("duration");
|
2017-06-02 10:25:49 +02:00
|
|
|
tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
|
|
|
|
tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
|
2017-05-22 12:52:19 +02:00
|
|
|
tempTarget.reason = trJson.getString("reason");
|
|
|
|
tempTarget._id = trJson.getString("_id");
|
2017-05-23 22:59:06 +02:00
|
|
|
createOrUpdate(tempTarget);
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-22 12:52:19 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteTempTargetById(String _id) {
|
|
|
|
try {
|
2017-05-23 22:59:06 +02:00
|
|
|
QueryBuilder<TempTarget, Long> queryBuilder = getDaoTempTargets().queryBuilder();
|
2017-05-22 12:52:19 +02:00
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
|
|
|
PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<TempTarget> list = getDaoTempTargets().query(preparedQuery);
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
TempTarget record = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Removing TempTarget record from database: " + record.log());
|
2017-05-29 15:26:25 +02:00
|
|
|
delete(record);
|
2017-05-22 12:52:19 +02:00
|
|
|
} else {
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("TempTarget not found database: " + _id);
|
|
|
|
}
|
2017-01-09 10:57:15 +01:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
// ----------------- DanaRHistory handling --------------------
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
public void createOrUpdate(DanaRHistoryRecord record) {
|
2017-05-22 12:52:19 +02:00
|
|
|
try {
|
2017-05-23 22:59:06 +02:00
|
|
|
getDaoDanaRHistory().createOrUpdate(record);
|
2017-05-22 12:52:19 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DanaRHistoryRecord> getDanaRHistoryRecordsByType(byte type) {
|
|
|
|
List<DanaRHistoryRecord> historyList;
|
|
|
|
try {
|
|
|
|
QueryBuilder<DanaRHistoryRecord, String> queryBuilder = getDaoDanaRHistory().queryBuilder();
|
|
|
|
queryBuilder.orderBy("recordDate", false);
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("recordCode", type);
|
|
|
|
queryBuilder.limit(200L);
|
|
|
|
PreparedQuery<DanaRHistoryRecord> preparedQuery = queryBuilder.prepare();
|
|
|
|
historyList = getDaoDanaRHistory().query(preparedQuery);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
historyList = new ArrayList<>();
|
|
|
|
}
|
|
|
|
return historyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateDanaRHistoryRecordId(JSONObject trJson) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<DanaRHistoryRecord, String> queryBuilder = getDaoDanaRHistory().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.ge("bytes", trJson.get(DanaRNSHistorySync.DANARSIGNATURE));
|
|
|
|
PreparedQuery<DanaRHistoryRecord> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<DanaRHistoryRecord> list = getDaoDanaRHistory().query(preparedQuery);
|
|
|
|
if (list.size() == 0) {
|
|
|
|
// Record does not exists. Ignore
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
DanaRHistoryRecord record = list.get(0);
|
|
|
|
if (record._id == null || !record._id.equals(trJson.getString("_id"))) {
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating _id in DanaR history database: " + trJson.getString("_id"));
|
|
|
|
record._id = trJson.getString("_id");
|
|
|
|
getDaoDanaRHistory().update(record);
|
|
|
|
} else {
|
|
|
|
// already set
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-22 12:52:19 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
// ------------ TemporaryBasal handling ---------------
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
public void createOrUpdate(TemporaryBasal tempBasal) {
|
2017-05-22 22:10:56 +02:00
|
|
|
tempBasal.date = tempBasal.date - tempBasal.date % 1000;
|
2017-05-22 20:58:05 +02:00
|
|
|
try {
|
2017-05-23 22:59:06 +02:00
|
|
|
getDaoTemporaryBasal().createOrUpdate(tempBasal);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(tempBasal.date);
|
2017-05-22 20:58:05 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleTemporaryBasalChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(TemporaryBasal tempBasal) {
|
|
|
|
try {
|
|
|
|
getDaoTemporaryBasal().delete(tempBasal);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(tempBasal.date);
|
2017-05-22 20:58:05 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleTemporaryBasalChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<TemporaryBasal> getTemporaryBasalsDataFromTime(long mills, boolean ascending) {
|
|
|
|
try {
|
|
|
|
List<TemporaryBasal> tempbasals;
|
|
|
|
QueryBuilder<TemporaryBasal, Long> queryBuilder = getDaoTemporaryBasal().queryBuilder();
|
|
|
|
queryBuilder.orderBy("date", ascending);
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.ge("date", mills);
|
|
|
|
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
|
|
|
tempbasals = getDaoTemporaryBasal().query(preparedQuery);
|
|
|
|
return tempbasals;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<TemporaryBasal>();
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:44:26 +02:00
|
|
|
@Nullable
|
|
|
|
public TemporaryBasal findTempBasalByTime(Long timeIndex) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<TemporaryBasal, String> qb = null;
|
|
|
|
Dao<TemporaryBasal, Long> daoTemporaryBasal = getDaoTemporaryBasal();
|
|
|
|
QueryBuilder<TemporaryBasal, Long> queryBuilder = daoTemporaryBasal.queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("date", timeIndex);
|
|
|
|
queryBuilder.limit(10L);
|
|
|
|
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<TemporaryBasal> trList = daoTemporaryBasal.query(preparedQuery);
|
|
|
|
if (trList.size() != 1) {
|
|
|
|
//log.debug("TemporaryBasal findTempBasalByTime query size: " + trList.size());
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
//log.debug("TemporaryBasal findTempBasalByTime found: " + trList.get(0).log());
|
|
|
|
return trList.get(0);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleTemporaryBasalChange() {
|
2017-05-22 20:58:05 +02:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing EventTempBasalChange");
|
2017-05-29 11:39:12 +02:00
|
|
|
MainApp.bus().post(new EventReloadTempBasalData());
|
2017-05-22 20:58:05 +02:00
|
|
|
MainApp.bus().post(new EventTempBasalChange());
|
2017-06-01 20:52:38 +02:00
|
|
|
if (earliestDataChange != null)
|
|
|
|
MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
|
|
|
|
earliestDataChange = null;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledTemBasalsPost = null;
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 22:10:56 +02:00
|
|
|
// prepare task for execution in 1 sec
|
2017-05-22 20:58:05 +02:00
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledTemBasalsPost != null)
|
|
|
|
scheduledTemBasalsPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
2017-05-22 22:10:56 +02:00
|
|
|
final int sec = 1;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledTemBasalsPost = tempBasalsWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
/*
|
|
|
|
{
|
|
|
|
"_id": "59232e1ddd032d04218dab00",
|
|
|
|
"eventType": "Temp Basal",
|
|
|
|
"duration": 60,
|
|
|
|
"percent": -50,
|
|
|
|
"created_at": "2017-05-22T18:29:57Z",
|
|
|
|
"enteredBy": "AndroidAPS",
|
|
|
|
"notes": "Basal Temp Start 50% 60.0 min",
|
|
|
|
"NSCLIENT_ID": 1495477797863,
|
|
|
|
"mills": 1495477797000,
|
|
|
|
"mgdl": 194.5,
|
|
|
|
"endmills": 1495481397000
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-05-23 20:15:14 +02:00
|
|
|
public void createTempBasalFromJsonIfNotExists(JSONObject trJson) {
|
|
|
|
try {
|
2017-05-26 08:54:48 +02:00
|
|
|
if (trJson.has("originalExtendedAmount")) { // extended bolus uploaded as temp basal
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
|
|
|
ExtendedBolus extendedBolus;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
extendedBolus = new ExtendedBolus();
|
|
|
|
extendedBolus.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding ExtendedBolus record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
extendedBolus = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating ExtendedBolus record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
extendedBolus.date = trJson.getLong("mills");
|
|
|
|
extendedBolus.durationInMinutes = trJson.getInt("duration");
|
|
|
|
extendedBolus.insulin = trJson.getDouble("originalExtendedAmount");
|
|
|
|
extendedBolus._id = trJson.getString("_id");
|
|
|
|
createOrUpdate(extendedBolus);
|
|
|
|
} else if (trJson.has("isFakedTempBasal")) { // extended bolus end uploaded as temp basal end
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
|
|
|
ExtendedBolus extendedBolus;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
extendedBolus = new ExtendedBolus();
|
|
|
|
extendedBolus.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding ExtendedBolus record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
extendedBolus = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating ExtendedBolus record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
extendedBolus.date = trJson.getLong("mills");
|
|
|
|
extendedBolus.durationInMinutes = 0;
|
|
|
|
extendedBolus.insulin = 0;
|
|
|
|
extendedBolus._id = trJson.getString("_id");
|
|
|
|
createOrUpdate(extendedBolus);
|
2017-05-23 20:15:14 +02:00
|
|
|
} else {
|
2017-05-26 08:54:48 +02:00
|
|
|
QueryBuilder<TemporaryBasal, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoTemporaryBasal().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<TemporaryBasal> list = getDaoTemporaryBasal().query(preparedQuery);
|
|
|
|
TemporaryBasal tempBasal;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
tempBasal = new TemporaryBasal();
|
|
|
|
tempBasal.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding TemporaryBasal record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
tempBasal = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating TemporaryBasal record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tempBasal.date = trJson.getLong("mills");
|
|
|
|
if (trJson.has("duration")) {
|
|
|
|
tempBasal.durationInMinutes = trJson.getInt("duration");
|
|
|
|
}
|
|
|
|
if (trJson.has("percent")) {
|
|
|
|
tempBasal.percentRate = trJson.getInt("percent") + 100;
|
|
|
|
tempBasal.isAbsolute = false;
|
|
|
|
}
|
|
|
|
if (trJson.has("absolute")) {
|
|
|
|
tempBasal.absoluteRate = trJson.getDouble("absolute");
|
|
|
|
tempBasal.isAbsolute = true;
|
|
|
|
}
|
|
|
|
tempBasal._id = trJson.getString("_id");
|
|
|
|
createOrUpdate(tempBasal);
|
2017-05-23 20:15:14 +02:00
|
|
|
}
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-23 20:15:14 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteTempBasalById(String _id) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<TemporaryBasal, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoTemporaryBasal().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
|
|
|
PreparedQuery<TemporaryBasal> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<TemporaryBasal> list = getDaoTemporaryBasal().query(preparedQuery);
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
TemporaryBasal record = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Removing TempBasal record from database: " + record.log());
|
2017-05-29 15:26:25 +02:00
|
|
|
delete(record);
|
2017-05-23 20:15:14 +02:00
|
|
|
} else {
|
|
|
|
if (Config.logIncommingData)
|
2017-05-23 22:59:06 +02:00
|
|
|
log.debug("TempBasal not found database: " + _id);
|
2017-05-23 20:15:14 +02:00
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:58:05 +02:00
|
|
|
// ------------ ExtendedBolus handling ---------------
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
public void createOrUpdate(ExtendedBolus extendedBolus) {
|
|
|
|
extendedBolus.date = extendedBolus.date - extendedBolus.date % 1000;
|
2017-05-22 20:58:05 +02:00
|
|
|
try {
|
2017-05-23 22:59:06 +02:00
|
|
|
getDaoExtendedBolus().createOrUpdate(extendedBolus);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(extendedBolus.date);
|
2017-05-22 20:58:05 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleExtendedBolusChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(ExtendedBolus extendedBolus) {
|
|
|
|
try {
|
|
|
|
getDaoExtendedBolus().delete(extendedBolus);
|
2017-06-01 20:52:38 +02:00
|
|
|
updateEarliestDataChange(extendedBolus.date);
|
2017-05-22 20:58:05 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleExtendedBolusChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<ExtendedBolus> getExtendedBolusDataFromTime(long mills, boolean ascending) {
|
|
|
|
try {
|
|
|
|
List<ExtendedBolus> extendedBoluses;
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = getDaoExtendedBolus().queryBuilder();
|
|
|
|
queryBuilder.orderBy("date", ascending);
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.ge("date", mills);
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
extendedBoluses = getDaoExtendedBolus().query(preparedQuery);
|
|
|
|
return extendedBoluses;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<ExtendedBolus>();
|
|
|
|
}
|
|
|
|
|
2017-05-30 22:44:26 +02:00
|
|
|
@Nullable
|
|
|
|
public ExtendedBolus findExtendedBolusByTime(Long timeIndex) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<ExtendedBolus, String> qb = null;
|
|
|
|
Dao<ExtendedBolus, Long> daoExtendedBolus = getDaoExtendedBolus();
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = daoExtendedBolus.queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("date", timeIndex);
|
|
|
|
queryBuilder.limit(10L);
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ExtendedBolus> trList = daoExtendedBolus.query(preparedQuery);
|
|
|
|
if (trList.size() != 1) {
|
|
|
|
//log.debug("ExtendedBolus findExtendedBolusByTime query size: " + trList.size());
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
//log.debug("ExtendedBolus findExtendedBolusByTime found: " + trList.get(0).log());
|
|
|
|
return trList.get(0);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:59:06 +02:00
|
|
|
public void deleteExtendedBolusById(String _id) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
ExtendedBolus record = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Removing ExtendedBolus record from database: " + record.log());
|
2017-05-29 15:26:25 +02:00
|
|
|
delete(record);
|
2017-05-23 22:59:06 +02:00
|
|
|
} else {
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("ExtendedBolus not found database: " + _id);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"_id": "5924898d577eb0880e355337",
|
|
|
|
"eventType": "Combo Bolus",
|
|
|
|
"duration": 120,
|
|
|
|
"splitNow": 0,
|
|
|
|
"splitExt": 100,
|
|
|
|
"enteredinsulin": 1,
|
|
|
|
"relative": 1,
|
|
|
|
"created_at": "2017-05-23T19:12:14Z",
|
|
|
|
"enteredBy": "AndroidAPS",
|
|
|
|
"NSCLIENT_ID": 1495566734628,
|
|
|
|
"mills": 1495566734000,
|
|
|
|
"mgdl": 106
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void createExtendedBolusFromJsonIfNotExists(JSONObject trJson) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<ExtendedBolus, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoExtendedBolus().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<ExtendedBolus> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ExtendedBolus> list = getDaoExtendedBolus().query(preparedQuery);
|
|
|
|
ExtendedBolus extendedBolus;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
extendedBolus = new ExtendedBolus();
|
|
|
|
extendedBolus.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding ExtendedBolus record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
extendedBolus = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating ExtendedBolus record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
extendedBolus.date = trJson.getLong("mills");
|
|
|
|
extendedBolus.durationInMinutes = trJson.getInt("duration");
|
|
|
|
extendedBolus.insulin = trJson.getDouble("relative");
|
|
|
|
extendedBolus._id = trJson.getString("_id");
|
|
|
|
createOrUpdate(extendedBolus);
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-23 22:59:06 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleExtendedBolusChange() {
|
2017-05-22 20:58:05 +02:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing EventExtendedBolusChange");
|
2017-05-29 11:39:12 +02:00
|
|
|
MainApp.bus().post(new EventReloadTreatmentData());
|
2017-05-22 20:58:05 +02:00
|
|
|
MainApp.bus().post(new EventExtendedBolusChange());
|
2017-06-01 20:52:38 +02:00
|
|
|
if (earliestDataChange != null)
|
|
|
|
MainApp.bus().post(new EventNewHistoryData(earliestDataChange));
|
|
|
|
earliestDataChange = null;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledExtendedBolusPost = null;
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 22:10:56 +02:00
|
|
|
// prepare task for execution in 1 sec
|
2017-05-22 20:58:05 +02:00
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledExtendedBolusPost != null)
|
|
|
|
scheduledExtendedBolusPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
2017-05-22 22:10:56 +02:00
|
|
|
final int sec = 1;
|
2017-05-22 20:58:05 +02:00
|
|
|
scheduledExtendedBolusPost = extendedBolusWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-22 12:52:19 +02:00
|
|
|
|
2017-05-25 20:18:29 +02:00
|
|
|
// ------------ CareportalEvent handling ---------------
|
|
|
|
|
|
|
|
public void createOrUpdate(CareportalEvent careportalEvent) {
|
|
|
|
careportalEvent.date = careportalEvent.date - careportalEvent.date % 1000;
|
|
|
|
try {
|
|
|
|
getDaoCareportalEvents().createOrUpdate(careportalEvent);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleCareportalEventChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(CareportalEvent careportalEvent) {
|
|
|
|
try {
|
|
|
|
getDaoCareportalEvents().delete(careportalEvent);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
scheduleCareportalEventChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public CareportalEvent getLastCareportalEvent(String event) {
|
|
|
|
try {
|
|
|
|
List<CareportalEvent> careportalEvents;
|
|
|
|
QueryBuilder<CareportalEvent, Long> queryBuilder = getDaoCareportalEvents().queryBuilder();
|
|
|
|
queryBuilder.orderBy("date", false);
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("eventType", event);
|
|
|
|
queryBuilder.limit(1L);
|
|
|
|
PreparedQuery<CareportalEvent> preparedQuery = queryBuilder.prepare();
|
|
|
|
careportalEvents = getDaoCareportalEvents().query(preparedQuery);
|
|
|
|
if (careportalEvents.size() == 1)
|
|
|
|
return careportalEvents.get(0);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteCareportalEventById(String _id) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<CareportalEvent, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoCareportalEvents().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
|
|
|
PreparedQuery<CareportalEvent> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<CareportalEvent> list = getDaoCareportalEvents().query(preparedQuery);
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
CareportalEvent record = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Removing CareportalEvent record from database: " + record.log());
|
2017-05-29 15:26:25 +02:00
|
|
|
delete(record);
|
2017-05-25 20:18:29 +02:00
|
|
|
} else {
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("CareportalEvent not found database: " + _id);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createCareportalEventFromJsonIfNotExists(JSONObject trJson) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<CareportalEvent, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoCareportalEvents().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<CareportalEvent> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<CareportalEvent> list = getDaoCareportalEvents().query(preparedQuery);
|
|
|
|
CareportalEvent careportalEvent;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
careportalEvent = new CareportalEvent();
|
|
|
|
careportalEvent.source = Source.NIGHTSCOUT;
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding CareportalEvent record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
careportalEvent = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating CareportalEvent record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
careportalEvent.date = trJson.getLong("mills");
|
|
|
|
careportalEvent.eventType = trJson.getString("eventType");
|
|
|
|
careportalEvent.json = trJson.toString();
|
|
|
|
careportalEvent._id = trJson.getString("_id");
|
|
|
|
createOrUpdate(careportalEvent);
|
2017-06-02 10:25:49 +02:00
|
|
|
} catch (SQLException | JSONException e) {
|
2017-05-25 20:18:29 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
private static void scheduleCareportalEventChange() {
|
2017-05-25 20:18:29 +02:00
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-05-29 13:07:58 +02:00
|
|
|
log.debug("Firing scheduleCareportalEventChange");
|
2017-05-25 20:18:29 +02:00
|
|
|
MainApp.bus().post(new EventCareportalEventChange());
|
|
|
|
scheduledCareportalEventPost = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// prepare task for execution in 1 sec
|
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledCareportalEventPost != null)
|
|
|
|
scheduledCareportalEventPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
|
|
|
final int sec = 1;
|
|
|
|
scheduledCareportalEventPost = careportalEventWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:25:49 +02:00
|
|
|
// ---------------- ProfileSwitch handling ---------------
|
|
|
|
|
2017-06-02 12:27:21 +02:00
|
|
|
public List<ProfileSwitch> getProfileSwitchData(boolean ascending) {
|
2017-06-02 10:25:49 +02:00
|
|
|
try {
|
|
|
|
Dao<ProfileSwitch, Long> daoProfileSwitch = getDaoProfileSwitch();
|
|
|
|
List<ProfileSwitch> profileSwitches;
|
|
|
|
QueryBuilder<ProfileSwitch, Long> queryBuilder = daoProfileSwitch.queryBuilder();
|
|
|
|
queryBuilder.orderBy("date", ascending);
|
2017-06-02 12:27:21 +02:00
|
|
|
queryBuilder.limit(20L);
|
2017-06-02 10:25:49 +02:00
|
|
|
PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare();
|
|
|
|
profileSwitches = daoProfileSwitch.query(preparedQuery);
|
|
|
|
return profileSwitches;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return new ArrayList<ProfileSwitch>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createOrUpdate(ProfileSwitch profileSwitch) {
|
|
|
|
profileSwitch.date = profileSwitch.date - profileSwitch.date % 1000;
|
|
|
|
try {
|
|
|
|
getDaoProfileSwitch().createOrUpdate(profileSwitch);
|
|
|
|
scheduleProfileSwitchChange();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(ProfileSwitch profileSwitch) {
|
|
|
|
try {
|
|
|
|
getDaoProfileSwitch().delete(profileSwitch);
|
|
|
|
scheduleProfileSwitchChange();
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void scheduleProfileSwitchChange() {
|
|
|
|
class PostRunnable implements Runnable {
|
|
|
|
public void run() {
|
2017-06-02 12:27:21 +02:00
|
|
|
log.debug("Firing EventProfileSwitchChange");
|
|
|
|
MainApp.bus().post(new EventProfileSwitchChange());
|
2017-06-02 10:25:49 +02:00
|
|
|
scheduledProfileSwitchEventPost = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// prepare task for execution in 1 sec
|
|
|
|
// cancel waiting task to prevent sending multiple posts
|
|
|
|
if (scheduledProfileSwitchEventPost != null)
|
|
|
|
scheduledProfileSwitchEventPost.cancel(false);
|
|
|
|
Runnable task = new PostRunnable();
|
|
|
|
final int sec = 1;
|
|
|
|
scheduledProfileSwitchEventPost = profileSwitchEventWorker.schedule(task, sec, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
"_id":"592fa43ed97496a80da913d2",
|
|
|
|
"created_at":"2017-06-01T05:20:06Z",
|
|
|
|
"eventType":"Profile Switch",
|
|
|
|
"profile":"2016 +30%",
|
|
|
|
"units":"mmol",
|
|
|
|
"enteredBy":"sony",
|
|
|
|
"NSCLIENT_ID":1496294454309,
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
2017-06-02 10:25:49 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
public void createProfileSwitchFromJsonIfNotExists(JSONObject trJson) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<ProfileSwitch, Long> queryBuilder = null;
|
|
|
|
queryBuilder = getDaoProfileSwitch().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", trJson.getString("_id")).or().eq("date", trJson.getLong("mills"));
|
|
|
|
PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery);
|
|
|
|
ProfileSwitch profileSwitch;
|
|
|
|
if (list.size() == 0) {
|
|
|
|
profileSwitch = new ProfileSwitch();
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Adding ProfileSwitch record to database: " + trJson.toString());
|
|
|
|
// Record does not exists. add
|
|
|
|
} else if (list.size() == 1) {
|
|
|
|
profileSwitch = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Updating ProfileSwitch record in database: " + trJson.toString());
|
|
|
|
} else {
|
|
|
|
log.error("Something went wrong");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
profileSwitch.date = trJson.getLong("mills");
|
2017-06-02 23:59:34 +02:00
|
|
|
if (trJson.has("duration"))
|
|
|
|
profileSwitch.durationInMinutes = trJson.getInt("duration");
|
2017-06-02 10:25:49 +02:00
|
|
|
profileSwitch._id = trJson.getString("_id");
|
|
|
|
profileSwitch.profileName = trJson.getString("profile");
|
|
|
|
profileSwitch.isCPP = trJson.has("CircadianPercentageProfile");
|
|
|
|
if (trJson.has("timeshift"))
|
|
|
|
profileSwitch.timeshift = trJson.getInt("timeshift");
|
|
|
|
if (trJson.has("percentage"))
|
2017-06-02 12:27:21 +02:00
|
|
|
profileSwitch.percentage = trJson.getInt("percentage");
|
2017-06-02 10:25:49 +02:00
|
|
|
if (trJson.has("profileJson"))
|
2017-06-02 12:27:21 +02:00
|
|
|
profileSwitch.profileJson = trJson.getString("profileJson");
|
2017-06-02 10:25:49 +02:00
|
|
|
if (trJson.has("profilePlugin"))
|
2017-06-02 12:27:21 +02:00
|
|
|
profileSwitch.profilePlugin = trJson.getString("profilePlugin");
|
2017-06-02 10:25:49 +02:00
|
|
|
createOrUpdate(profileSwitch);
|
|
|
|
} catch (SQLException | JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteProfileSwitchById(String _id) {
|
|
|
|
try {
|
|
|
|
QueryBuilder<ProfileSwitch, Long> queryBuilder = getDaoProfileSwitch().queryBuilder();
|
|
|
|
Where where = queryBuilder.where();
|
|
|
|
where.eq("_id", _id);
|
|
|
|
PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare();
|
|
|
|
List<ProfileSwitch> list = getDaoProfileSwitch().query(preparedQuery);
|
|
|
|
|
|
|
|
if (list.size() == 1) {
|
|
|
|
ProfileSwitch record = list.get(0);
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("Removing ProfileSwitch record from database: " + record.log());
|
|
|
|
delete(record);
|
|
|
|
} else {
|
|
|
|
if (Config.logIncommingData)
|
|
|
|
log.debug("ProfileSwitch not found database: " + _id);
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|