From 38bab8404773ac76864c9c251ea4a580520a2bdd Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 18 Apr 2021 16:27:00 +0200 Subject: [PATCH 1/7] Combo: add reading MAC address to RuffyCommands. --- .../org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl | 1 + .../pump/combo/ruffyscripter/RuffyCommands.java | 4 ++++ .../pump/combo/ruffyscripter/RuffyScripter.java | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/combo/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl b/combo/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl index 6c988aa038..8861938f8a 100644 --- a/combo/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl +++ b/combo/src/main/aidl/org/monkey/d/ruffy/ruffy/driver/IRuffyService.aidl @@ -20,4 +20,5 @@ interface IRuffyService { void rtSendKey(byte keyCode, boolean changed); void resetPairing(); boolean isConnected(); + String getMacAddress(); } diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyCommands.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyCommands.java index 0567e1cb12..1b9b5414ec 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyCommands.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyCommands.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.combo.ruffyscripter; +import androidx.annotation.Nullable; import info.nightscout.androidaps.plugins.pump.combo.ruffyscripter.history.PumpHistoryRequest; public interface RuffyCommands { @@ -45,5 +46,8 @@ public interface RuffyCommands { CommandResult getDateAndTime(); CommandResult setDateAndTime(); + + @Nullable + String getMacAddress(); } diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java index c60fca36a9..a8d9ab025b 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ruffyscripter/RuffyScripter.java @@ -822,6 +822,16 @@ public class RuffyScripter implements RuffyCommands { throw new RuntimeException("Not supported"); } + @Nullable + public String getMacAddress() { + try { + return ruffyService.getMacAddress(); + } catch (RemoteException e) { + // ignore; ruffy version is probably old and doesn't support reading MAC address yet + return null; + } + } + /** * Confirms and dismisses the given alert if it's raised before the timeout */ From 97bf95a6fa9d5f73b5efd8908a2f77eb96bcf12e Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sun, 18 Apr 2021 16:29:38 +0200 Subject: [PATCH 2/7] Combo: provide pump serial number, notify about pump change. --- .../plugins/pump/combo/ComboPlugin.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index 1f17b035cc..8c40c60bf8 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -73,9 +73,12 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP; */ @Singleton public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { + // TODO clean up? + static final String COMBO_PUMP_SERIAL = "combo_pump_serial"; static final String COMBO_TBRS_SET = "combo_tbrs_set"; static final String COMBO_BOLUSES_DELIVERED = "combo_boluses_delivered"; + // collaborators private final ProfileFunction profileFunction; private final SP sp; private RxBusWrapper rxBus; @@ -403,6 +406,17 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { } } + // read pump BT mac address and use it as the pump's serial + String macAddress = ruffyScripter.getMacAddress(); + if (macAddress != null) { + String lastKnownSN = serialNumber(); + if (!lastKnownSN.equals(fakeSerialNumber()) && !lastKnownSN.equals(macAddress)) { + getAapsLogger().info(LTag.PUMP, "Pump serial number changed " + lastKnownSN + " -> " + macAddress); + pumpSync.connectNewPump(); + } + sp.putString(COMBO_PUMP_SERIAL, macAddress); + } + // ComboFragment updates state fully only after the pump has initialized, // so force an update after initialization completed rxBus.send(new EventComboPumpUpdateGUI()); @@ -1309,7 +1323,11 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { @NonNull @Override public String serialNumber() { - return InstanceId.INSTANCE.instanceId(); // TODO replace by real serial + return sp.getString(COMBO_PUMP_SERIAL, fakeSerialNumber()); + } + + private String fakeSerialNumber() { + return InstanceId.INSTANCE.instanceId(); } @NonNull @Override From 76568cac38e584401604b99df6c078a4c0030e01 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Sat, 24 Apr 2021 23:48:09 +0200 Subject: [PATCH 3/7] Combo: show pump id in UI. --- .../plugins/pump/combo/ComboFragment.java | 3 ++ .../main/res/layout/combopump_fragment.xml | 45 +++++++++++++++++++ combo/src/main/res/values/strings.xml | 1 + 3 files changed, 49 insertions(+) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java index 159e637eaa..12d2d43d38 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java @@ -54,6 +54,7 @@ public class ComboFragment extends DaggerFragment { private Button refreshButton; private TextView bolusCount; private TextView tbrCount; + private TextView serialNumber; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @@ -70,6 +71,7 @@ public class ComboFragment extends DaggerFragment { tempBasalText = view.findViewById(R.id.combo_temp_basal); bolusCount = view.findViewById(R.id.combo_bolus_count); tbrCount = view.findViewById(R.id.combo_tbr_count); + serialNumber= view.findViewById(R.id.combo_serial_number); refreshButton = view.findViewById(R.id.combo_refresh_button); refreshButton.setOnClickListener(v -> { @@ -242,6 +244,7 @@ public class ComboFragment extends DaggerFragment { // stats bolusCount.setText(String.valueOf(sp.getLong(ComboPlugin.COMBO_BOLUSES_DELIVERED, 0L))); tbrCount.setText(String.valueOf(sp.getLong(ComboPlugin.COMBO_TBRS_SET, 0L))); + serialNumber.setText(comboPlugin.serialNumber()); } } } \ No newline at end of file diff --git a/combo/src/main/res/layout/combopump_fragment.xml b/combo/src/main/res/layout/combopump_fragment.xml index 8e13a5fb8f..2ee9e41508 100644 --- a/combo/src/main/res/layout/combopump_fragment.xml +++ b/combo/src/main/res/layout/combopump_fragment.xml @@ -462,6 +462,51 @@ + + + + + + + + + + + + TBR count Bolus stopped Stopping bolus + Pump identifier \ No newline at end of file From 84622a30a0555b7580c2fa1f85e9d72f394eac37 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Thu, 29 Apr 2021 21:22:36 +0200 Subject: [PATCH 4/7] Combo: clean up SP access. --- .../plugins/pump/combo/ComboFragment.java | 4 ++-- .../plugins/pump/combo/ComboPlugin.java | 22 +++++++++++-------- combo/src/main/res/values/strings.xml | 3 +++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java index 12d2d43d38..ef21aea9f1 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java @@ -242,8 +242,8 @@ public class ComboFragment extends DaggerFragment { tempBasalText.setText(tbrStr); // stats - bolusCount.setText(String.valueOf(sp.getLong(ComboPlugin.COMBO_BOLUSES_DELIVERED, 0L))); - tbrCount.setText(String.valueOf(sp.getLong(ComboPlugin.COMBO_TBRS_SET, 0L))); + bolusCount.setText(String.valueOf(comboPlugin.getBolusesDelivered())); + tbrCount.setText(String.valueOf(comboPlugin.getTbrsSet())); serialNumber.setText(comboPlugin.serialNumber()); } } diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index 8c40c60bf8..61dbb1d71d 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -73,11 +73,6 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP; */ @Singleton public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { - // TODO clean up? - static final String COMBO_PUMP_SERIAL = "combo_pump_serial"; - static final String COMBO_TBRS_SET = "combo_tbrs_set"; - static final String COMBO_BOLUSES_DELIVERED = "combo_boluses_delivered"; - // collaborators private final ProfileFunction profileFunction; private final SP sp; @@ -408,13 +403,14 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { // read pump BT mac address and use it as the pump's serial String macAddress = ruffyScripter.getMacAddress(); + getAapsLogger().debug("Connected pump has MAC address: " + macAddress); if (macAddress != null) { String lastKnownSN = serialNumber(); if (!lastKnownSN.equals(fakeSerialNumber()) && !lastKnownSN.equals(macAddress)) { getAapsLogger().info(LTag.PUMP, "Pump serial number changed " + lastKnownSN + " -> " + macAddress); pumpSync.connectNewPump(); } - sp.putString(COMBO_PUMP_SERIAL, macAddress); + sp.putString(R.string.combo_pump_serial, macAddress); } // ComboFragment updates state fully only after the pump has initialized, @@ -633,7 +629,7 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { private void incrementTbrCount() { try { - sp.putLong(COMBO_TBRS_SET, sp.getLong(COMBO_TBRS_SET, 0L) + 1); + sp.putLong(R.string.combo_tbrs_set, sp.getLong(R.string.combo_tbrs_set, 0L) + 1); } catch (Exception e) { // ignore } @@ -641,12 +637,20 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { private void incrementBolusCount() { try { - sp.putLong(COMBO_BOLUSES_DELIVERED, sp.getLong(COMBO_BOLUSES_DELIVERED, 0L) + 1); + sp.putLong(R.string.combo_boluses_delivered, sp.getLong(R.string.combo_boluses_delivered, 0L) + 1); } catch (Exception e) { // ignore } } + public Long getTbrsSet() { + return sp.getLong(R.string.combo_tbrs_set, 0L); + } + + public Long getBolusesDelivered() { + return sp.getLong(R.string.combo_boluses_delivered, 0L); + } + /** * Creates a treatment record based on the request in DetailBolusInfo and the delivered bolus. */ @@ -1323,7 +1327,7 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { @NonNull @Override public String serialNumber() { - return sp.getString(COMBO_PUMP_SERIAL, fakeSerialNumber()); + return sp.getString(R.string.combo_pump_serial, fakeSerialNumber()); } private String fakeSerialNumber() { diff --git a/combo/src/main/res/values/strings.xml b/combo/src/main/res/values/strings.xml index 555a910c3f..144d9de921 100644 --- a/combo/src/main/res/values/strings.xml +++ b/combo/src/main/res/values/strings.xml @@ -55,4 +55,7 @@ Bolus stopped Stopping bolus Pump identifier + combo_pump_serial + combo_tbrs_set + combo_boluses_delivered \ No newline at end of file From 1fed867eb3846e40a3ababae63682d66962df87c Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Thu, 29 Apr 2021 22:00:50 +0200 Subject: [PATCH 5/7] Revert "Combo: show pump id in UI." This reverts commit 76568cac --- .../plugins/pump/combo/ComboFragment.java | 3 -- .../main/res/layout/combopump_fragment.xml | 45 ------------------- combo/src/main/res/values/strings.xml | 1 - 3 files changed, 49 deletions(-) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java index ef21aea9f1..e6d8b990bf 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboFragment.java @@ -54,7 +54,6 @@ public class ComboFragment extends DaggerFragment { private Button refreshButton; private TextView bolusCount; private TextView tbrCount; - private TextView serialNumber; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @@ -71,7 +70,6 @@ public class ComboFragment extends DaggerFragment { tempBasalText = view.findViewById(R.id.combo_temp_basal); bolusCount = view.findViewById(R.id.combo_bolus_count); tbrCount = view.findViewById(R.id.combo_tbr_count); - serialNumber= view.findViewById(R.id.combo_serial_number); refreshButton = view.findViewById(R.id.combo_refresh_button); refreshButton.setOnClickListener(v -> { @@ -244,7 +242,6 @@ public class ComboFragment extends DaggerFragment { // stats bolusCount.setText(String.valueOf(comboPlugin.getBolusesDelivered())); tbrCount.setText(String.valueOf(comboPlugin.getTbrsSet())); - serialNumber.setText(comboPlugin.serialNumber()); } } } \ No newline at end of file diff --git a/combo/src/main/res/layout/combopump_fragment.xml b/combo/src/main/res/layout/combopump_fragment.xml index 2ee9e41508..8e13a5fb8f 100644 --- a/combo/src/main/res/layout/combopump_fragment.xml +++ b/combo/src/main/res/layout/combopump_fragment.xml @@ -462,51 +462,6 @@ - - - - - - - - - - - - TBR count Bolus stopped Stopping bolus - Pump identifier combo_pump_serial combo_tbrs_set combo_boluses_delivered From 92b441927e43725da4f4739b4bd63f6db5a95190 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Thu, 29 Apr 2021 22:11:27 +0200 Subject: [PATCH 6/7] Combo: class javadoc. --- .../plugins/pump/combo/ComboPlugin.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index 61dbb1d71d..a0b039ce4b 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -69,6 +69,25 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.sharedPreferences.SP; /** + * Driver for the Roche Accu-Chek Combo pump, using the ruffy app for BT communication. + * + * For boluses, the logic is to request a bolus and then read it back from the history to see what was + * actually delivered. + * + * TBR-handling doesn't read the pump history. On the pump, TBR records are only created after a TBR has finished. + * So when a TBR is started on the pump, it can't be known when it started until the TBR ends or is cancelled. + * Cancelling would assume a user works against the loop, and creating a temporary TBR (AAPS-side) and updating it + * once a record exists on the pump has other problems, since there's no ID for TBRs, only timestamps. + * For the regular uses where the user doesn't set TBR (or rather infrequently for some special cases), TBRs are + * only changed on the pump for error conditions where the pump is stopped, those need to be synced. + * The approach taken is to create a TBR record in AAPS when AAPS requests a TBR and forego the TBR history on + * the pump entirely. The pump state is refreshed often enough to tolerate not seeing the full length of a TBR + * on the pump if it was changed. Thus, during a pump refresh a new TBR starting now is created in AAPS if a + * mismatch between expected state and actual pump state is detected see {@link #checkAndResolveTbrMismatch(PumpState)}. + * This approach skipped implementing edge-cases that pose no real risk, in part due to limited resources to + * implement every edge-case scenario. Insulin amount given via boluses are significantly higher, so the + * priority was there to make that as safe as possible. + * * Created by mike on 05.08.2016. */ @Singleton From 572d7c45650af8fab4a844fee4b3114a1e0ecdfe Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Thu, 29 Apr 2021 22:17:01 +0200 Subject: [PATCH 7/7] Combo: minor cleanups. --- .../nightscout/androidaps/plugins/pump/combo/ComboPlugin.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index a0b039ce4b..f398f620d1 100644 --- a/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/combo/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -1102,11 +1102,10 @@ public class ComboPlugin extends PumpPluginBase implements Pump, Constraints { * Checks the main screen to determine if TBR on pump matches app state. */ private void checkAndResolveTbrMismatch(PumpState state) { - // compare with: info.nightscout.androidaps.plugins.PumpDanaR.comm.MsgStatusTempBasal.updateTempBasalInDB() long now = System.currentTimeMillis(); // Combo doesn't have nor uses IDs for TBRs, see note in #setTempBasalPercent + //noinspection UnnecessaryLocalVariable long tbrId = now; - //TemporaryBasal aapsTbr = treatmentsPlugin.getTempBasalFromHistoryIncludingConvertedExtended(now); PumpSync.PumpState.TemporaryBasal aapsTbr = pumpSync.expectedPumpState().getTemporaryBasal(); if (aapsTbr == null && state.tbrActive && state.tbrRemainingDuration > 2) { getAapsLogger().debug(LTag.PUMP, "Creating temp basal from pump TBR");