Alarm on pump suspended states

This commit is contained in:
jbr7rr 2023-06-08 15:48:32 +02:00
parent 3ae145524c
commit ecb2c977ec
2 changed files with 53 additions and 15 deletions

View file

@ -10,10 +10,10 @@ import dagger.android.DaggerService
import dagger.android.HasAndroidInjector
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.interfaces.constraints.Constraints
import info.nightscout.interfaces.notifications.Notification
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileFunction
import info.nightscout.interfaces.pump.PumpEnactResult
import info.nightscout.interfaces.pump.PumpSync
import info.nightscout.interfaces.queue.Callback
import info.nightscout.interfaces.queue.CommandQueue
@ -24,12 +24,11 @@ import info.nightscout.pump.medtrum.R
import info.nightscout.pump.medtrum.code.ConnectionState
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
import info.nightscout.pump.medtrum.comm.packets.*
import info.nightscout.pump.medtrum.extension.toInt
import info.nightscout.pump.medtrum.extension.toLong
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
import info.nightscout.rx.AapsSchedulers
import info.nightscout.rx.bus.RxBus
import info.nightscout.rx.events.EventAppExit
import info.nightscout.rx.events.EventDismissNotification
import info.nightscout.rx.events.EventOverviewBolusProgress
import info.nightscout.rx.events.EventPreferenceChange
import info.nightscout.rx.events.EventPumpStatusChanged
@ -46,7 +45,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Inject
import kotlin.math.abs
import kotlin.math.round
@ -73,6 +71,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
@Inject lateinit var dateUtil: DateUtil
companion object {
// TODO: Test and further increase?
private const val COMMAND_DEFAULT_TIMEOUT_SEC: Long = 60
private const val COMMAND_SYNC_TIMEOUT_SEC: Long = 120
@ -109,8 +108,45 @@ class MedtrumService : DaggerService(), BLECommCallback {
changePump()
}
}, fabricPrivacy::logException)
scope.launch {
medtrumPump.pumpStateFlow.collect { state ->
when (state) {
MedtrumPumpState.LOWBG_SUSPENDED,
MedtrumPumpState.LOWBG_SUSPENDED2,
MedtrumPumpState.AUTO_SUSPENDED,
MedtrumPumpState.HMAX_SUSPENDED,
MedtrumPumpState.DMAX_SUSPENDED,
MedtrumPumpState.SUSPENDED,
MedtrumPumpState.PAUSED,
MedtrumPumpState.OCCLUSION,
MedtrumPumpState.EXPIRED,
MedtrumPumpState.RESERVOIR_EMPTY,
MedtrumPumpState.PATCH_FAULT,
MedtrumPumpState.PATCH_FAULT2,
MedtrumPumpState.BASE_FAULT,
MedtrumPumpState.BATTERY_OUT,
MedtrumPumpState.NO_CALIBRATION -> {
// Pump suspended show error!
uiInteraction.addNotificationWithSound(
Notification.PUMP_ERROR,
rh.gs(R.string.pump_error, state.toString()),
Notification.URGENT,
info.nightscout.core.ui.R.raw.alarm
)
}
MedtrumPumpState.STOPPED -> {
rxBus.send(EventDismissNotification(Notification.PUMP_ERROR))
}
else -> {
// Do nothing
}
}
}
}
changePump()
// TODO: We should probably listen to the pump state as well and handle some state changes? Or do we handle that in the packets or medtrumPump?
}
override fun onDestroy() {
@ -297,7 +333,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
result = sendPacketAndGetResponse(PollPatchPacket(injector))
SystemClock.sleep(100)
// Get our record
if (result) result = sendPacketAndGetResponse(GetRecordPacket(injector, sequence))
if (result) result = sendPacketAndGetResponse(GetRecordPacket(injector, sequence), COMMAND_SYNC_TIMEOUT_SEC)
if (result == false) break
}
}
@ -431,6 +467,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
// State for connect flow
private inner class AuthState : State() {
val retryCounter = 0
override fun onEnter() {

View file

@ -14,6 +14,7 @@
<string name="medtrum_pump_shortname">MT</string>
<string name="medtrum_pump_description">Medtrum Nano</string>
<string name="medtrumpump_settings">Medtrum pump settings</string>
<string name="pump_error">Pump error: %1$s !! </string>
<!-- overview fragment -->
<string name="medtrum_ble_status">BLE Status</string>