TempBasal, TempTarget refactor; NonOverlapingIntervalsTest

This commit is contained in:
Milos Kozak 2018-03-26 22:37:53 +02:00
parent a4cb4cbbd5
commit cc436a09d9
18 changed files with 296 additions and 192 deletions

View file

@ -22,10 +22,6 @@ public abstract class Intervals<T extends Interval> {
rawData = new LongSparseArray<T>();
}
public Intervals(LongSparseArray<T> data) {
rawData = data;
}
public synchronized Intervals reset() {
rawData = new LongSparseArray<T>();
return this;

View file

@ -58,7 +58,7 @@ import info.nightscout.utils.PercentageSplitter;
/**
* This Helper contains all resource to provide a central DB management functionality. Only methods handling
* data-structure (and not the DB content) should be contained in here (meaning DDL and not SQL).
*
* <p>
* This class can safely be called from Services, but should not call Services to avoid circular dependencies.
* One major issue with this (right now) are the scheduled events, which are put into the service. Therefor all
* direct calls to the corresponding methods (eg. resetDatabases) should be done by a central service.
@ -893,14 +893,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void createTemptargetFromJsonIfNotExists(JSONObject trJson) {
try {
String units = MainApp.getConfigBuilder().getProfileUnits();
TempTarget tempTarget = new TempTarget();
tempTarget.date = trJson.getLong("mills");
tempTarget.durationInMinutes = trJson.getInt("duration");
tempTarget.low = Profile.toMgdl(trJson.getDouble("targetBottom"), units);
tempTarget.high = Profile.toMgdl(trJson.getDouble("targetTop"), units);
tempTarget.reason = trJson.getString("reason");
tempTarget._id = trJson.getString("_id");
tempTarget.source = Source.NIGHTSCOUT;
TempTarget tempTarget = new TempTarget()
.date(trJson.getLong("mills"))
.duration(trJson.getInt("duration"))
.low(Profile.toMgdl(trJson.getDouble("targetBottom"), units))
.high(Profile.toMgdl(trJson.getDouble("targetTop"), units))
.reason(trJson.getString("reason"))
._id(trJson.getString("_id"))
.source(Source.NIGHTSCOUT);
createOrUpdate(tempTarget);
} catch (JSONException e) {
log.error("Unhandled exception", e);
@ -1169,10 +1169,10 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
}
createOrUpdate(extendedBolus);
} else {
TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = trJson.getLong("mills");
tempBasal.source = Source.NIGHTSCOUT;
tempBasal.pumpId = trJson.has("pumpId") ? trJson.getLong("pumpId") : 0;
TemporaryBasal tempBasal = new TemporaryBasal()
.date(trJson.getLong("mills"))
.source(Source.NIGHTSCOUT)
.pumpId(trJson.has("pumpId") ? trJson.getLong("pumpId") : 0);
if (trJson.has("duration")) {
tempBasal.durationInMinutes = trJson.getInt("duration");
}

View file

@ -91,6 +91,21 @@ public class TempTarget implements Interval {
return this;
}
public TempTarget reason(String reason) {
this.reason = reason;
return this;
}
public TempTarget _id(String _id) {
this._id = _id;
return this;
}
public TempTarget source(int source) {
this.source = source;
return this;
}
// -------- Interval interface ---------
Long cuttedEnd = null;

View file

@ -60,8 +60,36 @@ public class TemporaryBasal implements Interval {
public TemporaryBasal() {
}
public TemporaryBasal(long date) {
public TemporaryBasal date(long date) {
this.date = date;
return this;
}
public TemporaryBasal duration(int durationInMinutes) {
this.durationInMinutes = durationInMinutes;
return this;
}
public TemporaryBasal absolute(double absoluteRate) {
this.absoluteRate = absoluteRate;
this.isAbsolute = true;
return this;
}
public TemporaryBasal percent(int percentRate) {
this.percentRate = percentRate;
this.isAbsolute = false;
return this;
}
public TemporaryBasal source(int source) {
this.source = source;
return this;
}
public TemporaryBasal pumpId(long pumpId) {
this.pumpId = pumpId;
return this;
}
public TemporaryBasal(ExtendedBolus extendedBolus) {

View file

@ -701,17 +701,16 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
} else if (options.executeTempTarget) {
try {
if ((data.has("targetBottom") && data.has("targetTop")) || (data.has("duration") && data.getInt("duration") == 0)) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = eventTime.getTime();
tempTarget.durationInMinutes = data.getInt("duration");
tempTarget.reason = data.getString("reason");
tempTarget.source = Source.USER;
TempTarget tempTarget = new TempTarget()
.date(eventTime.getTime())
.duration(data.getInt("duration"))
.reason(data.getString("reason"))
.source(Source.USER);
if (tempTarget.durationInMinutes != 0) {
tempTarget.low = Profile.toMgdl(data.getDouble("targetBottom"), profile.getUnits());
tempTarget.high = Profile.toMgdl(data.getDouble("targetTop"), profile.getUnits());
tempTarget.low(Profile.toMgdl(data.getDouble("targetBottom"), profile.getUnits()))
.high(Profile.toMgdl(data.getDouble("targetTop"), profile.getUnits()));
} else {
tempTarget.low = 0;
tempTarget.high = 0;
tempTarget.low(0).high(0);
}
log.debug("Creating new TempTarget db record: " + tempTarget.toString());
MainApp.getDbHelper().createOrUpdate(tempTarget);

View file

@ -352,11 +352,10 @@ public class IobCobCalculatorPlugin implements PluginBase {
if (OpenAPSSMBPlugin.getPlugin().isEnabled(PluginBase.APS)) {
// Add expected zere temp basal for next 240 mins
IobTotal basalIobWithZeroTemp = basalIob.clone();
TemporaryBasal t = new TemporaryBasal();
t.date = now + 60 * 1000L;
t.durationInMinutes = 240;
t.isAbsolute = true;
t.absoluteRate = 0;
TemporaryBasal t = new TemporaryBasal()
.date(now + 60 * 1000L)
.duration(240)
.absolute(0);
if (t.date < time) {
IobTotal calc = t.iobCalc(time);
basalIobWithZeroTemp.plus(calc);

View file

@ -380,31 +380,31 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D
accepted = true;
if (startActivityTTCheckbox.isChecked()) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = System.currentTimeMillis();
tempTarget.durationInMinutes = finalActivityTTDuration;
tempTarget.reason = MainApp.gs(R.string.activity);
tempTarget.source = Source.USER;
tempTarget.low = Profile.toMgdl(finalActivityTT, currentProfile.getUnits());
tempTarget.high = Profile.toMgdl(finalActivityTT, currentProfile.getUnits());
TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis())
.duration(finalActivityTTDuration)
.reason(MainApp.gs(R.string.activity))
.source(Source.USER)
.low(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()))
.high(Profile.toMgdl(finalActivityTT, currentProfile.getUnits()));
MainApp.getDbHelper().createOrUpdate(tempTarget);
} else if (startEatingSoonTTCheckbox.isChecked()) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = System.currentTimeMillis();
tempTarget.durationInMinutes = finalEatingSoonTTDuration;
tempTarget.reason = MainApp.gs(R.string.eatingsoon);
tempTarget.source = Source.USER;
tempTarget.low = Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits());
tempTarget.high = Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits());
TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis())
.duration(finalEatingSoonTTDuration)
.reason(MainApp.gs(R.string.eatingsoon))
.source(Source.USER)
.low(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()))
.high(Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()));
MainApp.getDbHelper().createOrUpdate(tempTarget);
} else if (startHypoTTCheckbox.isChecked()) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = System.currentTimeMillis();
tempTarget.durationInMinutes = finalHypoTTDuration;
tempTarget.reason = MainApp.gs(R.string.hypo);
tempTarget.source = Source.USER;
tempTarget.low = Profile.toMgdl(finalHypoTT, currentProfile.getUnits());
tempTarget.high = Profile.toMgdl(finalHypoTT, currentProfile.getUnits());
TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis())
.duration(finalHypoTTDuration)
.reason(MainApp.gs(R.string.hypo))
.source(Source.USER)
.low(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()))
.high(Profile.toMgdl(finalHypoTT, currentProfile.getUnits()));
MainApp.getDbHelper().createOrUpdate(tempTarget);
}

View file

@ -281,13 +281,13 @@ public class NewInsulinDialog extends DialogFragment implements OnClickListener,
accepted = true;
if (startESMCheckbox.isChecked()) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = System.currentTimeMillis();
tempTarget.durationInMinutes = (int) ttDuration;
tempTarget.reason = "Eating soon";
tempTarget.source = Source.USER;
tempTarget.low = (int) finalTT;
tempTarget.high = (int) finalTT;
TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis())
.duration((int) ttDuration)
.reason("Eating soon")
.source(Source.USER)
.low((int) finalTT)
.high((int) finalTT);
MainApp.getDbHelper().createOrUpdate(tempTarget);
}

View file

@ -109,10 +109,14 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
@NonNull
private static final ComboPump pump = new ComboPump();
/** This is used to determine when to pass a bolus cancel request to the scripter */
/**
* This is used to determine when to pass a bolus cancel request to the scripter
*/
private volatile boolean scripterIsBolusing;
/** This is set to true to request a bolus cancellation. {@link #deliverBolus(DetailedBolusInfo)}
* will reset this flag. */
/**
* This is set to true to request a bolus cancellation. {@link #deliverBolus(DetailedBolusInfo)}
* will reset this flag.
*/
private volatile boolean cancelBolus;
/**
@ -131,10 +135,12 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
*/
private volatile boolean pumpHistoryChanged = false;
/** Cache of the last <=2 boluses on the pump. Used to detect changes in pump history,
/**
* Cache of the last <=2 boluses on the pump. Used to detect changes in pump history,
* requiring reading pump more history. This is read/set in {@link #checkHistory()} when changed
* pump history was detected and was read, as well as in {@link #deliverBolus(DetailedBolusInfo)}
* after bolus delivery. Newest record is the first one. */
* after bolus delivery. Newest record is the first one.
*/
private volatile List<Bolus> recentBoluses = new ArrayList<>(0);
public static ComboPlugin getPlugin() {
@ -373,7 +379,9 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return new Date(pump.lastSuccessfulCmdTime);
}
/** Runs pump initializing if needed and reads the pump state from the main screen. */
/**
* Runs pump initializing if needed and reads the pump state from the main screen.
*/
@Override
public synchronized void getPumpStatus() {
log.debug("getPumpStatus called");
@ -445,7 +453,9 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
MainApp.bus().post(new EventComboPumpUpdateGUI());
}
/** Updates local cache with state (reservoir level, last bolus ...) returned from the pump */
/**
* Updates local cache with state (reservoir level, last bolus ...) returned from the pump
*/
private void updateLocalData(CommandResult result) {
if (result.reservoirLevel != PumpState.UNKNOWN) {
pump.reservoirLevel = result.reservoirLevel;
@ -556,7 +566,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// reject a bolus if one with the exact same size was successfully delivered
// within the last 1-2 minutes
if (Math.abs(previousBolus.amount - detailedBolusInfo.insulin) < 0.01
&& previousBolus.timestamp + 60 * 1000 > System.currentTimeMillis()) {
&& previousBolus.timestamp + 60 * 1000 > System.currentTimeMillis()) {
log.debug("Bolu request rejected, same bolus was successfully delivered very recently");
return new PumpEnactResult().success(false).enacted(false)
.comment(MainApp.gs(R.string.bolus_frequency_exceeded));
@ -737,7 +747,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
cancelBolus = true;
}
/** Note: AAPS calls this solely to enact OpenAPS suggestions
/**
* Note: AAPS calls this solely to enact OpenAPS suggestions
*
* @param force the force parameter isn't used currently since we always set the tbr -
* there might be room for optimization to first test the currently running tbr
@ -804,12 +815,11 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
PumpState state = commandResult.state;
if (state.tbrActive && state.tbrPercent == adjustedPercent
&& (state.tbrRemainingDuration == durationInMinutes || state.tbrRemainingDuration == durationInMinutes - 1)) {
TemporaryBasal tempStart = new TemporaryBasal();
tempStart.date = state.timestamp;
tempStart.durationInMinutes = state.tbrRemainingDuration;
tempStart.percentRate = state.tbrPercent;
tempStart.isAbsolute = false;
tempStart.source = Source.USER;
TemporaryBasal tempStart = new TemporaryBasal()
.date(state.timestamp)
.duration(state.tbrRemainingDuration)
.percent(state.tbrPercent)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempStart);
MainApp.bus().post(new EventComboPumpUpdateGUI());
@ -825,7 +835,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return OPERATION_NOT_SUPPORTED;
}
/** Cancel an active Temp Basal. Mostly sets a fake Temp Basal to avoid a TBR CANCELLED
/**
* Cancel an active Temp Basal. Mostly sets a fake Temp Basal to avoid a TBR CANCELLED
* alert. This relies on TemporaryBasal objects to properly reflect the pumps state,
* which is ensured by {@link #checkAndResolveTbrMismatch(PumpState)}, which runs on each
* connect. When a hard cancel is requested, the pump is queried for it's TBR state to
@ -850,10 +861,10 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return new PumpEnactResult().success(false).enacted(false);
}
if (!cancelResult.state.tbrActive) {
TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = cancelResult.state.timestamp;
tempBasal.durationInMinutes = 0;
tempBasal.source = Source.USER;
TemporaryBasal tempBasal = new TemporaryBasal()
.date(cancelResult.state.timestamp)
.duration(0)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal);
return new PumpEnactResult().isTempCancel(true).success(true).enacted(true);
} else {
@ -995,12 +1006,11 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
TemporaryBasal aapsTbr = MainApp.getConfigBuilder().getTempBasalFromHistory(now);
if (aapsTbr == null || aapsTbr.percentRate != 0) {
log.debug("Creating 15m zero temp since pump is suspended");
TemporaryBasal newTempBasal = new TemporaryBasal();
newTempBasal.date = now;
newTempBasal.percentRate = 0;
newTempBasal.isAbsolute = false;
newTempBasal.durationInMinutes = 15;
newTempBasal.source = Source.USER;
TemporaryBasal newTempBasal = new TemporaryBasal()
.date(now)
.percent(0)
.duration(15)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(newTempBasal);
}
}
@ -1047,8 +1057,10 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
}
}
/** Check pump time (on the main menu) and raise notification if time is off.
* (setting clock is not supported by ruffy) */
/**
* Check pump time (on the main menu) and raise notification if time is off.
* (setting clock is not supported by ruffy)
*/
private void checkPumpTime(PumpState state) {
if (state.pumpTime == 0) {
// time couldn't be read (e.g. a warning is displayed on the menu , hiding the time field)
@ -1124,12 +1136,11 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
.putCustomAttribute("buildversion", BuildConfig.BUILDVERSION)
.putCustomAttribute("version", BuildConfig.VERSION)
.putCustomAttribute("type", "new TBR on pump"));
TemporaryBasal newTempBasal = new TemporaryBasal();
newTempBasal.date = now;
newTempBasal.percentRate = state.tbrPercent;
newTempBasal.isAbsolute = false;
newTempBasal.durationInMinutes = state.tbrRemainingDuration;
newTempBasal.source = Source.USER;
TemporaryBasal newTempBasal = new TemporaryBasal()
.date(now)
.percent(state.tbrPercent)
.duration(state.tbrRemainingDuration)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(newTempBasal);
} else if (aapsTbr != null && aapsTbr.getPlannedRemainingMinutes() > 2 && !state.tbrActive) {
log.debug("Ending AAPS-TBR since pump has no TBR active");
@ -1137,10 +1148,10 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
.putCustomAttribute("buildversion", BuildConfig.BUILDVERSION)
.putCustomAttribute("version", BuildConfig.VERSION)
.putCustomAttribute("type", "TBR cancelled on pump"));
TemporaryBasal tempStop = new TemporaryBasal();
tempStop.date = now;
tempStop.durationInMinutes = 0;
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal()
.date(now)
.duration(0)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempStop);
} else if (aapsTbr != null && state.tbrActive
&& (aapsTbr.percentRate != state.tbrPercent ||
@ -1150,23 +1161,24 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
.putCustomAttribute("buildversion", BuildConfig.BUILDVERSION)
.putCustomAttribute("version", BuildConfig.VERSION)
.putCustomAttribute("type", "TBR on pump differs from AAPS TBR"));
TemporaryBasal tempStop = new TemporaryBasal();
tempStop.date = now - 1000;
tempStop.durationInMinutes = 0;
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal()
.date(now - 1000)
.duration(0)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempStop);
TemporaryBasal newTempBasal = new TemporaryBasal();
newTempBasal.date = now;
newTempBasal.percentRate = state.tbrPercent;
newTempBasal.isAbsolute = false;
newTempBasal.durationInMinutes = state.tbrRemainingDuration;
newTempBasal.source = Source.USER;
TemporaryBasal newTempBasal = new TemporaryBasal()
.date(now)
.percent(state.tbrPercent)
.duration(state.tbrRemainingDuration)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(newTempBasal);
}
}
/**Reads the pump's history and updates the DB accordingly. */
/**
* Reads the pump's history and updates the DB accordingly.
*/
private boolean readHistory(@Nullable PumpHistoryRequest request) {
CommandResult historyResult = runCommand(MainApp.gs(R.string.combo_activity_reading_pump_history), 3, () -> ruffyScripter.readHistory(request));
if (!historyResult.success) {
@ -1203,7 +1215,8 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
return updated;
}
/** Adds the bolus to the timestamp to be able to differentiate multiple boluses in the same
/**
* Adds the bolus to the timestamp to be able to differentiate multiple boluses in the same
* minute. Best effort, since this covers only boluses up to 6.0 U and relies on other code
* to prevent a boluses with the same amount to be delivered within the same minute.
* Should be good enough, even with command mode, it's a challenge to create that situation
@ -1220,7 +1233,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// ConfigBuilderPlugin.getCommandQueue().custom(new Callback() {
// @Override
// public void run() {
readHistory(new PumpHistoryRequest().tddHistory(PumpHistoryRequest.FULL));
readHistory(new PumpHistoryRequest().tddHistory(PumpHistoryRequest.FULL));
// }
// }, post);
if (post != null) {
@ -1237,7 +1250,7 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// ConfigBuilderPlugin.getCommandQueue().custom(new Callback() {
// @Override
// public void run() {
readHistory(new PumpHistoryRequest().pumpErrorHistory(PumpHistoryRequest.FULL));
readHistory(new PumpHistoryRequest().pumpErrorHistory(PumpHistoryRequest.FULL));
// }
// }, post);
if (post != null) {
@ -1254,14 +1267,14 @@ public class ComboPlugin implements PluginBase, PumpInterface, ConstraintsInterf
// ConfigBuilderPlugin.getCommandQueue().custom(new Callback() {
// @Override
// public void run() {
readHistory(new PumpHistoryRequest()
.bolusHistory(PumpHistoryRequest.FULL)
.pumpErrorHistory(PumpHistoryRequest.FULL)
.tddHistory(PumpHistoryRequest.FULL));
CommandResult readBasalResult = runCommand(MainApp.gs(R.string.combo_actvity_reading_basal_profile), 2, ruffyScripter::readBasalProfile);
if (readBasalResult.success) {
pump.basalProfile = readBasalResult.basalProfile;
}
readHistory(new PumpHistoryRequest()
.bolusHistory(PumpHistoryRequest.FULL)
.pumpErrorHistory(PumpHistoryRequest.FULL)
.tddHistory(PumpHistoryRequest.FULL));
CommandResult readBasalResult = runCommand(MainApp.gs(R.string.combo_actvity_reading_basal_profile), 2, ruffyScripter::readBasalProfile);
if (readBasalResult.success) {
pump.basalProfile = readBasalResult.basalProfile;
}
// }
// }, post);
if (post != null) {

View file

@ -68,33 +68,29 @@ public class MsgStatusTempBasal extends MessageBase {
if (danaRPump.isTempBasalInProgress) {
if (tempBasal.percentRate != danaRPump.tempBasalPercent) {
// Close current temp basal
TemporaryBasal tempStop = new TemporaryBasal(danaRPump.tempBasalStart.getTime() - 1000);
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal().date(danaRPump.tempBasalStart.getTime() - 1000).source(Source.USER);
treatmentsInterface.addToHistoryTempBasal(tempStop);
// Create new
TemporaryBasal newTempBasal = new TemporaryBasal();
newTempBasal.date = danaRPump.tempBasalStart.getTime();
newTempBasal.percentRate = danaRPump.tempBasalPercent;
newTempBasal.isAbsolute = false;
newTempBasal.durationInMinutes = danaRPump.tempBasalTotalSec / 60;
newTempBasal.source = Source.USER;
TemporaryBasal newTempBasal = new TemporaryBasal()
.date(danaRPump.tempBasalStart.getTime())
.percent(danaRPump.tempBasalPercent)
.duration(danaRPump.tempBasalTotalSec / 60)
.source(Source.USER);
treatmentsInterface.addToHistoryTempBasal(newTempBasal);
}
} else {
// Close current temp basal
TemporaryBasal tempStop = new TemporaryBasal(now);
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal().date(now).source(Source.USER);
treatmentsInterface.addToHistoryTempBasal(tempStop);
}
} else {
if (danaRPump.isTempBasalInProgress) {
// Create new
TemporaryBasal newTempBasal = new TemporaryBasal();
newTempBasal.date = danaRPump.tempBasalStart.getTime();
newTempBasal.percentRate = danaRPump.tempBasalPercent;
newTempBasal.isAbsolute = false;
newTempBasal.durationInMinutes = danaRPump.tempBasalTotalSec / 60;
newTempBasal.source = Source.USER;
TemporaryBasal newTempBasal = new TemporaryBasal()
.date(danaRPump.tempBasalStart.getTime())
.percent(danaRPump.tempBasalPercent)
.duration(danaRPump.tempBasalTotalSec / 60)
.source(Source.USER);
treatmentsInterface.addToHistoryTempBasal(newTempBasal);
}
}

View file

@ -85,10 +85,7 @@ public class DanaRS_Packet_APS_History_Events extends DanaRS_Packet {
int param1 = ((intFromBuff(data, 7, 1) << 8) & 0xFF00) + (intFromBuff(data, 8, 1) & 0xFF);
int param2 = ((intFromBuff(data, 9, 1) << 8) & 0xFF00) + (intFromBuff(data, 10, 1) & 0xFF);
TemporaryBasal temporaryBasal = new TemporaryBasal();
temporaryBasal.date = datetime.getTime();
temporaryBasal.source = Source.PUMP;
temporaryBasal.pumpId = datetime.getTime();
TemporaryBasal temporaryBasal = new TemporaryBasal().date(datetime.getTime()).source(Source.PUMP).pumpId(datetime.getTime());
ExtendedBolus extendedBolus = new ExtendedBolus();
extendedBolus.date = datetime.getTime();

View file

@ -56,10 +56,10 @@ public class MsgHistoryEvents_v2 extends MessageBase {
int param1 = intFromBuff(bytes, 7, 2);
int param2 = intFromBuff(bytes, 9, 2);
TemporaryBasal temporaryBasal = new TemporaryBasal();
temporaryBasal.date = datetime.getTime();
temporaryBasal.source = Source.PUMP;
temporaryBasal.pumpId = datetime.getTime();
TemporaryBasal temporaryBasal = new TemporaryBasal()
.date(datetime.getTime())
.source(Source.PUMP)
.pumpId(datetime.getTime());
ExtendedBolus extendedBolus = new ExtendedBolus();
extendedBolus.date = datetime.getTime();

View file

@ -592,12 +592,11 @@ public class InsightPlugin implements PluginBase, PumpInterface, ConstraintsInte
if (pumpEnactResult.success) {
// create log entry
final TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = System.currentTimeMillis();
tempBasal.isAbsolute = false;
tempBasal.percentRate = percent_amount;
tempBasal.durationInMinutes = durationInMinutes;
tempBasal.source = Source.USER;
final TemporaryBasal tempBasal = new TemporaryBasal()
.date(System.currentTimeMillis())
.percent(percent_amount)
.duration(durationInMinutes)
.source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal);
}
@ -643,12 +642,11 @@ public class InsightPlugin implements PluginBase, PumpInterface, ConstraintsInte
if (pumpEnactResult.success) {
// create log entry
final TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = System.currentTimeMillis();
tempBasal.isAbsolute = false;
tempBasal.percentRate = percent;
tempBasal.durationInMinutes = durationInMinutes;
tempBasal.source = Source.USER; // TODO check this is correct
final TemporaryBasal tempBasal = new TemporaryBasal()
.date(System.currentTimeMillis())
.percent(percent)
.duration(durationInMinutes)
.source(Source.USER); // TODO check this is correct
MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal);
}
@ -683,13 +681,10 @@ public class InsightPlugin implements PluginBase, PumpInterface, ConstraintsInte
}
// TODO isn't conditional on one apparently being in progress only the history change
boolean enacted = false;
final Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
if (MainApp.getConfigBuilder().isTempBasalInProgress()) {
enacted = true;
TemporaryBasal tempStop = new TemporaryBasal(System.currentTimeMillis());
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal().date(System.currentTimeMillis()).source(Source.USER);
MainApp.getConfigBuilder().addToHistoryTempBasal(tempStop);
}
lastDataTime = new Date();

View file

@ -10,9 +10,8 @@ import info.nightscout.androidaps.db.TemporaryBasal;
/**
* Created by jamorham on 27/01/2018.
*
* <p>
* Write to the History Log
*
*/
class HistoryLogAdapter {
@ -25,7 +24,7 @@ class HistoryLogAdapter {
void createTBRrecord(Date eventDate, int percent, int duration, long record_id) {
TemporaryBasal temporaryBasal = new TemporaryBasal(eventDate.getTime());
TemporaryBasal temporaryBasal = new TemporaryBasal().date(eventDate.getTime());
final TemporaryBasal temporaryBasalFromHistory = MainApp.getConfigBuilder().getTempBasalFromHistory(eventDate.getTime());
@ -50,10 +49,10 @@ class HistoryLogAdapter {
}
}
temporaryBasal.source = Source.PUMP;
temporaryBasal.pumpId = record_id;
temporaryBasal.percentRate = percent;
temporaryBasal.durationInMinutes = duration;
temporaryBasal.source(Source.PUMP)
.pumpId(record_id)
.percent(percent)
.duration(duration);
MainApp.getConfigBuilder().addToHistoryTempBasal(temporaryBasal);
}

View file

@ -291,12 +291,11 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface {
@Override
public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes, Profile profile, boolean enforceNew) {
TreatmentsInterface treatmentsInterface = MainApp.getConfigBuilder();
TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = System.currentTimeMillis();
tempBasal.isAbsolute = true;
tempBasal.absoluteRate = absoluteRate;
tempBasal.durationInMinutes = durationInMinutes;
tempBasal.source = Source.USER;
TemporaryBasal tempBasal = new TemporaryBasal()
.date(System.currentTimeMillis())
.absolute(absoluteRate)
.duration(durationInMinutes)
.source(Source.USER);
PumpEnactResult result = new PumpEnactResult();
result.success = true;
result.enacted = true;
@ -321,12 +320,11 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface {
if (!result.success)
return result;
}
TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = System.currentTimeMillis();
tempBasal.isAbsolute = false;
tempBasal.percentRate = percent;
tempBasal.durationInMinutes = durationInMinutes;
tempBasal.source = Source.USER;
TemporaryBasal tempBasal = new TemporaryBasal()
.date(System.currentTimeMillis())
.percent(percent)
.duration(durationInMinutes)
.source(Source.USER);
result.success = true;
result.enacted = true;
result.percent = percent;
@ -376,8 +374,7 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface {
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (treatmentsInterface.isTempBasalInProgress()) {
result.enacted = true;
TemporaryBasal tempStop = new TemporaryBasal(System.currentTimeMillis());
tempStop.source = Source.USER;
TemporaryBasal tempStop = new TemporaryBasal().date(System.currentTimeMillis()).source(Source.USER);
treatmentsInterface.addToHistoryTempBasal(tempStop);
//tempBasal = null;
if (Config.logPumpComm)

View file

@ -309,8 +309,8 @@ public class ActionStringHandler {
} else {
sendStatusmessage("TDD", generateTDDMessage(historyList, dummies));
}
}
});
}
});
}
} else {
// if up to date: prepare, send (check if CPP is activated -> add CPP stats)
@ -604,17 +604,15 @@ public class ActionStringHandler {
}
private static void generateTempTarget(int duration, double low, double high) {
TempTarget tempTarget = new TempTarget();
tempTarget.date = System.currentTimeMillis();
tempTarget.durationInMinutes = duration;
tempTarget.reason = "WearPlugin";
tempTarget.source = Source.USER;
TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis())
.duration(duration)
.reason("WearPlugin")
.source(Source.USER);
if (tempTarget.durationInMinutes != 0) {
tempTarget.low = low;
tempTarget.high = high;
tempTarget.low(low).high(high);
} else {
tempTarget.low = 0;
tempTarget.high = 0;
tempTarget.low(0).high(0);
}
MainApp.getDbHelper().createOrUpdate(tempTarget);

View file

@ -0,0 +1,49 @@
package info.nightscout.androidaps.data;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.T;
/**
* Created by mike on 26.03.2018.
*/
@RunWith(PowerMockRunner.class)
public class NonOverapingIntervalsTest {
private final long startDate = DateUtil.now();
NonOverlappingIntervals<TemporaryBasal> list = new NonOverlappingIntervals<>();
@Test
public void doTests() {
// create one 10h interval and test value in and out
list.add(new TemporaryBasal().date(startDate).duration((int) T.hours(10).mins()).absolute(1));
Assert.assertEquals(null, list.getValueByInterval(startDate - T.secs(1).msecs()));
Assert.assertEquals(1d, list.getValueByInterval(startDate).absoluteRate, 0.01d);
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(10).msecs() + 1));
// stop temp after 5h
list.add(new TemporaryBasal().date(startDate + T.hours(5).msecs()).duration(0));
Assert.assertEquals(null, list.getValueByInterval(startDate - T.secs(1).msecs()));
Assert.assertEquals(1d, list.getValueByInterval(startDate).absoluteRate, 0.01d);
Assert.assertEquals(1d, list.getValueByInterval(startDate + T.hours(5).msecs() - 1).absoluteRate, 0.01d);
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(5).msecs() + 1));
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(10).msecs() + 1));
// insert 1h interval inside
list.add(new TemporaryBasal().date(startDate + T.hours(3).msecs()).duration((int) T.hours(1).mins()).absolute(2));
Assert.assertEquals(null, list.getValueByInterval(startDate - T.secs(1).msecs()));
Assert.assertEquals(1d, list.getValueByInterval(startDate).absoluteRate, 0.01d);
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(5).msecs() - 1));
Assert.assertEquals(2d, list.getValueByInterval(startDate + T.hours(3).msecs()).absoluteRate, 0.01d);
Assert.assertEquals(2d, list.getValueByInterval(startDate + T.hours(4).msecs() - 1).absoluteRate, 0.01d);
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(4).msecs() + 1));
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(10).msecs() + 1));
}
}

View file

@ -5,6 +5,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.List;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.T;
@ -46,4 +49,24 @@ public class OverapingIntervalsTest {
Assert.assertEquals(null, list.getValueByInterval(startDate + T.hours(10).msecs() + 1));
}
@Test
public void testCopyConstructor() {
list.reset();
list.add(new TempTarget().date(startDate).duration((int) T.hours(10).mins()).low(100).high(100));
OverlappingIntervals<TempTarget> list2 = new OverlappingIntervals<>(list);
Assert.assertEquals(1, list2.getList().size());
}
@Test
public void testReversingArrays() {
List<TempTarget> someList = new ArrayList<>();
someList.add(new TempTarget().date(startDate).duration((int) T.hours(3).mins()).low(200).high(200));
someList.add(new TempTarget().date(startDate + T.hours(1).msecs()).duration((int) T.hours(1).mins()).low(100).high(100));
list.reset();
list.add(someList);
Assert.assertEquals(startDate, list.get(0).date);
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).date);
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversedList().get(0).date);
}
}