diff --git a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/notifications/Notification.kt b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/notifications/Notification.kt index ba138abaf8..f9eb549c7b 100644 --- a/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/notifications/Notification.kt +++ b/core/interfaces/src/main/kotlin/app/aaps/core/interfaces/notifications/Notification.kt @@ -138,6 +138,7 @@ open class Notification { const val PUMP_SETTINGS_FAILED = 84 const val PUMP_TIMEZONE_UPDATE_FAILED = 85 const val BLUETOOTH_NOT_SUPPORTED = 86 + const val PUMP_WARNING = 87 const val USER_MESSAGE = 1000 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 48cba7b7a9..4a7f99e566 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 @@ -71,6 +71,15 @@ class MedtrumPump @Inject constructor( _activeAlarms = value } + // New pump warnings + private val _pumpWarning = MutableStateFlow(AlarmState.NONE) + val pumpWarningFlow: StateFlow = _pumpWarning + var pumpWarning: AlarmState + get() = _pumpWarning.value + set(value) { + _pumpWarning.value = value + } + // Prime progress as state flow private val _primeProgress = MutableStateFlow(0) val primeProgressFlow: StateFlow = _primeProgress diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt index cc3d1c9178..32ed8e39d6 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt @@ -200,7 +200,11 @@ class NotificationPacket(val injector: HasAndroidInjector) { val alarmState = AlarmState.values()[i] if ((alarmFlags shr i) and 1 != 0) { // If the alarm bit is set, add the corresponding alarm to activeAlarms - medtrumPump.addAlarm(alarmState) + if (!medtrumPump.activeAlarms.contains(alarmState)) { + aapsLogger.debug(LTag.PUMPCOMM, "Adding alarm $alarmState to active alarms") + medtrumPump.addAlarm(alarmState) + medtrumPump.pumpWarning = alarmState + } } else if (medtrumPump.activeAlarms.contains(alarmState)) { // If the alarm bit is not set, and the corresponding alarm is in activeAlarms, remove it medtrumPump.removeAlarm(alarmState) 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 ca6d0207aa..d60ab4936d 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 @@ -164,6 +164,11 @@ class MedtrumService : DaggerService(), BLECommCallback { handleConnectionStateChange(connectionState) } } + scope.launch { + medtrumPump.pumpWarningFlow.collect { pumpWarning -> + notifyPumpWarning(pumpWarning) + } + } } override fun onDestroy() { @@ -583,6 +588,7 @@ class MedtrumService : DaggerService(), BLECommCallback { when (state) { MedtrumPumpState.NONE, MedtrumPumpState.STOPPED -> { + rxBus.send(EventDismissNotification(Notification.PUMP_WARNING)) rxBus.send(EventDismissNotification(Notification.PUMP_ERROR)) rxBus.send(EventDismissNotification(Notification.PUMP_SUSPENDED)) uiInteraction.addNotification( @@ -631,20 +637,22 @@ class MedtrumService : DaggerService(), BLECommCallback { } MedtrumPumpState.HOURLY_MAX_SUSPENDED -> { - uiInteraction.addNotification( + uiInteraction.addNotificationWithSound( Notification.PUMP_SUSPENDED, rh.gs(R.string.pump_is_suspended_hour_max), - Notification.NORMAL, + Notification.URGENT, + app.aaps.core.ui.R.raw.alarm ) // Pump will report proper TBR for this from loadEvents() commandQueue.loadEvents(null) } MedtrumPumpState.DAILY_MAX_SUSPENDED -> { - uiInteraction.addNotification( + uiInteraction.addNotificationWithSound( Notification.PUMP_SUSPENDED, rh.gs(R.string.pump_is_suspended_day_max), - Notification.NORMAL, + Notification.URGENT, + app.aaps.core.ui.R.raw.alarm ) // Pump will report proper TBR for this from loadEvents() commandQueue.loadEvents(null) @@ -689,6 +697,23 @@ 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) { + uiInteraction.addNotification( + Notification.PUMP_WARNING, + rh.gs(R.string.pump_warning, medtrumPump.alarmStateToString(alarmState)), + Notification.ANNOUNCEMENT, + ) + pumpSync.insertAnnouncement( + medtrumPump.alarmStateToString(alarmState), + 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 147c018513..b508b7150d 100644 --- a/pump/medtrum/src/main/res/values/strings.xml +++ b/pump/medtrum/src/main/res/values/strings.xml @@ -4,6 +4,7 @@ sn_input alarm_setting patch_expiration + pump_warning_notification hourly_max_insulin daily_max_insulin @@ -30,6 +31,7 @@ Pump integration for Medtrum Nano and Medtrum 300U Medtrum pump settings Pump error: %1$s !! + Pump warning: %1$s Pump is suspended Pump is suspended due to hourly max insulin exceeded Pump is suspended due to daily max insulin exceeded @@ -138,6 +140,8 @@ Pump untested: %1$d! Please contact us at discord or github for support Alarm Settings 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. Patch Expiration When enabled, the patch will expire after 3 days, with a grace period of 8 hours after that. Hourly Maximum Insulin 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 b27de7bd1e..4e4ff321ba 100644 --- a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml +++ b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml @@ -27,6 +27,12 @@ android:entries="@array/alarmSettings" android:entryValues="@array/alarmSettingsValues" /> + +