Medtrum: Option to show notification on non critical pump alerts

This commit is contained in:
jbr7rr 2023-10-08 12:16:09 +02:00
parent 5945aa40ab
commit ccb7961e50
6 changed files with 54 additions and 5 deletions

View file

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

View file

@ -71,6 +71,15 @@ class MedtrumPump @Inject constructor(
_activeAlarms = value
}
// New pump warnings
private val _pumpWarning = MutableStateFlow(AlarmState.NONE)
val pumpWarningFlow: StateFlow<AlarmState> = _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<Int> = _primeProgress

View file

@ -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)

View file

@ -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")

View file

@ -4,6 +4,7 @@
<string name="key_sn_input" translatable="false">sn_input</string>
<string name="key_alarm_setting" translatable="false">alarm_setting</string>
<string name="key_patch_expiration" translatable="false">patch_expiration</string>
<string name="key_pump_warning_notification" translatable="false">pump_warning_notification</string>
<string name="key_hourly_max_insulin" translatable="false">hourly_max_insulin</string>
<string name="key_daily_max_insulin" translatable="false">daily_max_insulin</string>
@ -30,6 +31,7 @@
<string name="medtrum_pump_description">Pump integration for Medtrum Nano and Medtrum 300U</string>
<string name="medtrum_pump_setting">Medtrum pump settings</string>
<string name="pump_error">Pump error: %1$s !! </string>
<string name="pump_warning">Pump warning: %1$s </string>
<string name="pump_is_suspended">Pump is suspended</string>
<string name="pump_is_suspended_hour_max">Pump is suspended due to hourly max insulin exceeded</string>
<string name="pump_is_suspended_day_max">Pump is suspended due to daily max insulin exceeded</string>
@ -138,6 +140,8 @@
<string name="pump_unsupported">Pump untested: %1$d! Please contact us at discord or github for support</string>
<string name="alarm_setting_title">Alarm Settings</string>
<string name="alarm_setting_summary">Select your preferred pump alarm settings.</string>
<string name="pump_warning_notification_title">Notification on pump warning</string>
<string name="pump_warning_notification_summary">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.</string>
<string name="patch_expiration_title">Patch Expiration</string>
<string name="patch_expiration_summary">When enabled, the patch will expire after 3 days, with a grace period of 8 hours after that.</string>
<string name="hourly_max_insulin_title">Hourly Maximum Insulin</string>

View file

@ -27,6 +27,12 @@
android:entries="@array/alarmSettings"
android:entryValues="@array/alarmSettingsValues" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_pump_warning_notification"
android:title="@string/pump_warning_notification_title"
android:summary="@string/pump_warning_notification_summary" />
<app.aaps.core.validators.ValidatingEditTextPreference
android:defaultValue="25"
android:inputType="number"