Merge pull request #2683 from jbr7rr/medtrum-improvements
Medtrum: Handle pumpType when SN is entered
This commit is contained in:
commit
b5a47af79a
|
@ -5,11 +5,14 @@ import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.ServiceConnection
|
import android.content.ServiceConnection
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.TextWatcher
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
import dagger.android.HasAndroidInjector
|
import dagger.android.HasAndroidInjector
|
||||||
|
import info.nightscout.core.ui.dialogs.OKDialog
|
||||||
import info.nightscout.core.ui.toast.ToastUtils
|
import info.nightscout.core.ui.toast.ToastUtils
|
||||||
import info.nightscout.core.utils.fabric.FabricPrivacy
|
import info.nightscout.core.utils.fabric.FabricPrivacy
|
||||||
import info.nightscout.interfaces.constraints.Constraint
|
import info.nightscout.interfaces.constraints.Constraint
|
||||||
|
@ -39,6 +42,7 @@ import info.nightscout.interfaces.utils.TimeChangeType
|
||||||
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
||||||
import info.nightscout.pump.medtrum.ui.MedtrumOverviewFragment
|
import info.nightscout.pump.medtrum.ui.MedtrumOverviewFragment
|
||||||
import info.nightscout.pump.medtrum.services.MedtrumService
|
import info.nightscout.pump.medtrum.services.MedtrumService
|
||||||
|
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||||
import info.nightscout.rx.AapsSchedulers
|
import info.nightscout.rx.AapsSchedulers
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventAppExit
|
import info.nightscout.rx.events.EventAppExit
|
||||||
|
@ -123,7 +127,54 @@ import kotlin.math.abs
|
||||||
|
|
||||||
override fun preprocessPreferences(preferenceFragment: PreferenceFragmentCompat) {
|
override fun preprocessPreferences(preferenceFragment: PreferenceFragmentCompat) {
|
||||||
super.preprocessPreferences(preferenceFragment)
|
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?.setOnBindEditTextListener { editText ->
|
||||||
|
editText.addTextChangedListener(object : TextWatcher {
|
||||||
|
override fun afterTextChanged(newValue: Editable?) {
|
||||||
|
val newSN = newValue.toString().toLongOrNull(radix = 16)
|
||||||
|
val newDeviceType = MedtrumSnUtil().getDeviceTypeFromSerial(newSN ?: 0)
|
||||||
|
if (newDeviceType == MedtrumSnUtil.INVALID) {
|
||||||
|
editText.error = rh.gs(R.string.sn_input_invalid)
|
||||||
|
} else {
|
||||||
|
editText.error = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||||
|
// Nothing to do here
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||||
|
// Nothing to do here
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
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 alarmSetting = preferenceFragment.findPreference<ListPreference>(rh.gs(R.string.key_alarm_setting))
|
||||||
val allAlarmEntries = preferenceFragment.resources.getStringArray(R.array.alarmSettings)
|
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.comm.enums.MedtrumPumpState
|
||||||
import info.nightscout.pump.medtrum.extension.toByteArray
|
import info.nightscout.pump.medtrum.extension.toByteArray
|
||||||
import info.nightscout.pump.medtrum.extension.toInt
|
import info.nightscout.pump.medtrum.extension.toInt
|
||||||
|
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||||
import info.nightscout.rx.events.EventOverviewBolusProgress
|
import info.nightscout.rx.events.EventOverviewBolusProgress
|
||||||
import info.nightscout.rx.logging.AAPSLogger
|
import info.nightscout.rx.logging.AAPSLogger
|
||||||
import info.nightscout.rx.logging.LTag
|
import info.nightscout.rx.logging.LTag
|
||||||
|
@ -170,7 +171,7 @@ class MedtrumPump @Inject constructor(
|
||||||
sp.putLong(R.string.key_last_connection, value)
|
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
|
var deviceType: Int
|
||||||
get() = _deviceType
|
get() = _deviceType
|
||||||
set(value) {
|
set(value) {
|
||||||
|
@ -280,13 +281,15 @@ class MedtrumPump @Inject constructor(
|
||||||
try {
|
try {
|
||||||
_actualBasalProfile = Base64.decode(encodedString, Base64.DEFAULT)
|
_actualBasalProfile = Base64.decode(encodedString, Base64.DEFAULT)
|
||||||
} catch (e: Exception) {
|
} 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 =
|
fun pumpType(): PumpType = pumpType(deviceType)
|
||||||
when (deviceType) {
|
|
||||||
80, 88 -> PumpType.MEDTRUM_NANO
|
fun pumpType(type: Int): PumpType =
|
||||||
|
when (type) {
|
||||||
|
MedtrumSnUtil.MD_0201, MedtrumSnUtil.MD_8201 -> PumpType.MEDTRUM_NANO
|
||||||
else -> PumpType.MEDTRUM_UNTESTED
|
else -> PumpType.MEDTRUM_UNTESTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ class AuthorizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) {
|
||||||
).toInt()
|
).toInt()
|
||||||
|
|
||||||
if (medtrumPump.deviceType != deviceType) {
|
if (medtrumPump.deviceType != deviceType) {
|
||||||
|
aapsLogger.warn(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType changed from ${medtrumPump.deviceType} to $deviceType")
|
||||||
medtrumPump.deviceType = deviceType
|
medtrumPump.deviceType = deviceType
|
||||||
}
|
}
|
||||||
if (medtrumPump.swVersion != swVersion) {
|
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.AlarmState
|
||||||
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState
|
||||||
import info.nightscout.pump.medtrum.comm.packets.*
|
import info.nightscout.pump.medtrum.comm.packets.*
|
||||||
|
import info.nightscout.pump.medtrum.util.MedtrumSnUtil
|
||||||
import info.nightscout.rx.AapsSchedulers
|
import info.nightscout.rx.AapsSchedulers
|
||||||
import info.nightscout.rx.bus.RxBus
|
import info.nightscout.rx.bus.RxBus
|
||||||
import info.nightscout.rx.events.EventAppExit
|
import info.nightscout.rx.events.EventAppExit
|
||||||
|
@ -109,8 +110,9 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
.subscribe({ event ->
|
.subscribe({ event ->
|
||||||
if (event.isChanged(rh.gs(R.string.key_sn_input))) {
|
if (event.isChanged(rh.gs(R.string.key_sn_input))) {
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "Serial number changed, reporting new pump!")
|
aapsLogger.debug(LTag.PUMPCOMM, "Serial number changed, reporting new pump!")
|
||||||
pumpSync.connectNewPump()
|
|
||||||
medtrumPump.loadUserSettingsFromSP()
|
medtrumPump.loadUserSettingsFromSP()
|
||||||
|
medtrumPump.deviceType = MedtrumSnUtil().getDeviceTypeFromSerial(medtrumPump.pumpSN)
|
||||||
|
pumpSync.connectNewPump()
|
||||||
medtrumPump.setFakeTBRIfNeeded()
|
medtrumPump.setFakeTBRIfNeeded()
|
||||||
}
|
}
|
||||||
if (event.isChanged(rh.gs(R.string.key_alarm_setting))
|
if (event.isChanged(rh.gs(R.string.key_alarm_setting))
|
||||||
|
@ -752,21 +754,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Success!
|
// Success!
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = 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())
|
toState(GetDeviceTypeState())
|
||||||
}
|
|
||||||
} else if (mPacket?.failed == true) {
|
} else if (mPacket?.failed == true) {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
|
|
|
@ -29,7 +29,7 @@ class MedtrumActivity : MedtrumBaseActivity<ActivityMedtrumBinding>() {
|
||||||
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
|
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
|
||||||
title = getString(R.string.step_prepare_patch)
|
title = getString(R.string.change_patch_label)
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
supportActionBar?.setDisplayShowHomeEnabled(true)
|
supportActionBar?.setDisplayShowHomeEnabled(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_description">Pump integration for Medtrum Nano</string>
|
||||||
<string name="medtrum_pump_setting">Medtrum pump settings</string>
|
<string name="medtrum_pump_setting">Medtrum pump settings</string>
|
||||||
<string name="pump_error">Pump error: %1$s !! </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">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_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>
|
<string name="pump_is_suspended_day_max">Pump is suspended due to daily max insulin exceeded</string>
|
||||||
|
@ -51,7 +50,7 @@
|
||||||
<string name="patch_expiry_label">Patch expires</string>
|
<string name="patch_expiry_label">Patch expires</string>
|
||||||
<string name="refresh_label">Refresh</string>
|
<string name="refresh_label">Refresh</string>
|
||||||
<string name="reset_alarms_label">Reset alarms</string>
|
<string name="reset_alarms_label">Reset alarms</string>
|
||||||
<string name="change_patch_label">Change patch</string>
|
<string name="change_patch_label">Change Patch</string>
|
||||||
<string name="requested_by_user" comment="26 characters max for translation">Requested by user</string>
|
<string name="requested_by_user" comment="26 characters max for translation">Requested by user</string>
|
||||||
<string name="expiry_not_enabled">Not enabled</string>
|
<string name="expiry_not_enabled">Not enabled</string>
|
||||||
|
|
||||||
|
@ -131,6 +130,8 @@
|
||||||
<!-- settings-->
|
<!-- settings-->
|
||||||
<string name="sn_input_title">Serial Number</string>
|
<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_summary">Enter the serial number of your pump base.</string>
|
||||||
|
<string name="sn_input_invalid">Invalid serial number!</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_title">Alarm Settings</string>
|
||||||
<string name="alarm_setting_summary">Select your preferred pump alarm settings.</string>
|
<string name="alarm_setting_summary">Select your preferred pump alarm settings.</string>
|
||||||
<string name="patch_expiration_title">Patch Expiration</string>
|
<string name="patch_expiration_title">Patch Expiration</string>
|
||||||
|
|
Loading…
Reference in a new issue