diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodFragment.kt b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodFragment.kt index 1930c459e4..0546589834 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodFragment.kt +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodFragment.kt @@ -281,7 +281,7 @@ class OmnipodFragment : DaggerFragment() { .getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "-" // total delivered - omnipod_total_delivered.text = if (podStateManager.isPodActivationCompleted && podStateManager.totalInsulinDelivered != null) { + omnipod_total_delivered.text = if (podStateManager.isPodActivationCompleted && podStateManager.totalInsulinDelivered != null) { // Null check for backwards compatibility resourceHelper.gs(R.string.omnipod_total_delivered, podStateManager.totalInsulinDelivered - OmnipodConst.POD_SETUP_UNITS); } else { "-" diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java index 1b4aa096d1..1a98a8e6f5 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/OmnipodPumpPlugin.java @@ -58,6 +58,7 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks.ServiceTaskExecutor; import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoRecentPulseLog; +import info.nightscout.androidaps.plugins.pump.omnipod.data.ActiveBolus; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCommandType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCustomActionType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodPumpPluginInterface; @@ -212,16 +213,16 @@ public class OmnipodPumpPlugin extends PumpPluginAbstract implements OmnipodPump // If so, add it to history // Needs to be done after EventAppInitialized because otherwise, TreatmentsPlugin.onStart() hasn't been called yet // so it didn't initialize a TreatmentService yet, resulting in a NullPointerException - if (sp.contains(OmnipodConst.Prefs.CurrentBolus)) { - String currentBolusString = sp.getString(OmnipodConst.Prefs.CurrentBolus, ""); - aapsLogger.warn(LTag.PUMP, "Found active bolus in SP. Adding Treatment: {}", currentBolusString); + if (sp.contains(OmnipodConst.Prefs.ActiveBolus)) { + String activeBolusString = sp.getString(OmnipodConst.Prefs.ActiveBolus, ""); + aapsLogger.warn(LTag.PUMP, "Found active bolus in SP: {}. Adding Treatment.", activeBolusString); try { - DetailedBolusInfo detailedBolusInfo = omnipodUtil.getGsonInstance().fromJson(currentBolusString, DetailedBolusInfo.class); - aapsOmnipodManager.addBolusToHistory(detailedBolusInfo); + ActiveBolus activeBolus = omnipodUtil.getGsonInstance().fromJson(activeBolusString, ActiveBolus.class); + aapsOmnipodManager.addBolusToHistory(activeBolus.toDetailedBolusInfo(aapsLogger)); } catch (Exception ex) { aapsLogger.error(LTag.PUMP, "Failed to add active bolus to history", ex); } - sp.remove(OmnipodConst.Prefs.CurrentBolus); + sp.remove(OmnipodConst.Prefs.ActiveBolus); } }, fabricPrivacy::logException) ); diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/data/ActiveBolus.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/data/ActiveBolus.java new file mode 100644 index 0000000000..481c78157b --- /dev/null +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/data/ActiveBolus.java @@ -0,0 +1,79 @@ +package info.nightscout.androidaps.plugins.pump.omnipod.data; + +import org.apache.commons.lang3.StringUtils; +import org.json.JSONException; +import org.json.JSONObject; + +import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.logging.AAPSLogger; +import info.nightscout.androidaps.logging.LTag; + +// Used for storing active bolus during bolus, +// so we can recover it and add it to treatments after the app crashed or got killed +// Storing DetailedBolusInfo itself is no good because it contains a reference to Context +// and to JSONObject, which are both not serializable +// TODO add tests +public class ActiveBolus { + private long date; + private long lastKnownBolusTime; + private String eventType; + private double insulin; + private double carbs; + private int source; + private boolean isValid; + private double glucose; + private String glucoseType; + private int carbTime; + private String boluscalc; + private long pumpId; + private boolean isSMB; + private long deliverAt; + private String notes; + + public static ActiveBolus fromDetailedBolusInfo(DetailedBolusInfo detailedBolusInfo) { + ActiveBolus activeBolus = new ActiveBolus(); + activeBolus.date = detailedBolusInfo.date; + activeBolus.lastKnownBolusTime = detailedBolusInfo.lastKnownBolusTime; + activeBolus.eventType = detailedBolusInfo.eventType; + activeBolus.insulin = detailedBolusInfo.insulin; + activeBolus.carbs = detailedBolusInfo.carbs; + activeBolus.source = detailedBolusInfo.source; + activeBolus.isValid = detailedBolusInfo.isValid; + activeBolus.glucose = detailedBolusInfo.glucose; + activeBolus.glucoseType = detailedBolusInfo.glucoseType; + activeBolus.carbTime = detailedBolusInfo.carbTime; + activeBolus.boluscalc = detailedBolusInfo.boluscalc == null ? null : detailedBolusInfo.boluscalc.toString(); + activeBolus.pumpId = detailedBolusInfo.pumpId; + activeBolus.isSMB = detailedBolusInfo.isSMB; + activeBolus.deliverAt = detailedBolusInfo.deliverAt; + activeBolus.notes = detailedBolusInfo.notes; + return activeBolus; + } + + public DetailedBolusInfo toDetailedBolusInfo(AAPSLogger aapsLogger) { + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.date = date; + detailedBolusInfo.lastKnownBolusTime = lastKnownBolusTime; + detailedBolusInfo.eventType = eventType; + detailedBolusInfo.insulin = insulin; + detailedBolusInfo.carbs = carbs; + detailedBolusInfo.source = source; + detailedBolusInfo.isValid = isValid; + detailedBolusInfo.glucose = glucose; + detailedBolusInfo.glucoseType = glucoseType; + detailedBolusInfo.carbTime = carbTime; + if (!StringUtils.isEmpty(boluscalc)) { + try { + detailedBolusInfo.boluscalc = new JSONObject(boluscalc); + } catch (JSONException ex) { + // ignore + aapsLogger.warn(LTag.PUMP, "Could not parse bolusCalc string to JSON: " + boluscalc, ex); + } + } + detailedBolusInfo.pumpId = pumpId; + detailedBolusInfo.isSMB = isSMB; + detailedBolusInfo.deliverAt = deliverAt; + detailedBolusInfo.notes = notes; + return detailedBolusInfo; + } +} diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java index 645802bd87..bd657d3043 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/driver/comm/AapsOmnipodManager.java @@ -60,6 +60,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.comm.exception.PodReturne import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.StatusResponse; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoRecentPulseLog; import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoResponse; +import info.nightscout.androidaps.plugins.pump.omnipod.data.ActiveBolus; import info.nightscout.androidaps.plugins.pump.omnipod.defs.FaultEventCode; import info.nightscout.androidaps.plugins.pump.omnipod.defs.IOmnipodManager; import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType; @@ -295,9 +296,11 @@ public class AapsOmnipodManager implements IOmnipodManager { // // I discussed this with the AAPS team but nobody seems to care so we're stuck with this ugly workaround for now try { - sp.putString(OmnipodConst.Prefs.CurrentBolus, omnipodUtil.getGsonInstance().toJson(detailedBolusInfo)); + ActiveBolus activeBolus = ActiveBolus.fromDetailedBolusInfo(detailedBolusInfo); + sp.putString(OmnipodConst.Prefs.ActiveBolus, omnipodUtil.getGsonInstance().toJson(activeBolus)); + aapsLogger.debug(LTag.PUMP, "Stored active bolus to SP for recovery"); } catch (Exception ex) { - aapsLogger.error(LTag.PUMP, "Failed to store current bolus to SP", ex); + aapsLogger.error(LTag.PUMP, "Failed to store active bolus to SP", ex); } // Wait for the bolus to finish @@ -308,7 +311,7 @@ public class AapsOmnipodManager implements IOmnipodManager { addBolusToHistory(detailedBolusInfo); - sp.remove(OmnipodConst.Prefs.CurrentBolus); + sp.remove(OmnipodConst.Prefs.ActiveBolus); return new PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(detailedBolusInfo.insulin); } diff --git a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java index 05d97e003a..a036571bf4 100644 --- a/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java +++ b/omnipod/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/util/OmnipodConst.java @@ -14,7 +14,7 @@ public class OmnipodConst { public static class Prefs { public static final String PodState = Prefix + "pod_state"; - public static final String CurrentBolus = Prefix + "current_bolus"; + public static final String ActiveBolus = Prefix + "current_bolus"; public static final int BeepBasalEnabled = R.string.key_omnipod_beep_basal_enabled; public static final int BeepBolusEnabled = R.string.key_omnipod_beep_bolus_enabled; public static final int BeepSMBEnabled = R.string.key_omnipod_beep_smb_enabled;