diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/notifications/Notification.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/notifications/Notification.kt index 51989bbaa7..24f55d074e 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/notifications/Notification.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/notifications/Notification.kt @@ -135,6 +135,7 @@ open class Notification { const val COMBO_UNKNOWN_TBR = 81 const val BLUETOOTH_NOT_ENABLED = 82 const val PATCH_NOT_ACTIVE = 83 + const val PUMP_SETTINGS_FAILED = 84 const val USER_MESSAGE = 1000 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 d17e58d45f..7ae90a7b01 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 @@ -6,4 +6,5 @@ package info.nightscout.interfaces.pump interface Medtrum { fun loadEvents(): PumpEnactResult // events history to build treatments from + fun setUserOptions(): PumpEnactResult // set user settings } \ No newline at end of file diff --git a/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandSetUserSettings.kt b/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandSetUserSettings.kt index 471c83c670..cb120cf32a 100644 --- a/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandSetUserSettings.kt +++ b/implementation/src/main/java/info/nightscout/implementation/queue/commands/CommandSetUserSettings.kt @@ -4,6 +4,7 @@ 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 @@ -30,6 +31,12 @@ class CommandSetUserSettings( aapsLogger.debug(LTag.PUMPQUEUE, "Result success: ${r.success} enacted: ${r.enacted}") callback?.result(r)?.run() } + + if (pump is Medtrum) { + val r = pump.setUserOptions() + 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.set_user_settings) 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 bfef4fa928..2b66931397 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 @@ -373,4 +373,14 @@ import kotlin.math.round val connectionOK = medtrumService?.loadEvents() ?: false return PumpEnactResult(injector).success(connectionOK) } + + override fun setUserOptions(): PumpEnactResult { + if (!isInitialized()) { + val result = PumpEnactResult(injector).success(false) + result.comment = "pump not initialized" + return result + } + val connectionOK = medtrumService?.setUserSettings() ?: false + return PumpEnactResult(injector).success(connectionOK) + } } 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 6fa1677ced..f069dbea00 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 @@ -177,14 +177,13 @@ class MedtrumPump @Inject constructor( var lastStopSequence = 0 var lastStopPatchId = 0L - // TODO set these setting on init // User settings (desired values, to be set on pump) var desiredPatchExpiration = false - var desiredAlarmSetting = AlarmSetting.LIGHT_VIBRATE_AND_BEEP.code + var desiredAlarmSetting = AlarmSetting.LIGHT_VIBRATE_AND_BEEP var desiredHourlyMaxInsulin: Int = 40 var desiredDailyMaxInsulin: Int = 180 - init { + fun loadFromSP() { // Load stuff from SP _patchSessionToken = sp.getLong(R.string.key_session_token, 0L) _currentSequenceNumber = sp.getInt(R.string.key_current_sequence_number, 0) @@ -203,14 +202,14 @@ class MedtrumPump @Inject constructor( } fun loadUserSettingsFromSP() { - // TODO - // desiredPatchExpiration = sp.getBoolean(R.string.key_patch_expiration, false) - // desiredAlarmSetting = sp.getInt(R.string.key_alarm_setting, AlarmSetting.LIGHT_VIBRATE_AND_BEEP.code) - // desiredHourlyMaxInsulin = sp.getInt(R.string.key_hourly_max_insulin, 40) - // desiredDailyMaxInsulin = sp.getInt(R.string.key_daily_max_insulin, 180) + desiredPatchExpiration = sp.getBoolean(info.nightscout.pump.medtrum.R.string.key_patch_expiration, false) + val alarmSettingCode = sp.getString(info.nightscout.pump.medtrum.R.string.key_alarm_setting, AlarmSetting.LIGHT_VIBRATE_AND_BEEP.code.toString())?.toByte() + desiredAlarmSetting = AlarmSetting.values().firstOrNull { it.code == alarmSettingCode } ?: AlarmSetting.LIGHT_VIBRATE_AND_BEEP + desiredHourlyMaxInsulin = sp.getInt(info.nightscout.pump.medtrum.R.string.key_hourly_max_insulin, 40) + desiredDailyMaxInsulin = sp.getInt(info.nightscout.pump.medtrum.R.string.key_daily_max_insulin, 180) try { - _pumpSN = sp.getString(info.nightscout.pump.medtrum.R.string.key_snInput, " ").toLong(radix = 16) + _pumpSN = sp.getString(info.nightscout.pump.medtrum.R.string.key_sn_input, " ").toLong(radix = 16) } catch (e: NumberFormatException) { aapsLogger.debug(LTag.PUMP, "changePump: Invalid input!") } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacket.kt index f18998d186..2eaeb17b86 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacket.kt @@ -65,7 +65,7 @@ class ActivatePacket(injector: HasAndroidInjector, private val basalProfile: Byt val autoSuspendTime: Byte = 12 // Not sure why, but pump needs this in order to activate val patchExpiration: Byte = medtrumPump.desiredPatchExpiration.toByte() - val alarmSetting: Byte = medtrumPump.desiredAlarmSetting + val alarmSetting: Byte = medtrumPump.desiredAlarmSetting.code val lowSuspend: Byte = 0 val predictiveLowSuspend: Byte = 0 diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacket.kt index fad3171614..c9196f5bee 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacket.kt @@ -2,6 +2,7 @@ package info.nightscout.pump.medtrum.comm.packets import dagger.android.HasAndroidInjector import info.nightscout.pump.medtrum.MedtrumPump +import info.nightscout.pump.medtrum.comm.enums.AlarmSetting import info.nightscout.pump.medtrum.comm.enums.CommandType.SET_PATCH import info.nightscout.pump.medtrum.extension.toByte import info.nightscout.pump.medtrum.extension.toByteArray @@ -20,8 +21,8 @@ class SetPatchPacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { /** * byte 0: opCode * byte 1: alarmSetting // See AlarmSetting - * byte 2-3: hourlyMaxInsulin // Max hourly dose of insulin not used for now, divided by 0.05 - * byte 4-5: dailyMaxSet // Max daily dose of insulin not used for now, divided by 0.05 + * byte 2-3: hourlyMaxInsulin // Max hourly dose of insulin, divided by 0.05 + * byte 4-5: dailyMaxSet // Max daily dose of insulin, divided by 0.05 * byte 6: expirationTimer // Expiration timer, 0 = no expiration 1 = 12 hour reminder and expiration * byte 7: autoSuspendEnable // Value for auto mode, not used for AAPS * byte 8: autoSuspendTime // Value for auto mode, not used for AAPS @@ -30,11 +31,16 @@ class SetPatchPacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { * byte 11: predictiveLowSuspendRange // Value for auto mode, not used for AAPS */ - val alarmSetting: Byte = medtrumPump.desiredAlarmSetting + val alarmSetting: AlarmSetting = medtrumPump.desiredAlarmSetting val hourlyMaxInsulin: Int = round(medtrumPump.desiredHourlyMaxInsulin / 0.05).toInt() val dailyMaxInsulin: Int = round(medtrumPump.desiredDailyMaxInsulin / 0.05).toInt() val patchExpiration: Byte = medtrumPump.desiredPatchExpiration.toByte() + val autoSuspendEnable: Byte = 0 + val autoSuspendTime: Byte = 12 // Not sure why, but pump needs this + val lowSuspend: Byte = 0 + val predictiveLowSuspend: Byte = 0 + val predictiveLowSuspendRange: Byte = 30 // Not sure why, but pump needs this - return byteArrayOf(opCode) + alarmSetting + hourlyMaxInsulin.toByteArray(2) + dailyMaxInsulin.toByteArray(2) + patchExpiration + 0.toByteArray(5) + return byteArrayOf(opCode) + alarmSetting.code + hourlyMaxInsulin.toByteArray(2) + dailyMaxInsulin.toByteArray(2) + patchExpiration + autoSuspendEnable + autoSuspendTime + lowSuspend + predictiveLowSuspend + predictiveLowSuspendRange } } 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 80ddda49f8..659c3b397c 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 @@ -103,8 +103,26 @@ class MedtrumService : DaggerService(), BLECommCallback { .toObservable(EventPreferenceChange::class.java) .observeOn(aapsSchedulers.io) .subscribe({ event -> - if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_snInput))) { - changePump() + if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_sn_input))) { + medtrumPump.loadUserSettingsFromSP() + } + if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_alarm_setting)) + || event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_patch_expiration)) + || event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_hourly_max_insulin)) + || event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_daily_max_insulin)) + ) { + medtrumPump.loadUserSettingsFromSP() + commandQueue.setUserOptions(object : Callback() { + override fun run() { + if (medtrumPlugin.isInitialized() && this.result.success == false) { + uiInteraction.addNotification( + Notification.PUMP_SETTINGS_FAILED, + rh.gs(R.string.pump_setting_failed), + Notification.NORMAL, + ) + } + } + }) } }, fabricPrivacy::logException) scope.launch { @@ -173,7 +191,7 @@ class MedtrumService : DaggerService(), BLECommCallback { } } - changePump() + medtrumPump.loadFromSP() } override fun onDestroy() { @@ -247,6 +265,11 @@ class MedtrumService : DaggerService(), BLECommCallback { return result } + fun setUserSettings(): Boolean { + rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.settingpumpsettings))) + return sendPacketAndGetResponse(SetPatchPacket(injector)) + } + fun setBolus(insulin: Double, t: EventOverviewBolusProgress.Treatment): Boolean { if (!isConnected) return false val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin)) @@ -314,7 +337,7 @@ class MedtrumService : DaggerService(), BLECommCallback { } if (result) result = sendPacketAndGetResponse(SetTempBasalPacket(injector, absoluteRate, durationInMinutes)) - // Get history records, this will update the prevoius basals + // Get history records, this will update the prevoius basals // Do not call update status directly, reconnection may be needed commandQueue.loadEvents(object : Callback() { override fun run() { @@ -328,7 +351,7 @@ class MedtrumService : DaggerService(), BLECommCallback { fun cancelTempBasal(): Boolean { var result = sendPacketAndGetResponse(CancelTempBasalPacket(injector)) - // Get history records, this will update the prevoius basals + // Get history records, this will update the prevoius basals // Do not call update status directly, reconnection may be needed commandQueue.loadEvents(object : Callback() { override fun run() { @@ -355,11 +378,6 @@ class MedtrumService : DaggerService(), BLECommCallback { return result } - fun changePump() { - aapsLogger.debug(LTag.PUMP, "changePump: called!") - medtrumPump.loadUserSettingsFromSP() - } - /** This gets the history records from the pump */ private fun syncRecords(): Boolean { aapsLogger.debug(LTag.PUMP, "syncRecords: called!, syncedSequenceNumber: ${medtrumPump.syncedSequenceNumber}, currentSequenceNumber: ${medtrumPump.currentSequenceNumber}") diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumViewModel.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumViewModel.kt index 873e674116..01fb12016a 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumViewModel.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumViewModel.kt @@ -67,6 +67,7 @@ class MedtrumViewModel @Inject constructor( if (patchStep.value == PatchStep.START_DEACTIVATION) { updateSetupStep(SetupStep.READY_DEACTIVATE) } + medtrumPump.lastConnection = System.currentTimeMillis() } ConnectionState.DISCONNECTED -> { diff --git a/pump/medtrum/src/main/res/values/arrays.xml b/pump/medtrum/src/main/res/values/arrays.xml new file mode 100644 index 0000000000..f2184b0875 --- /dev/null +++ b/pump/medtrum/src/main/res/values/arrays.xml @@ -0,0 +1,23 @@ + + + + + + + + Vibrate and beep + Vibrate only + Beep only + None + + + + + + + 4 + 5 + 6 + 7 + + diff --git a/pump/medtrum/src/main/res/values/strings.xml b/pump/medtrum/src/main/res/values/strings.xml index 8049096766..958da20f9d 100644 --- a/pump/medtrum/src/main/res/values/strings.xml +++ b/pump/medtrum/src/main/res/values/strings.xml @@ -1,7 +1,12 @@ - snInput + sn_input + alarm_setting + patch_expiration + hourly_max_insulin + daily_max_insulin + medtrumpump_settings pump_state medtrum_session_token @@ -17,6 +22,7 @@ Pump error: %1$s !! Pump is suspended Patch not activated + Setting user settings to pump failed! BLE Status @@ -45,13 +51,22 @@ Deactivation - SN - Serial number pump base + Serial Number + Enter the serial number of your pump base. + Alarm Settings + Select your preferred pump alarm settings. + Patch Expiration + Turn this on to get an alert 12 hours before the patch expiration. When enabled, the patch will expire after 3 days. + Hourly Maximum Insulin + Specify the maximum units of insulin allowed per hour. If exceeded, the pump will suspend. + Daily Maximum Insulin + Specify the maximum units of insulin allowed per day. If exceeded, the pump will suspend. Waiting for bolus end. Remaining %1$d sec. Getting pump status Getting bolus status Getting temporary basal status + Setting user options 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 9b40fc9864..c87d2384d2 100644 --- a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml +++ b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml @@ -1,6 +1,6 @@ - + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:validate="http://schemas.android.com/apk/res-auto"> + android:title="@string/sn_input_title" /> + + + + + + + + + - \ No newline at end of file + diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacketTest.kt index 7a29b2e516..56c82c95a4 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/ActivatePacketTest.kt @@ -26,7 +26,7 @@ class ActivatePacketTest : MedtrumTestBase() { @Test fun getRequestGivenPacketWhenValuesSetThenReturnCorrectByteArray() { // Inputs medtrumPump.desiredPatchExpiration = true - medtrumPump.desiredAlarmSetting = AlarmSetting.BEEP_ONLY.code + medtrumPump.desiredAlarmSetting = AlarmSetting.BEEP_ONLY medtrumPump.desiredDailyMaxInsulin = 40 medtrumPump.desiredDailyMaxInsulin = 180 @@ -44,7 +44,7 @@ class ActivatePacketTest : MedtrumTestBase() { @Test fun handleResponseGivenPacketWhenValuesSetThenReturnCorrectValues() { // Inputs medtrumPump.desiredPatchExpiration = true - medtrumPump.desiredAlarmSetting = AlarmSetting.BEEP_ONLY.code + medtrumPump.desiredAlarmSetting = AlarmSetting.BEEP_ONLY medtrumPump.desiredDailyMaxInsulin = 40 medtrumPump.desiredDailyMaxInsulin = 180 diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacketTest.kt index eb15c6f032..80bdf591fd 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetPatchPacketTest.kt @@ -23,7 +23,7 @@ class SetPatchPacketTest : MedtrumTestBase() { @Test fun getRequestGivenValuesWhenCalledThenReturnValidArray() { // Inputs medtrumPump.desiredPatchExpiration = false - medtrumPump.desiredAlarmSetting = AlarmSetting.LIGHT_AND_VIBRATE.code + medtrumPump.desiredAlarmSetting = AlarmSetting.LIGHT_AND_VIBRATE medtrumPump.desiredDailyMaxInsulin = 40 medtrumPump.desiredDailyMaxInsulin = 180 @@ -32,7 +32,7 @@ class SetPatchPacketTest : MedtrumTestBase() { val result = packet.getRequest() // Expected values - val expected = byteArrayOf(35, 1, 32, 3, 16, 14, 0, 0, 0, 0, 0, 0) + val expected = byteArrayOf(35, 1, 32, 3, 16, 14, 0, 0, 12, 0, 0, 30) assertEquals(expected.contentToString(), result.contentToString()) } }