From bcc767211686496b87202fc6d48268e3de47ec5f Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Wed, 14 Apr 2021 16:06:33 +0100 Subject: [PATCH] - starting pumpSync changes --- .../pump/common/PumpPluginAbstract.java | 39 ++++++++++++++++++- .../plugins/pump/common/data/PumpDbEntry.java | 21 ++++++++++ .../pump/medtronic/MedtronicPumpPlugin.java | 27 ++++++++++--- 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpDbEntry.java diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java index b6bc68a47a..b9e1f4f387 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/PumpPluginAbstract.java @@ -10,6 +10,8 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import dagger.android.HasAndroidInjector; import info.nightscout.androidaps.core.R; @@ -27,11 +29,13 @@ import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpPluginBase; +import info.nightscout.androidaps.interfaces.PumpSync; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.common.ManufacturerType; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; +import info.nightscout.androidaps.plugins.pump.common.data.PumpDbEntry; import info.nightscout.androidaps.plugins.pump.common.data.PumpStatus; import info.nightscout.androidaps.plugins.pump.common.defs.PumpDriverState; import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; @@ -69,6 +73,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI protected boolean displayConnectionMessages = false; protected PumpType pumpType; protected AapsSchedulers aapsSchedulers; + protected PumpSync pumpSync; protected PumpPluginAbstract( @@ -84,7 +89,8 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI Context context, FabricPrivacy fabricPrivacy, DateUtil dateUtil, - AapsSchedulers aapsSchedulers + AapsSchedulers aapsSchedulers, + PumpSync pumpSync ) { super(pluginDescription, injector, aapsLogger, resourceHelper, commandQueue); @@ -101,6 +107,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI this.pumpType = pumpType; this.dateUtil = dateUtil; this.aapsSchedulers = aapsSchedulers; + this.pumpSync = pumpSync; } @@ -335,6 +342,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI } catch (Exception ignored) { } + // TODO fix TemporaryBasal tb = activePlugin.getActiveTreatments().getTempBasalFromHistory(System.currentTimeMillis()); if (tb != null) { extended.put("TempBasalAbsoluteRate", @@ -343,6 +351,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI extended.put("TempBasalRemaining", tb.getPlannedRemainingMinutes()); } + // TODO fix ExtendedBolus eb = activePlugin.getActiveTreatments().getExtendedBolusFromHistory(System.currentTimeMillis()); if (eb != null) { extended.put("ExtendedBolusAbsoluteRate", eb.absoluteRate()); @@ -377,10 +386,12 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI ret += "LastBolus: " + DecimalFormatter.INSTANCE.to2Decimal(getPumpStatusData().lastBolusAmount) + "U @" + // android.text.format.DateFormat.format("HH:mm", getPumpStatusData().lastBolusTime) + "\n"; } + // TODO fix TemporaryBasal activeTemp = activePlugin.getActiveTreatments().getRealTempBasalFromHistory(System.currentTimeMillis()); if (activeTemp != null) { ret += "Temp: " + activeTemp.toStringFull() + "\n"; } + // TODO fix ExtendedBolus activeExtendedBolus = activePlugin.getActiveTreatments().getExtendedBolusFromHistory( System.currentTimeMillis()); if (activeExtendedBolus != null) { @@ -413,6 +424,7 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI //if (MedtronicHistoryData.doubleBolusDebug) // aapsLogger.debug("DoubleBolusDebug: deliverTreatment::(carb only entry)"); + // TODO fix // no bolus required, carb only treatment activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true); @@ -471,4 +483,29 @@ public abstract class PumpPluginAbstract extends PumpPluginBase implements PumpI private PumpEnactResult getOperationNotSupportedWithCustomText(int resourceId) { return new PumpEnactResult(getInjector()).success(false).enacted(false).comment(resourceId); } + + // PumpSync + + Map driverHistory = new HashMap<>(); + + public abstract long generateTempId(long timeMillis); + + public boolean addBolusWithTempId(DetailedBolusInfo detailedBolusInfo, boolean writeToInternalHistory) { + long temporaryId = generateTempId(detailedBolusInfo.timestamp); + boolean response = pumpSync.addBolusWithTempId(detailedBolusInfo.timestamp, detailedBolusInfo.insulin, + generateTempId(detailedBolusInfo.timestamp), detailedBolusInfo.getBolusType(), + getPumpType(), serialNumber()); + + if (response && writeToInternalHistory) { + driverHistory.put(temporaryId, new PumpDbEntry(temporaryId, model(), serialNumber(), detailedBolusInfo)); + } + + return response; + } + + public void removeTemporaryId(long temporaryId) { + driverHistory.remove(temporaryId); + } + + } diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpDbEntry.java b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpDbEntry.java new file mode 100644 index 0000000000..f67a9741f8 --- /dev/null +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/common/data/PumpDbEntry.java @@ -0,0 +1,21 @@ +package info.nightscout.androidaps.plugins.pump.common.data; + +import info.nightscout.androidaps.data.DetailedBolusInfo; +import info.nightscout.androidaps.plugins.pump.common.defs.PumpType; + +public class PumpDbEntry { + + long temporaryId; + PumpType pumpType; + String serialNumber; + DetailedBolusInfo detailedBolusInfo; + + public PumpDbEntry(long temporaryId, PumpType pumpType, String serialNumber, DetailedBolusInfo detailedBolusInfo) { + this.temporaryId = temporaryId; + this.pumpType = pumpType; + this.serialNumber = serialNumber; + this.detailedBolusInfo = detailedBolusInfo; + } + + +} diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index 48cdc84322..efc0a8c117 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -40,6 +40,7 @@ import info.nightscout.androidaps.interfaces.CommandQueueProvider; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.interfaces.PumpSync; import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.plugins.bus.RxBusWrapper; @@ -98,7 +99,6 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP; @Singleton public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInterface, RileyLinkPumpDevice { - private final SP sp; private final MedtronicUtil medtronicUtil; private final MedtronicPumpStatus medtronicPumpStatus; private final MedtronicHistoryData medtronicHistoryData; @@ -117,7 +117,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter public static boolean isBusy = false; private final List busyTimestamps = new ArrayList<>(); private boolean hasTimeDateOrTimeZoneChanged = false; - + private boolean usePumpSync = false; @Inject public MedtronicPumpPlugin( @@ -136,7 +136,8 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter RileyLinkServiceData rileyLinkServiceData, ServiceTaskExecutor serviceTaskExecutor, DateUtil dateUtil, - AapsSchedulers aapsSchedulers + AapsSchedulers aapsSchedulers, + PumpSync pumpSync ) { super(new PluginDescription() // @@ -148,11 +149,10 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter .preferencesId(R.xml.pref_medtronic) .description(R.string.description_pump_medtronic), // PumpType.MEDTRONIC_522_722, // we default to most basic model, correct model from config is loaded later - injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, sp, context, fabricPrivacy, dateUtil, aapsSchedulers + injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, sp, context, fabricPrivacy, dateUtil, aapsSchedulers, pumpSync ); this.medtronicUtil = medtronicUtil; - this.sp = sp; this.medtronicPumpStatus = medtronicPumpStatus; this.medtronicHistoryData = medtronicHistoryData; this.rileyLinkServiceData = rileyLinkServiceData; @@ -733,6 +733,14 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter rxBus.send(new EventMedtronicPumpValuesChanged()); } + @Override public long generateTempId(long timeMillis) { + return 0; + } + + @Override public String getSerial() { + return null; + } + private BolusDeliveryType bolusDeliveryType = BolusDeliveryType.Idle; private enum BolusDeliveryType { @@ -878,7 +886,12 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter detailedBolusInfo.timestamp = now; detailedBolusInfo.deliverAtTheLatest = now; // not sure about that one - activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true); + // TODO fix + if (usePumpSync) { + addBolusWithTempId(detailedBolusInfo, true); + } else { + activePlugin.getActiveTreatments().addToHistoryTreatment(detailedBolusInfo, true); + } // we subtract insulin, exact amount will be visible with next remainingInsulin update. medtronicPumpStatus.reservoirRemainingUnits -= detailedBolusInfo.insulin; @@ -1044,6 +1057,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter .absolute(absoluteRate) // .source(Source.USER); + // TODO fix activePlugin.getActiveTreatments().addToHistoryTempBasal(tempStart); incrementStatistics(MedtronicConst.Statistics.TBRsSet); @@ -1391,6 +1405,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter .duration(0) // .source(Source.USER); + // TODO fix activePlugin.getActiveTreatments().addToHistoryTempBasal(tempBasal); return new PumpEnactResult(getInjector()).success(true).enacted(true) //