handle calculation properly when historical data changed

This commit is contained in:
Milos Kozak 2017-04-30 10:23:26 +02:00
parent 4dd9281276
commit 76993e17e5
5 changed files with 48 additions and 16 deletions

View file

@ -4,8 +4,6 @@ package info.nightscout.androidaps;
* Created by mike on 07.06.2016. * Created by mike on 07.06.2016.
*/ */
public class Config { public class Config {
public static final boolean CACHECALCULATIONS = true;
// MAIN FUCTIONALITY // MAIN FUCTIONALITY
public static final boolean APS = BuildConfig.APS; public static final boolean APS = BuildConfig.APS;
// PLUGINS // PLUGINS

View file

@ -29,6 +29,7 @@ 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.events.EventTreatmentChange; import info.nightscout.androidaps.events.EventTreatmentChange;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventNewHistoryData;
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);
@ -43,7 +44,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final int DATABASE_VERSION = 6; 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 final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledPost = null; private static ScheduledFuture<?> scheduledPost = null;
@ -131,7 +132,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; latestTreatmentChange = 0L;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -141,7 +142,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; latestTreatmentChange = 0L;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -259,15 +260,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// TREATMENT HANDLING // TREATMENT HANDLING
public boolean isDataUnchanged(long time) { public boolean affectingIobCob(Treatment t) {
if (time >= latestTreatmentChange) return true; Treatment existing = findTreatmentByTimeIndex(t.timeIndex);
else return false; if (existing == null)
return true;
if (existing.insulin == t.insulin && existing.carbs == t.carbs)
return false;
return true;
} }
public int update(Treatment treatment) { public int update(Treatment treatment) {
int updated = 0; int updated = 0;
try { try {
boolean historyChange = affectingIobCob(treatment);
updated = getDaoTreatments().update(treatment); updated = getDaoTreatments().update(treatment);
if (historyChange)
latestTreatmentChange = treatment.getTimeIndex(); latestTreatmentChange = treatment.getTimeIndex();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -279,7 +286,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) { public Dao.CreateOrUpdateStatus createOrUpdate(Treatment treatment) {
Dao.CreateOrUpdateStatus status = null; Dao.CreateOrUpdateStatus status = null;
try { try {
boolean historyChange = affectingIobCob(treatment);
status = getDaoTreatments().createOrUpdate(treatment); status = getDaoTreatments().createOrUpdate(treatment);
if (historyChange)
latestTreatmentChange = treatment.getTimeIndex(); latestTreatmentChange = treatment.getTimeIndex();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@ -379,6 +388,9 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
class PostRunnable implements Runnable { class PostRunnable implements Runnable {
public void run() { public void run() {
MainApp.bus().post(new EventTreatmentChange()); MainApp.bus().post(new EventTreatmentChange());
if (latestTreatmentChange != null)
MainApp.bus().post(new EventNewHistoryData(latestTreatmentChange));
latestTreatmentChange = null;
scheduledPost = null; scheduledPost = null;
} }
} }

View file

@ -27,6 +27,7 @@ import info.nightscout.androidaps.events.EventNewBasalProfile;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; 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.IobCobCalculator.events.EventNewHistoryData;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
@ -324,13 +325,14 @@ public class IobCobCalculatorPlugin implements PluginBase {
log.debug(autosensData.log(bgTime)); log.debug(autosensData.log(bgTime));
} }
} }
MainApp.bus().post(new EventAutosensCalculationFinished());
//log.debug("Releasing calculateSensitivityData"); //log.debug("Releasing calculateSensitivityData");
} }
public static IobTotal calulateFromTreatmentsAndTemps(long time) { public static IobTotal calulateFromTreatmentsAndTemps(long time) {
long now = new Date().getTime(); long now = new Date().getTime();
time = roundUpTime(time); time = roundUpTime(time);
if (Config.CACHECALCULATIONS && time < now && iobTable.get(time) != null) { if (time < now && iobTable.get(time) != null) {
//log.debug(">>> Cache hit"); //log.debug(">>> Cache hit");
return iobTable.get(time); return iobTable.get(time);
} else { } else {
@ -344,7 +346,7 @@ public class IobCobCalculatorPlugin implements PluginBase {
} }
*/ */
IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round(); IobTotal iobTotal = IobTotal.combine(bolusIob, basalIob).round();
if (Config.CACHECALCULATIONS && time < new Date().getTime()) { if (time < new Date().getTime()) {
iobTable.put(time, iobTotal); iobTable.put(time, iobTotal);
} }
return iobTotal; return iobTotal;
@ -356,11 +358,11 @@ public class IobCobCalculatorPlugin implements PluginBase {
return null; return null;
time = roundUpTime(time); time = roundUpTime(time);
AutosensData data = autosensDataTable.get(time); AutosensData data = autosensDataTable.get(time);
if (Config.CACHECALCULATIONS && data != null) { if (data != null) {
log.debug(">>> Cache hit " + data.log(time)); //log.debug(">>> Cache hit " + data.log(time));
return data; return data;
} else { } else {
log.debug(">>> Cache miss " + new Date(time).toLocaleString()); //log.debug(">>> Cache miss " + new Date(time).toLocaleString());
return null; return null;
} }
} }
@ -541,6 +543,12 @@ public class IobCobCalculatorPlugin implements PluginBase {
} }
} }
} }
sHandler.post(new Runnable() {
@Override
public void run() {
calculateSensitivityData();
}
});
//log.debug("Releasing onNewHistoryData"); //log.debug("Releasing onNewHistoryData");
} }

View file

@ -0,0 +1,8 @@
package info.nightscout.androidaps.plugins.IobCobCalculator.events;
/**
* Created by mike on 30.04.2017.
*/
public class EventAutosensCalculationFinished {
}

View file

@ -89,6 +89,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; 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.LoopPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile;
@ -702,6 +703,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
scheduleUpdateGUI("EventRefreshGui"); scheduleUpdateGUI("EventRefreshGui");
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
scheduleUpdateGUI("EventRefreshGui");
}
@Subscribe @Subscribe
public void onStatusEvent(final EventTreatmentChange ev) { public void onStatusEvent(final EventTreatmentChange ev) {
scheduleUpdateGUI("EventTreatmentChange"); scheduleUpdateGUI("EventTreatmentChange");