move all treatments db actions to DatabaseHelper
This commit is contained in:
parent
8d0c1dcacf
commit
9722130299
|
@ -6,7 +6,6 @@ import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.provider.Telephony;
|
import android.provider.Telephony;
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import com.j256.ormlite.dao.Dao;
|
import com.j256.ormlite.dao.Dao;
|
||||||
import com.j256.ormlite.stmt.PreparedQuery;
|
import com.j256.ormlite.stmt.PreparedQuery;
|
||||||
|
@ -33,7 +32,6 @@ import info.nightscout.androidaps.db.TempTarget;
|
||||||
import info.nightscout.androidaps.db.Treatment;
|
import info.nightscout.androidaps.db.Treatment;
|
||||||
import info.nightscout.androidaps.events.EventNewBG;
|
import info.nightscout.androidaps.events.EventNewBG;
|
||||||
import info.nightscout.androidaps.events.EventNewBasalProfile;
|
import info.nightscout.androidaps.events.EventNewBasalProfile;
|
||||||
import info.nightscout.androidaps.events.EventTreatmentChange;
|
|
||||||
import info.nightscout.androidaps.interfaces.PluginBase;
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||||
import info.nightscout.androidaps.interfaces.PumpInterface;
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
||||||
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
||||||
|
@ -53,7 +51,6 @@ import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTarget
|
||||||
import info.nightscout.androidaps.receivers.DataReceiver;
|
import info.nightscout.androidaps.receivers.DataReceiver;
|
||||||
import info.nightscout.client.data.NSProfile;
|
import info.nightscout.client.data.NSProfile;
|
||||||
import info.nightscout.client.data.NSSgv;
|
import info.nightscout.client.data.NSSgv;
|
||||||
import info.nightscout.utils.ToastUtils;
|
|
||||||
|
|
||||||
|
|
||||||
public class DataService extends IntentService {
|
public class DataService extends IntentService {
|
||||||
|
@ -388,7 +385,7 @@ public class DataService extends IntentService {
|
||||||
String trstring = bundles.getString("treatment");
|
String trstring = bundles.getString("treatment");
|
||||||
JSONObject trJson = new JSONObject(trstring);
|
JSONObject trJson = new JSONObject(trstring);
|
||||||
String _id = trJson.getString("_id");
|
String _id = trJson.getString("_id");
|
||||||
removeTreatmentFromDb(_id);
|
MainApp.getDbHelper().delete(_id);
|
||||||
handleRemoveTempTargetRecord(trJson);
|
handleRemoveTempTargetRecord(trJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -398,7 +395,7 @@ public class DataService extends IntentService {
|
||||||
for (int i = 0; i < jsonArray.length(); i++) {
|
for (int i = 0; i < jsonArray.length(); i++) {
|
||||||
JSONObject trJson = jsonArray.getJSONObject(i);
|
JSONObject trJson = jsonArray.getJSONObject(i);
|
||||||
String _id = trJson.getString("_id");
|
String _id = trJson.getString("_id");
|
||||||
removeTreatmentFromDb(_id);
|
MainApp.getDbHelper().delete(_id);
|
||||||
handleRemoveTempTargetRecord(trJson);
|
handleRemoveTempTargetRecord(trJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -468,9 +465,9 @@ public class DataService extends IntentService {
|
||||||
if (trJson.has("timeIndex")) {
|
if (trJson.has("timeIndex")) {
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("ADD: timeIndex found: " + trstring);
|
log.debug("ADD: timeIndex found: " + trstring);
|
||||||
stored = findByTimeIndex(trJson.getLong("timeIndex"));
|
stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("timeIndex"));
|
||||||
} else {
|
} else {
|
||||||
stored = findById(_id);
|
stored = MainApp.getDbHelper().findTreatmentById(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stored != null) {
|
if (stored != null) {
|
||||||
|
@ -478,10 +475,9 @@ public class DataService extends IntentService {
|
||||||
log.debug("ADD: Existing treatment: " + trstring);
|
log.debug("ADD: Existing treatment: " + trstring);
|
||||||
if (trJson.has("timeIndex")) {
|
if (trJson.has("timeIndex")) {
|
||||||
stored._id = _id;
|
stored._id = _id;
|
||||||
int updated = MainApp.getDbHelper().getDaoTreatments().update(stored);
|
int updated = MainApp.getDbHelper().update(stored);
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("Records updated: " + updated);
|
log.debug("Records updated: " + updated);
|
||||||
scheduleTreatmentChange();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
|
@ -506,14 +502,9 @@ public class DataService extends IntentService {
|
||||||
treatment.mealBolus = false;
|
treatment.mealBolus = false;
|
||||||
}
|
}
|
||||||
treatment.setTimeIndex(treatment.getTimeIndex());
|
treatment.setTimeIndex(treatment.getTimeIndex());
|
||||||
try {
|
MainApp.getDbHelper().createOrUpdate(treatment);
|
||||||
MainApp.getDbHelper().getDaoTreatments().createOrUpdate(treatment);
|
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("ADD: Stored treatment: " + treatment.log());
|
log.debug("ADD: Stored treatment: " + treatment.log());
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
scheduleTreatmentChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -533,15 +524,15 @@ public class DataService extends IntentService {
|
||||||
if (trJson.has("timeIndex")) {
|
if (trJson.has("timeIndex")) {
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("ADD: timeIndex found: " + trstring);
|
log.debug("ADD: timeIndex found: " + trstring);
|
||||||
stored = findByTimeIndex(trJson.getLong("timeIndex"));
|
stored = MainApp.getDbHelper().findTreatmentByTimeIndex(trJson.getLong("timeIndex"));
|
||||||
} else {
|
} else {
|
||||||
stored = findById(_id);
|
stored = MainApp.getDbHelper().findTreatmentById(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stored != null) {
|
if (stored != null) {
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("CHANGE: Removing old: " + trstring);
|
log.debug("CHANGE: Removing old: " + trstring);
|
||||||
removeTreatmentFromDb(_id);
|
MainApp.getDbHelper().delete(_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
|
@ -567,16 +558,11 @@ public class DataService extends IntentService {
|
||||||
treatment.mealBolus = false;
|
treatment.mealBolus = false;
|
||||||
}
|
}
|
||||||
treatment.setTimeIndex(treatment.getTimeIndex());
|
treatment.setTimeIndex(treatment.getTimeIndex());
|
||||||
try {
|
Dao.CreateOrUpdateStatus status = MainApp.getDbHelper().createOrUpdate(treatment);
|
||||||
Dao.CreateOrUpdateStatus status = MainApp.getDbHelper().getDaoTreatments().createOrUpdate(treatment);
|
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("Records updated: " + status.getNumLinesChanged());
|
log.debug("Records updated: " + status.getNumLinesChanged());
|
||||||
if (Config.logIncommingData)
|
if (Config.logIncommingData)
|
||||||
log.debug("CHANGE: Stored treatment: " + treatment.log());
|
log.debug("CHANGE: Stored treatment: " + treatment.log());
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
scheduleTreatmentChange();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleDanaRHistoryRecords(JSONObject trJson) throws JSONException, SQLException {
|
public void handleDanaRHistoryRecords(JSONObject trJson) throws JSONException, SQLException {
|
||||||
|
@ -683,75 +669,10 @@ public class DataService extends IntentService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static Treatment findById(String _id) {
|
|
||||||
try {
|
|
||||||
Dao<Treatment, Long> daoTreatments = MainApp.getDbHelper().getDaoTreatments();
|
|
||||||
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
|
||||||
Where where = queryBuilder.where();
|
|
||||||
where.eq("_id", _id);
|
|
||||||
queryBuilder.limit(10);
|
|
||||||
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
||||||
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
|
||||||
if (trList.size() != 1) {
|
|
||||||
//log.debug("Treatment findById query size: " + trList.size());
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
//log.debug("Treatment findById found: " + trList.get(0).log());
|
|
||||||
return trList.get(0);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static Treatment findByTimeIndex(Long timeIndex) {
|
|
||||||
try {
|
|
||||||
QueryBuilder<Treatment, String> qb = null;
|
|
||||||
Dao<Treatment, Long> daoTreatments = MainApp.getDbHelper().getDaoTreatments();
|
|
||||||
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
|
||||||
Where where = queryBuilder.where();
|
|
||||||
where.eq("timeIndex", timeIndex);
|
|
||||||
queryBuilder.limit(10);
|
|
||||||
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
|
||||||
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
|
||||||
if (trList.size() != 1) {
|
|
||||||
log.debug("Treatment findByTimeIndex query size: " + trList.size());
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
log.debug("Treatment findByTimeIndex found: " + trList.get(0).log());
|
|
||||||
return trList.get(0);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeTreatmentFromDb(String _id) throws SQLException {
|
|
||||||
Treatment stored = findById(_id);
|
|
||||||
if (stored != null) {
|
|
||||||
log.debug("REMOVE: Existing treatment (removing): " + _id);
|
|
||||||
int removed = MainApp.getDbHelper().getDaoTreatments().delete(stored);
|
|
||||||
if (Config.logIncommingData)
|
|
||||||
log.debug("Records removed: " + removed);
|
|
||||||
scheduleTreatmentChange();
|
|
||||||
} else {
|
|
||||||
log.debug("REMOVE: Not stored treatment (ignoring): " + _id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleNewSMS(Intent intent) {
|
private void handleNewSMS(Intent intent) {
|
||||||
Bundle bundle = intent.getExtras();
|
Bundle bundle = intent.getExtras();
|
||||||
if (bundle == null) return;
|
if (bundle == null) return;
|
||||||
MainApp.bus().post(new EventNewSMS(bundle));
|
MainApp.bus().post(new EventNewSMS(bundle));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void scheduleTreatmentChange() {
|
|
||||||
MainApp.bus().post(new EventTreatmentChange());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,10 +20,15 @@ import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.Config;
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.data.GlucoseStatus;
|
import info.nightscout.androidaps.events.EventTreatmentChange;
|
||||||
|
|
||||||
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
|
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
|
||||||
|
@ -37,6 +42,11 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
|
|
||||||
private static final int DATABASE_VERSION = 5;
|
private static final int DATABASE_VERSION = 5;
|
||||||
|
|
||||||
|
private long latestTreatmentChange = 0;
|
||||||
|
|
||||||
|
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
private static ScheduledFuture<?> scheduledPost = null;
|
||||||
|
|
||||||
public DatabaseHelper(Context context) {
|
public DatabaseHelper(Context context) {
|
||||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||||
onCreate(getWritableDatabase(), getConnectionSource());
|
onCreate(getWritableDatabase(), getConnectionSource());
|
||||||
|
@ -117,6 +127,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
||||||
|
latestTreatmentChange = 0;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -126,6 +137,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
try {
|
try {
|
||||||
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
||||||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||||
|
latestTreatmentChange = 0;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -148,7 +160,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return getDao(TempTarget.class);
|
return getDao(TempTarget.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dao<Treatment, Long> getDaoTreatments() throws SQLException {
|
private Dao<Treatment, Long> getDaoTreatments() throws SQLException {
|
||||||
return getDao(Treatment.class);
|
return getDao(Treatment.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +180,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
List<BgReading> bgList = null;
|
List<BgReading> bgList = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Dao<BgReading, Long> daoBgReadings = MainApp.getDbHelper().getDaoBgReadings();
|
Dao<BgReading, Long> daoBgReadings = getDaoBgReadings();
|
||||||
QueryBuilder<BgReading, Long> queryBuilder = daoBgReadings.queryBuilder();
|
QueryBuilder<BgReading, Long> queryBuilder = daoBgReadings.queryBuilder();
|
||||||
queryBuilder.orderBy("timeIndex", false);
|
queryBuilder.orderBy("timeIndex", false);
|
||||||
queryBuilder.limit(1L);
|
queryBuilder.limit(1L);
|
||||||
|
@ -219,6 +231,131 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||||
return new ArrayList<BgReading>();
|
return new ArrayList<BgReading>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TREATMENT HANDLING
|
||||||
|
|
||||||
|
public boolean isDataUnchanged(long time) {
|
||||||
|
if (time >= latestTreatmentChange) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int update(Treatment treatment) {
|
||||||
|
int updated = 0;
|
||||||
|
try {
|
||||||
|
updated = getDaoTreatments().update(treatment);
|
||||||
|
latestTreatmentChange = treatment.getTimeIndex();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
|
||||||
|
Dao.CreateOrUpdateStatus status = null;
|
||||||
|
try {
|
||||||
|
status = getDaoTreatments().createOrUpdate(treatment);
|
||||||
|
latestTreatmentChange = treatment.getTimeIndex();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void create(Treatment treatment) {
|
||||||
|
try {
|
||||||
|
getDaoTreatments().create(treatment);
|
||||||
|
latestTreatmentChange = treatment.getTimeIndex();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int delete(String _id) {
|
||||||
|
Treatment stored = findTreatmentById(_id);
|
||||||
|
int removed = 0;
|
||||||
|
if (stored != null) {
|
||||||
|
log.debug("REMOVE: Existing treatment (removing): " + _id);
|
||||||
|
try {
|
||||||
|
removed = getDaoTreatments().delete(stored);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if (Config.logIncommingData)
|
||||||
|
log.debug("Records removed: " + removed);
|
||||||
|
latestTreatmentChange = stored.getTimeIndex();
|
||||||
|
scheduleTreatmentChange();
|
||||||
|
} else {
|
||||||
|
log.debug("REMOVE: Not stored treatment (ignoring): " + _id);
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
queryBuilder.limit(10);
|
||||||
|
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
|
||||||
|
public Treatment findTreatmentByTimeIndex(Long timeIndex) {
|
||||||
|
try {
|
||||||
|
QueryBuilder<Treatment, String> qb = null;
|
||||||
|
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
||||||
|
QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder();
|
||||||
|
Where where = queryBuilder.where();
|
||||||
|
where.eq("timeIndex", timeIndex);
|
||||||
|
queryBuilder.limit(10);
|
||||||
|
PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare();
|
||||||
|
List<Treatment> trList = daoTreatments.query(preparedQuery);
|
||||||
|
if (trList.size() != 1) {
|
||||||
|
log.debug("Treatment findTreatmentByTimeIndex query size: " + trList.size());
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
log.debug("Treatment findTreatmentByTimeIndex found: " + trList.get(0).log());
|
||||||
|
return trList.get(0);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void scheduleTreatmentChange() {
|
||||||
|
class PostRunnable implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
MainApp.bus().post(new EventTreatmentChange());
|
||||||
|
scheduledPost = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// prepare task for execution in 5 sec
|
||||||
|
// cancel waiting task to prevent sending multiple posts
|
||||||
|
if (scheduledPost != null)
|
||||||
|
scheduledPost.cancel(false);
|
||||||
|
Runnable task = new PostRunnable();
|
||||||
|
final int sec = 5;
|
||||||
|
scheduledPost = worker.schedule(task, sec, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public List<Treatment> getTreatmentDataFromTime(long mills, boolean ascending) {
|
public List<Treatment> getTreatmentDataFromTime(long mills, boolean ascending) {
|
||||||
try {
|
try {
|
||||||
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
Dao<Treatment, Long> daoTreatments = getDaoTreatments();
|
||||||
|
|
|
@ -16,7 +16,6 @@ import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
||||||
import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
|
import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA;
|
||||||
import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA;
|
import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResultMA;
|
||||||
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog;
|
import info.nightscout.androidaps.plugins.Overview.Dialogs.BolusProgressDialog;
|
||||||
import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog;
|
|
||||||
import info.nightscout.androidaps.plugins.Overview.Notification;
|
import info.nightscout.androidaps.plugins.Overview.Notification;
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
|
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
||||||
|
@ -82,7 +80,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
public ConfigBuilderPlugin() {
|
public ConfigBuilderPlugin() {
|
||||||
MainApp.bus().register(this);
|
MainApp.bus().register(this);
|
||||||
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
|
||||||
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "ConfigBuilderPlugin");;
|
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "ConfigBuilderPlugin");
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -103,7 +102,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
@Override
|
@Override
|
||||||
public String getNameShort() {
|
public String getNameShort() {
|
||||||
String name = MainApp.sResources.getString(R.string.configbuilder_shortname);
|
String name = MainApp.sResources.getString(R.string.configbuilder_shortname);
|
||||||
if (!name.trim().isEmpty()){
|
if (!name.trim().isEmpty()) {
|
||||||
//only if translation exists
|
//only if translation exists
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -444,15 +443,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout
|
t.carbs = (double) result.carbsDelivered; // with different carbTime record will come back from nightscout
|
||||||
t.created_at = new Date();
|
t.created_at = new Date();
|
||||||
t.mealBolus = result.carbsDelivered > 0;
|
t.mealBolus = result.carbsDelivered > 0;
|
||||||
try {
|
MainApp.getDbHelper().create(t);
|
||||||
MainApp.getDbHelper().getDaoTreatments().create(t);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
t.setTimeIndex(t.getTimeIndex());
|
t.setTimeIndex(t.getTimeIndex());
|
||||||
t.carbs = (double) result.carbsDelivered;
|
t.carbs = (double) result.carbsDelivered;
|
||||||
uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
|
uploadBolusWizardRecord(t, glucose, glucoseType, carbTime, boluscalc);
|
||||||
MainApp.bus().post(new EventTreatmentChange());
|
|
||||||
}
|
}
|
||||||
mWakeLock.release();
|
mWakeLock.release();
|
||||||
return result;
|
return result;
|
||||||
|
@ -496,21 +490,15 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
t.carbs = (double) result.carbsDelivered;
|
t.carbs = (double) result.carbsDelivered;
|
||||||
t.created_at = new Date();
|
t.created_at = new Date();
|
||||||
t.mealBolus = t.carbs > 0;
|
t.mealBolus = t.carbs > 0;
|
||||||
try {
|
MainApp.getDbHelper().create(t);
|
||||||
MainApp.getDbHelper().getDaoTreatments().create(t);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
t.setTimeIndex(t.getTimeIndex());
|
t.setTimeIndex(t.getTimeIndex());
|
||||||
t.sendToNSClient();
|
t.sendToNSClient();
|
||||||
MainApp.bus().post(new EventTreatmentChange());
|
|
||||||
}
|
}
|
||||||
mWakeLock.release();
|
mWakeLock.release();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopBolusDelivering() {
|
public void stopBolusDelivering() {
|
||||||
activePump.stopBolusDelivering();
|
activePump.stopBolusDelivering();
|
||||||
|
@ -819,7 +807,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
data.put("absolute", absolute);
|
data.put("absolute", absolute);
|
||||||
data.put("created_at", DateUtil.toISOString(new Date()));
|
data.put("created_at", DateUtil.toISOString(new Date()));
|
||||||
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
|
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
|
||||||
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + absolute + "u/h " + durationInMinutes +" min"); // ECOR
|
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + absolute + "u/h " + durationInMinutes + " min"); // ECOR
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString("action", "dbAdd");
|
bundle.putString("action", "dbAdd");
|
||||||
bundle.putString("collection", "treatments");
|
bundle.putString("collection", "treatments");
|
||||||
|
@ -849,7 +837,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
|
||||||
data.put("percent", percent - 100);
|
data.put("percent", percent - 100);
|
||||||
data.put("created_at", DateUtil.toISOString(new Date()));
|
data.put("created_at", DateUtil.toISOString(new Date()));
|
||||||
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
|
data.put("enteredBy", MainApp.instance().getString(R.string.app_name));
|
||||||
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + percent + "% " + durationInMinutes +" min"); // ECOR
|
data.put("notes", MainApp.sResources.getString(R.string.androidaps_tempbasalstartnote) + " " + percent + "% " + durationInMinutes + " min"); // ECOR
|
||||||
Bundle bundle = new Bundle();
|
Bundle bundle = new Bundle();
|
||||||
bundle.putString("action", "dbAdd");
|
bundle.putString("action", "dbAdd");
|
||||||
bundle.putString("collection", "treatments");
|
bundle.putString("collection", "treatments");
|
||||||
|
|
Loading…
Reference in a new issue