From 275a7e39d822f0ed37f3ea6dabcccb06a0349e9f Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Mon, 14 Aug 2023 21:10:04 +0200 Subject: [PATCH 1/3] Medtrum: Remove unused, typo's and other lints --- .../nightscout/pump/medtrum/MedtrumPlugin.kt | 4 +- .../nightscout/pump/medtrum/MedtrumPump.kt | 26 ++--- .../bindingadapters/ViewBindingAdapter.kt | 18 --- .../pump/medtrum/comm/ManufacturerData.kt | 4 +- .../pump/medtrum/comm/WriteCommandPackets.kt | 13 +-- .../pump/medtrum/comm/enums/AlarmState.kt | 8 +- .../medtrum/comm/enums/MedtrumPumpState.kt | 9 +- .../medtrum/comm/packets/ActivatePacket.kt | 4 +- .../comm/packets/CancelTempBasalPacket.kt | 1 - .../comm/packets/ClearPumpAlarmPacket.kt | 2 +- .../medtrum/comm/packets/GetRecordPacket.kt | 23 ++-- .../medtrum/comm/packets/MedtrumPacket.kt | 4 +- .../comm/packets/NotificationPacket.kt | 22 ++-- .../medtrum/comm/packets/ResumePumpPacket.kt | 1 - .../medtrum/comm/packets/SetTimeZonePacket.kt | 10 +- .../medtrum/comm/packets/SynchronizePacket.kt | 3 +- .../pump/medtrum/encryption/Crypt.kt | 4 +- .../medtrum/extension/ByteArrayExtension.kt | 2 +- .../pump/medtrum/extension/ViewExtension.kt | 23 ---- .../pump/medtrum/services/BLEComm.kt | 14 +-- .../pump/medtrum/services/MedtrumService.kt | 105 +++++++++--------- .../medtrum/ui/MedtrumPreparePatchFragment.kt | 1 - .../ui/MedtrumPrimeCompleteFragment.kt | 1 - .../pump/medtrum/ui/MedtrumPrimeFragment.kt | 1 - .../pump/medtrum/ui/MedtrumPrimingFragment.kt | 1 - .../MedtrumRetryActivationConnectFragment.kt | 1 - .../ui/MedtrumRetryActivationFragment.kt | 3 +- .../ui/MedtrumStartDeactivationFragment.kt | 1 - .../medtrum/ui/viewmodel/BaseViewModel.kt | 2 +- .../ui/viewmodel/MedtrumOverviewViewModel.kt | 4 +- .../medtrum/ui/viewmodel/MedtrumViewModel.kt | 13 +-- .../pump/medtrum/util/MedtrumTimeUtil.kt | 10 +- .../src/main/res/layout/activity_medtrum.xml | 3 +- ...fragment_medtrum_deactivation_complete.xml | 18 ++- .../res/layout/fragment_medtrum_overview.xml | 3 +- pump/medtrum/src/main/res/values/strings.xml | 32 +++--- .../src/main/res/xml/pref_medtrum_pump.xml | 4 +- .../androidaps/ProfileStoreObject.kt | 2 +- .../pump/medtrum/MedtrumPumpTest.kt | 6 +- .../pump/medtrum/MedtrumTestBase.kt | 4 +- .../medtrum/comm/WriteCommandPacketsTest.kt | 4 +- .../comm/packets/ActivatePacketTest.kt | 6 +- .../comm/packets/AuthorizePacketTest.kt | 2 +- .../comm/packets/CancelBolusPacketTest.kt | 2 +- .../comm/packets/CancelTempBasalPacketTest.kt | 6 +- .../comm/packets/ClearPumpAlarmPacketTest.kt | 2 +- .../comm/packets/GetDeviceTypePacketTest.kt | 2 +- .../comm/packets/GetRecordPacketTest.kt | 3 +- .../medtrum/comm/packets/GetTimePacketTest.kt | 2 +- .../medtrum/comm/packets/MedtrumPacketTest.kt | 7 +- .../comm/packets/NotificationPacketTest.kt | 5 +- .../comm/packets/PollPatchPacketTest.kt | 2 +- .../medtrum/comm/packets/PrimePacketTest.kt | 2 +- .../comm/packets/ReadBolusStatePacketTest.kt | 2 +- .../medtrum/comm/packets/ResumePumpPacket.kt | 2 +- .../comm/packets/SetBasalProfilePacketTest.kt | 6 +- .../comm/packets/SetBolusMotorPacketTest.kt | 2 +- .../comm/packets/SetBolusPacketTest.kt | 2 +- .../comm/packets/SetPatchPacketTest.kt | 2 +- .../comm/packets/SetTempBasalPacketTest.kt | 2 +- .../medtrum/comm/packets/SetTimePacketTest.kt | 2 +- .../comm/packets/SetTimeZonePacketTest.kt | 10 +- .../comm/packets/StopPatchPacketTest.kt | 3 +- .../comm/packets/SubscribePacketTest.kt | 2 +- .../comm/packets/SynchronizePacketTest.kt | 3 +- .../pump/medtrum/encryption/CryptTest.kt | 4 +- 66 files changed, 217 insertions(+), 280 deletions(-) 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 07446b938c..1b035cc580 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 @@ -295,7 +295,7 @@ import kotlin.math.abs val connectionOK = medtrumService?.setTempBasal(pumpRate, durationInMinutes) ?: false if (connectionOK && medtrumPump.tempBasalInProgress - && Math.abs(medtrumPump.tempBasalAbsoluteRate - pumpRate) <= 0.05 + && abs(medtrumPump.tempBasalAbsoluteRate - pumpRate) <= 0.05 ) { return PumpEnactResult(injector).success(true).enacted(true).duration(durationInMinutes).absolute(medtrumPump.tempBasalAbsoluteRate) @@ -436,7 +436,7 @@ import kotlin.math.abs if (isInitialized()) { commandQueue.updateTime(object : Callback() { override fun run() { - if (this.result.success == false) { + if (!this.result.success) { aapsLogger.error(LTag.PUMP, "Medtrum time update failed") // Only notify here on failure (connection may be failed), service will handle success medtrumService?.timeUpdateNotification(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 fd24934cda..fb1ce89d05 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 @@ -291,11 +291,11 @@ class MedtrumPump @Inject constructor( } fun loadUserSettingsFromSP() { - 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() + desiredPatchExpiration = sp.getBoolean(R.string.key_patch_expiration, false) + val alarmSettingCode = sp.getString(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) + desiredHourlyMaxInsulin = sp.getInt(R.string.key_hourly_max_insulin, 40) + desiredDailyMaxInsulin = sp.getInt(R.string.key_daily_max_insulin, 180) _pumpSN = pumpSNFromSP } @@ -502,12 +502,12 @@ class MedtrumPump @Inject constructor( AlarmState.PUMP_LOW_BATTERY -> R.string.alarm_pump_low_battery AlarmState.PUMP_LOW_RESERVOIR -> R.string.alarm_pump_low_reservoir AlarmState.PUMP_EXPIRES_SOON -> R.string.alarm_pump_expires_soon - AlarmState.LOWBG_SUSPENDED -> R.string.alarm_lowbg_suspended - AlarmState.LOWBG_SUSPENDED2 -> R.string.alarm_lowbg_suspended2 - AlarmState.AUTO_SUSPENDED -> R.string.alarm_auto_suspended - AlarmState.HMAX_SUSPENDED -> R.string.alarm_hmax_suspended - AlarmState.DMAX_SUSPENDED -> R.string.alarm_dmax_suspended - AlarmState.SUSPENDED -> R.string.alarm_suspended + AlarmState.LOW_BG_SUSPENDED -> R.string.alarm_low_bg_suspended + AlarmState.LOW_BG_SUSPENDED2 -> R.string.alarm_low_bg_suspended2 + AlarmState.AUTO_SUSPENDED -> R.string.alarm_auto_suspended + AlarmState.HOURLY_MAX_SUSPENDED -> R.string.alarm_hourly_max_suspended + AlarmState.DAILY_MAX_SUSPENDED -> R.string.alarm_daily_max_suspended + AlarmState.SUSPENDED -> R.string.alarm_suspended AlarmState.PAUSED -> R.string.alarm_paused AlarmState.OCCLUSION -> R.string.alarm_occlusion AlarmState.EXPIRED -> R.string.alarm_expired @@ -528,10 +528,10 @@ class MedtrumPump @Inject constructor( private fun loadActiveAlarms() { val alarmsStr = sp.getString(R.string.key_active_alarms, "") - if (alarmsStr.isNullOrEmpty()) { - activeAlarms = EnumSet.noneOf(AlarmState::class.java) + activeAlarms = if (alarmsStr.isEmpty()) { + EnumSet.noneOf(AlarmState::class.java) } else { - activeAlarms = alarmsStr.split(",") + alarmsStr.split(",") .mapNotNull { AlarmState.values().find { alarm -> alarm.name == it } } .let { EnumSet.copyOf(it) } } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/bindingadapters/ViewBindingAdapter.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/bindingadapters/ViewBindingAdapter.kt index f4b51efe1a..17f5090276 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/bindingadapters/ViewBindingAdapter.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/bindingadapters/ViewBindingAdapter.kt @@ -1,20 +1,7 @@ package info.nightscout.pump.medtrum.bindingadapters import android.view.View -import android.widget.TextView -import androidx.annotation.ColorRes import androidx.databinding.BindingAdapter -import info.nightscout.pump.medtrum.extension.setVisibleOrGone - -@BindingAdapter("android:visibility") -fun setVisibility(view: View, visible: Boolean) { - view.setVisibleOrGone(visible) -} - -@BindingAdapter("visibleOrGone") -fun setVisibleOrGone(view: View, visibleOrGone: Boolean) { - view.setVisibleOrGone(visibleOrGone) -} @BindingAdapter("onSafeClick") fun View.setOnSafeClickListener(clickListener: View.OnClickListener?) { @@ -22,8 +9,3 @@ fun View.setOnSafeClickListener(clickListener: View.OnClickListener?) { setOnClickListener(OnSafeClickListener(it)) } ?: setOnClickListener(null) } - -@BindingAdapter("textColor") -fun setTextColor(view: TextView, @ColorRes colorResId: Int) { - view.setTextColor(view.context.getColor(colorResId)) -} diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/ManufacturerData.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/ManufacturerData.kt index 098d31d984..a6ef2f3ae8 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/ManufacturerData.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/ManufacturerData.kt @@ -3,7 +3,7 @@ package info.nightscout.pump.medtrum.comm import kotlin.experimental.and import info.nightscout.pump.medtrum.extension.toLong -class ManufacturerData(private val manufacturerDataBytes: ByteArray) { +class ManufacturerData(manufacturerDataBytes: ByteArray) { private var deviceID: Long = 0 private var deviceType = 0 private var version = 0 @@ -12,7 +12,7 @@ class ManufacturerData(private val manufacturerDataBytes: ByteArray) { setData(manufacturerDataBytes) } - fun setData(inputData: ByteArray) { + private fun setData(inputData: ByteArray) { var index = 0 val deviceIDBytes: ByteArray = inputData.copyOfRange(index, index + 4) deviceID = deviceIDBytes.toLong() diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/WriteCommandPackets.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/WriteCommandPackets.kt index ae0f18e9f4..31655f3ed9 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/WriteCommandPackets.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/WriteCommandPackets.kt @@ -9,21 +9,20 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) { init { // PackageIndex: 0 initially, if there are multiple packets, for the first packet it is set to 0 (not included in CRC calculation but sent in actual header) - var pkgIndex = 0 - var header = byteArrayOf( + val header = byteArrayOf( (data.size + 4).toByte(), data[0], sequenceNumber.toByte(), - pkgIndex.toByte() + 0.toByte() // pkgIndex ) var tmp: ByteArray = header + data.copyOfRange(1, data.size) - var totalCommand: ByteArray = tmp + calcCrc8(tmp, tmp.size).toByte() + val totalCommand: ByteArray = tmp + calcCrc8(tmp, tmp.size).toByte() if ((totalCommand.size - header.size) <= 15) { packages.add(totalCommand + 0.toByte()) } else { - pkgIndex = 1 + var pkgIndex = 1 var remainingCommand = totalCommand.copyOfRange(4, totalCommand.size) while (remainingCommand.size > 15) { @@ -52,13 +51,13 @@ class WriteCommandPackets(data: ByteArray, sequenceNumber: Int) { } fun allPacketsConsumed(): Boolean { - return !(index < packages.size) + return index >= packages.size } private fun calcCrc8(value: ByteArray, size: Int): Int { var crc8 = 0 for (i in 0 until size) { - crc8 = CRC_8_TABLE[(value[i].toInt() and 255) xor (crc8 and 255)].toInt() and 255 + crc8 = CRC_8_TABLE[(value[i].toInt() and 255) xor (crc8 and 255)] and 255 } return crc8 } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/AlarmState.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/AlarmState.kt index 50ae4e30f6..ebc4fc5601 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/AlarmState.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/AlarmState.kt @@ -5,11 +5,11 @@ enum class AlarmState { PUMP_LOW_BATTERY, // Mapped from error flag 1 PUMP_LOW_RESERVOIR, // Mapped from error flag 2 PUMP_EXPIRES_SOON, // Mapped from error flag 3 - LOWBG_SUSPENDED, // Mapped from pump status 64 - LOWBG_SUSPENDED2, // Mapped from pump status 65 + LOW_BG_SUSPENDED, // Mapped from pump status 64 + LOW_BG_SUSPENDED2, // Mapped from pump status 65 AUTO_SUSPENDED, // Mapped from pump status 66 - HMAX_SUSPENDED, // Mapped from pump status 67 - DMAX_SUSPENDED, // Mapped from pump status 68 + HOURLY_MAX_SUSPENDED, // Mapped from pump status 67 + DAILY_MAX_SUSPENDED, // Mapped from pump status 68 SUSPENDED, // Mapped from pump status 69 PAUSED, // Mapped from pump status 70 OCCLUSION, // Mapped from pump status 96 diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/MedtrumPumpState.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/MedtrumPumpState.kt index b52b9c1793..c17caa6ad9 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/MedtrumPumpState.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/MedtrumPumpState.kt @@ -10,11 +10,11 @@ enum class MedtrumPumpState(val state: Byte) { EJECTED(6), ACTIVE(32), ACTIVE_ALT(33), - LOWBG_SUSPENDED(64), - LOWBG_SUSPENDED2(65), + LOW_BG_SUSPENDED(64), + LOW_BG_SUSPENDED2(65), AUTO_SUSPENDED(66), - HMAX_SUSPENDED(67), - DMAX_SUSPENDED(68), + HOURLY_MAX_SUSPENDED(67), + DAILY_MAX_SUSPENDED(68), SUSPENDED(69), PAUSED(70), OCCLUSION(96), @@ -28,6 +28,7 @@ enum class MedtrumPumpState(val state: Byte) { STOPPED(128.toByte()); companion object { + fun fromByte(state: Byte) = values().find { it.state == state } ?: throw IllegalAccessException("") } 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 838fff9727..8ec37c2e2c 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 @@ -55,7 +55,7 @@ class ActivatePacket(injector: HasAndroidInjector, private val basalProfile: Byt * byte 6: predictiveLowSuspend // Value for auto mode, not used for AAPS * byte 7: predictiveLowSuspendRange // Value for auto mode, not used for AAPS * byte 8-9: hourlyMaxInsulin // Max hourly dose of insulin, divided by 0.05 - * byte 10-11: daylyMaxSet // Max daily dose of insulin, divided by 0.05 + * byte 10-11: dailyMaxSet // Max daily dose of insulin, divided by 0.05 * byte 12-13: tddToday // Current TDD (of present day), divided by 0.05 * byte 14: 1 // Always 1 * bytes 15 - end // Basal profile > see MedtrumPump @@ -98,7 +98,7 @@ class ActivatePacket(injector: HasAndroidInjector, private val basalProfile: Byt medtrumPump.currentSequenceNumber = basalSequence // We are activated, set the new seq nr medtrumPump.syncedSequenceNumber = basalSequence // We are activated, reset the synced seq nr () - // Sync canula change + // Sync cannula change pumpSync.insertTherapyEventIfNewWithTimestamp( timestamp = System.currentTimeMillis(), type = DetailedBolusInfo.EventType.CANNULA_CHANGE, diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/CancelTempBasalPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/CancelTempBasalPacket.kt index f6b54aaa18..e271d53db7 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/CancelTempBasalPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/CancelTempBasalPacket.kt @@ -7,7 +7,6 @@ import info.nightscout.pump.medtrum.comm.enums.BasalType import info.nightscout.pump.medtrum.extension.toInt import info.nightscout.pump.medtrum.extension.toLong import info.nightscout.pump.medtrum.util.MedtrumTimeUtil -import info.nightscout.rx.logging.LTag import info.nightscout.shared.utils.DateUtil import javax.inject.Inject 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 index 15f860fc50..023572873b 100644 --- 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 @@ -4,7 +4,7 @@ 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) { +class ClearPumpAlarmPacket(injector: HasAndroidInjector, private val clearType: Int) : MedtrumPacket(injector) { init { opCode = CLEAR_ALARM.code diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt index ad127fed14..3c3757f66e 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt @@ -15,7 +15,6 @@ import info.nightscout.pump.medtrum.extension.toLong import info.nightscout.pump.medtrum.util.MedtrumTimeUtil import info.nightscout.rx.logging.LTag import info.nightscout.shared.utils.DateUtil -import info.nightscout.shared.utils.T import javax.inject.Inject class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int) : MedtrumPacket(injector) { @@ -38,9 +37,9 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int private const val RESP_RECORD_UNKNOWN1_END = RESP_RECORD_UNKNOWN1_START + 1 private const val RESP_RECORD_SERIAL_START = RESP_RECORD_UNKNOWN1_END private const val RESP_RECORD_SERIAL_END = RESP_RECORD_SERIAL_START + 4 - private const val RESP_RECORD_PATCHID_START = RESP_RECORD_SERIAL_END - private const val RESP_RECORD_PATCHID_END = RESP_RECORD_PATCHID_START + 2 - private const val RESP_RECORD_SEQUENCE_START = RESP_RECORD_PATCHID_END + private const val RESP_RECORD_PATCH_ID_START = RESP_RECORD_SERIAL_END + private const val RESP_RECORD_PATCH_ID_END = RESP_RECORD_PATCH_ID_START + 2 + private const val RESP_RECORD_SEQUENCE_START = RESP_RECORD_PATCH_ID_END private const val RESP_RECORD_SEQUENCE_END = RESP_RECORD_SEQUENCE_START + 2 private const val RESP_RECORD_DATA_START = RESP_RECORD_SEQUENCE_END @@ -75,7 +74,7 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int val recordUnknown = data.copyOfRange(RESP_RECORD_UNKNOWN_START, RESP_RECORD_UNKNOWN_END).toInt() val recordType = data.copyOfRange(RESP_RECORD_TYPE_START, RESP_RECORD_TYPE_END).toInt() val recordSerial = data.copyOfRange(RESP_RECORD_SERIAL_START, RESP_RECORD_SERIAL_END).toLong() - val recordPatchId = data.copyOfRange(RESP_RECORD_PATCHID_START, RESP_RECORD_PATCHID_END).toInt() + val recordPatchId = data.copyOfRange(RESP_RECORD_PATCH_ID_START, RESP_RECORD_PATCH_ID_END).toInt() val recordSequence = data.copyOfRange(RESP_RECORD_SEQUENCE_START, RESP_RECORD_SEQUENCE_END).toInt() aapsLogger.debug( @@ -101,13 +100,15 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int val bolusCarb = data.copyOfRange(RESP_RECORD_DATA_START + 18, RESP_RECORD_DATA_START + 20).toInt() val bolusGlucose = data.copyOfRange(RESP_RECORD_DATA_START + 20, RESP_RECORD_DATA_START + 22).toInt() val bolusIOB = data.copyOfRange(RESP_RECORD_DATA_START + 22, RESP_RECORD_DATA_START + 24).toInt() - val unkown1 = data.copyOfRange(RESP_RECORD_DATA_START + 24, RESP_RECORD_DATA_START + 26).toInt() - val unkown2 = data.copyOfRange(RESP_RECORD_DATA_START + 26, RESP_RECORD_DATA_START + 28).toInt() + val unknown1 = data.copyOfRange(RESP_RECORD_DATA_START + 24, RESP_RECORD_DATA_START + 26).toInt() + val unknown2 = data.copyOfRange(RESP_RECORD_DATA_START + 26, RESP_RECORD_DATA_START + 28).toInt() val bolusType = enumValues()[typeAndWizard and 0x0F] val bolusWizard = (typeAndWizard and 0xF0) != 0 aapsLogger.debug( LTag.PUMPCOMM, - "GetRecordPacket HandleResponse: BOLUS_RECORD: typeAndWizard: $typeAndWizard, bolusCause: $bolusCause, unknown: $unknown, bolusStartTime: $bolusStartTime, " + "bolusNormalAmount: $bolusNormalAmount, bolusNormalDelivered: $bolusNormalDelivered, bolusExtendedAmount: $bolusExtendedAmount, bolusExtendedDuration: $bolusExtendedDuration, " + "bolusExtendedDelivered: $bolusExtendedDelivered, bolusCarb: $bolusCarb, bolusGlucose: $bolusGlucose, bolusIOB: $bolusIOB, unkown1: $unkown1, unkown2: $unkown2, " + "bolusType: $bolusType, bolusWizard: $bolusWizard" + "GetRecordPacket HandleResponse: BOLUS_RECORD: typeAndWizard: $typeAndWizard, bolusCause: $bolusCause, unknown: $unknown, bolusStartTime: $bolusStartTime, " + + "bolusNormalAmount: $bolusNormalAmount, bolusNormalDelivered: $bolusNormalDelivered, bolusExtendedAmount: $bolusExtendedAmount, bolusExtendedDuration: " + + "$bolusExtendedDuration, " + "bolusExtendedDelivered: $bolusExtendedDelivered, bolusCarb: $bolusCarb, bolusGlucose: $bolusGlucose, bolusIOB: $bolusIOB, unknown1: $unknown1, unknown2: $unknown2, " + "bolusType: $bolusType, bolusWizard: $bolusWizard" ) if (bolusType == BolusType.NORMAL) { @@ -123,7 +124,7 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int pumpType = medtrumPump.pumpType(), pumpSerial = medtrumPump.pumpSN.toString(radix = 16) ) - if (syncOk == false) { + if (!syncOk) { aapsLogger.warn(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: BOLUS_RECORD: Failed to sync bolus with tempId: ${detailedBolusInfo.timestamp}") // detailedInfo can be from another similar record. Reinsert detailedBolusInfoStorage.add(detailedBolusInfo) @@ -298,7 +299,7 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int aapsLogger.debug(LTag.PUMPCOMM, "GetRecordPacket HandleResponse: TDD_RECORD") val timestamp = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(RESP_RECORD_DATA_START, RESP_RECORD_DATA_START + 4).toLong()) val timeZoneOffset = data.copyOfRange(RESP_RECORD_DATA_START + 4, RESP_RECORD_DATA_START + 6).toInt() - val tddMins = data.copyOfRange(RESP_RECORD_DATA_START + 6, RESP_RECORD_DATA_START + 8).toInt() + val tddMinutes = data.copyOfRange(RESP_RECORD_DATA_START + 6, RESP_RECORD_DATA_START + 8).toInt() val glucoseRecordTime = data.copyOfRange(RESP_RECORD_DATA_START + 8, RESP_RECORD_DATA_START + 12).toLong() val tdd = data.copyOfRange(RESP_RECORD_DATA_START + 12, RESP_RECORD_DATA_START + 16).toFloat() val basalTdd = data.copyOfRange(RESP_RECORD_DATA_START + 16, RESP_RECORD_DATA_START + 20).toFloat() @@ -315,7 +316,7 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int val newUMax = data.copyOfRange(RESP_RECORD_DATA_START + 60, RESP_RECORD_DATA_START + 64).toFloat() aapsLogger.debug( - LTag.PUMPCOMM, "TDD_RECORD: timestamp: $timestamp, timeZoneOffset: $timeZoneOffset, tddMins: $tddMins, glucoseRecordTime: $glucoseRecordTime, tdd: " + + LTag.PUMPCOMM, "TDD_RECORD: timestamp: $timestamp, timeZoneOffset: $timeZoneOffset, tddMinutes: $tddMinutes, glucoseRecordTime: $glucoseRecordTime, tdd: " + "$tdd, basalTdd: $basalTdd, glucose: $glucose, unknown: $unknown, meanSomething: $meanSomething, usedTdd: $usedTdd, usedIBasal: $usedIBasal, usedSgBasal: " + "$usedSgBasal, usedUMax: $usedUMax, newTdd: $newTdd, newIBasal: $newIBasal, newSgBasal: $newSgBasal, newUMax: $newUMax" ) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/MedtrumPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/MedtrumPacket.kt index 6b175c06df..cc6e7f5f77 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/MedtrumPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/MedtrumPacket.kt @@ -25,7 +25,7 @@ open class MedtrumPacket(protected var injector: HasAndroidInjector) { } init { - // @Suppress("LeakingThis") + @Suppress("LeakingThis") injector.androidInjector().inject(this) } @@ -33,7 +33,7 @@ open class MedtrumPacket(protected var injector: HasAndroidInjector) { return byteArrayOf(opCode) } - /** handles a response from the Medtrum pump, returns true if command was successfull, returns false if command failed or waiting for response */ + /** handles a response from the Medtrum pump, returns true if command was successful, returns false if command failed or waiting for response */ open fun handleResponse(data: ByteArray): Boolean { // Check for broken packets if (RESP_RESULT_END > data.size) { diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt index a37b5323cd..e9a1cdae87 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt @@ -19,7 +19,7 @@ class NotificationPacket(val injector: HasAndroidInjector) { * but a notification packet. It is sent by the pump to the phone * when the pump has a notification to send. * - * Notifications are sent regualary, regardless of the pump state. + * Notifications are sent regularly, regardless of the pump state. * * There can be multiple messages in one packet, this is noted by the fieldMask. * @@ -97,10 +97,10 @@ class NotificationPacket(val injector: HasAndroidInjector) { if (fieldMask and MASK_NORMAL_BOLUS != 0) { aapsLogger.debug(LTag.PUMPCOMM, "Normal bolus notification received") - var bolusData = data.copyOfRange(offset, offset + 1).toInt() - var bolusType = bolusData and 0x7F + val bolusData = data.copyOfRange(offset, offset + 1).toInt() + val bolusType = bolusData and 0x7F val bolusCompleted: Boolean = ((bolusData shr 7) and 0x01) != 0 - var bolusDelivered = data.copyOfRange(offset + 1, offset + 3).toInt() * 0.05 + val bolusDelivered = data.copyOfRange(offset + 1, offset + 3).toInt() * 0.05 aapsLogger.debug(LTag.PUMPCOMM, "Bolus type: $bolusType, bolusData: $bolusData bolus completed: $bolusCompleted, bolus delivered: $bolusDelivered") medtrumPump.handleBolusStatusUpdate(bolusType, bolusCompleted, bolusDelivered) offset += 3 @@ -115,12 +115,12 @@ class NotificationPacket(val injector: HasAndroidInjector) { if (fieldMask and MASK_BASAL != 0) { aapsLogger.debug(LTag.PUMPCOMM, "Basal notification received") val basalType = enumValues()[data.copyOfRange(offset, offset + 1).toInt()] - var basalSequence = data.copyOfRange(offset + 1, offset + 3).toInt() - var basalPatchId = data.copyOfRange(offset + 3, offset + 5).toLong() - var basalStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(offset + 5, offset + 9).toLong()) - var basalRateAndDelivery = data.copyOfRange(offset + 9, offset + 12).toInt() - var basalRate = (basalRateAndDelivery and 0xFFF) * 0.05 - var basalDelivery = (basalRateAndDelivery shr 12) * 0.05 + val basalSequence = data.copyOfRange(offset + 1, offset + 3).toInt() + val basalPatchId = data.copyOfRange(offset + 3, offset + 5).toLong() + val basalStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(offset + 5, offset + 9).toLong()) + val basalRateAndDelivery = data.copyOfRange(offset + 9, offset + 12).toInt() + val basalRate = (basalRateAndDelivery and 0xFFF) * 0.05 + val basalDelivery = (basalRateAndDelivery shr 12) * 0.05 aapsLogger.debug( LTag.PUMPCOMM, "Basal type: $basalType, basal sequence: $basalSequence, basal patch id: $basalPatchId, basal time: $basalStartTime, basal rate: $basalRate, basal delivery: $basalDelivery" @@ -159,7 +159,7 @@ class NotificationPacket(val injector: HasAndroidInjector) { if (fieldMask and MASK_BATTERY != 0) { aapsLogger.debug(LTag.PUMPCOMM, "Battery notification received") - var parameter = data.copyOfRange(offset, offset + 3).toInt() + val parameter = data.copyOfRange(offset, offset + 3).toInt() // Precision for voltage A is a guess, voltage B is the important one, threshold: < 2.64 medtrumPump.batteryVoltage_A = (parameter and 0xFFF) / 512.0 medtrumPump.batteryVoltage_B = (parameter shr 12) / 512.0 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 index aa613b3656..63ecaab62c 100644 --- 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 @@ -2,7 +2,6 @@ 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) { diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt index 129e665cf8..cbddb8c75d 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt @@ -13,7 +13,7 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) @Inject lateinit var dateUtil: DateUtil @Inject lateinit var medtrumPump: MedtrumPump - var offsetMins: Int = 0 + private var offsetMinutes: Int = 0 init { opCode = SET_TIME_ZONE.code @@ -21,15 +21,15 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) override fun getRequest(): ByteArray { val time = MedtrumTimeUtil().getCurrentTimePumpSeconds() - offsetMins = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) - if (offsetMins < 0) offsetMins += 65536 - return byteArrayOf(opCode) + offsetMins.toByteArray(2) + time.toByteArray(4) + offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) + if (offsetMinutes < 0) offsetMinutes += 65536 + return byteArrayOf(opCode) + offsetMinutes.toByteArray(2) + time.toByteArray(4) } override fun handleResponse(data: ByteArray): Boolean { val success = super.handleResponse(data) if (success) { - medtrumPump.pumpTimeZoneOffset = offsetMins + medtrumPump.pumpTimeZoneOffset = offsetMinutes } return success } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt index 4df32dafe7..c7fd95acea 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SynchronizePacket.kt @@ -16,7 +16,6 @@ class SynchronizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) companion object { private const val RESP_STATE_START = 6 - private const val RESP_STATE_END = RESP_STATE_START + 1 private const val RESP_FIELDS_START = 7 private const val RESP_FIELDS_END = RESP_FIELDS_START + 2 private const val RESP_SYNC_DATA_START = 9 @@ -34,7 +33,7 @@ class SynchronizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) override fun handleResponse(data: ByteArray): Boolean { val success = super.handleResponse(data) if (success) { - var state = MedtrumPumpState.fromByte(data[RESP_STATE_START]) + val state = MedtrumPumpState.fromByte(data[RESP_STATE_START]) aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: state: $state") if (state != medtrumPump.pumpState) { diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/encryption/Crypt.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/encryption/Crypt.kt index ce5126de48..a4f7e6c0df 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/encryption/Crypt.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/encryption/Crypt.kt @@ -8,7 +8,7 @@ class Crypt { private val RIJNDEAL_S_BOX: IntArray = intArrayOf(99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22) private val RIJNDEAL_INVERSE_S_BOX: IntArray = intArrayOf(82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125) - val MED_CIPHER: Long = 1344751489 + private val MED_CIPHER: Long = 1344751489 fun keyGen(input: Long): Long { val key = randomGen(randomGen(MED_CIPHER xor input)) @@ -25,7 +25,7 @@ class Crypt { private fun simpleCrypt(inputData: Long): Long { var temp = inputData xor MED_CIPHER for (i in 0 until 32) { - temp = changeByTable(rotatoLeft(temp, 32, 1), RIJNDEAL_S_BOX).toLong() + temp = changeByTable(rotatoLeft(temp, 32, 1), RIJNDEAL_S_BOX) } return temp } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ByteArrayExtension.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ByteArrayExtension.kt index 4e1d431086..dac514c20a 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ByteArrayExtension.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ByteArrayExtension.kt @@ -21,7 +21,7 @@ fun ByteArray.toInt(): Int { var result = 0 for (i in this.indices) { val byte = this[i] - val shifted = (byte.toInt() and 0xFF).toInt() shl 8 * i + val shifted = (byte.toInt() and 0xFF) shl 8 * i result = result or shifted } return result diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ViewExtension.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ViewExtension.kt index 5cf65baeb6..2bab7086c4 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ViewExtension.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/extension/ViewExtension.kt @@ -9,26 +9,3 @@ fun View?.visible(vararg views: View?) { for (view in views) view.visible() } - -fun View?.invisible() = this?.run { visibility = View.INVISIBLE } - -fun View?.invisible(vararg views: View?) { - invisible() - for (view in views) - view.invisible() -} - -fun View?.gone() = this?.run { visibility = View.GONE } - -fun View?.gone(vararg views: View?) { - gone() - for (view in views) - view.gone() -} - -fun View?.setVisibleOrGone(visibleOrGone: Boolean, vararg views: View?) { - for (view in views) - if (visibleOrGone) view.visible() else view.gone() -} - -fun View?.setVisibleOrGone(visibleOrGone: Boolean) = setVisibleOrGone(visibleOrGone, this) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt index b4058efd49..13de8dfacc 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt @@ -241,16 +241,16 @@ class BLEComm @Inject internal constructor( } override fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { - aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicRead data: " + characteristic.value.contentToString() + " UUID: " + characteristic.getUuid().toString() + " status: " + status) + aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicRead data: " + characteristic.value.contentToString() + " UUID: " + characteristic.uuid.toString() + " status: " + status) } override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { - aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicChanged data: " + characteristic.value.contentToString() + " UUID: " + characteristic.getUuid().toString()) + aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicChanged data: " + characteristic.value.contentToString() + " UUID: " + characteristic.uuid.toString()) - val value = characteristic.getValue() - if (characteristic.getUuid() == UUID.fromString(READ_UUID)) { + val value = characteristic.value + if (characteristic.uuid == UUID.fromString(READ_UUID)) { mCallback?.onNotification(value) - } else if (characteristic.getUuid() == UUID.fromString(WRITE_UUID)) { + } else if (characteristic.uuid == UUID.fromString(WRITE_UUID)) { synchronized(readLock) { if (mReadPacket == null) { mReadPacket = ReadDataPacket(value) @@ -398,7 +398,7 @@ class BLEComm @Inject internal constructor( @Synchronized fun sendMessage(message: ByteArray) { - aapsLogger.debug(LTag.PUMPBTCOMM, "sendMessage message = " + Arrays.toString(message)) + aapsLogger.debug(LTag.PUMPBTCOMM, "sendMessage message = " + message.contentToString()) if (mWritePackets?.allPacketsConsumed() == false) { aapsLogger.error(LTag.PUMPBTCOMM, "sendMessage not all packets consumed!! unable to sent message!") return @@ -455,7 +455,7 @@ class BLEComm @Inject internal constructor( val gattService = getGattService() ?: return var uuid: String val gattCharacteristics = gattService.characteristics - for (i in 0..gattCharacteristics.size - 1) { + for (i in 0 until gattCharacteristics.size) { val gattCharacteristic = gattCharacteristics.get(i) // Check whether read or write properties is set, the pump needs us to enable notifications on all characteristics that have these properties if (gattCharacteristic.properties and NEEDS_ENABLE > 0) { 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 9fc02a1625..da05d95f01 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 @@ -29,7 +29,6 @@ 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.MedtrumTimeUtil import info.nightscout.rx.AapsSchedulers import info.nightscout.rx.bus.RxBus import info.nightscout.rx.events.EventAppExit @@ -84,8 +83,6 @@ class MedtrumService : DaggerService(), BLECommCallback { private const val ALARM_DAILY_MAX_CLEAR_CODE = 5 } - val timeUtil = MedtrumTimeUtil() - private val disposable = CompositeDisposable() private val mBinder: IBinder = LocalBinder() @@ -124,7 +121,7 @@ class MedtrumService : DaggerService(), BLECommCallback { medtrumPump.loadUserSettingsFromSP() commandQueue.setUserOptions(object : Callback() { override fun run() { - if (medtrumPlugin.isInitialized() && this.result.success == false) { + if (medtrumPlugin.isInitialized() && !this.result.success) { uiInteraction.addNotification( Notification.PUMP_SETTINGS_FAILED, rh.gs(R.string.pump_setting_failed), @@ -204,7 +201,7 @@ class MedtrumService : DaggerService(), BLECommCallback { } fun readPumpStatus() { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingpumpstatus))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.getting_pump_status))) updateTimeIfNeeded(false) loadEvents() } @@ -229,7 +226,7 @@ class MedtrumService : DaggerService(), BLECommCallback { fun updateTimeIfNeeded(needLoadHistory: Boolean = true): Boolean { // Note we only check timeZone here, time is updated each connection attempt if needed, because the pump requires it to be checked - // But we dont check timeZone each time, therefore we do it here (if needed) + // But we don't check timeZone each time, therefore we do it here (if needed) var result = true if (medtrumPump.pumpTimeZoneOffset != dateUtil.getTimeZoneOffsetMinutes(dateUtil.now())) { result = sendPacketAndGetResponse(SetTimePacket(injector)) @@ -245,7 +242,7 @@ class MedtrumService : DaggerService(), BLECommCallback { } fun loadEvents(): Boolean { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingpumpstatus))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.getting_pump_status))) // Sync records val result = syncRecords() if (result) { @@ -261,16 +258,16 @@ class MedtrumService : DaggerService(), BLECommCallback { var result = true if (medtrumPump.pumpState in listOf( MedtrumPumpState.PAUSED, - MedtrumPumpState.HMAX_SUSPENDED, - MedtrumPumpState.DMAX_SUSPENDED + MedtrumPumpState.HOURLY_MAX_SUSPENDED, + MedtrumPumpState.DAILY_MAX_SUSPENDED ) ) { when (medtrumPump.pumpState) { - MedtrumPumpState.HMAX_SUSPENDED -> { + MedtrumPumpState.HOURLY_MAX_SUSPENDED -> { result = sendPacketAndGetResponse(ClearPumpAlarmPacket(injector, ALARM_HOURLY_MAX_CLEAR_CODE)) } - MedtrumPumpState.DMAX_SUSPENDED -> { + MedtrumPumpState.DAILY_MAX_SUSPENDED -> { result = sendPacketAndGetResponse(ClearPumpAlarmPacket(injector, ALARM_DAILY_MAX_CLEAR_CODE)) } @@ -285,7 +282,7 @@ class MedtrumService : DaggerService(), BLECommCallback { } fun setUserSettings(): Boolean { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.settingpumpsettings))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.setting_pump_settings))) return sendPacketAndGetResponse(SetPatchPacket(injector)) } @@ -301,16 +298,16 @@ class MedtrumService : DaggerService(), BLECommCallback { medtrumPump.bolusStopped = false medtrumPump.bolusProgressLastTimeStamp = bolusStart - if (insulin > 0 && !medtrumPump.bolusStopped) { + if (insulin > 0) { val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin)) - if (result == false) { + if (!result) { aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus") commandQueue.loadEvents(null) // make sure if anything is delivered (which is highly unlikely at this point) we get it t.insulin = 0.0 return false } } else { - aapsLogger.debug(LTag.PUMPCOMM, "Bolus not set, insulin: $insulin, bolusStopped: ${medtrumPump.bolusStopped}") + aapsLogger.debug(LTag.PUMPCOMM, "Bolus not set, insulin: $insulin") t.insulin = 0.0 return false } @@ -384,7 +381,7 @@ class MedtrumService : DaggerService(), BLECommCallback { // Do not call update status directly, reconnection may be needed commandQueue.loadEvents(object : Callback() { override fun run() { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingbolusstatus))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.getting_bolus_status))) bolusingEvent.percent = 100 } }) @@ -395,7 +392,7 @@ class MedtrumService : DaggerService(), BLECommCallback { aapsLogger.debug(LTag.PUMPCOMM, "bolusStop >>>>> @ " + if (medtrumPump.bolusingTreatment == null) "" else medtrumPump.bolusingTreatment?.insulin) if (isConnected) { var success = sendPacketAndGetResponse(CancelBolusPacket(injector)) - var timeout = System.currentTimeMillis() + T.secs(30).msecs() + val timeout = System.currentTimeMillis() + T.secs(30).msecs() while (!success && System.currentTimeMillis() < timeout) { success = sendPacketAndGetResponse(CancelBolusPacket(injector)) SystemClock.sleep(200) @@ -414,11 +411,11 @@ 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 previous basals // Do not call update status directly, reconnection may be needed commandQueue.loadEvents(object : Callback() { override fun run() { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingtempbasalstatus))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.getting_temp_basal_status))) } }) @@ -426,13 +423,13 @@ class MedtrumService : DaggerService(), BLECommCallback { } fun cancelTempBasal(): Boolean { - var result = sendPacketAndGetResponse(CancelTempBasalPacket(injector)) + val result = sendPacketAndGetResponse(CancelTempBasalPacket(injector)) - // Get history records, this will update the prevoius basals + // Get history records, this will update the previous basals // Do not call update status directly, reconnection may be needed commandQueue.loadEvents(object : Callback() { override fun run() { - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingtempbasalstatus))) + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.getting_temp_basal_status))) } }) @@ -462,7 +459,7 @@ class MedtrumService : DaggerService(), BLECommCallback { if (medtrumPump.syncedSequenceNumber < medtrumPump.currentSequenceNumber) { for (sequence in (medtrumPump.syncedSequenceNumber + 1)..medtrumPump.currentSequenceNumber) { result = sendPacketAndGetResponse(GetRecordPacket(injector, sequence), COMMAND_SYNC_TIMEOUT_SEC) - if (result == false) break + if (!result) break } } return result @@ -471,23 +468,23 @@ class MedtrumService : DaggerService(), BLECommCallback { private fun handlePumpStateUpdate(state: MedtrumPumpState) { // Map the pump state to an alarm state and add it to the active alarms val alarmState = when (state) { - MedtrumPumpState.NONE -> AlarmState.NONE - MedtrumPumpState.LOWBG_SUSPENDED -> AlarmState.LOWBG_SUSPENDED - MedtrumPumpState.LOWBG_SUSPENDED2 -> AlarmState.LOWBG_SUSPENDED2 - MedtrumPumpState.AUTO_SUSPENDED -> AlarmState.AUTO_SUSPENDED - MedtrumPumpState.HMAX_SUSPENDED -> AlarmState.HMAX_SUSPENDED - MedtrumPumpState.DMAX_SUSPENDED -> AlarmState.DMAX_SUSPENDED - MedtrumPumpState.SUSPENDED -> AlarmState.SUSPENDED - MedtrumPumpState.PAUSED -> AlarmState.PAUSED - MedtrumPumpState.OCCLUSION -> AlarmState.OCCLUSION - MedtrumPumpState.EXPIRED -> AlarmState.EXPIRED - MedtrumPumpState.RESERVOIR_EMPTY -> AlarmState.RESERVOIR_EMPTY - MedtrumPumpState.PATCH_FAULT -> AlarmState.PATCH_FAULT - MedtrumPumpState.PATCH_FAULT2 -> AlarmState.PATCH_FAULT2 - MedtrumPumpState.BASE_FAULT -> AlarmState.BASE_FAULT - MedtrumPumpState.BATTERY_OUT -> AlarmState.BATTERY_OUT - MedtrumPumpState.NO_CALIBRATION -> AlarmState.NO_CALIBRATION - else -> null + MedtrumPumpState.NONE -> AlarmState.NONE + MedtrumPumpState.LOW_BG_SUSPENDED -> AlarmState.LOW_BG_SUSPENDED + MedtrumPumpState.LOW_BG_SUSPENDED2 -> AlarmState.LOW_BG_SUSPENDED2 + MedtrumPumpState.AUTO_SUSPENDED -> AlarmState.AUTO_SUSPENDED + MedtrumPumpState.HOURLY_MAX_SUSPENDED -> AlarmState.HOURLY_MAX_SUSPENDED + MedtrumPumpState.DAILY_MAX_SUSPENDED -> AlarmState.DAILY_MAX_SUSPENDED + MedtrumPumpState.SUSPENDED -> AlarmState.SUSPENDED + MedtrumPumpState.PAUSED -> AlarmState.PAUSED + MedtrumPumpState.OCCLUSION -> AlarmState.OCCLUSION + MedtrumPumpState.EXPIRED -> AlarmState.EXPIRED + MedtrumPumpState.RESERVOIR_EMPTY -> AlarmState.RESERVOIR_EMPTY + MedtrumPumpState.PATCH_FAULT -> AlarmState.PATCH_FAULT + MedtrumPumpState.PATCH_FAULT2 -> AlarmState.PATCH_FAULT2 + MedtrumPumpState.BASE_FAULT -> AlarmState.BASE_FAULT + MedtrumPumpState.BATTERY_OUT -> AlarmState.BATTERY_OUT + MedtrumPumpState.NO_CALIBRATION -> AlarmState.NO_CALIBRATION + else -> null } if (alarmState != null && alarmState != AlarmState.NONE) { medtrumPump.addAlarm(alarmState) @@ -533,8 +530,8 @@ class MedtrumService : DaggerService(), BLECommCallback { medtrumPump.clearAlarmState() } - MedtrumPumpState.LOWBG_SUSPENDED, - MedtrumPumpState.LOWBG_SUSPENDED2, + MedtrumPumpState.LOW_BG_SUSPENDED, + MedtrumPumpState.LOW_BG_SUSPENDED2, MedtrumPumpState.AUTO_SUSPENDED, MedtrumPumpState.SUSPENDED, MedtrumPumpState.PAUSED -> { @@ -546,7 +543,7 @@ class MedtrumService : DaggerService(), BLECommCallback { // Pump will report proper TBR for this } - MedtrumPumpState.HMAX_SUSPENDED -> { + MedtrumPumpState.HOURLY_MAX_SUSPENDED -> { uiInteraction.addNotification( Notification.PUMP_SUSPENDED, rh.gs(R.string.pump_is_suspended_hour_max), @@ -555,7 +552,7 @@ class MedtrumService : DaggerService(), BLECommCallback { // Pump will report proper TBR for this } - MedtrumPumpState.DMAX_SUSPENDED -> { + MedtrumPumpState.DAILY_MAX_SUSPENDED -> { uiInteraction.addNotification( Notification.PUMP_SUSPENDED, rh.gs(R.string.pump_is_suspended_day_max), @@ -721,8 +718,6 @@ class MedtrumService : DaggerService(), BLECommCallback { // State for connect flow private inner class AuthState : State() { - val retryCounter = 0 - override fun onEnter() { aapsLogger.debug(LTag.PUMPCOMM, "Medtrum Service reached AuthState") mPacket = AuthorizePacket(injector) @@ -734,7 +729,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true // Check if we have a supported pump @@ -776,7 +771,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true // Place holder, not really used (yet) @@ -808,7 +803,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true val currTime = dateUtil.now() @@ -843,7 +838,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true toState(SetTimeZoneState()) @@ -871,7 +866,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true medtrumPump.needCheckTimeUpdate = false @@ -901,7 +896,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true toState(SubscribeState()) @@ -929,7 +924,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true toState(ReadyState()) @@ -949,7 +944,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onEnter() { aapsLogger.debug(LTag.PUMPCOMM, "Medtrum Service reached ReadyState!") // Now we are fully connected and authenticated and we can start sending commands. Let AAPS know - if (isConnected == false) { + if (!isConnected) { medtrumPump.connectionState = ConnectionState.CONNECTED } } @@ -964,7 +959,7 @@ class MedtrumService : DaggerService(), BLECommCallback { override fun onIndication(data: ByteArray) { if (mPacket?.handleResponse(data) == true) { - // Succes! + // Success! responseHandled = true responseSuccess = true toState(ReadyState()) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPreparePatchFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPreparePatchFragment.kt index 5e23d96632..425f7fac3f 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPreparePatchFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPreparePatchFragment.kt @@ -5,7 +5,6 @@ import android.view.View import androidx.lifecycle.ViewModelProvider import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.databinding.FragmentMedtrumPreparePatchBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeCompleteFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeCompleteFragment.kt index 75b2bad284..f7e3c402d2 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeCompleteFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeCompleteFragment.kt @@ -8,7 +8,6 @@ import info.nightscout.core.ui.toast.ToastUtils import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.PatchStep import info.nightscout.pump.medtrum.databinding.FragmentMedtrumPrimeCompleteBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeFragment.kt index 3322c8a54f..80020abe46 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimeFragment.kt @@ -8,7 +8,6 @@ import info.nightscout.core.ui.toast.ToastUtils import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.PatchStep import info.nightscout.pump.medtrum.databinding.FragmentMedtrumPrimeBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimingFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimingFragment.kt index fe1234a5c3..eeee99aa71 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimingFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumPrimingFragment.kt @@ -8,7 +8,6 @@ import info.nightscout.core.ui.toast.ToastUtils import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.PatchStep import info.nightscout.pump.medtrum.databinding.FragmentMedtrumPrimingBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationConnectFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationConnectFragment.kt index 466be92e7b..47de209db4 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationConnectFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationConnectFragment.kt @@ -7,7 +7,6 @@ import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.PatchStep import info.nightscout.pump.medtrum.databinding.FragmentMedtrumRetryActivationConnectBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationFragment.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationFragment.kt index 5e1673685b..829f80d217 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationFragment.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumRetryActivationFragment.kt @@ -7,7 +7,6 @@ import info.nightscout.core.ui.dialogs.OKDialog import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.PatchStep import info.nightscout.pump.medtrum.databinding.FragmentMedtrumRetryActivationBinding -import info.nightscout.pump.medtrum.ui.MedtrumBaseFragment import info.nightscout.pump.medtrum.ui.viewmodel.MedtrumViewModel import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag @@ -32,7 +31,7 @@ class MedtrumRetryActivationFragment : MedtrumBaseFragment : ViewModel() { private var _navigator: WeakReference? = null - var navigator: N? + private var navigator: N? set(value) { _navigator = WeakReference(value) } 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 da742ea91d..1cd1de8a95 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 @@ -114,7 +114,7 @@ class MedtrumOverviewViewModel @Inject constructor( aapsLogger.debug(LTag.PUMP, "MedtrumViewModel pumpStateFlow: $state") _canDoResetAlarms.postValue( medtrumPump.pumpState in listOf( - MedtrumPumpState.PAUSED, MedtrumPumpState.HMAX_SUSPENDED, MedtrumPumpState.DMAX_SUSPENDED + MedtrumPumpState.PAUSED, MedtrumPumpState.HOURLY_MAX_SUSPENDED, MedtrumPumpState.DAILY_MAX_SUSPENDED ) ) @@ -153,7 +153,7 @@ class MedtrumOverviewViewModel @Inject constructor( } } - fun updateGUI() { + private fun updateGUI() { // Update less dynamic values if (medtrumPump.lastConnection != 0L) { val agoMilliseconds = System.currentTimeMillis() - medtrumPump.lastConnection 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 688770f884..f12f49e6aa 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 @@ -10,13 +10,10 @@ import info.nightscout.pump.medtrum.MedtrumPump import info.nightscout.pump.medtrum.R import info.nightscout.pump.medtrum.code.ConnectionState import info.nightscout.pump.medtrum.services.MedtrumService -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.pump.medtrum.encryption.Crypt import info.nightscout.pump.medtrum.ui.MedtrumBaseNavigator -import info.nightscout.pump.medtrum.ui.event.SingleLiveEvent -import info.nightscout.pump.medtrum.ui.event.UIEvent import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.LTag import kotlinx.coroutines.CoroutineScope @@ -43,10 +40,6 @@ class MedtrumViewModel @Inject constructor( val title: LiveData get() = _title - private val _eventHandler = SingleLiveEvent>() - val eventHandler: LiveData> - get() = _eventHandler - private var oldPatchStep: PatchStep? = null private var mInitPatchStep: PatchStep? = null private var connectRetryCounter = 0 @@ -155,7 +148,7 @@ class MedtrumViewModel @Inject constructor( PatchStep.RETRY_ACTIVATION_CONNECT, PatchStep.PREPARE_PATCH_CONNECT -> { - // Make sure we are disconnected, else dont move step + // Make sure we are disconnected, else don't move step if (medtrumService?.isConnected == true) { aapsLogger.info(LTag.PUMP, "moveStep: connected, not moving step") return @@ -167,7 +160,7 @@ class MedtrumViewModel @Inject constructor( PatchStep.PRIME_COMPLETE, PatchStep.ATTACH_PATCH, PatchStep.ACTIVATE -> { - // Make sure we are connected, else dont move step + // Make sure we are connected, else don't move step if (medtrumService?.isConnected == false) { aapsLogger.info(LTag.PUMP, "moveStep: not connected, not moving step") return @@ -321,7 +314,7 @@ class MedtrumViewModel @Inject constructor( aapsLogger.info(LTag.PUMP, "prepareStep: title before cond: $stringResId") if (currentTitle != stringResId) { aapsLogger.info(LTag.PUMP, "prepareStep: title: $stringResId") - _title.postValue(stringResId) + stringResId?.let { _title.postValue(it) } } patchStep.postValue(newStep) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumTimeUtil.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumTimeUtil.kt index ad3910f0ec..fd6dc2ddd6 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumTimeUtil.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/util/MedtrumTimeUtil.kt @@ -5,19 +5,13 @@ import java.time.Instant class MedtrumTimeUtil { - fun getCurrentTimePumpSeconds() : Long { + fun getCurrentTimePumpSeconds(): Long { val startInstant = Instant.parse("2014-01-01T00:00:00Z") val currentInstant = Instant.now() return Duration.between(startInstant, currentInstant).seconds } - fun getCurrentTimePumpMillis() : Long { - val startInstant = Instant.parse("2014-01-01T00:00:00Z") - val currentInstant = Instant.now() - return Duration.between(startInstant, currentInstant).seconds * 1000 - } - - fun convertPumpTimeToSystemTimeMillis(pumpTime: Long) : Long { + fun convertPumpTimeToSystemTimeMillis(pumpTime: Long): Long { val startInstant = Instant.parse("2014-01-01T00:00:00Z") val pumpInstant = startInstant.plusSeconds(pumpTime) val epochInstant = Instant.EPOCH diff --git a/pump/medtrum/src/main/res/layout/activity_medtrum.xml b/pump/medtrum/src/main/res/layout/activity_medtrum.xml index 2c1d16aad2..50b4d5451b 100644 --- a/pump/medtrum/src/main/res/layout/activity_medtrum.xml +++ b/pump/medtrum/src/main/res/layout/activity_medtrum.xml @@ -34,8 +34,7 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" - tools:text="@string/string_change_patch" /> + app:layout_constraintTop_toTopOf="parent" /> diff --git a/pump/medtrum/src/main/res/layout/fragment_medtrum_deactivation_complete.xml b/pump/medtrum/src/main/res/layout/fragment_medtrum_deactivation_complete.xml index cc564f230f..1fb333cb19 100644 --- a/pump/medtrum/src/main/res/layout/fragment_medtrum_deactivation_complete.xml +++ b/pump/medtrum/src/main/res/layout/fragment_medtrum_deactivation_complete.xml @@ -26,19 +26,33 @@ tools:context=".ui.MedtrumActivity"> + + + xmlns:app="http://schemas.android.com/apk/res-auto"> diff --git a/pump/medtrum/src/main/res/values/strings.xml b/pump/medtrum/src/main/res/values/strings.xml index 575782c9eb..361b32dbdd 100644 --- a/pump/medtrum/src/main/res/values/strings.xml +++ b/pump/medtrum/src/main/res/values/strings.xml @@ -6,8 +6,8 @@ patch_expiration hourly_max_insulin daily_max_insulin - - medtrumpump_settings + + medtrum_pump_setting pump_state active_alarms last_connection @@ -26,7 +26,7 @@ Medtrum MT Pump integration for Medtrum Nano - Medtrum pump settings + Medtrum pump settings Pump error: %1$s !! Pump untested: %1$d! Please contact us at discord or github for support Pump is suspended @@ -34,14 +34,14 @@ Pump is suspended due to daily max insulin exceeded Patch not activated Setting user settings to pump failed! - + BLE Status Last connected Pump state Active alarms %.2f U - %.2f V + %.2f V Basal type Basal rate %.2f U/h @@ -60,11 +60,11 @@ Pump low battery Pump low reservoir Pump expires soon - Low BG suspended - Low BG suspended 2 + Low BG suspended + Low BG suspended 2 Auto suspended - hourly max suspended - daily max suspended + hourly max suspended + daily max suspended Suspended Paused Occlusion @@ -101,16 +101,15 @@ No active patch. Press Next to begin the activation process. Pump base should not be connected to the patch until the next step! Connect pump base to a new patch, remove the residual air and fill with insulin, then press Next. - Note: A minimum of 70 units is required for actvation. + Note: A minimum of 70 units is required for activation. Do not attach the patch to the body yet. Half-press needle button. Then tap Next to start prime. Please wait for the priming to complete. Failed to prime, press Retry to try again. Press Next to continue. Press Next to start activation. - Remove the safety lock. Attach the pump to the body. Press the needle button. Remove the safety lock. Attach the pump to the body. Press the needle button. - Activating pump and settng initial basal rate. Please Wait. + Activating pump and setting initial basal rate. Please Wait. Failed to activate, press Retry to try again. New patch activated. %.2f Units remaining. Press OK to return to main screen. @@ -142,10 +141,9 @@ 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 + 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 7d58491568..64081101e5 100644 --- a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml +++ b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml @@ -3,8 +3,8 @@ xmlns:validate="http://schemas.android.com/apk/res-auto"> Date: Mon, 14 Aug 2023 21:12:23 +0200 Subject: [PATCH 2/3] Medtrum: handle on back pressed properly --- .../pump/medtrum/ui/MedtrumActivity.kt | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt index 683da88042..dbf48c36aa 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/MedtrumActivity.kt @@ -6,6 +6,7 @@ import android.content.pm.ActivityInfo import android.os.Bundle import android.view.MotionEvent import android.view.WindowManager +import androidx.activity.OnBackPressedCallback import androidx.lifecycle.ViewModelProvider import info.nightscout.core.utils.extensions.safeGetSerializableExtra import info.nightscout.pump.medtrum.R @@ -19,10 +20,6 @@ class MedtrumActivity : MedtrumBaseActivity() { override fun getLayoutId(): Int = R.layout.activity_medtrum - override fun dispatchTouchEvent(event: MotionEvent): Boolean { - return super.dispatchTouchEvent(event) - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED @@ -70,6 +67,28 @@ class MedtrumActivity : MedtrumBaseActivity() { } } } + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + binding.viewModel?.apply { + when (patchStep.value) { + PatchStep.PREPARE_PATCH, + PatchStep.START_DEACTIVATION, + PatchStep.RETRY_ACTIVATION -> { + handleCancel() + this@MedtrumActivity.finish() + } + + PatchStep.COMPLETE, + PatchStep.DEACTIVATION_COMPLETE -> { + handleComplete() + this@MedtrumActivity.finish() + } + + else -> Unit + } + } + } + }) } override fun onNewIntent(intent: Intent?) { @@ -105,5 +124,4 @@ class MedtrumActivity : MedtrumBaseActivity() { private fun setupViewFragment(baseFragment: MedtrumBaseFragment<*>) { replaceFragmentInActivity(baseFragment, R.id.framelayout_fragment, false) } - } From 7662f3cc45db3e5bac0e50d1674882c2d128963c Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Tue, 15 Aug 2023 08:42:20 +0200 Subject: [PATCH 3/3] Medtrum: Fix unit tests, more lints --- .../info/nightscout/pump/medtrum/MedtrumPlugin.kt | 12 ++++++------ .../info/nightscout/pump/medtrum/MedtrumTestBase.kt | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) 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 1b035cc580..e9eb94d844 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 @@ -293,12 +293,12 @@ import kotlin.math.abs val pumpRate = constraintChecker.applyBasalConstraints(Constraint(absoluteRate), profile).value() temporaryBasalStorage.add(PumpSync.PumpState.TemporaryBasal(dateUtil.now(), T.mins(durationInMinutes.toLong()).msecs(), pumpRate, true, tbrType, 0L, 0L)) val connectionOK = medtrumService?.setTempBasal(pumpRate, durationInMinutes) ?: false - if (connectionOK + return if (connectionOK && medtrumPump.tempBasalInProgress && abs(medtrumPump.tempBasalAbsoluteRate - pumpRate) <= 0.05 ) { - return PumpEnactResult(injector).success(true).enacted(true).duration(durationInMinutes).absolute(medtrumPump.tempBasalAbsoluteRate) + PumpEnactResult(injector).success(true).enacted(true).duration(durationInMinutes).absolute(medtrumPump.tempBasalAbsoluteRate) .isPercent(false) .isTempCancel(false) } else { @@ -306,7 +306,7 @@ import kotlin.math.abs LTag.PUMP, "setTempBasalAbsolute failed, connectionOK: $connectionOK, tempBasalInProgress: ${medtrumPump.tempBasalInProgress}, tempBasalAbsoluteRate: ${medtrumPump.tempBasalAbsoluteRate}" ) - return PumpEnactResult(injector).success(false).enacted(false).comment("Medtrum setTempBasalAbsolute failed") + PumpEnactResult(injector).success(false).enacted(false).comment("Medtrum setTempBasalAbsolute failed") } } @@ -325,11 +325,11 @@ import kotlin.math.abs aapsLogger.info(LTag.PUMP, "cancelTempBasal - enforceNew: $enforceNew") val connectionOK = medtrumService?.cancelTempBasal() ?: false - if (connectionOK && !medtrumPump.tempBasalInProgress) { - return PumpEnactResult(injector).success(true).enacted(true).isTempCancel(true) + return if (connectionOK && !medtrumPump.tempBasalInProgress) { + PumpEnactResult(injector).success(true).enacted(true).isTempCancel(true) } else { aapsLogger.error(LTag.PUMP, "cancelTempBasal failed, connectionOK: $connectionOK, tempBasalInProgress: ${medtrumPump.tempBasalInProgress}") - return PumpEnactResult(injector).success(false).enacted(false).comment("Medtrum cancelTempBasal failed") + PumpEnactResult(injector).success(false).enacted(false).comment("Medtrum cancelTempBasal failed") } } diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumTestBase.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumTestBase.kt index e800fdb118..77e45b6834 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumTestBase.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/MedtrumTestBase.kt @@ -7,6 +7,7 @@ import info.nightscout.interfaces.pump.TemporaryBasalStorage import info.nightscout.interfaces.stats.TddCalculator import org.junit.jupiter.api.BeforeEach import org.mockito.Mock +import org.mockito.Mockito open class MedtrumTestBase : TestBaseWithProfile() { @@ -19,6 +20,7 @@ open class MedtrumTestBase : TestBaseWithProfile() { @BeforeEach fun setup() { + Mockito.`when`(sp.getString(R.string.key_active_alarms, "")).thenReturn("") medtrumPump = MedtrumPump(aapsLogger, rh, sp, dateUtil, pumpSync, temporaryBasalStorage) } }