From 194cecce167e79f534e49aa2f15ffffae73ad988 Mon Sep 17 00:00:00 2001
From: jbr7rr <>
Date: Sun, 21 Jan 2024 13:20:10 +0100
Subject: [PATCH] Show ref no instead of raw pumpType value
---
.../nightscout/pump/medtrum/MedtrumPlugin.kt | 7 +++--
.../nightscout/pump/medtrum/MedtrumPump.kt | 11 ++++---
.../pump/medtrum/comm/enums/ModelType.kt | 17 ++++++++++
.../pump/medtrum/services/MedtrumService.kt | 2 +-
.../ui/viewmodel/MedtrumOverviewViewModel.kt | 3 +-
.../pump/medtrum/util/MedtrumSnUtil.kt | 31 ++++++-------------
pump/medtrum/src/main/res/values/strings.xml | 2 +-
.../pump/medtrum/MedtrumPumpTest.kt | 23 +++++++-------
8 files changed, 52 insertions(+), 44 deletions(-)
create mode 100644 pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/ModelType.kt
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 bf15b3a240..76abaee1f7 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
@@ -53,6 +53,7 @@ import app.aaps.core.ui.toast.ToastUtils
import app.aaps.core.validators.ValidatingEditTextPreference
import dagger.android.HasAndroidInjector
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
+import info.nightscout.pump.medtrum.comm.enums.ModelType
import info.nightscout.pump.medtrum.services.MedtrumService
import info.nightscout.pump.medtrum.ui.MedtrumOverviewFragment
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
@@ -153,7 +154,7 @@ import kotlin.math.abs
override fun afterTextChanged(newValue: Editable?) {
val newSN = newValue?.toString()?.toLongOrNull(radix = 16) ?: 0
val newDeviceType = MedtrumSnUtil().getDeviceTypeFromSerial(newSN)
- editText.error = if (newDeviceType == MedtrumSnUtil.INVALID) {
+ editText.error = if (newDeviceType == ModelType.INVALID) {
rh.gs(R.string.sn_input_invalid)
} else {
null
@@ -174,7 +175,7 @@ import kotlin.math.abs
val newDeviceType = MedtrumSnUtil().getDeviceTypeFromSerial(newSN)
when {
- newDeviceType == MedtrumSnUtil.INVALID -> {
+ newDeviceType == ModelType.INVALID -> {
preferenceFragment.activity?.let { activity ->
OKDialog.show(activity, rh.gs(R.string.sn_input_title), rh.gs(R.string.sn_input_invalid))
}
@@ -183,7 +184,7 @@ import kotlin.math.abs
medtrumPump.pumpType(newDeviceType) == PumpType.MEDTRUM_UNTESTED -> {
preferenceFragment.activity?.let { activity ->
- OKDialog.show(activity, rh.gs(R.string.sn_input_title), rh.gs(R.string.pump_unsupported, newDeviceType))
+ OKDialog.show(activity, rh.gs(R.string.sn_input_title), rh.gs(R.string.pump_unsupported, newDeviceType.toString()))
}
false
}
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 91223a9cab..dbea336d27 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
@@ -18,6 +18,7 @@ import info.nightscout.pump.medtrum.comm.enums.AlarmSetting
import info.nightscout.pump.medtrum.comm.enums.AlarmState
import info.nightscout.pump.medtrum.comm.enums.BasalType
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
+import info.nightscout.pump.medtrum.comm.enums.ModelType
import info.nightscout.pump.medtrum.extension.toByteArray
import info.nightscout.pump.medtrum.extension.toInt
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
@@ -291,13 +292,13 @@ class MedtrumPump @Inject constructor(
var desiredPumpWarning = true
var desiredPumpWarningExpiryThresholdHours = 72L
- fun pumpType(): PumpType = pumpType(deviceType)
+ fun pumpType(): PumpType = pumpType(ModelType.fromValue(deviceType))
- fun pumpType(type: Int): PumpType =
+ fun pumpType(type: ModelType): PumpType =
when (type) {
- MedtrumSnUtil.MD_0201, MedtrumSnUtil.MD_8201 -> PumpType.MEDTRUM_NANO
- MedtrumSnUtil.MD_8301 -> PumpType.MEDTRUM_300U
- else -> PumpType.MEDTRUM_UNTESTED
+ ModelType.MD0201, ModelType.MD8201 -> PumpType.MEDTRUM_NANO
+ ModelType.MD8301 -> PumpType.MEDTRUM_300U
+ else -> PumpType.MEDTRUM_UNTESTED
}
fun loadVarsFromSP() {
diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/ModelType.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/ModelType.kt
new file mode 100644
index 0000000000..da1afde177
--- /dev/null
+++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/ModelType.kt
@@ -0,0 +1,17 @@
+package info.nightscout.pump.medtrum.comm.enums
+
+enum class ModelType(val value: Int) {
+ INVALID(-1),
+ MD0201(80),
+ MD5201(81),
+ MD0202(82),
+ MD5202(83),
+ MD8201(88),
+ MD8301(98);
+
+ companion object {
+ fun fromValue(value: Int): ModelType {
+ return values().find { it.value == value } ?: INVALID
+ }
+ }
+}
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 4d9d52dd38..8a0153ae54 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
@@ -132,7 +132,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
if (event.isChanged(rh.gs(R.string.key_sn_input))) {
aapsLogger.debug(LTag.PUMPCOMM, "Serial number changed, reporting new pump!")
medtrumPump.loadUserSettingsFromSP()
- medtrumPump.deviceType = MedtrumSnUtil().getDeviceTypeFromSerial(medtrumPump.pumpSN)
+ medtrumPump.deviceType = MedtrumSnUtil().getDeviceTypeFromSerial(medtrumPump.pumpSN).value
medtrumPump.resetPatchParameters()
pumpSync.connectNewPump()
medtrumPump.setFakeTBRIfNotSet()
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 decdcf3745..c01f23fbce 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
@@ -14,6 +14,7 @@ import info.nightscout.pump.medtrum.R
import info.nightscout.pump.medtrum.code.ConnectionState
import info.nightscout.pump.medtrum.code.EventType
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
+import info.nightscout.pump.medtrum.comm.enums.ModelType
import info.nightscout.pump.medtrum.ui.MedtrumBaseNavigator
import info.nightscout.pump.medtrum.ui.event.SingleLiveEvent
import info.nightscout.pump.medtrum.ui.event.UIEvent
@@ -199,7 +200,7 @@ class MedtrumOverviewViewModel @Inject constructor(
val activeAlarmStrings = medtrumPump.activeAlarms.map { medtrumPump.alarmStateToString(it) }
_activeAlarms.postValue(activeAlarmStrings.joinToString("\n"))
- _pumpType.postValue(medtrumPump.deviceType.toString())
+ _pumpType.postValue(ModelType.fromValue(medtrumPump.deviceType).toString())
_fwVersion.postValue(medtrumPump.swVersion)
_patchNo.postValue(medtrumPump.patchId.toString())
diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumSnUtil.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumSnUtil.kt
index ad16adc630..7f133abbfa 100644
--- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumSnUtil.kt
+++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumSnUtil.kt
@@ -1,33 +1,20 @@
package info.nightscout.pump.medtrum.util
+import info.nightscout.pump.medtrum.comm.enums.ModelType
import info.nightscout.pump.medtrum.encryption.Crypt
class MedtrumSnUtil {
- companion object {
-
- const val INVALID = -1
- const val MD_0201 = 80
- const val MD_5201 = 81
- const val MD_0202 = 82
- const val MD_5202 = 83
- const val MD_8201 = 88
- const val MD_8301 = 98
- }
-
- fun getDeviceTypeFromSerial(serial: Long): Int {
- if (serial in 106000000..106999999) {
- return INVALID
- }
+ fun getDeviceTypeFromSerial(serial: Long): ModelType {
return when (Crypt().simpleDecrypt(serial)) {
- in 126000000..126999999 -> MD_0201
- in 127000000..127999999 -> MD_5201
- in 128000000..128999999 -> MD_8201
- in 130000000..130999999 -> MD_0202
- in 131000000..131999999 -> MD_5202
- in 148000000..148999999 -> MD_8301
- else -> INVALID
+ in 126000000..126999999 -> ModelType.MD0201
+ in 127000000..127999999 -> ModelType.MD5201
+ in 128000000..128999999 -> ModelType.MD8201
+ in 130000000..130999999 -> ModelType.MD0202
+ in 131000000..131999999 -> ModelType.MD5202
+ in 148000000..148999999 -> ModelType.MD8301
+ else -> ModelType.INVALID
}
}
}
diff --git a/pump/medtrum/src/main/res/values/strings.xml b/pump/medtrum/src/main/res/values/strings.xml
index d7e473b17b..608c18b437 100644
--- a/pump/medtrum/src/main/res/values/strings.xml
+++ b/pump/medtrum/src/main/res/values/strings.xml
@@ -141,7 +141,7 @@
Serial Number
Enter the serial number of your pump base.
Invalid serial number!
- Pump untested: %1$d! Please contact us at discord or github for support
+ Pump untested: %1$s! Please contact us at discord or github for support
Alarm Settings
Select your preferred pump alarm settings.
Notification on pump warning
diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumPumpTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumPumpTest.kt
index 5ba7ff5227..78ca63cfe6 100644
--- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumPumpTest.kt
+++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumPumpTest.kt
@@ -9,6 +9,7 @@ import app.aaps.core.interfaces.rx.events.EventOverviewBolusProgress
import app.aaps.core.interfaces.utils.T
import com.google.common.truth.Truth.assertThat
import info.nightscout.pump.medtrum.comm.enums.BasalType
+import info.nightscout.pump.medtrum.comm.enums.ModelType
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
import org.json.JSONObject
import org.junit.jupiter.api.Test
@@ -139,7 +140,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val receivedTime = 1500L
val duration = T.mins(5).msecs()
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -191,7 +192,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -240,7 +241,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -293,7 +294,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val duration = T.mins(5).msecs()
medtrumPump.actualBasalProfile = medtrumPump.buildMedtrumProfileArray(validProfile)!!
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -346,7 +347,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -441,7 +442,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -492,7 +493,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
Mockito.`when`(pumpSync.expectedPumpState()).thenReturn(
@@ -537,7 +538,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -581,7 +582,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
val basalStartTime = 1000L
val receivedTime = 1500L
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
Mockito.`when`(pumpSync.expectedPumpState()).thenReturn(
@@ -650,7 +651,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
@Test fun setFakeTBRIfNotSetWhenNoFakeTBRAlreadyRunningExpectPumpSync() {
// Inputs
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)
@@ -684,7 +685,7 @@ class MedtrumPumpTest : MedtrumTestBase() {
@Test fun setFakeTBRIfNotSetWhenFakeTBRAlreadyRunningExpectNoPumpSync() {
// Inputs
- medtrumPump.deviceType = MedtrumSnUtil.MD_8301
+ medtrumPump.deviceType = ModelType.MD8301.value
// Mocks
val expectedTemporaryBasal: PumpSync.PumpState.TemporaryBasal = mock(PumpSync.PumpState.TemporaryBasal::class.java)