Make sure pumpType is set correctly before connectNewPump() is called,
Move check for untested pump to settings
This commit is contained in:
parent
aef65a2d17
commit
9ef3b6fa2d
6 changed files with 99 additions and 45 deletions
|
@ -10,6 +10,7 @@ import androidx.preference.EditTextPreference
|
|||
import androidx.preference.ListPreference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import dagger.android.HasAndroidInjector
|
||||
import info.nightscout.core.ui.dialogs.OKDialog
|
||||
import info.nightscout.core.ui.toast.ToastUtils
|
||||
import info.nightscout.core.utils.fabric.FabricPrivacy
|
||||
import info.nightscout.interfaces.constraints.Constraint
|
||||
|
@ -39,6 +40,7 @@ import info.nightscout.interfaces.utils.TimeChangeType
|
|||
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
||||
import info.nightscout.pump.medtrum.ui.MedtrumOverviewFragment
|
||||
import info.nightscout.pump.medtrum.services.MedtrumService
|
||||
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||
import info.nightscout.rx.AapsSchedulers
|
||||
import info.nightscout.rx.bus.RxBus
|
||||
import info.nightscout.rx.events.EventAppExit
|
||||
|
@ -123,7 +125,33 @@ import kotlin.math.abs
|
|||
|
||||
override fun preprocessPreferences(preferenceFragment: PreferenceFragmentCompat) {
|
||||
super.preprocessPreferences(preferenceFragment)
|
||||
preferenceFragment.findPreference<EditTextPreference>(rh.gs(R.string.key_sn_input))?.isEnabled = !isInitialized()
|
||||
val serialSetting = preferenceFragment.findPreference<EditTextPreference>(rh.gs(R.string.key_sn_input))
|
||||
serialSetting?.isEnabled = !isInitialized()
|
||||
serialSetting?.setOnPreferenceChangeListener { _, newValue ->
|
||||
if (newValue is String) {
|
||||
val newSN = newValue.toLongOrNull(radix = 16)
|
||||
val newDeviceType = MedtrumSnUtil().getDeviceTypeFromSerial(newSN ?: 0)
|
||||
when {
|
||||
newDeviceType == MedtrumSnUtil.INVALID -> {
|
||||
preferenceFragment.activity?.let { activity ->
|
||||
OKDialog.show(activity, rh.gs(R.string.sn_input_title), rh.gs(R.string.sn_input_invalid))
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
else -> true
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
val alarmSetting = preferenceFragment.findPreference<ListPreference>(rh.gs(R.string.key_alarm_setting))
|
||||
val allAlarmEntries = preferenceFragment.resources.getStringArray(R.array.alarmSettings)
|
||||
|
|
|
@ -12,6 +12,7 @@ import info.nightscout.pump.medtrum.comm.enums.BasalType
|
|||
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
||||
import info.nightscout.pump.medtrum.extension.toByteArray
|
||||
import info.nightscout.pump.medtrum.extension.toInt
|
||||
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||
import info.nightscout.rx.events.EventOverviewBolusProgress
|
||||
import info.nightscout.rx.logging.AAPSLogger
|
||||
import info.nightscout.rx.logging.LTag
|
||||
|
@ -170,7 +171,7 @@ class MedtrumPump @Inject constructor(
|
|||
sp.putLong(R.string.key_last_connection, value)
|
||||
}
|
||||
|
||||
private var _deviceType: Int = 80 // As reported by pump
|
||||
private var _deviceType: Int = 0 // As reported by pump
|
||||
var deviceType: Int
|
||||
get() = _deviceType
|
||||
set(value) {
|
||||
|
@ -280,13 +281,15 @@ class MedtrumPump @Inject constructor(
|
|||
try {
|
||||
_actualBasalProfile = Base64.decode(encodedString, Base64.DEFAULT)
|
||||
} catch (e: Exception) {
|
||||
aapsLogger.error(LTag.PUMP, "Error decoding basal profile from SP: $encodedString")
|
||||
aapsLogger.warn(LTag.PUMP, "Error decoding basal profile from SP: $encodedString")
|
||||
}
|
||||
}
|
||||
|
||||
fun pumpType(): PumpType =
|
||||
when (deviceType) {
|
||||
80, 88 -> PumpType.MEDTRUM_NANO
|
||||
fun pumpType(): PumpType = pumpType(deviceType)
|
||||
|
||||
fun pumpType(type: Int): PumpType =
|
||||
when (type) {
|
||||
MedtrumSnUtil.MD_0201, MedtrumSnUtil.MD_8201 -> PumpType.MEDTRUM_NANO
|
||||
else -> PumpType.MEDTRUM_UNTESTED
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,7 @@ class AuthorizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) {
|
|||
).toInt()
|
||||
|
||||
if (medtrumPump.deviceType != deviceType) {
|
||||
aapsLogger.warn(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType changed from ${medtrumPump.deviceType} to $deviceType")
|
||||
medtrumPump.deviceType = deviceType
|
||||
}
|
||||
if (medtrumPump.swVersion != swVersion) {
|
||||
|
|
|
@ -29,6 +29,7 @@ import info.nightscout.pump.medtrum.code.ConnectionState
|
|||
import info.nightscout.pump.medtrum.comm.enums.AlarmState
|
||||
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
||||
import info.nightscout.pump.medtrum.comm.packets.*
|
||||
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||
import info.nightscout.rx.AapsSchedulers
|
||||
import info.nightscout.rx.bus.RxBus
|
||||
import info.nightscout.rx.events.EventAppExit
|
||||
|
@ -109,8 +110,9 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
|||
.subscribe({ event ->
|
||||
if (event.isChanged(rh.gs(R.string.key_sn_input))) {
|
||||
aapsLogger.debug(LTag.PUMPCOMM, "Serial number changed, reporting new pump!")
|
||||
pumpSync.connectNewPump()
|
||||
medtrumPump.loadUserSettingsFromSP()
|
||||
medtrumPump.deviceType = MedtrumSnUtil().getDeviceTypeFromSerial(medtrumPump.pumpSN)
|
||||
pumpSync.connectNewPump()
|
||||
medtrumPump.setFakeTBRIfNeeded()
|
||||
}
|
||||
if (event.isChanged(rh.gs(R.string.key_alarm_setting))
|
||||
|
@ -752,21 +754,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
|||
// Success!
|
||||
responseHandled = true
|
||||
responseSuccess = true
|
||||
// Check if we have a supported pump
|
||||
if (medtrumPump.pumpType() == PumpType.MEDTRUM_UNTESTED) {
|
||||
// Throw error
|
||||
aapsLogger.error(LTag.PUMPCOMM, "Unsupported pump type")
|
||||
uiInteraction.addNotificationWithSound(
|
||||
Notification.PUMP_ERROR,
|
||||
rh.gs(R.string.pump_unsupported, medtrumPump.deviceType),
|
||||
Notification.URGENT,
|
||||
info.nightscout.core.ui.R.raw.alarm
|
||||
)
|
||||
disconnect("Unsupported pump")
|
||||
toState(IdleState())
|
||||
} else {
|
||||
toState(GetDeviceTypeState())
|
||||
}
|
||||
} else if (mPacket?.failed == true) {
|
||||
// Failure
|
||||
responseHandled = true
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package info.nightscout.pump.medtrum.util
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,7 +28,6 @@
|
|||
<string name="medtrum_pump_description">Pump integration for Medtrum Nano</string>
|
||||
<string name="medtrum_pump_setting">Medtrum pump settings</string>
|
||||
<string name="pump_error">Pump error: %1$s !! </string>
|
||||
<string name="pump_unsupported">Pump untested: %1$d! Please contact us at discord or github for support</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>
|
||||
|
@ -131,6 +130,8 @@
|
|||
<!-- settings-->
|
||||
<string name="sn_input_title">Serial Number</string>
|
||||
<string name="sn_input_summary">Enter the serial number of your pump base.</string>
|
||||
<string name="sn_input_invalid">Invalid serial number! Check serial number and try again.</string>
|
||||
<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="patch_expiration_title">Patch Expiration</string>
|
||||
|
|
Loading…
Reference in a new issue