From 56e0837e516f714cd12a7e17c027b7b613e061f9 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Thu, 19 Oct 2023 12:59:14 +0200 Subject: [PATCH] Medtrum: Add local warning for expiry --- .../nightscout/pump/medtrum/MedtrumPlugin.kt | 9 +++++ .../nightscout/pump/medtrum/MedtrumPump.kt | 4 +++ .../pump/medtrum/services/MedtrumService.kt | 34 ++++++++++++++++++- pump/medtrum/src/main/res/values/strings.xml | 7 ++-- .../src/main/res/xml/pref_medtrum_pump.xml | 22 ++++++++---- 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt index 2adb563031..c237d9c2fd 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt @@ -141,6 +141,7 @@ import kotlin.math.abs preprocessAlarmSettings(preferenceFragment) preprocessMaxInsulinSettings(preferenceFragment) preprocessConnectionAlertSettings(preferenceFragment) + preprocessPumpWarningSettings(preferenceFragment) } private fun preprocessSerialSettings(preferenceFragment: PreferenceFragmentCompat) { @@ -263,6 +264,14 @@ import kotlin.math.abs } } + private fun preprocessPumpWarningSettings(preferenceFragment: PreferenceFragmentCompat) { + val patchExpirationPref = preferenceFragment.findPreference(rh.gs(R.string.key_patch_expiration)) + val pumpWarningNotificationPref = preferenceFragment.findPreference(rh.gs(R.string.key_pump_warning_notification)) + val pumpWarningExpiryHourPref = preferenceFragment.findPreference(rh.gs(R.string.key_pump_warning_expiry_hour)) + + pumpWarningExpiryHourPref?.isEnabled = patchExpirationPref?.isChecked == true && pumpWarningNotificationPref?.isChecked == true + } + override fun isInitialized(): Boolean { return medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt index 4a7f99e566..54dadef505 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt @@ -288,6 +288,8 @@ class MedtrumPump @Inject constructor( var desiredAlarmSetting = AlarmSetting.LIGHT_VIBRATE_AND_BEEP var desiredHourlyMaxInsulin: Int = 40 var desiredDailyMaxInsulin: Int = 180 + var desiredPumpWarning = true + var desiredPumpWarningExpiryThresholdHours = 72L fun pumpType(): PumpType = pumpType(deviceType) @@ -333,6 +335,8 @@ class MedtrumPump @Inject constructor( desiredAlarmSetting = AlarmSetting.values().firstOrNull { it.code == alarmSettingCode } ?: AlarmSetting.LIGHT_VIBRATE_AND_BEEP desiredHourlyMaxInsulin = sp.getInt(R.string.key_hourly_max_insulin, 40) desiredDailyMaxInsulin = sp.getInt(R.string.key_daily_max_insulin, 180) + desiredPumpWarning = sp.getBoolean(R.string.key_pump_warning_notification, true) + desiredPumpWarningExpiryThresholdHours = sp.getLong(R.string.key_pump_warning_expiry_hour, 72L) _pumpSN = pumpSNFromSP } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index ae596414e9..887c4c1a95 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -101,6 +101,8 @@ class MedtrumService : DaggerService(), BLECommCallback { private const val COMMAND_CONNECTING_TIMEOUT_SEC: Long = 30 private const val ALARM_HOURLY_MAX_CLEAR_CODE = 4 private const val ALARM_DAILY_MAX_CLEAR_CODE = 5 + + private const val CHECK_EXPIRY_WARNING_TIME_MS = 5 * 60 * 1000L } private val disposable = CompositeDisposable() @@ -135,6 +137,11 @@ class MedtrumService : DaggerService(), BLECommCallback { pumpSync.connectNewPump() medtrumPump.setFakeTBRIfNotSet() } + if (event.isChanged(rh.gs(R.string.key_pump_warning_notification)) + || event.isChanged(rh.gs(R.string.key_pump_warning_expiry_hour)) + ) { + medtrumPump.loadUserSettingsFromSP() + } if (event.isChanged(rh.gs(R.string.key_alarm_setting)) || event.isChanged(rh.gs(R.string.key_patch_expiration)) || event.isChanged(rh.gs(R.string.key_hourly_max_insulin)) @@ -169,6 +176,12 @@ class MedtrumService : DaggerService(), BLECommCallback { notifyPumpWarning(pumpWarning) } } + scope.launch { + while (true) { + checkExpiryWarning() + kotlinx.coroutines.delay(CHECK_EXPIRY_WARNING_TIME_MS) + } + } } override fun onDestroy() { @@ -700,7 +713,7 @@ class MedtrumService : DaggerService(), BLECommCallback { private fun notifyPumpWarning(alarmState: AlarmState) { // Notification on pump warning - if (sp.getBoolean(R.string.key_pump_warning_notification, true) && alarmState != AlarmState.NONE) { + if (medtrumPump.desiredPumpWarning && alarmState != AlarmState.NONE) { uiInteraction.addNotification( Notification.PUMP_WARNING, rh.gs(R.string.pump_warning, medtrumPump.alarmStateToString(alarmState)), @@ -715,6 +728,25 @@ class MedtrumService : DaggerService(), BLECommCallback { } } + private fun checkExpiryWarning() { + if (medtrumPump.desiredPatchExpiration && medtrumPump.desiredPumpWarning) { + val warningAt = medtrumPump.patchStartTime + T.hours(medtrumPump.desiredPumpWarningExpiryThresholdHours).msecs() + if (dateUtil.now() >= warningAt && dateUtil.now() <= warningAt + CHECK_EXPIRY_WARNING_TIME_MS) { + uiInteraction.addNotification( + Notification.PUMP_WARNING, + rh.gs(R.string.alarm_pump_expires_soon), + Notification.ANNOUNCEMENT, + ) + pumpSync.insertAnnouncement( + rh.gs(R.string.alarm_pump_expires_soon), + null, + medtrumPump.pumpType(), + medtrumPump.pumpSN.toString(radix = 16) + ) + } + } + } + /** BLECommCallbacks */ override fun onBLEConnected() { aapsLogger.debug(LTag.PUMPCOMM, "<<<<< onBLEConnected") diff --git a/pump/medtrum/src/main/res/values/strings.xml b/pump/medtrum/src/main/res/values/strings.xml index 01bbe48505..f2a283e42f 100644 --- a/pump/medtrum/src/main/res/values/strings.xml +++ b/pump/medtrum/src/main/res/values/strings.xml @@ -5,6 +5,7 @@ alarm_setting patch_expiration pump_warning_notification + pump_expiry_warning_hour hourly_max_insulin daily_max_insulin @@ -145,11 +146,13 @@ Select your preferred pump alarm settings. Notification on pump warning Show notification on non critical pump warnings: low battery, low reservoir (20 units) and expires soon. Recommended to leave enabled when pump alarms are set to silent. + Pump expiry warning [hours] + Show notification on specified hour after activation. Patch Expiration When enabled, the patch will expire after 3 days, with a grace period of 8 hours after that. - Hourly Maximum Insulin + Hourly Maximum Insulin [Units] Specify the maximum units of insulin allowed per hour. If exceeded, the pump will suspend. - Daily Maximum Insulin + Daily Maximum Insulin [Units] Specify the maximum units of insulin allowed per day. If exceeded, the pump will suspend. diff --git a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml index 4e4ff321ba..67f807e549 100644 --- a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml +++ b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml @@ -13,12 +13,6 @@ android:singleLine="true" android:title="@string/sn_input_title" /> - - + + + +