This commit is contained in:
AdrianLxM 2018-03-14 00:57:48 +01:00
parent 3f6a612d42
commit 1e1a58468f
12 changed files with 208 additions and 45 deletions

View file

@ -75,6 +75,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public static final String DATABASE_DBREQUESTS = "DBRequests"; public static final String DATABASE_DBREQUESTS = "DBRequests";
public static final String DATABASE_CAREPORTALEVENTS = "CareportalEvents"; public static final String DATABASE_CAREPORTALEVENTS = "CareportalEvents";
public static final String DATABASE_PROFILESWITCHES = "ProfileSwitches"; public static final String DATABASE_PROFILESWITCHES = "ProfileSwitches";
public static final String DATABASE_TDDS = "TDDs";
private static final int DATABASE_VERSION = 8; private static final int DATABASE_VERSION = 8;
@ -123,6 +124,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
TableUtils.createTableIfNotExists(connectionSource, TDD.class);
} catch (SQLException e) { } catch (SQLException e) {
log.error("Can't create database", e); log.error("Can't create database", e);
throw new RuntimeException(e); throw new RuntimeException(e);
@ -226,6 +228,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.dropTable(connectionSource, ExtendedBolus.class, true); TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
TableUtils.dropTable(connectionSource, CareportalEvent.class, true); TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
TableUtils.dropTable(connectionSource, ProfileSwitch.class, true); TableUtils.dropTable(connectionSource, ProfileSwitch.class, true);
TableUtils.dropTable(connectionSource, TDD.class, true);
TableUtils.createTableIfNotExists(connectionSource, TempTarget.class); TableUtils.createTableIfNotExists(connectionSource, TempTarget.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class); TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class); TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
@ -235,6 +238,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class); TableUtils.createTableIfNotExists(connectionSource, ProfileSwitch.class);
TableUtils.createTableIfNotExists(connectionSource, TDD.class);
updateEarliestDataChange(0); updateEarliestDataChange(0);
} catch (SQLException e) { } catch (SQLException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -322,6 +326,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
scheduleProfileSwitchChange(); scheduleProfileSwitchChange();
} }
public void resetTDDs() {
try {
TableUtils.dropTable(connectionSource, TDD.class, true);
TableUtils.createTableIfNotExists(connectionSource, TDD.class);
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
}
// ------------------ getDao ------------------------------------------- // ------------------ getDao -------------------------------------------
@ -341,6 +353,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return getDao(DanaRHistoryRecord.class); return getDao(DanaRHistoryRecord.class);
} }
private Dao<TDD, String> getDaoTDD() throws SQLException {
return getDao(TDD.class);
}
private Dao<DbRequest, String> getDaoDbRequest() throws SQLException { private Dao<DbRequest, String> getDaoDbRequest() throws SQLException {
return getDao(DbRequest.class); return getDao(DbRequest.class);
} }
@ -494,6 +510,32 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return new ArrayList<BgReading>(); return new ArrayList<BgReading>();
} }
// ------------------- BgReading handling -----------------------
public void createOrUpdateTDD(TDD tdd){
try {
getDaoTDD().createOrUpdate(tdd);
} catch (SQLException e) {
log.error("Unhandled exception", e);
}
}
public List<TDD> getTDDs() {
List<TDD> tddList;
try {
QueryBuilder<TDD, String> queryBuilder = getDaoTDD().queryBuilder();
queryBuilder.orderBy("date", false);
queryBuilder.limit(10L);
PreparedQuery<TDD> preparedQuery = queryBuilder.prepare();
tddList = getDaoTDD().query(preparedQuery);
} catch (SQLException e) {
log.error("Unhandled exception", e);
tddList = new ArrayList<>();
}
return tddList;
}
// ------------- DbRequests handling ------------------- // ------------- DbRequests handling -------------------
public void create(DbRequest dbr) { public void create(DbRequest dbr) {

View file

@ -0,0 +1,46 @@
package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
/**
* Created by mike on 20.09.2017.
*/
@DatabaseTable(tableName = DatabaseHelper.DATABASE_TDDS)
public class TDD {
private static Logger log = LoggerFactory.getLogger(TDD.class);
@DatabaseField(id = true)
public long date;
@DatabaseField
public double bolus;
@DatabaseField
public double basal;
@DatabaseField
public double total;
public double getTotal(){
return (total > 0d) ? total:(bolus+basal);
}
public TDD() { }
public TDD(long date, double bolus, double basal, double total){
this.date = date;
this.bolus = bolus;
this.basal = basal;
this.total = total;
}
}

View file

@ -54,4 +54,7 @@ public interface PumpInterface {
String shortStatus(boolean veryShort); String shortStatus(boolean veryShort);
boolean isFakingTempsByExtendedBoluses(); boolean isFakingTempsByExtendedBoluses();
PumpEnactResult loadTDDs();
} }

View file

@ -30,6 +30,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotificati
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.PumpDanaR.services.AbstractDanaRExecutionService; import info.nightscout.androidaps.plugins.PumpDanaR.services.AbstractDanaRExecutionService;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
@ -435,6 +436,11 @@ public abstract class AbstractDanaRPlugin implements PluginBase, PumpInterface,
* Constraint interface * Constraint interface
*/ */
@Override
public PumpEnactResult loadTDDs() {
return loadHistory(RecordTypes.RECORD_TYPE_DAILY);
}
@Override @Override
public boolean isLoopEnabled() { public boolean isLoopEnabled() {
return true; return true;

View file

@ -44,6 +44,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRFragment; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRFragment;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPump;
import info.nightscout.androidaps.plugins.PumpDanaR.comm.RecordTypes;
import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSDeviceChange; import info.nightscout.androidaps.plugins.PumpDanaRS.events.EventDanaRSDeviceChange;
import info.nightscout.androidaps.plugins.PumpDanaRS.services.DanaRSService; import info.nightscout.androidaps.plugins.PumpDanaRS.services.DanaRSService;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
@ -851,4 +852,9 @@ public class DanaRSPlugin implements PluginBase, PumpInterface, DanaRInterface,
public boolean isFakingTempsByExtendedBoluses() { public boolean isFakingTempsByExtendedBoluses() {
return false; return false;
} }
@Override
public PumpEnactResult loadTDDs() {
return loadHistory(RecordTypes.RECORD_TYPE_DAILY);
}
} }

View file

@ -268,6 +268,13 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
return false; return false;
} }
@Override
public PumpEnactResult loadTDDs() {
//TODO: read TDDs and store to DB
PumpEnactResult result = new PumpEnactResult();
return result;
}
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return initialized; return initialized;

View file

@ -114,6 +114,13 @@ public class MDIPlugin implements PluginBase, PumpInterface {
return false; return false;
} }
@Override
public PumpEnactResult loadTDDs() {
//no result, could read DB in the future?
PumpEnactResult result = new PumpEnactResult();
return result;
}
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return true; return true;

View file

@ -175,6 +175,13 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface {
return (Config.NSCLIENT || Config.G5UPLOADER) && fromNSAreCommingFakedExtendedBoluses; return (Config.NSCLIENT || Config.G5UPLOADER) && fromNSAreCommingFakedExtendedBoluses;
} }
@Override
public PumpEnactResult loadTDDs() {
//no result, could read DB in the future?
PumpEnactResult result = new PumpEnactResult();
return result;
}
@Override @Override
public boolean isInitialized() { public boolean isInitialized() {
return true; return true;

View file

@ -24,6 +24,7 @@ import info.nightscout.androidaps.db.DanaRHistoryRecord;
import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TDD;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.APSInterface;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
@ -270,23 +271,10 @@ public class ActionStringHandler {
} else if ("tddstats".equals(act[0])) { } else if ("tddstats".equals(act[0])) {
Object activePump = MainApp.getConfigBuilder().getActivePump(); Object activePump = MainApp.getConfigBuilder().getActivePump();
PumpInterface dana = MainApp.getSpecificPlugin(DanaRPlugin.class); if (activePump!= null) {
PumpInterface danaRS = MainApp.getSpecificPlugin(DanaRSPlugin.class);
PumpInterface danaV2 = MainApp.getSpecificPlugin(DanaRv2Plugin.class);
PumpInterface danaKorean = MainApp.getSpecificPlugin(DanaRKoreanPlugin.class);
if ((dana == null || dana != activePump) &&
(danaV2 == null || danaV2 != activePump) &&
(danaKorean == null || danaKorean != activePump) &&
(danaRS == null || danaRS != activePump)
) {
sendError("Pump does not support TDDs!");
return;
} else {
// check if DB up to date // check if DB up to date
List<DanaRHistoryRecord> dummies = new LinkedList<DanaRHistoryRecord>(); List<TDD> dummies = new LinkedList<TDD>();
List<DanaRHistoryRecord> historyList = getTDDList(dummies); List<TDD> historyList = getTDDList(dummies);
if (isOldData(historyList)) { if (isOldData(historyList)) {
rTitle = "TDD"; rTitle = "TDD";
@ -300,13 +288,13 @@ public class ActionStringHandler {
} else { } else {
rMessage += "trying to fetch data from pump."; rMessage += "trying to fetch data from pump.";
ConfigBuilderPlugin.getCommandQueue().loadHistory(RecordTypes.RECORD_TYPE_DAILY, new Callback() { ConfigBuilderPlugin.getCommandQueue().loadTDDs(new Callback() {
@Override @Override
public void run() { public void run() {
List<DanaRHistoryRecord> dummies = new LinkedList<DanaRHistoryRecord>(); List<TDD> dummies = new LinkedList<TDD>();
List<DanaRHistoryRecord> historyList = getTDDList(dummies); List<TDD> historyList = getTDDList(dummies);
if (isOldData(historyList)) { if (isOldData(historyList)) {
sendStatusmessage("TDD", "TDD: Still old data! Cannot load from pump."); sendStatusmessage("TDD", "TDD: Still old data! Cannot load from pump.\n" + generateTDDMessage(historyList, dummies));
} else { } else {
sendStatusmessage("TDD", generateTDDMessage(historyList, dummies)); sendStatusmessage("TDD", generateTDDMessage(historyList, dummies));
} }
@ -330,7 +318,7 @@ public class ActionStringHandler {
lastConfirmActionString = rAction; lastConfirmActionString = rAction;
} }
private static String generateTDDMessage(List<DanaRHistoryRecord> historyList, List<DanaRHistoryRecord> dummies) { private static String generateTDDMessage(List<TDD> historyList, List<TDD> dummies) {
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
@ -350,8 +338,8 @@ public class ActionStringHandler {
double weighted07 = 0d; double weighted07 = 0d;
Collections.reverse(historyList); Collections.reverse(historyList);
for (DanaRHistoryRecord record : historyList) { for (TDD record : historyList) {
double tdd = record.recordDailyBolus + record.recordDailyBasal; double tdd = record.getTotal();
if (i == 0) { if (i == 0) {
weighted03 = tdd; weighted03 = tdd;
weighted05 = tdd; weighted05 = tdd;
@ -379,47 +367,47 @@ public class ActionStringHandler {
//add TDDs: //add TDDs:
Collections.reverse(historyList); Collections.reverse(historyList);
for (DanaRHistoryRecord record : historyList) { for (TDD record : historyList) {
double tdd = record.recordDailyBolus + record.recordDailyBasal; double tdd = record.getTotal();
message += df.format(new Date(record.recordDate)) + " " + DecimalFormatter.to2Decimal(tdd) + "U " + (DecimalFormatter.to0Decimal(100 * tdd / refTDD) + "%") + (dummies.contains(record) ? "x" : "") + "\n"; message += df.format(new Date(record.date)) + " " + DecimalFormatter.to2Decimal(tdd) + "U " + (DecimalFormatter.to0Decimal(100 * tdd / refTDD) + "%") + (dummies.contains(record) ? "x" : "") + "\n";
} }
return message; return message;
} }
public static boolean isOldData(List<DanaRHistoryRecord> historyList) { public static boolean isOldData(List<TDD> historyList) {
DateFormat df = new SimpleDateFormat("dd.MM."); DateFormat df = new SimpleDateFormat("dd.MM.");
return (historyList.size() < 3 || !(df.format(new Date(historyList.get(0).recordDate)).equals(df.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24))))); return (historyList.size() < 3 || !(df.format(new Date(historyList.get(0).date)).equals(df.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24)))));
} }
@NonNull @NonNull
public static List<DanaRHistoryRecord> getTDDList(List<DanaRHistoryRecord> returnDummies) { public static List<TDD> getTDDList(List<TDD> returnDummies) {
List<DanaRHistoryRecord> historyList = MainApp.getDbHelper().getDanaRHistoryRecordsByType(RecordTypes.RECORD_TYPE_DAILY); List<TDD> historyList = MainApp.getDbHelper().getTDDs();
//only use newest 10 //only use newest 10
historyList = historyList.subList(0, Math.min(10, historyList.size())); historyList = historyList.subList(0, Math.min(10, historyList.size()));
//fill single gaps //fill single gaps - only needed for Dana*R data
List<DanaRHistoryRecord> dummies = (returnDummies != null) ? returnDummies : (new LinkedList()); List<TDD> dummies = (returnDummies != null) ? returnDummies : (new LinkedList());
DateFormat df = new SimpleDateFormat("dd.MM."); DateFormat df = new SimpleDateFormat("dd.MM.");
for (int i = 0; i < historyList.size() - 1; i++) { for (int i = 0; i < historyList.size() - 1; i++) {
DanaRHistoryRecord elem1 = historyList.get(i); TDD elem1 = historyList.get(i);
DanaRHistoryRecord elem2 = historyList.get(i + 1); TDD elem2 = historyList.get(i + 1);
if (!df.format(new Date(elem1.recordDate)).equals(df.format(new Date(elem2.recordDate + 25 * 60 * 60 * 1000)))) { if (!df.format(new Date(elem1.date)).equals(df.format(new Date(elem2.date + 25 * 60 * 60 * 1000)))) {
DanaRHistoryRecord dummy = new DanaRHistoryRecord(); TDD dummy = new TDD();
dummy.recordDate = elem1.recordDate - 24 * 60 * 60 * 1000; dummy.date = elem1.date - 24 * 60 * 60 * 1000;
dummy.recordDailyBasal = elem1.recordDailyBasal / 2; dummy.date = elem1.date / 2;
dummy.recordDailyBolus = elem1.recordDailyBolus / 2; dummy.date = elem1.date / 2;
dummies.add(dummy); dummies.add(dummy);
elem1.recordDailyBasal /= 2; elem1.date /= 2;
elem1.recordDailyBolus /= 2; elem1.date /= 2;
} }
} }
historyList.addAll(dummies); historyList.addAll(dummies);
Collections.sort(historyList, new Comparator<DanaRHistoryRecord>() { Collections.sort(historyList, new Comparator<TDD>() {
@Override @Override
public int compare(DanaRHistoryRecord lhs, DanaRHistoryRecord rhs) { public int compare(TDD lhs, TDD rhs) {
return (int) (rhs.recordDate - lhs.recordDate); return (int) (rhs.date - lhs.date);
} }
}); });
return historyList; return historyList;

View file

@ -32,6 +32,7 @@ import info.nightscout.androidaps.queue.commands.CommandCancelTempBasal;
import info.nightscout.androidaps.queue.commands.CommandExtendedBolus; import info.nightscout.androidaps.queue.commands.CommandExtendedBolus;
import info.nightscout.androidaps.queue.commands.CommandLoadEvents; import info.nightscout.androidaps.queue.commands.CommandLoadEvents;
import info.nightscout.androidaps.queue.commands.CommandLoadHistory; import info.nightscout.androidaps.queue.commands.CommandLoadHistory;
import info.nightscout.androidaps.queue.commands.CommandLoadTDDs;
import info.nightscout.androidaps.queue.commands.CommandReadStatus; import info.nightscout.androidaps.queue.commands.CommandReadStatus;
import info.nightscout.androidaps.queue.commands.CommandSMBBolus; import info.nightscout.androidaps.queue.commands.CommandSMBBolus;
import info.nightscout.androidaps.queue.commands.CommandSetProfile; import info.nightscout.androidaps.queue.commands.CommandSetProfile;
@ -390,6 +391,25 @@ public class CommandQueue {
return true; return true;
} }
// returns true if command is queued
public boolean loadTDDs(Callback callback) {
if (isRunning(Command.CommandType.LOADHISTORY)) {
if (callback != null)
callback.result(executingNowError()).run();
return false;
}
// remove all unfinished
removeAll(Command.CommandType.LOADHISTORY);
// add new command to queue
add(new CommandLoadTDDs(callback));
notifyAboutNewCommand();
return true;
}
// returns true if command is queued // returns true if command is queued
public boolean loadEvents(Callback callback) { public boolean loadEvents(Callback callback) {
if (isRunning(Command.CommandType.LOADEVENTS)) { if (isRunning(Command.CommandType.LOADEVENTS)) {

View file

@ -16,7 +16,7 @@ public abstract class Command {
EXTENDEDBOLUS, EXTENDEDBOLUS,
BASALPROFILE, BASALPROFILE,
READSTATUS, READSTATUS,
LOADHISTORY, // so far only Dana specific LOADHISTORY, // TDDs and so far only Dana specific
LOADEVENTS // so far only Dana specific LOADEVENTS // so far only Dana specific
} }

View file

@ -0,0 +1,31 @@
package info.nightscout.androidaps.queue.commands;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.queue.Callback;
/**
* Created by mike on 10.11.2017.
*/
public class CommandLoadTDDs extends Command {
public CommandLoadTDDs(Callback callback) {
commandType = CommandType.LOADHISTORY; //belongs to the history group of commands
this.callback = callback;
}
@Override
public void execute() {
PumpInterface pump = ConfigBuilderPlugin.getActivePump();
PumpEnactResult r = pump.loadTDDs();
if (callback != null)
callback.result(r).run();
}
@Override
public String status() {
return "LOADTDDS";
}
}