Improve fix for #149 by not trying to serialize non-serializable fields

This commit is contained in:
Bart Sopers 2020-08-24 00:18:20 +02:00
parent 15b0701d06
commit ac922316a9
5 changed files with 94 additions and 11 deletions

View file

@ -281,7 +281,7 @@ class OmnipodFragment : DaggerFragment() {
.getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "-" .getTempBasalFromHistory(System.currentTimeMillis())?.toStringFull() ?: "-"
// total delivered // 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); resourceHelper.gs(R.string.omnipod_total_delivered, podStateManager.totalInsulinDelivered - OmnipodConst.POD_SETUP_UNITS);
} else { } else {
"-" "-"

View file

@ -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.hw.rileylink.service.tasks.ServiceTaskExecutor;
import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil; 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.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.OmnipodCommandType;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCustomActionType; import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodCustomActionType;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.OmnipodPumpPluginInterface; 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 // If so, add it to history
// Needs to be done after EventAppInitialized because otherwise, TreatmentsPlugin.onStart() hasn't been called yet // 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 // so it didn't initialize a TreatmentService yet, resulting in a NullPointerException
if (sp.contains(OmnipodConst.Prefs.CurrentBolus)) { if (sp.contains(OmnipodConst.Prefs.ActiveBolus)) {
String currentBolusString = sp.getString(OmnipodConst.Prefs.CurrentBolus, ""); String activeBolusString = sp.getString(OmnipodConst.Prefs.ActiveBolus, "");
aapsLogger.warn(LTag.PUMP, "Found active bolus in SP. Adding Treatment: {}", currentBolusString); aapsLogger.warn(LTag.PUMP, "Found active bolus in SP: {}. Adding Treatment.", activeBolusString);
try { try {
DetailedBolusInfo detailedBolusInfo = omnipodUtil.getGsonInstance().fromJson(currentBolusString, DetailedBolusInfo.class); ActiveBolus activeBolus = omnipodUtil.getGsonInstance().fromJson(activeBolusString, ActiveBolus.class);
aapsOmnipodManager.addBolusToHistory(detailedBolusInfo); aapsOmnipodManager.addBolusToHistory(activeBolus.toDetailedBolusInfo(aapsLogger));
} catch (Exception ex) { } catch (Exception ex) {
aapsLogger.error(LTag.PUMP, "Failed to add active bolus to history", 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) }, fabricPrivacy::logException)
); );

View file

@ -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;
}
}

View file

@ -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.StatusResponse;
import info.nightscout.androidaps.plugins.pump.omnipod.comm.message.response.podinfo.PodInfoRecentPulseLog; 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.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.FaultEventCode;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.IOmnipodManager; import info.nightscout.androidaps.plugins.pump.omnipod.defs.IOmnipodManager;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.PodInfoType; 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 // I discussed this with the AAPS team but nobody seems to care so we're stuck with this ugly workaround for now
try { 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) { } 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 // Wait for the bolus to finish
@ -308,7 +311,7 @@ public class AapsOmnipodManager implements IOmnipodManager {
addBolusToHistory(detailedBolusInfo); addBolusToHistory(detailedBolusInfo);
sp.remove(OmnipodConst.Prefs.CurrentBolus); sp.remove(OmnipodConst.Prefs.ActiveBolus);
return new PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(detailedBolusInfo.insulin); return new PumpEnactResult(injector).success(true).enacted(true).bolusDelivered(detailedBolusInfo.insulin);
} }

View file

@ -14,7 +14,7 @@ public class OmnipodConst {
public static class Prefs { public static class Prefs {
public static final String PodState = Prefix + "pod_state"; 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 BeepBasalEnabled = R.string.key_omnipod_beep_basal_enabled;
public static final int BeepBolusEnabled = R.string.key_omnipod_beep_bolus_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; public static final int BeepSMBEnabled = R.string.key_omnipod_beep_smb_enabled;