wear temp targets working

This commit is contained in:
AdrianLxM 2017-02-10 02:36:38 +01:00
parent 8ece312b6a
commit 9785d507a8
3 changed files with 124 additions and 15 deletions

View file

@ -5,22 +5,32 @@ import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;
import android.preference.PreferenceManager;
import android.support.annotation.BoolRes;
import android.support.v7.app.AlertDialog;
import android.view.View;
import com.j256.ormlite.dao.Dao;
import org.json.JSONException;
import org.json.JSONObject;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Date;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Overview.QuickWizard;
import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin;
import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil;
@ -76,7 +86,7 @@ public class ActionStringHandler {
rAction += "fill " + insulinAfterConstraints;
} else if ("fill".equals(act[0])) {
///////////////////////////////////// PRIME/FILL
////////////////////////////////////////////// PRIME/FILL
double amount = SafeParse.stringToDouble(act[1]);
Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(amount);
@ -86,11 +96,68 @@ public class ActionStringHandler {
rAction += "fill " + insulinAfterConstraints;
} else if ("bolus".equals(act[0])) {
////////////////////////////////////////////// BOLUS
double insulin = SafeParse.stringToDouble(act[1]);
int carbs = SafeParse.stringToInt(act[2]);
Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(insulin);
Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbs);
rMessage += MainApp.instance().getString(R.string.bolus) + ": " + insulinAfterConstraints + "U\n";
rMessage += MainApp.instance().getString(R.string.carbs) + ": " + carbsAfterConstraints + "g";
if ((insulinAfterConstraints - insulin != 0) || (carbsAfterConstraints - carbs != 0)) {
rMessage += "\n" + MainApp.instance().getString(R.string.constraintapllied);
}
rAction += "bolus " + insulinAfterConstraints + " " + carbsAfterConstraints;
} else if ("temptarget".equals(act[0])) {
///////////////////////////////////////////////////////// TEMPTARGET
boolean isMGDL = Boolean.parseBoolean(act[1]);
NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
TempTargetRangePlugin tempTargetRangePlugin = (TempTargetRangePlugin) MainApp.getSpecificPlugin(TempTargetRangePlugin.class);
if (!(Config.APS && tempTargetRangePlugin != null && tempTargetRangePlugin.isEnabled(PluginBase.GENERAL))) {
sendError("TempTargets not possible! Please check your configuration.");
return;
}
if (profile == null) {
sendError("No profile found!");
return;
}
if(profile.getUnits().equals(Constants.MGDL) != isMGDL){
sendError("Different units used on watch and phone!");
return;
}
int duration = SafeParse.stringToInt(act[2]);
if (duration == 0){
rMessage += "Zero-Temp-Target - cancelling running Temp-Targets?";
rAction = "temptarget true 0 0 0";
} else {
double low = SafeParse.stringToDouble(act[3]);
double high = SafeParse.stringToDouble(act[4]);
if(!isMGDL){
low *= Constants.MMOLL_TO_MGDL;
high *= Constants.MMOLL_TO_MGDL;
}
if (low < Constants.VERY_HARD_LIMIT_TEMP_MIN_BG[0] || low > Constants.VERY_HARD_LIMIT_TEMP_MIN_BG[1]) {
sendError("Min-BG out of range!");
return;
}
if (high < Constants.VERY_HARD_LIMIT_TEMP_MAX_BG[0] || high > Constants.VERY_HARD_LIMIT_TEMP_MAX_BG[1]) {
sendError("Max-BG out of range!");
return;
}
rMessage += "Temptarget:\nMin: " + act[3] + "\nMax: " + act[4] + "\nDuration: " + act[2];
rAction = actionstring;
}
} else if ("status".equals(act[0])) {
///////////////////////////////////// STATUS
////////////////////////////////////////////// STATUS
rTitle = "STATUS";
rAction = "statusmessage";
//TODO: add meaningfull status
//TODO: add meaningful status
if("general".equals(act[1])){
rMessage = "Today is going to be a good day!";
@ -108,6 +175,7 @@ public class ActionStringHandler {
rMessage += "\n\n\nTODO:\nAdd some meaningful status.";
} else if ("wizard".equals(act[0])) {
////////////////////////////////////////////// WIZARD
Integer carbsBeforeConstraints = SafeParse.stringToInt(act[1]);
Integer carbsAfterConstraints = MainApp.getConfigBuilder().applyCarbsConstraints(carbsBeforeConstraints);
//TODO: wizard calculation
@ -141,7 +209,7 @@ public class ActionStringHandler {
// do the parsing, check constraints and enact!
String[] act = actionString.split("\\s+");
if (false && "fill".equals(act[0])){
if ("fill".equals(act[0])){
Double amount = SafeParse.stringToDouble(act[1]);
Double insulinAfterConstraints = MainApp.getConfigBuilder().applyBolusConstraints(amount);
if(amount - insulinAfterConstraints != 0){
@ -150,12 +218,48 @@ public class ActionStringHandler {
return;
}
doFillBolus(amount);
} else if ("temptarget".equals(act[0])) {
int duration = SafeParse.stringToInt(act[2]);
double low = SafeParse.stringToDouble(act[3]);
double high = SafeParse.stringToDouble(act[4]);
boolean isMGDL = Boolean.parseBoolean(act[1]);
if(!isMGDL){
low *= Constants.MMOLL_TO_MGDL;
high *= Constants.MMOLL_TO_MGDL;
}
generateTempTarget(duration, low, high);
}
}
private static void generateTempTarget(int duration, double low, double high) {
TempTarget tempTarget = new TempTarget();
tempTarget.timeStart = new Date();
tempTarget.duration = duration;
tempTarget.reason = "WearPlugin";
if(tempTarget.duration != 0) {
tempTarget.low = low;
tempTarget.high = high;
} else {
tempTarget.low = 0;
tempTarget.high = 0;
}
tempTarget.setTimeIndex(tempTarget.getTimeIndex());
Dao<TempTarget, Long> dao = null;
try {
dao = MainApp.getDbHelper().getDaoTempTargets();
dao.createIfNotExists(tempTarget);
MainApp.bus().post(new EventTempTargetRangeChange());
//TODO: Nightscout-Treatment for Temp-Target!
//ConfigBuilderPlugin.uploadCareportalEntryToNS(data);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void doFillBolus(final Double amount) {
Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {

View file

@ -77,7 +77,7 @@ public class TempTargetActivity extends Activity {
final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_editplusminus_item, container, false);
final TextView textView = (TextView) view.findViewById(R.id.label);
textView.setText("duration");
time = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, 0d, 0d, 24*60d, 1d, new DecimalFormat("0"), false);
time = new PlusMinusEditText(view, R.id.amountfield, R.id.plusbutton, R.id.minusbutton, 60d, 0d, 24*60d, 1d, new DecimalFormat("0"), false);
container.addView(view);
return view;
@ -117,8 +117,8 @@ public class TempTargetActivity extends Activity {
// (you have to swipe past them anyways - but still)
String actionstring = "temptarget "
+ SafeParse.stringToInt(time.editText.getText().toString())
+ " " + isMGDL
+ " " + SafeParse.stringToInt(time.editText.getText().toString())
+ " " + SafeParse.stringToDouble(lowRange.editText.getText().toString())
+ " " + SafeParse.stringToDouble(highRange.editText.getText().toString())
;

View file

@ -106,14 +106,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
@ -122,6 +114,15 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
@ -134,10 +135,14 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/ustwo-clockwise-debug/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
@ -153,4 +158,4 @@
<orderEntry type="library" exported="" name="play-services-base-7.3.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
</component>
</module>
</module>