diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/pump/Medtrum.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/pump/Medtrum.kt index 7ae90a7b01..92ebebf2dc 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/pump/Medtrum.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/pump/Medtrum.kt @@ -7,4 +7,5 @@ interface Medtrum { fun loadEvents(): PumpEnactResult // events history to build treatments from fun setUserOptions(): PumpEnactResult // set user settings + fun clearAlarms(): PumpEnactResult // clear alarms } \ No newline at end of file diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/queue/Command.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/queue/Command.kt index 977613aa81..fce81c02bf 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/queue/Command.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/queue/Command.kt @@ -23,11 +23,12 @@ abstract class Command( BASAL_PROFILE, READSTATUS, LOAD_HISTORY, // TDDs and so far only Dana specific - LOAD_EVENTS, // so far only Dana specific + LOAD_EVENTS, LOAD_TDD, SET_USER_SETTINGS, // so far only Dana specific, START_PUMP, STOP_PUMP, + CLEAR_ALARMS, // so far only Medtrum specific INSIGHT_SET_TBR_OVER_ALARM, // insight only CUSTOM_COMMAND } diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/queue/CommandQueue.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/queue/CommandQueue.kt index eadf4663db..cbc413d97d 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/queue/CommandQueue.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/queue/CommandQueue.kt @@ -31,6 +31,7 @@ interface CommandQueue { fun setUserOptions(callback: Callback?): Boolean fun loadTDDs(callback: Callback?): Boolean fun loadEvents(callback: Callback?): Boolean + fun clearAlarms(callback: Callback?): Boolean fun customCommand(customCommand: CustomCommand, callback: Callback?): Boolean fun isCustomCommandRunning(customCommandType: Class): Boolean fun isCustomCommandInQueue(customCommandType: Class): Boolean diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index f59fd97717..3d27666bf4 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -393,6 +393,7 @@ CARBS %1$d g EXTENDED BOLUS %1$.2f U %2$d min LOAD EVENTS + CLEAR_ALARMS LOAD HISTORY %1$d LOAD TDDs SET PROFILE diff --git a/implementation/src/main/java/info/nightscout/implementation/di/CommandQueueModule.kt b/implementation/src/main/java/info/nightscout/implementation/di/CommandQueueModule.kt index e6864c9508..0e9a04ba45 100644 --- a/implementation/src/main/java/info/nightscout/implementation/di/CommandQueueModule.kt +++ b/implementation/src/main/java/info/nightscout/implementation/di/CommandQueueModule.kt @@ -14,6 +14,7 @@ import info.nightscout.implementation.queue.commands.CommandTempBasalPercent import info.nightscout.implementation.queue.commands.CommandBolus import info.nightscout.implementation.queue.commands.CommandCancelExtendedBolus import info.nightscout.implementation.queue.commands.CommandCancelTempBasal +import info.nightscout.implementation.queue.commands.CommandClearAlarms import info.nightscout.implementation.queue.commands.CommandCustomCommand import info.nightscout.implementation.queue.commands.CommandExtendedBolus import info.nightscout.implementation.queue.commands.CommandInsightSetTBROverNotification @@ -32,6 +33,7 @@ abstract class CommandQueueModule { @ContributesAndroidInjector abstract fun commandExtendedBolusInjector(): CommandExtendedBolus @ContributesAndroidInjector abstract fun commandInsightSetTBROverNotificationInjector(): CommandInsightSetTBROverNotification @ContributesAndroidInjector abstract fun commandLoadEventsInjector(): CommandLoadEvents + @ContributesAndroidInjector abstract fun commandClearAlarmsInjector(): CommandClearAlarms @ContributesAndroidInjector abstract fun commandLoadHistoryInjector(): CommandLoadHistory @ContributesAndroidInjector abstract fun commandLoadTDDsInjector(): CommandLoadTDDs @ContributesAndroidInjector abstract fun commandReadStatusInjector(): CommandReadStatus diff --git a/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt b/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt index 894d7aa308..18cbf76b38 100644 --- a/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt +++ b/implementation/src/main/java/info/nightscout/implementation/queue/CommandQueueImplementation.kt @@ -22,6 +22,7 @@ import info.nightscout.implementation.R import info.nightscout.implementation.queue.commands.CommandBolus import info.nightscout.implementation.queue.commands.CommandCancelExtendedBolus import info.nightscout.implementation.queue.commands.CommandCancelTempBasal +import info.nightscout.implementation.queue.commands.CommandClearAlarms import info.nightscout.implementation.queue.commands.CommandCustomCommand import info.nightscout.implementation.queue.commands.CommandExtendedBolus import info.nightscout.implementation.queue.commands.CommandInsightSetTBROverNotification @@ -535,6 +536,20 @@ class CommandQueueImplementation @Inject constructor( return true } + // returns true if command is queued + override fun clearAlarms(callback: Callback?): Boolean { + if (isRunning(CommandType.CLEAR_ALARMS)) { + callback?.result(executingNowError())?.run() + return false + } + // remove all unfinished + removeAll(CommandType.CLEAR_ALARMS) + // add new command to queue + add(CommandClearAlarms(injector, callback)) + notifyAboutNewCommand() + return true + } + override fun customCommand(customCommand: CustomCommand, callback: Callback?): Boolean { if (isCustomCommandInQueue(customCommand.javaClass)) { callback?.result(executingNowError())?.run() diff --git a/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandClearAlarms.kt b/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandClearAlarms.kt new file mode 100644 index 0000000000..cbfb184d61 --- /dev/null +++ b/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandClearAlarms.kt @@ -0,0 +1,39 @@ +package info.nightscout.implementation.queue.commands + +import dagger.android.HasAndroidInjector +import info.nightscout.interfaces.plugin.ActivePlugin +import info.nightscout.interfaces.pump.Dana +import info.nightscout.interfaces.pump.Diaconn +import info.nightscout.interfaces.pump.Medtrum +import info.nightscout.interfaces.pump.PumpEnactResult +import info.nightscout.interfaces.queue.Callback +import info.nightscout.interfaces.queue.Command +import info.nightscout.rx.logging.LTag +import javax.inject.Inject + +class CommandClearAlarms( + injector: HasAndroidInjector, + callback: Callback? +) : Command(injector, CommandType.CLEAR_ALARMS, callback) { + + @Inject lateinit var activePlugin: ActivePlugin + + override fun execute() { + val pump = activePlugin.activePump + + if (pump is Medtrum) { + val medtrumPump = pump as Medtrum + val r = medtrumPump.clearAlarms() + aapsLogger.debug(LTag.PUMPQUEUE, "Result success: ${r.success} enacted: ${r.enacted}") + callback?.result(r)?.run() + } + } + + override fun status(): String = rh.gs(info.nightscout.core.ui.R.string.clear_alarms) + + override fun log(): String = "CLEAR ALAMRS" + override fun cancel() { + aapsLogger.debug(LTag.PUMPQUEUE, "Result cancel") + callback?.result(PumpEnactResult(injector).success(false).comment(info.nightscout.core.ui.R.string.connectiontimedout))?.run() + } +} diff --git a/implementation/src/test/java/info/nightscout/implementation/queue/CommandQueueImplementationTest.kt b/implementation/src/test/java/info/nightscout/implementation/queue/CommandQueueImplementationTest.kt index aaaa6657ce..38165f5c90 100644 --- a/implementation/src/test/java/info/nightscout/implementation/queue/CommandQueueImplementationTest.kt +++ b/implementation/src/test/java/info/nightscout/implementation/queue/CommandQueueImplementationTest.kt @@ -238,6 +238,11 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { // add loadEvents commandQueue.loadEvents(null) Assertions.assertEquals(4, commandQueue.size()) + + // add clearAlarms + commandQueue.clearAlarms(null) + Assertions.assertEquals(5, commandQueue.size()) + commandQueue.clear() commandQueue.tempBasalAbsolute(0.0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null) commandQueue.pickup() @@ -354,6 +359,22 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { Assertions.assertEquals(1, commandQueue.size()) } + @Test + fun isClearAlarmsCommandInQueue() { + // given + Assertions.assertEquals(0, commandQueue.size()) + + // when + commandQueue.clearAlarms(null) + + // then + Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.CLEAR_ALARMS)) + Assertions.assertEquals(1, commandQueue.size()) + // next should be ignored + commandQueue.clearAlarms(null) + Assertions.assertEquals(1, commandQueue.size()) + } + @Test fun isLoadTDDsCommandInQueue() { // given 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 2b66931397..de293443a1 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 @@ -383,4 +383,14 @@ import kotlin.math.round val connectionOK = medtrumService?.setUserSettings() ?: false return PumpEnactResult(injector).success(connectionOK) } + + override fun clearAlarms(): PumpEnactResult { + if (!isInitialized()) { + val result = PumpEnactResult(injector).success(false) + result.comment = "pump not initialized" + return result + } + val connectionOK = medtrumService?.clearAlarms() ?: false + return PumpEnactResult(injector).success(connectionOK) + } } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/code/EventType.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/code/EventType.kt index 32b4efb4ed..b1328d48cc 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/code/EventType.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/code/EventType.kt @@ -1,8 +1,7 @@ package info.nightscout.pump.medtrum.code enum class EventType { - ACTIVATION_CLICKED, - DEACTIVATION_CLICKED, + CHANGE_PATCH_CLICKED, INVALID_BASAL_RATE, PROFILE_NOT_SET, FINISH_ACTIVITY, diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt index b38941ac91..035174e815 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt @@ -15,10 +15,12 @@ enum class CommandType(val code: Byte) { SET_BASAL_PROFILE(21), SET_TEMP_BASAL(24), CANCEL_TEMP_BASAL(25), + RESUME_PUMP(29), POLL_PATCH(30), STOP_PATCH(31), READ_BOLUS_STATE(34), SET_PATCH(35), SET_BOLUS_MOTOR(36), - GET_RECORD(99) + GET_RECORD(99), + CLEAR_ALARM(115) } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ClearPumpAlarmPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ClearPumpAlarmPacket.kt new file mode 100644 index 0000000000..15f860fc50 --- /dev/null +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ClearPumpAlarmPacket.kt @@ -0,0 +1,16 @@ +package info.nightscout.pump.medtrum.comm.packets + +import dagger.android.HasAndroidInjector +import info.nightscout.pump.medtrum.comm.enums.CommandType.CLEAR_ALARM +import info.nightscout.pump.medtrum.extension.toByteArray + +class ClearPumpAlarmPacket(injector: HasAndroidInjector, val clearType: Int) : MedtrumPacket(injector) { + + init { + opCode = CLEAR_ALARM.code + } + + override fun getRequest(): ByteArray { + return byteArrayOf(opCode) + clearType.toByteArray(2) + } +} diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ResumePumpPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ResumePumpPacket.kt new file mode 100644 index 0000000000..aa613b3656 --- /dev/null +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ResumePumpPacket.kt @@ -0,0 +1,16 @@ +package info.nightscout.pump.medtrum.comm.packets + +import dagger.android.HasAndroidInjector +import info.nightscout.pump.medtrum.comm.enums.CommandType.RESUME_PUMP +import info.nightscout.pump.medtrum.extension.toByteArray + +class ResumePumpPacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { + + init { + opCode = RESUME_PUMP.code + } + + override fun getRequest(): ByteArray { + return byteArrayOf(opCode) + } +} diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/di/MedtrumCommModule.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/di/MedtrumCommModule.kt index 393a208d06..d58d9d2f33 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/di/MedtrumCommModule.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/di/MedtrumCommModule.kt @@ -6,6 +6,7 @@ import info.nightscout.pump.medtrum.comm.packets.ActivatePacket import info.nightscout.pump.medtrum.comm.packets.AuthorizePacket import info.nightscout.pump.medtrum.comm.packets.CancelBolusPacket import info.nightscout.pump.medtrum.comm.packets.CancelTempBasalPacket +import info.nightscout.pump.medtrum.comm.packets.ClearPumpAlarmPacket import info.nightscout.pump.medtrum.comm.packets.GetDeviceTypePacket import info.nightscout.pump.medtrum.comm.packets.GetRecordPacket import info.nightscout.pump.medtrum.comm.packets.GetTimePacket @@ -14,6 +15,7 @@ import info.nightscout.pump.medtrum.comm.packets.NotificationPacket import info.nightscout.pump.medtrum.comm.packets.PollPatchPacket import info.nightscout.pump.medtrum.comm.packets.PrimePacket import info.nightscout.pump.medtrum.comm.packets.ReadBolusStatePacket +import info.nightscout.pump.medtrum.comm.packets.ResumePumpPacket import info.nightscout.pump.medtrum.comm.packets.SetBasalProfilePacket import info.nightscout.pump.medtrum.comm.packets.SetBolusMotorPacket import info.nightscout.pump.medtrum.comm.packets.SetBolusPacket @@ -32,6 +34,7 @@ abstract class MedtrumCommModule { @ContributesAndroidInjector abstract fun contributesAuthorizePacket(): AuthorizePacket @ContributesAndroidInjector abstract fun contributesCancelBolusPacket(): CancelBolusPacket @ContributesAndroidInjector abstract fun contributesCancelTempBasalPacket(): CancelTempBasalPacket + @ContributesAndroidInjector abstract fun contributesClearPumpAlarmPacket(): ClearPumpAlarmPacket @ContributesAndroidInjector abstract fun contributesGetDeviceTypePacket(): GetDeviceTypePacket @ContributesAndroidInjector abstract fun contributesGetRecordPacket(): GetRecordPacket @ContributesAndroidInjector abstract fun contributesGetTimePacket(): GetTimePacket @@ -40,6 +43,7 @@ abstract class MedtrumCommModule { @ContributesAndroidInjector abstract fun contributesPollPatchPacket(): PollPatchPacket @ContributesAndroidInjector abstract fun contributesPrimePacket(): PrimePacket @ContributesAndroidInjector abstract fun contributesReadBolusStatePacket(): ReadBolusStatePacket + @ContributesAndroidInjector abstract fun contributesResumePumpPacket(): ResumePumpPacket @ContributesAndroidInjector abstract fun contributesSetBasalProfilePacket(): SetBasalProfilePacket @ContributesAndroidInjector abstract fun contributesSetBolusMotorPacket(): SetBolusMotorPacket @ContributesAndroidInjector abstract fun contributesSetBolusPacket(): SetBolusPacket 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 1f6a836c89..00647d05cc 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 @@ -75,6 +75,8 @@ class MedtrumService : DaggerService(), BLECommCallback { private const val COMMAND_DEFAULT_TIMEOUT_SEC: Long = 60 private const val COMMAND_SYNC_TIMEOUT_SEC: Long = 120 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 } val timeUtil = MedtrumTimeUtil() @@ -155,11 +157,8 @@ class MedtrumService : DaggerService(), BLECommCallback { MedtrumPumpState.LOWBG_SUSPENDED, MedtrumPumpState.LOWBG_SUSPENDED2, MedtrumPumpState.AUTO_SUSPENDED, - MedtrumPumpState.HMAX_SUSPENDED, - MedtrumPumpState.DMAX_SUSPENDED, MedtrumPumpState.SUSPENDED, MedtrumPumpState.PAUSED -> { - // TODO: Message with reason uiInteraction.addNotification( Notification.PUMP_SUSPENDED, rh.gs(R.string.pump_is_suspended), @@ -168,6 +167,23 @@ class MedtrumService : DaggerService(), BLECommCallback { // Pump will report proper TBR for this } + MedtrumPumpState.HMAX_SUSPENDED -> { + uiInteraction.addNotification( + Notification.PUMP_SUSPENDED, + rh.gs(R.string.pump_is_suspended_hour_max), + Notification.NORMAL, + ) + // Pump will report proper TBR for this + } + MedtrumPumpState.DMAX_SUSPENDED -> { + uiInteraction.addNotification( + Notification.PUMP_SUSPENDED, + rh.gs(R.string.pump_is_suspended_day_max), + Notification.NORMAL, + ) + // Pump will report proper TBR for this + } + MedtrumPumpState.OCCLUSION, MedtrumPumpState.EXPIRED, MedtrumPumpState.RESERVOIR_EMPTY, @@ -250,18 +266,49 @@ class MedtrumService : DaggerService(), BLECommCallback { fun loadEvents(): Boolean { // Send a poll patch, to workaround connection losses? + rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingpumpstatus))) var result = sendPacketAndGetResponse(PollPatchPacket(injector)) // So just do a syncronize to make sure we have the latest data if (result) result = sendPacketAndGetResponse(SynchronizePacket(injector)) // Sync records (based on the info we have from the sync) if (result) result = syncRecords() - if (!result) { + if (result) { + aapsLogger.debug(LTag.PUMPCOMM, "Events loaded") + medtrumPump.lastConnection = System.currentTimeMillis() + } else { aapsLogger.error(LTag.PUMPCOMM, "Failed to sync records") } - if (result) medtrumPump.lastConnection = System.currentTimeMillis() - aapsLogger.debug(LTag.PUMPCOMM, "Events loaded") - rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingpumpstatus))) + return result + } + + fun clearAlarms(): Boolean { + var result = true + when (medtrumPump.pumpState) { + MedtrumPumpState.HMAX_SUSPENDED -> { + result = sendPacketAndGetResponse(ClearPumpAlarmPacket(injector, ALARM_HOURLY_MAX_CLEAR_CODE)) + } + MedtrumPumpState.DMAX_SUSPENDED -> { + result = sendPacketAndGetResponse(ClearPumpAlarmPacket(injector, ALARM_DAILY_MAX_CLEAR_CODE)) + } + else -> { + // Do nothing + // TODO: Remove me before release!!! + // Try to brute force the commands + aapsLogger.warn(LTag.PUMPCOMM, "Trying to clear alarms brutus!") + for (i in 0..100) { + result = sendPacketAndGetResponse(ClearPumpAlarmPacket(injector, i)) + if (result) { + aapsLogger.warn(LTag.PUMPCOMM, "Alarm cleared: $i") + break + } + } + } + } + // Resume suspended pump + // TODO: We might not want to do this for alarms which don't suspend the pump + if (result) result = sendPacketAndGetResponse(ResumePumpPacket(injector)) + return result } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumOverviewFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumOverviewFragment.kt index eed99396b7..98c99febd8 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumOverviewFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumOverviewFragment.kt @@ -12,6 +12,7 @@ import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumOverviewViewModel import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.EventType import info.nightscout.pump.medtrum.code.PatchStep +import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag @@ -43,18 +44,12 @@ class MedtrumOverviewFragment : MedtrumBaseFragment when (evt.peekContent()) { - EventType.ACTIVATION_CLICKED -> requireContext().apply { - // TODO: Check what to do with this, and if we need this, it currently messes up patch is last registered as priming - // var step = convertToPatchStep(medtrumPump.pumpState) - // if (step == PatchStep.DEACTIVATION_COMPLETE) { - // // Reset - // step = PatchStep.PREPARE_PATCH - // } - startActivity(MedtrumActivity.createIntentFromMenu(this, PatchStep.PREPARE_PATCH)) - } - - EventType.DEACTIVATION_CLICKED -> requireContext().apply { - startActivity(MedtrumActivity.createIntentFromMenu(this, PatchStep.START_DEACTIVATION)) + EventType.CHANGE_PATCH_CLICKED -> requireContext().apply { + if (medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED) { + startActivity(MedtrumActivity.createIntentFromMenu(this, PatchStep.START_DEACTIVATION)) + } else { + startActivity(MedtrumActivity.createIntentFromMenu(this, PatchStep.PREPARE_PATCH)) + } } else -> Unit } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt index 51eb6503d9..1e17003062 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt @@ -9,6 +9,7 @@ import info.nightscout.pump.medtrum.ui.event.SingleLiveEvent import info.nightscout.pump.medtrum.ui.event.UIEvent import info.nightscout.pump.medtrum.ui.viewmodel.BaseViewModel import info.nightscout.interfaces.profile.ProfileFunction +import info.nightscout.interfaces.queue.CommandQueue import info.nightscout.pump.medtrum.MedtrumPump import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.ConnectionState @@ -34,7 +35,8 @@ class MedtrumOverviewViewModel @Inject constructor( private val aapsSchedulers: AapsSchedulers, private val fabricPrivacy: FabricPrivacy, private val profileFunction: ProfileFunction, - private val medtrumPump: MedtrumPump + private val commandQueue: CommandQueue, + val medtrumPump: MedtrumPump ) : BaseViewModel() { private val scope = CoroutineScope(Dispatchers.Default) @@ -43,29 +45,41 @@ class MedtrumOverviewViewModel @Inject constructor( val eventHandler: LiveData> get() = _eventHandler + private val _canDoRefresh = SingleLiveEvent() + val canDoRefresh: LiveData + get() = _canDoRefresh + + private val _canDoResetAlarms = SingleLiveEvent() + val canDoResetAlarms: LiveData + get() = _canDoResetAlarms + private val _bleStatus = SingleLiveEvent() val bleStatus: LiveData get() = _bleStatus - private val _isPatchActivated = SingleLiveEvent() - val isPatchActivated: LiveData - get() = _isPatchActivated + private val _lastConnected = SingleLiveEvent() + val lastConnected: LiveData + get() = _lastConnected - private val _pumpState = SingleLiveEvent() - val pumpState: LiveData - get() = _pumpState + private val _activeAlarms = SingleLiveEvent() + val activeAlarms: LiveData + get() = _activeAlarms - private val _basalType = SingleLiveEvent() - val basalType: LiveData - get() = _basalType + private val _pumpType = SingleLiveEvent() + val pumpType: LiveData + get() = _pumpType - private val _runningBasalRate = SingleLiveEvent() - val runningBasalRate: LiveData - get() = _runningBasalRate + private val _fwVersion = SingleLiveEvent() + val fwVersion: LiveData + get() = _fwVersion + + private val _patchNo = SingleLiveEvent() + val patchNo: LiveData + get() = _patchNo - private val _reservoir = SingleLiveEvent() - val reservoir: LiveData - get() = _reservoir + private val _patchExpiry = SingleLiveEvent() + val patchExpiry: LiveData + get() = _patchExpiry init { scope.launch { @@ -74,14 +88,21 @@ class MedtrumOverviewViewModel @Inject constructor( when (state) { ConnectionState.CONNECTING -> { _bleStatus.postValue("{fa-bluetooth-b spin}") + _canDoRefresh.postValue(false) } ConnectionState.CONNECTED -> { _bleStatus.postValue("{fa-bluetooth}") + _canDoRefresh.postValue(false) } ConnectionState.DISCONNECTED -> { _bleStatus.postValue("{fa-bluetooth-b}") + if (medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED) { + _canDoRefresh.postValue(true) + } else { + _canDoRefresh.postValue(false) + } } } } @@ -89,37 +110,13 @@ class MedtrumOverviewViewModel @Inject constructor( scope.launch { medtrumPump.pumpStateFlow.collect { state -> aapsLogger.debug(LTag.PUMP, "MedtrumViewModel pumpStateFlow: $state") - if (state > MedtrumPumpState.EJECTED && state < MedtrumPumpState.STOPPED) { - _isPatchActivated.postValue(true) + if (medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED) { + _canDoResetAlarms.postValue(true) } else { - _isPatchActivated.postValue(false) + _canDoResetAlarms.postValue(false) } } } - scope.launch { - medtrumPump.lastBasalRateFlow.collect { rate -> - aapsLogger.debug(LTag.PUMP, "MedtrumViewModel runningBasalRateFlow: $rate") - _runningBasalRate.postValue(String.format(rh.gs(R.string.current_basal_rate), rate)) - } - } - scope.launch { - medtrumPump.pumpStateFlow.collect { state -> - aapsLogger.debug(LTag.PUMP, "MedtrumViewModel pumpStateFlow: $state") - _pumpState.postValue(state.toString()) - } - } - scope.launch { - medtrumPump.reservoirFlow.collect { reservoir -> - aapsLogger.debug(LTag.PUMP, "MedtrumViewModel reservoirFlow: $reservoir") - _reservoir.postValue(String.format(rh.gs(R.string.reservoir_level), reservoir)) - } - } - scope.launch { - medtrumPump.lastBasalTypeFlow.collect { basalType -> - aapsLogger.debug(LTag.PUMP, "MedtrumViewModel basalTypeFlow: $basalType") - _basalType.postValue(basalType.toString()) - } - } } override fun onCleared() { @@ -127,18 +124,22 @@ class MedtrumOverviewViewModel @Inject constructor( scope.cancel() } - fun onClickActivation() { - aapsLogger.debug(LTag.PUMP, "Start Patch clicked!") + fun onClickRefresh() { + commandQueue.readStatus(rh.gs(R.string.requested_by_user), null) + } + + fun onClickResetAlarms() { + commandQueue.clearAlarms(null) + } + + fun onClickChangePatch() { + aapsLogger.debug(LTag.PUMP, "ChangePatch Patch clicked!") val profile = profileFunction.getProfile() if (profile == null) { _eventHandler.postValue(UIEvent(EventType.PROFILE_NOT_SET)) } else { - _eventHandler.postValue(UIEvent(EventType.ACTIVATION_CLICKED)) + _eventHandler.postValue(UIEvent(EventType.CHANGE_PATCH_CLICKED)) } } - - fun onClickDeactivation() { - aapsLogger.debug(LTag.PUMP, "Stop Patch clicked!") - _eventHandler.postValue(UIEvent(EventType.DEACTIVATION_CLICKED)) - } -} \ No newline at end of file +} + \ No newline at end of file diff --git a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml index 398c351044..866e869bb0 100644 --- a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml +++ b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml @@ -27,235 +27,474 @@ + android:paddingBottom="5dp"> - + - + android:orientation="horizontal"> - + - + - + + + + + android:orientation="horizontal"> - + - + + + + + + + + + android:layout_marginVertical="3dp" + android:orientation="horizontal"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -267,26 +506,38 @@ android:layout_alignParentBottom="true" android:orientation="horizontal"> +