work on tempbasals

This commit is contained in:
Milos Kozak 2016-06-22 18:08:21 +02:00
parent 58388c4a87
commit 91b36a8528
12 changed files with 159 additions and 100 deletions

View file

@ -347,8 +347,6 @@ public class DataService extends IntentService {
}
}
}
MainApp.bus().post(new EventTreatmentChange());
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e1) {

View file

@ -144,6 +144,19 @@ public class TempBasal {
return (remainingMin < 0) ? 0 : (int) remainingMin;
}
public boolean isInProgress() {
long now = new Date().getTime();
if (timeStart.getTime() > now) return false; // in the future
if (timeEnd == null) { // open end
if (timeStart.getTime() < now && getPlannedTimeEnd().getTime() > now)
return true; // in interval
return false;
}
// closed end
if (timeStart.getTime() < now && timeEnd.getTime() > now) return true; // in interval
return false;
}
public String log() {
return "TempBasal{" +
"timeIndex=" + timeIndex +

View file

@ -339,6 +339,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
/**
* expect absolute request and allow both absolute and percent response based on pump capabilities
*
* @param request
* @return
*/
@ -346,23 +347,41 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
public Result applyAPSRequest(APSResult request) {
Double rateAfterConstraints = applyBasalConstraints(request.rate);
request.rate = rateAfterConstraints;
Result result = activePump.applyAPSRequest(request);
Result result = null;
if (request.rate == getBaseBasalRate()) {
if (isTempBasalInProgress()) {
result = cancelTempBasal();
if (result.enacted) {
if (result.isPercent) {
if (result.percent == 0) {
uploadTempBasalEnd();
} else {
uploadTempBasalStartPercent(result.percent, result.duration);
MainApp.bus().post(new EventTempBasalChange());
}
} else {
if (result.absolute == 0d) {
uploadTempBasalEnd();
result = new Result();
result.absolute = request.rate;
result.duration = 0;
result.enacted = false;
result.comment = "Basal set correctly";
result.success = true;
}
} else if (isTempBasalInProgress() && request.rate == getTempBasalAbsoluteRate()) {
result = new Result();
result.absolute = request.rate;
result.duration = activePump.getTempBasal().getPlannedRemainingMinutes();
result.enacted = false;
result.comment = "Temp basal set correctly";
result.success = true;
} else {
result = setTempBasalAbsolute(request.rate, request.duration);
if (result.enacted) {
if (result.isPercent) {
uploadTempBasalStartPercent(result.percent, result.duration);
} else {
uploadTempBasalStartAbsolute(result.absolute, result.duration);
}
}
MainApp.bus().post(new EventTempBasalChange());
}
}
return result;
}
@ -466,6 +485,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI
return convertView;
}
}
@Nullable

View file

@ -194,16 +194,11 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug
@Subscribe
public void onStatusEvent(final EventTreatmentChange ev) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// invoke();
ConstraintsInterface constraintsInterface = MainApp.getConfigBuilder();
if (constraintsInterface.isAutomaticProcessingEnabled()) {
invoke();
updateGUI();
}
});
else
log.debug("EventTreatmentChange: Activity is null");
}
@Subscribe

View file

@ -166,11 +166,6 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -184,9 +179,9 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener
resultView = (TextView) view.findViewById(R.id.lowsuspend_result);
requestView = (TextView) view.findViewById(R.id.lowsuspend_request);
if (savedInstanceState != null) {
lastRun = savedInstanceState.getParcelable("lastrun");
}
// if (savedInstanceState != null) {
// lastRun = savedInstanceState.getParcelable("lastrun");
// }
updateGUI();
return view;
}

View file

@ -318,12 +318,14 @@ public class ObjectivesFragment extends Fragment implements View.OnClickListener
**/
@Override
public boolean isAutomaticProcessingEnabled() {
return objectives.get(3).started.getTime() > 0;
return true; // TODO: revert back
//return objectives.get(3).started.getTime() > 0;
}
@Override
public boolean manualConfirmationNeeded() {
return objectives.get(3).started.getTime() < 0;
return false; // TODO: revert back
//return objectives.get(3).started.getTime() < 0;
}
@Override

View file

@ -152,11 +152,6 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -173,9 +168,9 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener,
resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request);
if (savedInstanceState != null) {
lastRun = savedInstanceState.getParcelable("lastrun");
}
// if (savedInstanceState != null) {
// lastRun = savedInstanceState.getParcelable("lastrun");
// }
updateGUI();
return view;
}

View file

@ -3,12 +3,11 @@ package info.nightscout.androidaps.plugins.TempBasals;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.j256.ormlite.dao.Dao;
@ -116,6 +115,33 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
queryBuilder.limit(30l);
PreparedQuery<TempBasal> preparedQuery = queryBuilder.prepare();
tempBasals = dao.query(preparedQuery);
// Update ended
long now = new Date().getTime();
for (int position = tempBasals.size() - 1; position >= 0; position--) {
TempBasal t = tempBasals.get(position);
boolean update = false;
if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) {
t.timeEnd = new Date(t.getPlannedTimeEnd().getTime());
update = true;
}
if (position > 0) {
Date startofnewer = tempBasals.get(position - 1).timeStart;
if (t.timeEnd == null) {
t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime()));
update = true;
} else if (t.timeEnd.getTime() > startofnewer.getTime()) {
t.timeEnd = startofnewer;
update = true;
}
}
if (update) {
dao.update(t);
log.debug("Fixing unfinished temp end: " + t.log());
if (position > 0) log.debug("Previous: " + tempBasals.get(position - 1).log());
}
}
} catch (SQLException e) {
log.debug(e.getMessage(), e);
tempBasals = new ArrayList<TempBasal>();
@ -144,8 +170,17 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
TempBasal t = tempBasals.get(pos);
total.plus(t.iobCalc(now));
}
final IobTotal finalTotal = total;
Activity activity = getActivity();
if (visibleNow && activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (iobTotal != null)
iobTotal.setText(formatNumber2decimalplaces.format(total.basaliob));
iobTotal.setText(formatNumber2decimalplaces.format(finalTotal.basaliob));
}
});
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;
@ -172,24 +207,33 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
// TODO: implement locales
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, new Locale("cs", "CZ"));
DateFormat enddf = DateFormat.getTimeInstance(DateFormat.SHORT, new Locale("cs", "CZ"));
if (tempBasals.get(position).timeEnd != null) {
holder.date.setText(df.format(tempBasals.get(position).timeStart) + " - " + enddf.format(tempBasals.get(position).timeEnd));
TempBasal tempBasal = tempBasals.get(position);
if (tempBasal.timeEnd != null) {
holder.date.setText(df.format(tempBasal.timeStart) + " - " + enddf.format(tempBasals.get(position).timeEnd));
} else {
holder.date.setText(df.format(tempBasals.get(position).timeStart));
holder.date.setText(df.format(tempBasal.timeStart));
}
holder.duration.setText(formatNumber0decimalplaces.format(tempBasals.get(position).duration) + " min");
if (tempBasals.get(position).isAbsolute) {
holder.absolute.setText(formatNumber0decimalplaces.format(tempBasals.get(position).absolute) + " U/h");
holder.duration.setText(formatNumber0decimalplaces.format(tempBasal.duration) + " min");
if (tempBasal.isAbsolute) {
holder.absolute.setText(formatNumber0decimalplaces.format(tempBasal.absolute) + " U/h");
holder.percent.setText("");
} else {
holder.absolute.setText("");
holder.percent.setText(formatNumber0decimalplaces.format(tempBasals.get(position).percent) + "%");
holder.percent.setText(formatNumber0decimalplaces.format(tempBasal.percent) + "%");
}
holder.realDuration.setText(formatNumber0decimalplaces.format(tempBasals.get(position).getRealDuration()) + " min");
IobTotal iob = tempBasals.get(position).iobCalc(new Date());
holder.realDuration.setText(formatNumber0decimalplaces.format(tempBasal.getRealDuration()) + " min");
IobTotal iob = tempBasal.iobCalc(new Date());
holder.iob.setText(formatNumber2decimalplaces.format(iob.basaliob) + " U");
holder.netInsulin.setText(formatNumber2decimalplaces.format(iob.netInsulin) + " U");
holder.netRatio.setText(formatNumber2decimalplaces.format(iob.netRatio) + " U/h");
if (tempBasal.isInProgress())
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.colorInProgress));
else if (tempBasal.timeEnd == null)
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.colorNotEnded));
else if (tempBasal.iobCalc(new Date()).basaliob != 0)
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.colorAffectingIOB));
else
holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.cardColorBackground));
}
@Override
@ -212,6 +256,7 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
TextView netRatio;
TextView netInsulin;
TextView iob;
LinearLayout dateLinearLayout;
TempBasalsViewHolder(View itemView) {
super(itemView);
@ -224,6 +269,7 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa
netRatio = (TextView) itemView.findViewById(R.id.tempbasals_netratio);
netInsulin = (TextView) itemView.findViewById(R.id.tempbasals_netinsulin);
iob = (TextView) itemView.findViewById(R.id.tempbasals_iob);
dateLinearLayout = (LinearLayout) itemView.findViewById(R.id.tempbasals_datelinearlayout);
}
}
}

View file

@ -153,10 +153,20 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
Iob bIOB = t.iobCalc(now, dia / 2);
total.bolussnooze += bIOB.iobContrib;
}
final IobTotal finalTotal = total;
Activity activity = getActivity();
if (visibleNow && activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (iobTotal != null)
iobTotal.setText(formatNumber2decimalplaces.format(total.iob));
iobTotal.setText(formatNumber2decimalplaces.format(finalTotal.iob));
if (activityTotal != null)
activityTotal.setText(formatNumber3decimalplaces.format(total.activity));
activityTotal.setText(formatNumber3decimalplaces.format(finalTotal.activity));
}
});
lastCalculationTimestamp = new Date().getTime();
lastCalculation = total;

View file

@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainActivity;
@ -225,7 +226,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
result.success = true;
result.bolusDelivered = insulin;
result.carbsDelivered = carbs;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (Config.logPumpComm)
log.debug("Delivering treatment insulin: " + insulin + "U carbs: " + carbs + "g " + result);
@ -248,13 +249,13 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
result.enacted = true;
result.absolute = absoluteRate;
result.duration = durationInMinutes;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = getString(R.string.virtualpump_sqlerror);
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting temp basal absolute: " + result);
@ -281,13 +282,13 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
result.percent = percent;
result.isPercent = true;
result.duration = durationInMinutes;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = getString(R.string.virtualpump_sqlerror);
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Settings temp basal percent: " + result);
@ -311,14 +312,14 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
result.enacted = true;
result.bolusDelivered = insulin;
result.duration = durationInMinutes;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
try {
MainApp.instance().getDbHelper().getDaoTempBasals().create(extendedBolus);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = getString(R.string.virtualpump_sqlerror);
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
if (Config.logPumpComm)
log.debug("Setting extended bolus: " + result);
@ -331,23 +332,23 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
checkForExpiredTempsAndExtended();
Result result = new Result();
result.success = true;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
if (isTempBasalInProgress()) {
result.enacted = true;
tempBasal.timeEnd = new Date();
try {
MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal);
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = getString(R.string.virtualpump_sqlerror);
}
}
tempBasal = null;
if (Config.logPumpComm)
log.debug("Canceling temp basal: " + result);
updateGUI();
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.enacted = false;
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
return result;
}
@ -362,12 +363,12 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
} catch (SQLException e) {
e.printStackTrace();
result.success = false;
result.comment = getString(R.string.virtualpump_sqlerror);
result.comment = MainApp.instance().getString(R.string.virtualpump_sqlerror);
}
}
result.success = true;
result.enacted = true;
result.comment = getString(R.string.virtualpump_resultok);
result.comment = MainApp.instance().getString(R.string.virtualpump_resultok);
extendedBolus = null;
if (Config.logPumpComm)
log.debug("Canceling extended basal: " + result);
@ -377,30 +378,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt
@Override
public Result applyAPSRequest(APSResult request) {
if (isTempBasalInProgress()) {
if (request.rate == getTempBasalAbsoluteRate()) {
Result noChange = new Result();
noChange.absolute = request.rate;
noChange.duration = tempBasal.getPlannedRemainingMinutes();
noChange.enacted = false;
noChange.comment = "Temp basal set correctly";
noChange.success = true;
return noChange;
} else {
return setTempBasalAbsolute(request.rate, request.duration);
}
}
if (request.rate == getBaseBasalRate()) {
Result noChange = new Result();
noChange.absolute = request.rate;
noChange.duration = 0;
noChange.enacted = false;
noChange.comment = "Basal set correctly";
noChange.success = true;
return noChange;
}
return setTempBasalAbsolute(request.rate, request.duration);
// This should be implemented only on ConfigBuilder
return null;
}
@Override

View file

@ -19,7 +19,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="horizontal">
android:orientation="horizontal"
android:id="@+id/tempbasals_datelinearlayout">
<TextView
android:id="@+id/tempbasals_date"

View file

@ -14,4 +14,9 @@
<color name="colorCancelTempButton">#FF47C8FF</color>
<color name="colorSetTempButton">#FF478EFF</color>
<color name="colorSetExtendedButton">#FFDD7792</color>
<color name="colorInProgress">#c45026</color>
<color name="colorAffectingIOB">#830400</color>
<color name="colorNotEnded">#190084</color>
</resources>