diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981032..fbb68289f4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index 2e5e87c93c..61a2d9ae1e 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -21,6 +21,7 @@ public class Config { public static final boolean logConfigBuilder = true; public static final boolean logConstraintsChanges = true; public static final boolean logTempBasalsCut = true; + public static final boolean logNSUpload = true; // Developing mode only - never turn on // TODO: remove fakeGlucoseData diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 32ffb61818..d3f23decb1 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -7,8 +7,11 @@ public class Constants { public static final String MGDL = "mg/dl"; // This is Nightscout representation public static final String MMOL = "mmol"; - public static final double MMOLL_TO_MGDL = 18.0182; + public static final double MMOLL_TO_MGDL = 18; // 18.0182; public static final double MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL; + public static final double basalAbsoluteOnlyForCheckLimit = 10101010d; + public static final Integer basalPercentOnlyForCheckLimit = 10101010; + public static final int hoursToKeepInDatabase = 24; } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 0edc28acfc..7cbebd8f0a 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps; import android.app.Application; +import android.content.res.Resources; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.squareup.otto.Bus; @@ -18,6 +19,7 @@ public class MainApp extends Application { private static Bus sBus; private static MainApp sInstance; + public static Resources resources; private static DatabaseHelper databaseHelper = null; private static ConfigBuilderFragment configBuilder = null; @@ -28,6 +30,7 @@ public class MainApp extends Application { sBus = new Bus(ThreadEnforcer.ANY); sInstance = this; + resources = getResources(); } public static Bus bus() { diff --git a/app/src/main/java/info/nightscout/androidaps/data/Result.java b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java similarity index 64% rename from app/src/main/java/info/nightscout/androidaps/data/Result.java rename to app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java index 92af8f91b1..27737007cd 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Result.java +++ b/app/src/main/java/info/nightscout/androidaps/data/PumpEnactResult.java @@ -3,7 +3,14 @@ package info.nightscout.androidaps.data; import android.os.Parcel; import android.os.Parcelable; -public class Result extends Object implements Parcelable{ +import org.json.JSONException; +import org.json.JSONObject; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.Round; + +public class PumpEnactResult extends Object implements Parcelable { public boolean success = false; // request was processed successfully (but possible no change was needed) public boolean enacted = false; // request was processed successfully and change has been made public String comment = ""; @@ -55,17 +62,17 @@ public class Result extends Object implements Parcelable{ dest.writeInt(percent); } - public final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public Result createFromParcel(Parcel in) { - return new Result(in); + public final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public PumpEnactResult createFromParcel(Parcel in) { + return new PumpEnactResult(in); } - public Result[] newArray(int size) { - return new Result[size]; + public PumpEnactResult[] newArray(int size) { + return new PumpEnactResult[size]; } }; - protected Result(Parcel in) { + protected PumpEnactResult(Parcel in) { success = in.readInt() == 1 ? true : false; enacted = in.readInt() == 1 ? true : false; isPercent = in.readInt() == 1 ? true : false; @@ -76,6 +83,27 @@ public class Result extends Object implements Parcelable{ } - public Result() {} + public PumpEnactResult() { + } + public JSONObject json() { + JSONObject result = new JSONObject(); + try { + if (isTempCancel) { + result.put("rate", 0); + result.put("duration", 0); + } else if (isPercent) { + // Nightscout is expecting absolute value + Double abs = Round.roundTo(MainApp.getConfigBuilder().getActiveProfile().getProfile().getBasal(NSProfile.secondsFromMidnight()) * percent / 100, 0.01); + result.put("rate", abs); + result.put("duration", duration); + } else { + result.put("rate", absolute); + result.put("duration", duration); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return result; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 62ec418503..e086997dc7 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -273,7 +273,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { if (sizeRecords < 4 || bgReadings.get(sizeRecords - 4).timestamp < new Date().getTime() - 7 * 60 * 1000l) { if (Config.fakeGlucoseData) { - return new GlucoseStatus(Math.random() * 400, (Math. random() - 0.5)* 18, (Math. random() - 0.5)* 18); + return new GlucoseStatus(Math.random() * 400 + 40, (Math. random() - 0.5)* 18, (Math. random() - 0.5)* 18); } return null; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index 56ce9a8a3f..6db41b5cd0 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import java.util.Date; import java.util.List; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.MainApp; @@ -119,7 +120,8 @@ public class Treatment { List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBADD No receivers"); - } else log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); } public void updateToNSClient() { @@ -146,6 +148,7 @@ public class Treatment { List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBUPDATE No receivers"); - } else log.debug("DBUPDATE dbUpdate " + q.size() + " receivers " + _id + " " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBUPDATE dbUpdate " + q.size() + " receivers " + _id + " " + data.toString()); } } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 0f2a55fb5b..845297105d 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -2,7 +2,7 @@ package info.nightscout.androidaps.interfaces; import org.json.JSONObject; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.plugins.APSResult; import info.nightscout.client.data.NSProfile; @@ -25,15 +25,17 @@ public interface PumpInterface { double getTempBasalAbsoluteRate(); double getTempBasalRemainingMinutes(); TempBasal getTempBasal(); + TempBasal getExtendedBolus(); - Result deliverTreatment(Double insulin, Integer carbs); - Result setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes); - Result setTempBasalPercent(Integer percent, Integer durationInMinutes); - Result setExtendedBolus(Double insulin, Integer durationInMinutes); - Result cancelTempBasal(); - Result cancelExtendedBolus(); - Result applyAPSRequest(APSResult request); + PumpEnactResult deliverTreatment(Double insulin, Integer carbs); + PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes); + PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes); + PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes); + PumpEnactResult cancelTempBasal(); + PumpEnactResult cancelExtendedBolus(); + PumpEnactResult applyAPSRequest(APSResult request); // Status to be passed to NS JSONObject getJSONStatus(); + String deviceID(); } diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java index 158a34f7f0..0abb176734 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TempBasalsInterface.java @@ -13,4 +13,5 @@ public interface TempBasalsInterface { IobTotal getLastCalculation(); TempBasal getTempBasal (Date time); + TempBasal getExtendedBolus (Date time); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java index c0e31174ee..475fdd9f61 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java @@ -4,6 +4,9 @@ import android.content.Context; import android.os.Parcel; import android.os.Parcelable; +import org.json.JSONException; +import org.json.JSONObject; + import java.text.DecimalFormat; import info.nightscout.androidaps.MainActivity; @@ -75,4 +78,17 @@ public class APSResult implements Parcelable { return newResult; } + public JSONObject json() { + JSONObject json = new JSONObject(); + try { + if (changeRequested) { + json.put("rate", rate); + json.put("duration", duration); + json.put("reason", reason); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return json; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java index 9a5da2af48..5cf8a7b261 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderFragment.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ResolveInfo; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -33,7 +32,7 @@ import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.Intents; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.events.EventRefreshGui; @@ -47,6 +46,9 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.plugins.APSResult; +import info.nightscout.androidaps.plugins.Loop.DeviceStatus; +import info.nightscout.androidaps.plugins.Loop.LoopFragment; +import info.nightscout.androidaps.plugins.OpenAPSMA.DetermineBasalResult; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.DateUtil; @@ -81,6 +83,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI ProfileInterface activeProfile; TreatmentsInterface activeTreatments; TempBasalsInterface activeTempBasals; + LoopFragment activeLoop; ArrayList pluginList; @@ -252,11 +255,16 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } @Override - public Result deliverTreatment(Double insulin, Integer carbs) { + public TempBasal getExtendedBolus() { + return activePump.getExtendedBolus(); + } + + @Override + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { insulin = applyBolusConstraints(insulin); carbs = applyCarbsConstraints(carbs); - Result result = activePump.deliverTreatment(insulin, carbs); + PumpEnactResult result = activePump.deliverTreatment(insulin, carbs); if (result.success) { Treatment t = new Treatment(); @@ -283,9 +291,9 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI * @return */ @Override - public Result setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { Double rateAfterConstraints = applyBasalConstraints(absoluteRate); - Result result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes); + PumpEnactResult result = activePump.setTempBasalAbsolute(rateAfterConstraints, durationInMinutes); if (result.enacted) { uploadTempBasalStartAbsolute(result.absolute, result.duration); MainApp.bus().post(new EventTempBasalChange()); @@ -301,9 +309,9 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI * @return result */ @Override - public Result setTempBasalPercent(Integer percent, Integer durationInMinutes) { + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) { Integer percentAfterConstraints = applyBasalConstraints(percent); - Result result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes); + PumpEnactResult result = activePump.setTempBasalPercent(percentAfterConstraints, durationInMinutes); if (result.enacted) { uploadTempBasalStartPercent(result.percent, result.duration); MainApp.bus().post(new EventTempBasalChange()); @@ -312,9 +320,9 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } @Override - public Result setExtendedBolus(Double insulin, Integer durationInMinutes) { - Double rateAfterConstraints = applyBasalConstraints(insulin); - Result result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes); + public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { + Double rateAfterConstraints = applyBolusConstraints(insulin); + PumpEnactResult result = activePump.setExtendedBolus(rateAfterConstraints, durationInMinutes); if (result.enacted) { uploadExtendedBolus(result.bolusDelivered, result.duration); MainApp.bus().post(new EventTreatmentChange()); @@ -323,8 +331,8 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } @Override - public Result cancelTempBasal() { - Result result = activePump.cancelTempBasal(); + public PumpEnactResult cancelTempBasal() { + PumpEnactResult result = activePump.cancelTempBasal(); if (result.enacted) { uploadTempBasalEnd(); MainApp.bus().post(new EventTempBasalChange()); @@ -333,7 +341,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } @Override - public Result cancelExtendedBolus() { + public PumpEnactResult cancelExtendedBolus() { return activePump.cancelExtendedBolus(); } @@ -344,10 +352,10 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI * @return */ @Override - public Result applyAPSRequest(APSResult request) { + public PumpEnactResult applyAPSRequest(APSResult request) { Double rateAfterConstraints = applyBasalConstraints(request.rate); request.rate = rateAfterConstraints; - Result result = null; + PumpEnactResult result = null; if (request.rate == getBaseBasalRate()) { if (isTempBasalInProgress()) { @@ -357,7 +365,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI MainApp.bus().post(new EventTempBasalChange()); } } else { - result = new Result(); + result = new PumpEnactResult(); result.absolute = request.rate; result.duration = 0; result.enacted = false; @@ -365,7 +373,7 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI result.success = true; } } else if (isTempBasalInProgress() && request.rate == getTempBasalAbsoluteRate()) { - result = new Result(); + result = new PumpEnactResult(); result.absolute = request.rate; result.duration = activePump.getTempBasal().getPlannedRemainingMinutes(); result.enacted = false; @@ -390,6 +398,11 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI return activePump.getJSONStatus(); } + @Override + public String deviceID() { + return activePump.deviceID(); + } + /* * ConfigBuilderFragment code */ @@ -488,12 +501,10 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } - @Nullable public BgSourceInterface getActiveBgSource() { return activeBgSource; } - @Nullable public PumpInterface getActivePump() { return this; } @@ -503,16 +514,18 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI return activeProfile; } - @Nullable public TreatmentsInterface getActiveTreatments() { return activeTreatments; } - @Nullable public TempBasalsInterface getActiveTempBasals() { return activeTempBasals; } + public LoopFragment getActiveLoop() { + return activeLoop; + } + void onEnabledCategoryChanged(PluginBase changedPlugin) { int category = changedPlugin.getType(); ArrayList pluginsInCategory = MainActivity.getSpecificPluginsList(category); @@ -588,11 +601,11 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI } break; case PluginBase.LOOP: - PluginBase loop = getTheOneEnabledInArray(pluginsInCategory); + activeLoop = (LoopFragment) getTheOneEnabledInArray(pluginsInCategory); if (Config.logConfigBuilder) - log.debug("Selected loop interface: " + loop.getName()); + log.debug("Selected loop interface: " + activeLoop.getName()); for (PluginBase p : pluginsInCategory) { - if (!p.getName().equals(loop.getName())) { + if (!p.getName().equals(activeLoop.getName())) { p.setFragmentVisible(false); } } @@ -800,7 +813,8 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBADD No receivers"); - } else log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); } catch (JSONException e) { } } @@ -825,7 +839,8 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBADD No receivers"); - } else log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); } catch (JSONException e) { } } @@ -848,7 +863,8 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBADD No receivers"); - } else log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); } catch (JSONException e) { } } @@ -876,9 +892,49 @@ public class ConfigBuilderFragment extends Fragment implements PluginBase, PumpI List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); if (q.size() < 1) { log.error("DBADD No receivers"); - } else log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + data.toString()); } catch (JSONException e) { } } + public void uploadDeviceStatus() { + DeviceStatus deviceStatus = new DeviceStatus(); + try { + LoopFragment.LastRun lastRun = LoopFragment.lastRun; + if (lastRun == null) return; + if (lastRun.lastAPSRun.getTime() < new Date().getTime() - 60 * 1000L) + return; // do not send if result is older than 1 min + + String openapsmaPluginName = MainApp.resources.getString(R.string.openapsma); + if (lastRun.source.equals(openapsmaPluginName)) { + DetermineBasalResult result = (DetermineBasalResult) lastRun.request; + result.json.put("timestamp", DateUtil.toISOString(lastRun.lastAPSRun)); + deviceStatus.suggested = result.json; + deviceStatus.iob = result.iob.json(); + deviceStatus.iob.put("time", DateUtil.toISOString(lastRun.lastAPSRun)); + } + + if (lastRun.setByPump != null && lastRun.setByPump.enacted) { // enacted + deviceStatus.enacted = lastRun.request.json(); + deviceStatus.enacted.put("rate", lastRun.setByPump.json().get("rate")); + deviceStatus.enacted.put("duration", lastRun.setByPump.json().get("duration")); + deviceStatus.enacted.put("received", true); + JSONObject requested = new JSONObject(); + requested.put("duration", lastRun.request.duration); + requested.put("rate", lastRun.request.rate); + requested.put("temp", "absolute"); + deviceStatus.enacted.put("requested", requested); + } + + deviceStatus.device = "openaps://" + getActivePump().deviceID(); + deviceStatus.pump = getActivePump().getJSONStatus(); + + deviceStatus.created_at = DateUtil.toISOString(new Date()); + + deviceStatus.sendToNSClient(); + } catch (JSONException e) { + e.printStackTrace(); + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java new file mode 100644 index 0000000000..1a67e16bde --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java @@ -0,0 +1,426 @@ +package info.nightscout.androidaps.plugins.Loop; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ResolveInfo; +import android.os.Bundle; + +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.Services.Intents; + +/* +{ + "_id": "576cfd15217b0bed77d63641", + "device": "openaps://indy2", + "pump": { + "battery": { + "status": "normal", + "voltage": 1.56 + }, + "status": { + "status": "normal", + "timestamp": "2016-06-24T09:26:38.000Z", + "bolusing": false, + "suspended": false + }, + "reservoir": 31.25, + "clock": "2016-06-24T02:26:16-07:00" + }, + "openaps": { + "suggested": { + "bg": 173, + "temp": "absolute", + "snoozeBG": 194, + "timestamp": "2016-06-24T09:27:40.000Z", + "predBGs": { + "IOB": [173, 178, 183, 187, 191, 194, 196, 197, 198, 197, 197, 195, 192, 190, 187, 184, 181, 178, 175, 172, 169, 167, 164, 162, 160, 158, 156, 154, 152, 151, 149, 148, 147, 146, 146, 145] + }, + "reason": "COB: 0, Dev: 46, BGI: -1.92, ISF: 80, Target: 115; Eventual BG 194>=115, adj. req. rate:2.7 to maxSafeBasal:2.3, temp 2.25 >~ req 2.3U/hr", + "COB": 0, + "eventualBG": 194, + "tick": "+6", + "IOB": 0.309 + }, + "iob": [{ + "netbasalinsulin": -0.3, + "activity": 0.0048, + "basaliob": 0.078, + "time": "2016-06-24T09:26:16.000Z", + "hightempinsulin": 0.25, + "bolussnooze": 0, + "iob": 0.309 + }, { + "netbasalinsulin": -0.15, + "activity": 0.0041, + "basaliob": 0.238, + "time": "2016-06-24T09:31:16.000Z", + "hightempinsulin": 0.4, + "bolussnooze": 0, + "iob": 0.438 + }, { + "netbasalinsulin": 0, + "activity": 0.0036, + "basaliob": 0.345, + "time": "2016-06-24T09:36:16.000Z", + "hightempinsulin": 0.5, + "bolussnooze": 0, + "iob": 0.52 + }, { + "netbasalinsulin": 0.2, + "activity": 0.0036, + "basaliob": 0.5, + "time": "2016-06-24T09:41:16.000Z", + "hightempinsulin": 0.65, + "bolussnooze": 0, + "iob": 0.653 + }, { + "netbasalinsulin": 0.35, + "activity": 0.0038, + "basaliob": 0.602, + "time": "2016-06-24T09:46:16.000Z", + "hightempinsulin": 0.75, + "bolussnooze": 0, + "iob": 0.734 + }, { + "netbasalinsulin": 0.45, + "activity": 0.0042, + "basaliob": 0.651, + "time": "2016-06-24T09:51:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.763 + }, { + "netbasalinsulin": 0.45, + "activity": 0.0045, + "basaliob": 0.647, + "time": "2016-06-24T09:56:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.74 + }, { + "netbasalinsulin": 0.5, + "activity": 0.0048, + "basaliob": 0.639, + "time": "2016-06-24T10:01:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.716 + }, { + "netbasalinsulin": 0.5, + "activity": 0.0052, + "basaliob": 0.628, + "time": "2016-06-24T10:06:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.691 + }, { + "netbasalinsulin": 0.5, + "activity": 0.0055, + "basaliob": 0.614, + "time": "2016-06-24T10:11:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.663 + }, { + "netbasalinsulin": 0.5, + "activity": 0.0059, + "basaliob": 0.596, + "time": "2016-06-24T10:16:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.633 + }, { + "netbasalinsulin": 0.55, + "activity": 0.0063, + "basaliob": 0.575, + "time": "2016-06-24T10:21:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.602 + }, { + "netbasalinsulin": 0.55, + "activity": 0.0067, + "basaliob": 0.549, + "time": "2016-06-24T10:26:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.568 + }, { + "netbasalinsulin": 0.55, + "activity": 0.0071, + "basaliob": 0.521, + "time": "2016-06-24T10:31:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.533 + }, { + "netbasalinsulin": 0.6, + "activity": 0.0074, + "basaliob": 0.489, + "time": "2016-06-24T10:36:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.496 + }, { + "netbasalinsulin": 0.6, + "activity": 0.0075, + "basaliob": 0.456, + "time": "2016-06-24T10:41:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.458 + }, { + "netbasalinsulin": 0.6, + "activity": 0.0075, + "basaliob": 0.42, + "time": "2016-06-24T10:46:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.421 + }, { + "netbasalinsulin": 0.6, + "activity": 0.0073, + "basaliob": 0.384, + "time": "2016-06-24T10:51:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.384 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0071, + "basaliob": 0.349, + "time": "2016-06-24T10:56:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.349 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0069, + "basaliob": 0.314, + "time": "2016-06-24T11:01:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.314 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0066, + "basaliob": 0.281, + "time": "2016-06-24T11:06:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.281 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0062, + "basaliob": 0.25, + "time": "2016-06-24T11:11:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.25 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0059, + "basaliob": 0.221, + "time": "2016-06-24T11:16:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.221 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0055, + "basaliob": 0.193, + "time": "2016-06-24T11:21:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.193 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0052, + "basaliob": 0.167, + "time": "2016-06-24T11:26:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.167 + }, { + "netbasalinsulin": 0.7, + "activity": 0.0049, + "basaliob": 0.143, + "time": "2016-06-24T11:31:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.143 + }, { + "netbasalinsulin": 0.7, + "activity": 0.0045, + "basaliob": 0.12, + "time": "2016-06-24T11:36:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.12 + }, { + "netbasalinsulin": 0.7, + "activity": 0.0041, + "basaliob": 0.1, + "time": "2016-06-24T11:41:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.1 + }, { + "netbasalinsulin": 0.7, + "activity": 0.0037, + "basaliob": 0.081, + "time": "2016-06-24T11:46:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.081 + }, { + "netbasalinsulin": 0.75, + "activity": 0.0034, + "basaliob": 0.064, + "time": "2016-06-24T11:51:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.064 + }, { + "netbasalinsulin": 0.75, + "activity": 0.003, + "basaliob": 0.049, + "time": "2016-06-24T11:56:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.049 + }, { + "netbasalinsulin": 0.8, + "activity": 0.0026, + "basaliob": 0.036, + "time": "2016-06-24T12:01:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.036 + }, { + "netbasalinsulin": 0.8, + "activity": 0.0021, + "basaliob": 0.026, + "time": "2016-06-24T12:06:16.000Z", + "hightempinsulin": 0.8, + "bolussnooze": 0, + "iob": 0.026 + }, { + "netbasalinsulin": 0.75, + "activity": 0.0017, + "basaliob": 0.017, + "time": "2016-06-24T12:11:16.000Z", + "hightempinsulin": 0.75, + "bolussnooze": 0, + "iob": 0.017 + }, { + "netbasalinsulin": 0.75, + "activity": 0.0013, + "basaliob": 0.011, + "time": "2016-06-24T12:16:16.000Z", + "hightempinsulin": 0.75, + "bolussnooze": 0, + "iob": 0.011 + }, { + "netbasalinsulin": 0.65, + "activity": 0.0009, + "basaliob": 0.006, + "time": "2016-06-24T12:21:16.000Z", + "hightempinsulin": 0.65, + "bolussnooze": 0, + "iob": 0.006 + }], + "enacted": { + "bg": 161, + "temp": "absolute", + "snoozeBG": 181, + "recieved": true, + "predBGs": { + "IOB": [161, 164, 166, 168, 170, 172, 174, 175, 176, 177, 177, 176, 175, 175, 174, 173, 173, 172, 172, 171, 171, 171, 171, 170, 170, 170, 170, 170, 169, 169, 169, 169, 169, 168] + }, + "reason": "COB: undefined, Dev: 33, BGI: -2.56, ISF: 80, Target: 115; Eventual BG 181>=115, adj. req. rate:2.4 to maxSafeBasal:2.3, temp 1<2.3U/hr", + "rate": 2.25, + "eventualBG": 181, + "timestamp": "2016-06-24T09:19:06.000Z", + "duration": 30, + "tick": "+5", + "IOB": 0.166 + } + }, + "mmtune": { + "scanDetails": [ + ["916.564", 5, -78], + ["916.588", 3, -80], + ["916.612", 4, -68], + ["916.636", 5, -65], + ["916.660", 5, -60], + ["916.684", 5, -67], + ["916.708", 5, -71] + ], + "setFreq": 916.66, + "timestamp": "2016-06-24T09:26:22.000Z", + "usedDefault": false + }, + "created_at": "2016-06-24T09:27:49.230Z" + } +*/ + +public class DeviceStatus { + private static Logger log = LoggerFactory.getLogger(DeviceStatus.class); + public static DeviceStatus deviceStatus; + + public static String device = null; + public static JSONObject pump = null; + public static JSONObject enacted = null; + public static JSONObject suggested = null; + public static JSONObject iob = null; + public static String created_at = null; + + public static JSONObject lowsuspend = null; + + public static JSONObject mongoRecord () { + JSONObject record = new JSONObject(); + + try { + if (device != null) record.put("device" , device); + if (pump != null) record.put("pump" , pump); + if (suggested != null) { + JSONObject openaps = new JSONObject(); + if (enacted != null) openaps.put("enacted", enacted); + if (suggested != null) openaps.put("suggested", suggested); + if (iob != null) openaps.put("iob", iob); + record.put("openaps", openaps); + } + if (lowsuspend != null) record.put("lowsuspend" , lowsuspend); + if (created_at != null) record.put("created_at" , created_at); + } catch (JSONException e) { + e.printStackTrace(); + } + return record; + } + + public void sendToNSClient() { + Context context = MainApp.instance().getApplicationContext(); + Bundle bundle = new Bundle(); + bundle.putString("action", "dbAdd"); + bundle.putString("collection", "devicestatus"); + bundle.putString("data", mongoRecord().toString()); + Intent intent = new Intent(Intents.ACTION_DATABASE); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + List q = context.getPackageManager().queryBroadcastReceivers(intent, 0); + if (q.size() < 1) { + log.error("DBADD No receivers"); + } else if (Config.logNSUpload) + log.debug("DBADD dbAdd " + q.size() + " receivers " + mongoRecord().toString()); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java index 5e0f80f613..ccd96e0398 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopFragment.java @@ -23,7 +23,7 @@ import java.util.Date; import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.APSInterface; @@ -45,10 +45,10 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug boolean confirmed; - class LastRun implements Parcelable { + public class LastRun implements Parcelable { public APSResult request = null; public APSResult constraintsProcessed = null; - public Result setByPump = null; + public PumpEnactResult setByPump = null; public String source = null; public Date lastAPSRun = null; public Date lastEnact = null; @@ -81,7 +81,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug private LastRun(Parcel in) { request = in.readParcelable(APSResult.class.getClassLoader()); constraintsProcessed = in.readParcelable(APSResult.class.getClassLoader()); - setByPump = in.readParcelable(Result.class.getClassLoader()); + setByPump = in.readParcelable(PumpEnactResult.class.getClassLoader()); source = in.readString(); lastAPSRun = new Date(in.readLong()); lastEnact = new Date(in.readLong()); @@ -91,7 +91,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug } } - static LastRun lastRun = null; + static public LastRun lastRun = null; private boolean fragmentEnabled = false; private boolean fragmentVisible = true; @@ -160,9 +160,9 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug runNowButton = (Button) view.findViewById(R.id.loop_run); runNowButton.setOnClickListener(this); - if (savedInstanceState != null) { - lastRun = savedInstanceState.getParcelable("lastrun"); - } + //if (savedInstanceState != null) { + // lastRun = savedInstanceState.getParcelable("lastrun"); + //} updateGUI(); return view; } @@ -260,7 +260,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug if (result.changeRequested) { constraintsInterface.applyBasalConstraints(resultAfterConstraints); - Result applyResult = pumpInterface.applyAPSRequest(resultAfterConstraints); + PumpEnactResult applyResult = pumpInterface.applyAPSRequest(resultAfterConstraints); Date lastEnact = lastRun != null ? lastRun.lastEnact : new Date(0, 0, 0); lastRun = new LastRun(); lastRun.request = result; @@ -281,6 +281,7 @@ public class LoopFragment extends Fragment implements View.OnClickListener, Plug lastRun.lastAPSRun = new Date(); } updateGUI(); + MainApp.getConfigBuilder().uploadDeviceStatus(); } void updateGUI() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java index c25200ceb5..bfb674c585 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalAdapterJS.java @@ -41,7 +41,7 @@ public class DetermineBasalAdapterJS implements Parcelable { private final String PARAM_meal_data = "meal_data"; private String storedCurrentTemp = null; - private String storedIobData = null; + public String storedIobData = null; private String storedGlucoseStatus = null; private String storedProfile = null; private String storedMeal_data = null; @@ -124,7 +124,6 @@ public class DetermineBasalAdapterJS implements Parcelable { mIobData = new V8Object(mV8rt); mIobData.add("iob", 0); //netIob mIobData.add("activity", 0); //netActivity - mIobData.add("bolusiob", 0); // backward compatibility with master mIobData.add("bolussnooze", 0); //bolusIob mIobData.add("basaliob", 0); mIobData.add("netbasalinsulin", 0); @@ -287,7 +286,6 @@ public class DetermineBasalAdapterJS implements Parcelable { mIobData.add("iob", iobData.iob); //netIob mIobData.add("activity", iobData.activity); //netActivity - mIobData.add("bolusiob", iobData.bolussnooze); // backward compatibility with master mIobData.add("bolussnooze", iobData.bolussnooze); //bolusIob mIobData.add("basaliob", iobData.basaliob); mIobData.add("netbasalinsulin", iobData.netbasalinsulin); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java index cd955d32ca..0d984f8f9f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/DetermineBasalResult.java @@ -16,6 +16,7 @@ public class DetermineBasalResult extends APSResult { public double eventualBG; public double snoozeBG; public String mealAssist; + public IobTotal iob; public DetermineBasalResult(V8Object result, JSONObject j) { json = j; @@ -75,11 +76,16 @@ public class DetermineBasalResult extends APSResult { mealAssist = in.readString(); } - public DetermineBasalResult() {} + public DetermineBasalResult() { + } @Override public DetermineBasalResult clone() { DetermineBasalResult newResult = new DetermineBasalResult(); + newResult.reason = new String(reason); + newResult.rate = rate; + newResult.duration = duration; + newResult.changeRequested = changeRequested; newResult.rate = rate; newResult.duration = duration; newResult.changeRequested = changeRequested; @@ -94,4 +100,16 @@ public class DetermineBasalResult extends APSResult { newResult.mealAssist = new String(mealAssist); return newResult; } + + @Override + public JSONObject json() { + try { + JSONObject ret = new JSONObject(this.json.toString()); + return ret; + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/IobTotal.java index 77e3786313..7cb640e377 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/IobTotal.java @@ -65,21 +65,11 @@ public class IobTotal { JSONObject json = new JSONObject(); try { json.put("iob", iob); - json.put("activity", activity); - json.put("bolusIob", bolussnooze); - } catch (JSONException e) { - e.printStackTrace(); - } - return json; - } - - public JSONObject nsJson() { - JSONObject json = new JSONObject(); - try { - json.put("iob", bolussnooze); + json.put("bolussnooze", bolussnooze); json.put("basaliob", iob); json.put("activity", activity); - json.put("timestamp", DateUtil.toISOString(new Date())); + json.put("hightempinsulin", hightempinsulin); + json.put("netbasalinsulin", netbasalinsulin); } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java index 5c42b18f75..5020fab6d1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAFragment.java @@ -277,6 +277,7 @@ public class OpenAPSMAFragment extends Fragment implements View.OnClickListener, DetermineBasalResult determineBasalResult = determineBasalAdapterJS.invoke(); + determineBasalResult.iob = iobTotal; determineBasalAdapterJS.release(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java index 3a32ac68fb..fd92050f9f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewExtendedBolusDialog.java @@ -11,10 +11,9 @@ import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.utils.SafeParse; @@ -74,7 +73,7 @@ public class NewExtendedBolusDialog extends DialogFragment implements View.OnCli builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); - Result result = pump.setExtendedBolus(finalInsulin, finalDurationInMinutes); + PumpEnactResult result = pump.setExtendedBolus(finalInsulin, finalDurationInMinutes); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java index 3cc5473495..ccebf17914 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTempBasalDialog.java @@ -11,10 +11,9 @@ import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.utils.SafeParse; @@ -88,7 +87,7 @@ public class NewTempBasalDialog extends DialogFragment implements View.OnClickLi builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); - Result result; + PumpEnactResult result; if (setAsPercent) { result = pump.setTempBasalPercent(finalBasalPercent, finalDurationInMinutes); } else { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java index d0bf1fb57d..89e76822f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewTreatmentDialog.java @@ -1,21 +1,17 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; import android.content.DialogInterface; -import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.view.*; import android.view.View.OnClickListener; -import android.view.animation.Interpolator; import android.widget.Button; import android.widget.TextView; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.utils.SafeParse; @@ -70,7 +66,7 @@ public class NewTreatmentDialog extends DialogFragment implements OnClickListene public void onClick(DialogInterface dialog, int id) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); - Result result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); + PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index b2010427d4..44b519347b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -1,9 +1,7 @@ package info.nightscout.androidaps.plugins.Overview.Dialogs; import android.content.DialogInterface; -import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.text.Editable; @@ -17,10 +15,9 @@ import android.widget.TextView; import java.text.DecimalFormat; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.data.Result; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.TempBasalsInterface; @@ -140,7 +137,7 @@ public class WizardDialog extends DialogFragment implements OnClickListener { public void onClick(DialogInterface dialog, int id) { if (finalInsulinAfterConstraints > 0 || finalCarbsAfterConstraints > 0) { PumpInterface pump = MainApp.getConfigBuilder().getActivePump(); - Result result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); + PumpEnactResult result = pump.deliverTreatment(finalInsulinAfterConstraints, finalCarbsAfterConstraints); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle(getContext().getString(R.string.treatmentdeliveryerror)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 2b4681dcdf..3e83542e95 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -50,6 +50,7 @@ import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTempBasalDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.NewTreatmentDialog; import info.nightscout.androidaps.plugins.Overview.Dialogs.WizardDialog; import info.nightscout.client.data.NSProfile; +import info.nightscout.utils.Round; public class OverviewFragment extends Fragment implements PluginBase { @@ -326,17 +327,17 @@ public class OverviewFragment extends Fragment implements PluginBase { // allign to hours Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(new Date().getTime()); + calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.add(Calendar.HOUR, 1); int hoursToFetch = 6; - long toTime = calendar.getTimeInMillis(); + long toTime = calendar.getTimeInMillis() + 100000; // little bit more to avoid wrong rounding long fromTime = toTime - hoursToFetch * 60 * 60 * 1000l; Double lowLine = NSProfile.toUnits(80d, 4d, units); // TODO: make this customisable Double highLine = NSProfile.toUnits(180d, 10d, units); - Double maxY = NSProfile.toUnits(400d, 20d, units); // TODO: add some scale support BarGraphSeries basalsSeries = null; LineGraphSeries seriesLow = null; @@ -345,6 +346,7 @@ public class OverviewFragment extends Fragment implements PluginBase { PointsGraphSeries seriesInRage = null; PointsGraphSeries seriesOutOfRange = null; + // remove old data from graph bgGraph.removeAllSeries(); // **** TEMP BASALS graph **** @@ -356,11 +358,11 @@ public class OverviewFragment extends Fragment implements PluginBase { public boolean isTempBasal = false; } - Double maxAllowedBasal = MainApp.getConfigBuilder().applyBasalConstraints(1000d); + Double maxAllowedBasal = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); long now = new Date().getTime(); List basalArray = new ArrayList(); - for (long time = fromTime; time < now; time += 5 * 60 * 1000l) { + for (long time = fromTime; time < now; time += 5 * 60 * 1000L) { TempBasal tb = MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date(time)); if (tb != null) basalArray.add(new BarDataPoint(time, tb.tempBasalConvertedToAbsolute(), true)); @@ -368,7 +370,6 @@ public class OverviewFragment extends Fragment implements PluginBase { basalArray.add(new BarDataPoint(time, profile.getBasal(NSProfile.secondsFromMidnight(new Date(time))), false)); } BarDataPoint[] basal = new BarDataPoint[basalArray.size()]; - log.debug("Bars: " + basalArray.size()); basal = basalArray.toArray(basal); bgGraph.addSeries(basalsSeries = new BarGraphSeries(basal)); basalsSeries.setValueDependentColor(new ValueDependentColor() { @@ -384,7 +385,7 @@ public class OverviewFragment extends Fragment implements PluginBase { bgGraph.getSecondScale().addSeries(basalsSeries); bgGraph.getSecondScale().setMinY(0); bgGraph.getSecondScale().setMaxY(maxAllowedBasal * 4); - bgGraph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(MainApp.instance().getResources().getColor(R.color.background_material_dark)); + bgGraph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(MainApp.instance().getResources().getColor(R.color.background_material_dark)); // same color as backround = hide // **** BG graph **** @@ -396,13 +397,19 @@ public class OverviewFragment extends Fragment implements PluginBase { return; Iterator it = bgReadingsArray.iterator(); + Double maxBgValue = 0d; while (it.hasNext()) { BgReading bg = it.next(); + if (bg.value > maxBgValue) maxBgValue = bg.value; if (bg.valueToUnits(units) < lowLine || bg.valueToUnits(units) > highLine) outOfRangeArray.add(bg); else inRangeArray.add(bg); } + maxBgValue = NSProfile.fromMgdlToUnits(maxBgValue, units); + maxBgValue = units.equals(Constants.MGDL) ? Round.roundTo(maxBgValue, 40d) + 80 : Round.roundTo(maxBgValue, 2d) + 4; + Integer numOfHorizLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); + BgReading[] inRange = new BgReading[inRangeArray.size()]; BgReading[] outOfRange = new BgReading[outOfRangeArray.size()]; inRange = inRangeArray.toArray(inRange); @@ -441,7 +448,7 @@ public class OverviewFragment extends Fragment implements PluginBase { // **** NOW line **** DataPoint[] nowPoints = new DataPoint[]{ new DataPoint(now, 0), - new DataPoint(now, maxY) + new DataPoint(now, maxBgValue) }; bgGraph.addSeries(seriesNow = new LineGraphSeries(nowPoints)); seriesNow.setColor(Color.GREEN); @@ -464,10 +471,10 @@ public class OverviewFragment extends Fragment implements PluginBase { bgGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space // set manual y bounds to have nice steps - bgGraph.getViewport().setMaxY(maxY); + bgGraph.getViewport().setMaxY(maxBgValue); bgGraph.getViewport().setMinY(0); bgGraph.getViewport().setYAxisBoundsManual(true); - bgGraph.getGridLabelRenderer().setNumVerticalLabels(11); + bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfHorizLines); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java index eab25c57d4..06f4342ee2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SafetyFragment/SafetyFragment.java @@ -9,7 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; -import info.nightscout.androidaps.MainActivity; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.interfaces.ConstraintsInterface; @@ -83,6 +83,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI @Override public Double applyBasalConstraints(Double absoluteRate) { + Double origAbsoluteRate = absoluteRate; SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); @@ -95,17 +96,17 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI Double origRate = absoluteRate; if (absoluteRate > maxBasal) { absoluteRate = maxBasal; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h"); } if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) { absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h"); } if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) { absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origAbsoluteRate != Constants.basalAbsoluteOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h"); } return absoluteRate; @@ -113,6 +114,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI @Override public Integer applyBasalConstraints(Integer percentRate) { + Integer origPercentRate = percentRate; SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); Double maxBasal = SafeParse.stringToDouble(SP.getString("openapsma_max_basal", "1")); @@ -132,17 +134,17 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI Double origRate = absoluteRate; if (absoluteRate > maxBasal) { absoluteRate = maxBasal; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by maxBasal preference to " + absoluteRate + "U/h"); } if (absoluteRate > maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight())) { absoluteRate = Math.floor(maxBasalMult * profile.getBasal(NSProfile.secondsFromMidnight()) * 100) / 100; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by maxBasalMult to " + absoluteRate + "U/h"); } if (absoluteRate > profile.getMaxDailyBasal() * maxBasalFromDaily) { absoluteRate = profile.getMaxDailyBasal() * maxBasalFromDaily; - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit) log.debug("Limiting rate " + origRate + " by 3 * maxDailyBasal to " + absoluteRate + "U/h"); } @@ -150,7 +152,7 @@ public class SafetyFragment extends Fragment implements PluginBase, ConstraintsI if (percentRateAfterConst < 100) Round.ceilTo(absoluteRate, 10d).intValue(); else Round.floorTo(absoluteRate, 10d).intValue(); - if (Config.logConstraintsChanges) + if (Config.logConstraintsChanges && origPercentRate != Constants.basalPercentOnlyForCheckLimit) log.debug("Recalculated percent rate " + percentRate + "% to " + percentRateAfterConst + "%"); return percentRateAfterConst; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java index aa1783ccdf..d7f5674e7d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java @@ -32,7 +32,6 @@ import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.TempBasal; -import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.TempBasalsInterface; import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal; @@ -55,6 +54,7 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa private static DecimalFormat formatNumber3decimalplaces = new DecimalFormat("0.000"); private List tempBasals; + private List extendedBoluses; boolean fragmentEnabled = true; boolean fragmentVisible = true; @@ -114,49 +114,75 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa queryBuilder.orderBy("timeIndex", false); Where where = queryBuilder.where(); where.eq("isExtended", false); - queryBuilder.limit(30l); + queryBuilder.limit(30L); PreparedQuery 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()); - if (Config.logTempBasalsCut) - log.debug("Add timeEnd to old record"); - 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())); - if (Config.logTempBasalsCut) - log.debug("Add timeEnd to old record"); - update = true; - } else if (t.timeEnd.getTime() > startofnewer.getTime()) { - t.timeEnd = startofnewer; - update = true; - } - } - if (update) { - dao.update(t); - if (Config.logTempBasalsCut) { - log.debug("Fixing unfinished temp end: " + t.log()); - if (position > 0) - log.debug("Previous: " + tempBasals.get(position - 1).log()); - } - } + QueryBuilder queryBuilderExt = dao.queryBuilder(); + queryBuilderExt.orderBy("timeIndex", false); + Where whereExt = queryBuilderExt.where(); + whereExt.eq("isExtended", true); + queryBuilderExt.limit(5L); + PreparedQuery preparedQueryExt = queryBuilderExt.prepare(); + extendedBoluses = dao.query(preparedQueryExt); + + // Update ended + checkForExpiredExtended(); + checkForExpiredTemps(); - } } catch (SQLException e) { log.debug(e.getMessage(), e); tempBasals = new ArrayList(); } } + public void checkForExpiredTemps() { + checkForExpired(tempBasals); + } + + public void checkForExpiredExtended() { + checkForExpired(extendedBoluses); + } + + private void checkForExpired(List list) { + long now = new Date().getTime(); + for (int position = list.size() - 1; position >= 0; position--) { + TempBasal t = list.get(position); + boolean update = false; + if (t.timeEnd == null && t.getPlannedTimeEnd().getTime() < now) { + t.timeEnd = new Date(t.getPlannedTimeEnd().getTime()); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); + update = true; + } + if (position > 0) { + Date startofnewer = list.get(position - 1).timeStart; + if (t.timeEnd == null) { + t.timeEnd = new Date(Math.min(startofnewer.getTime(), t.getPlannedTimeEnd().getTime())); + if (Config.logTempBasalsCut) + log.debug("Add timeEnd to old record"); + update = true; + } else if (t.timeEnd.getTime() > startofnewer.getTime()) { + t.timeEnd = startofnewer; + update = true; + } + } + if (update) { + try { + Dao dao = MainApp.getDbHelper().getDaoTempBasals(); + dao.update(t); + } catch (SQLException e) { + e.printStackTrace(); + } + if (Config.logTempBasalsCut) { + log.debug("Fixing unfinished temp end: " + t.log()); + if (position > 0) + log.debug("Previous: " + list.get(position - 1).log()); + } + } + } + } + /* * Recalculate IOB if value is older than 1 minute */ @@ -173,6 +199,8 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa @Override public void updateTotalIOB() { + checkForExpired(tempBasals); + checkForExpired(extendedBoluses); Date now = new Date(); IobTotal total = new IobTotal(); for (Integer pos = 0; pos < tempBasals.size(); pos++) { @@ -198,7 +226,17 @@ public class TempBasalsFragment extends Fragment implements PluginBase, TempBasa @Nullable @Override public TempBasal getTempBasal(Date time) { - for (TempBasal t: tempBasals) { + checkForExpired(tempBasals); + for (TempBasal t : tempBasals) { + if (t.isInProgress(time)) return t; + } + return null; + } + + @Override + public TempBasal getExtendedBolus(Date time) { + checkForExpired(extendedBoluses); + for (TempBasal t : extendedBoluses) { if (t.isInProgress(time)) return t; } return null; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java index bba47b6ea4..00da505d47 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpFragment.java @@ -18,15 +18,13 @@ 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; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.events.EventTreatmentChange; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.data.Result; import info.nightscout.androidaps.db.TempBasal; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.plugins.APSResult; @@ -38,8 +36,6 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt Double defaultBasalValue = 0.2d; - //TempBasal tempBasal = null; - TempBasal extendedBolus = null; Integer batteryPercent = 50; Integer resevoirInUnits = 50; @@ -124,54 +120,14 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt return view; } - void checkForExpiredTempsAndExtended() { - long now = new Date().getTime(); - if (isTempBasalInProgress()) { - //long plannedTimeEnd = tempBasal.getPlannedTimeEnd().getTime(); - long plannedTimeEnd = getTempBasal().getPlannedTimeEnd().getTime(); - if (plannedTimeEnd < now) { - //tempBasal.timeEnd = new Date(plannedTimeEnd); - getTempBasal().timeEnd = new Date(plannedTimeEnd); - try { - //MainApp.instance().getDbHelper().getDaoTempBasals().update(tempBasal); - MainApp.instance().getDbHelper().getDaoTempBasals().update(getTempBasal()); - } catch (SQLException e) { - e.printStackTrace(); - log.error(e.getMessage()); - } - if (Config.logPumpComm) - //log.debug("Canceling expired temp: " + tempBasal); - log.debug("Canceling expired temp: " + getTempBasal()); - //tempBasal = null; - MainApp.bus().post(new EventTreatmentChange()); - } - } - if (isExtendedBoluslInProgress()) { - long plannedTimeEnd = extendedBolus.getPlannedTimeEnd().getTime(); - if (plannedTimeEnd < now) { - extendedBolus.timeEnd = new Date(plannedTimeEnd); - try { - MainApp.instance().getDbHelper().getDaoTempBasals().update(extendedBolus); - } catch (SQLException e) { - e.printStackTrace(); - log.error(e.getMessage()); - } - if (Config.logPumpComm) - log.debug("Canceling expired extended bolus: " + extendedBolus); - extendedBolus = null; - } - } - } - @Override public boolean isTempBasalInProgress() { - //return tempBasal != null; return getTempBasal() != null; } @Override public boolean isExtendedBoluslInProgress() { - return extendedBolus != null; + return getExtendedBolus() != null; } @Override @@ -201,16 +157,13 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt public double getTempBasalAbsoluteRate() { if (!isTempBasalInProgress()) return 0; - //if (tempBasal.isAbsolute) { if (getTempBasal().isAbsolute) { - //return tempBasal.absolute; return getTempBasal().absolute; } else { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); if (profile == null) return defaultBasalValue; Double baseRate = profile.getBasal(profile.secondsFromMidnight()); - //Double tempRate = baseRate * (tempBasal.percent / 100d); Double tempRate = baseRate * (getTempBasal().percent / 100d); return baseRate + tempRate; } @@ -218,21 +171,24 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt @Override public TempBasal getTempBasal() { - //return tempBasal; return MainApp.getConfigBuilder().getActiveTempBasals().getTempBasal(new Date()); } + @Override + public TempBasal getExtendedBolus() { + return MainApp.getConfigBuilder().getActiveTempBasals().getExtendedBolus(new Date()); + } + @Override public double getTempBasalRemainingMinutes() { if (!isTempBasalInProgress()) return 0; - //return tempBasal.getPlannedRemainingMinutes(); return getTempBasal().getPlannedRemainingMinutes(); } @Override - public Result deliverTreatment(Double insulin, Integer carbs) { - Result result = new Result(); + public PumpEnactResult deliverTreatment(Double insulin, Integer carbs) { + PumpEnactResult result = new PumpEnactResult(); result.success = true; result.bolusDelivered = insulin; result.carbsDelivered = carbs; @@ -245,12 +201,10 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { - checkForExpiredTempsAndExtended(); - Result result = cancelTempBasal(); + public PumpEnactResult setTempBasalAbsolute(Double absoluteRate, Integer durationInMinutes) { + PumpEnactResult result = cancelTempBasal(); if (!result.success) return result; - //tempBasal = new TempBasal(); TempBasal tempBasal = new TempBasal(); tempBasal.timeStart = new Date(); tempBasal.isAbsolute = true; @@ -258,6 +212,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt tempBasal.duration = durationInMinutes; result.success = true; result.enacted = true; + result.isTempCancel = false; result.absolute = absoluteRate; result.duration = durationInMinutes; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); @@ -275,15 +230,13 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result setTempBasalPercent(Integer percent, Integer durationInMinutes) { - checkForExpiredTempsAndExtended(); - Result result = new Result(); + public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes) { + PumpEnactResult result = new PumpEnactResult(); if (isTempBasalInProgress()) { result = cancelTempBasal(); if (!result.success) return result; } - //tempBasal = new TempBasal(); TempBasal tempBasal = new TempBasal(); tempBasal.timeStart = new Date(); tempBasal.isAbsolute = false; @@ -293,6 +246,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt result.enacted = true; result.percent = percent; result.isPercent = true; + result.isTempCancel = false; result.duration = durationInMinutes; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); try { @@ -309,12 +263,11 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result setExtendedBolus(Double insulin, Integer durationInMinutes) { - checkForExpiredTempsAndExtended(); - Result result = cancelExtendedBolus(); + public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) { + PumpEnactResult result = cancelExtendedBolus(); if (!result.success) return result; - extendedBolus = new TempBasal(); + TempBasal extendedBolus = new TempBasal(); extendedBolus.timeStart = new Date(); extendedBolus.isExtended = true; extendedBolus.absolute = insulin * 60d / durationInMinutes; @@ -323,6 +276,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt result.success = true; result.enacted = true; result.bolusDelivered = insulin; + result.isTempCancel = false; result.duration = durationInMinutes; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); try { @@ -340,18 +294,17 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result cancelTempBasal() { - checkForExpiredTempsAndExtended(); - Result result = new Result(); + public PumpEnactResult cancelTempBasal() { + PumpEnactResult result = new PumpEnactResult(); result.success = true; result.isTempCancel = true; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); if (isTempBasalInProgress()) { result.enacted = true; - //tempBasal.timeEnd = new Date(); - getTempBasal().timeEnd = new Date(); + TempBasal tb = getTempBasal(); + tb.timeEnd = new Date(); try { - MainApp.instance().getDbHelper().getDaoTempBasals().update(getTempBasal()); + MainApp.instance().getDbHelper().getDaoTempBasals().update(tb); //tempBasal = null; if (Config.logPumpComm) log.debug("Canceling temp basal: " + result); @@ -367,10 +320,10 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result cancelExtendedBolus() { - checkForExpiredTempsAndExtended(); - Result result = new Result(); + public PumpEnactResult cancelExtendedBolus() { + PumpEnactResult result = new PumpEnactResult(); if (isExtendedBoluslInProgress()) { + TempBasal extendedBolus = getExtendedBolus(); extendedBolus.timeEnd = new Date(); try { MainApp.instance().getDbHelper().getDaoTempBasals().update(extendedBolus); @@ -382,8 +335,8 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } result.success = true; result.enacted = true; + result.isTempCancel = true; result.comment = MainApp.instance().getString(R.string.virtualpump_resultok); - extendedBolus = null; if (Config.logPumpComm) log.debug("Canceling extended basal: " + result); updateGUI(); @@ -391,7 +344,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } @Override - public Result applyAPSRequest(APSResult request) { + public PumpEnactResult applyAPSRequest(APSResult request) { // This should be implemented only on ConfigBuilder return null; } @@ -405,9 +358,6 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt battery.put("percent", batteryPercent); status.put("status", "normal"); if (isTempBasalInProgress()) { - //status.put("tempbasalpct", tempBasal.percent); - //status.put("tempbasalstart", DateUtil.toISOString(tempBasal.timeStart)); - //status.put("tempbasalremainmin", tempBasal.getPlannedRemainingMinutes()); status.put("tempbasalpct", getTempBasal().percent); status.put("tempbasalstart", DateUtil.toISOString(getTempBasal().timeStart)); status.put("tempbasalremainmin", getTempBasal().getPlannedRemainingMinutes()); @@ -423,6 +373,11 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt return pump; } + @Override + public String deviceID() { + return "VirtualPump"; + } + @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); @@ -436,7 +391,6 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt } public void updateGUI() { - checkForExpiredTempsAndExtended(); Activity activity = getActivity(); if (activity != null && visibleNow && basaBasalRateView != null) activity.runOnUiThread(new Runnable() { @@ -451,7 +405,7 @@ public class VirtualPumpFragment extends Fragment implements PluginBase, PumpInt tempBasalView.setText(""); } if (isExtendedBoluslInProgress()) { - extendedBolusView.setText(extendedBolus.toString()); + extendedBolusView.setText(getExtendedBolus().toString()); } else { extendedBolusView.setText(""); } diff --git a/app/src/main/res/layout/loop_fragment.xml b/app/src/main/res/layout/loop_fragment.xml index 83597b350d..6d2215cbe8 100644 --- a/app/src/main/res/layout/loop_fragment.xml +++ b/app/src/main/res/layout/loop_fragment.xml @@ -32,7 +32,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/openapsma_lastrun_label" - android:textAppearance="?android:attr/textAppearanceLarge" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceMedium" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:textAppearance="?android:attr/textAppearanceLarge" + android:layout_gravity="center_horizontal" /> + android:layout_height="160dip" />