handle calculation properly when historical data changed
This commit is contained in:
parent
4dd9281276
commit
76993e17e5
|
@ -4,8 +4,6 @@ package info.nightscout.androidaps;
|
|||
* Created by mike on 07.06.2016.
|
||||
*/
|
||||
public class Config {
|
||||
public static final boolean CACHECALCULATIONS = true;
|
||||
|
||||
// MAIN FUCTIONALITY
|
||||
public static final boolean APS = BuildConfig.APS;
|
||||
// PLUGINS
|
||||
|
|
|
@ -29,6 +29,7 @@ import info.nightscout.androidaps.Config;
|
|||
import info.nightscout.androidaps.Constants;
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.events.EventTreatmentChange;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
|
||||
|
||||
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
||||
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
|
||||
|
@ -43,7 +44,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
private static final int DATABASE_VERSION = 6;
|
||||
|
||||
private long latestTreatmentChange = 0;
|
||||
private static Long latestTreatmentChange = null;
|
||||
|
||||
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
|
||||
private static ScheduledFuture<?> scheduledPost = null;
|
||||
|
@ -131,7 +132,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
|
||||
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
|
||||
latestTreatmentChange = 0;
|
||||
latestTreatmentChange = 0L;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -141,7 +142,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
try {
|
||||
TableUtils.dropTable(connectionSource, Treatment.class, true);
|
||||
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
|
||||
latestTreatmentChange = 0;
|
||||
latestTreatmentChange = 0L;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -211,7 +212,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public int delete(DbRequest dbr) {
|
||||
public int delete(DbRequest dbr) {
|
||||
try {
|
||||
return getDaoDbRequest().delete(dbr);
|
||||
} catch (SQLException e) {
|
||||
|
@ -259,16 +260,22 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
|
||||
// TREATMENT HANDLING
|
||||
|
||||
public boolean isDataUnchanged(long time) {
|
||||
if (time >= latestTreatmentChange) return true;
|
||||
else return false;
|
||||
public boolean affectingIobCob(Treatment t) {
|
||||
Treatment existing = findTreatmentByTimeIndex(t.timeIndex);
|
||||
if (existing == null)
|
||||
return true;
|
||||
if (existing.insulin == t.insulin && existing.carbs == t.carbs)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public int update(Treatment treatment) {
|
||||
int updated = 0;
|
||||
try {
|
||||
boolean historyChange = affectingIobCob(treatment);
|
||||
updated = getDaoTreatments().update(treatment);
|
||||
latestTreatmentChange = treatment.getTimeIndex();
|
||||
if (historyChange)
|
||||
latestTreatmentChange = treatment.getTimeIndex();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -279,8 +286,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
|
||||
Dao.CreateOrUpdateStatus status = null;
|
||||
try {
|
||||
boolean historyChange = affectingIobCob(treatment);
|
||||
status = getDaoTreatments().createOrUpdate(treatment);
|
||||
latestTreatmentChange = treatment.getTimeIndex();
|
||||
if (historyChange)
|
||||
latestTreatmentChange = treatment.getTimeIndex();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -379,6 +388,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
class PostRunnable implements Runnable {
|
||||
public void run() {
|
||||
MainApp.bus().post(new EventTreatmentChange());
|
||||
if (latestTreatmentChange != null)
|
||||
MainApp.bus().post(new EventNewHistoryData(latestTreatmentChange));
|
||||
latestTreatmentChange = null;
|
||||
scheduledPost = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import info.nightscout.androidaps.events.EventNewBasalProfile;
|
|||
import info.nightscout.androidaps.interfaces.PluginBase;
|
||||
import info.nightscout.androidaps.interfaces.TreatmentsInterface;
|
||||
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
|
||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
|
||||
import info.nightscout.utils.Round;
|
||||
|
@ -324,13 +325,14 @@ public class IobCobCalculatorPlugin implements PluginBase {
|
|||
log.debug(autosensData.log(bgTime));
|
||||
}
|
||||
}
|
||||
MainApp.bus().post(new EventAutosensCalculationFinished());
|
||||
//log.debug("Releasing calculateSensitivityData");
|
||||
}
|
||||
|
||||
public static IobTotal calulateFromTreatmentsAndTemps(long time) {
|
||||
long now = new Date().getTime();
|
||||
time = roundUpTime(time);
|
||||
if (Config.CACHECALCULATIONS && time < now && iobTable.get(time) != null) {
|
||||
if (time < now && iobTable.get(time) != null) {
|
||||
//log.debug(">>> Cache hit");
|
||||
return iobTable.get(time);
|
||||
} else {
|
||||
|
@ -344,7 +346,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
|
|||
}
|
||||
*/
|
||||
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
|
||||
if (Config.CACHECALCULATIONS && time < new Date().getTime()) {
|
||||
if (time < new Date().getTime()) {
|
||||
iobTable.put(time, iobTotal);
|
||||
}
|
||||
return iobTotal;
|
||||
|
@ -356,11 +358,11 @@ public class IobCobCalculatorPlugin implements PluginBase {
|
|||
return null;
|
||||
time = roundUpTime(time);
|
||||
AutosensData data = autosensDataTable.get(time);
|
||||
if (Config.CACHECALCULATIONS && data != null) {
|
||||
log.debug(">>> Cache hit " + data.log(time));
|
||||
if (data != null) {
|
||||
//log.debug(">>> Cache hit " + data.log(time));
|
||||
return data;
|
||||
} else {
|
||||
log.debug(">>> Cache miss " + new Date(time).toLocaleString());
|
||||
//log.debug(">>> Cache miss " + new Date(time).toLocaleString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -541,6 +543,12 @@ public class IobCobCalculatorPlugin implements PluginBase {
|
|||
}
|
||||
}
|
||||
}
|
||||
sHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
calculateSensitivityData();
|
||||
}
|
||||
});
|
||||
//log.debug("Releasing onNewHistoryData");
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package info.nightscout.androidaps.plugins.IobCobCalculator.events;
|
||||
|
||||
/**
|
||||
* Created by mike on 30.04.2017.
|
||||
*/
|
||||
|
||||
public class EventAutosensCalculationFinished {
|
||||
}
|
|
@ -89,6 +89,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
|||
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
|
||||
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
|
||||
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
||||
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
|
||||
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
|
||||
|
@ -702,6 +703,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
|
|||
scheduleUpdateGUI("EventRefreshGui");
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
|
||||
scheduleUpdateGUI("EventRefreshGui");
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onStatusEvent(final EventTreatmentChange ev) {
|
||||
scheduleUpdateGUI("EventTreatmentChange");
|
||||
|
|
Loading…
Reference in a new issue