Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SP refactor #3733

Open
wants to merge 50 commits into
base: dev
Choose a base branch
from
Open
Changes from 6 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e898638
Merge branch 'dev' of https://github.com/nightscout/AndroidAPS into rl
MilosKozak Dec 16, 2024
9247767
RL -> kt
MilosKozak Dec 16, 2024
90e7e81
SP -> Preferences #1
MilosKozak Jan 10, 2025
d3d9215
SP -> Preferences #2
MilosKozak Jan 10, 2025
1da1a2d
SP -> Preferences #3
MilosKozak Jan 11, 2025
078ae35
SP -> Preferences #4
MilosKozak Jan 11, 2025
ca6d514
SP -> Preferences #5
MilosKozak Jan 11, 2025
76ec1e0
SP -> Preferences #6
MilosKozak Jan 11, 2025
42a383c
SP -> Preferences #7
MilosKozak Jan 11, 2025
c9c466e
SP -> Preferences #8
MilosKozak Jan 11, 2025
4445d4e
SP -> Preferences #9
MilosKozak Jan 12, 2025
485bdd0
SP -> Preferences #10
MilosKozak Jan 12, 2025
cadd391
Merge branch 'dev' into sprefactor
MilosKozak Jan 13, 2025
5fb825a
SP -> Preferences #10
MilosKozak Jan 14, 2025
fa16fed
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Jan 14, 2025
3e4b97b
Merge branch 'dev' into sprefactor
MilosKozak Jan 15, 2025
1d617b8
Merge branch 'dev' into rl
MilosKozak Jan 17, 2025
a5b3318
RL: resolve nullability
MilosKozak Jan 17, 2025
9b7938c
RL: kotlin cleanup
MilosKozak Jan 19, 2025
f04dc20
Merge branch 'dev' into sprefactor
MilosKozak Jan 20, 2025
f798220
fix tests
MilosKozak Jan 20, 2025
8fe7feb
Merge branch 'rl' into sprefactor
MilosKozak Jan 21, 2025
ebb4994
Medtronic,RL: migrate preferences
MilosKozak Jan 21, 2025
2b738f1
SP -> Preferences #12
MilosKozak Jan 22, 2025
3256617
SP -> Preferences #13
MilosKozak Jan 22, 2025
ab1f604
Cleanup
MilosKozak Jan 22, 2025
70237e1
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Jan 22, 2025
4928192
Dash: preferences -> Key
MilosKozak Jan 23, 2025
00459ed
MDT: fix keys (inc)
MilosKozak Jan 23, 2025
dc535d3
Merge branch 'sprefactor' of https://github.com/nightscout/AndroidAPS…
MilosKozak Jan 23, 2025
4912e04
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Jan 23, 2025
335781f
OmnipodErosPumpPlugin -> kt
MilosKozak Jan 24, 2025
0f50816
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Jan 29, 2025
8cb5a45
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Feb 8, 2025
23fb7df
SP -> Preferences #14
MilosKozak Feb 8, 2025
656a3d2
Merge remote-tracking branch 'origin/dev' into sprefactor
MilosKozak Feb 8, 2025
08c268e
SP -> Preferences #15
MilosKozak Feb 8, 2025
8686804
fix tests
MilosKozak Feb 9, 2025
cc90ed9
remove unused files
MilosKozak Feb 9, 2025
57f838b
SP -> Preferences #16
MilosKozak Feb 9, 2025
a309471
SP -> Preferences #17
MilosKozak Feb 9, 2025
dab13ce
SP -> Preferences #18
MilosKozak Feb 9, 2025
f72c587
SP -> Preferences #19
MilosKozak Feb 9, 2025
e98b0c6
SP -> Preferences #20
MilosKozak Feb 9, 2025
be64ae9
SP -> Preferences #21
MilosKozak Feb 9, 2025
7ebdc1d
SP -> Preferences #22
MilosKozak Feb 9, 2025
9f6f9af
SP -> Preferences #23
MilosKozak Feb 9, 2025
e483660
SP -> Preferences #24
MilosKozak Feb 10, 2025
496064e
SP -> Preferences #25
MilosKozak Feb 10, 2025
d3e9cb0
fix tests
MilosKozak Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ import app.aaps.core.interfaces.rx.events.EventTempBasalChange
import app.aaps.core.interfaces.utils.DateUtil
import app.aaps.core.interfaces.utils.fabric.FabricPrivacy
import app.aaps.core.ui.dialogs.OKDialog
import app.aaps.pump.common.events.EventRileyLinkDeviceStatusChange
import app.aaps.pump.common.extensions.stringResource
import app.aaps.pump.common.hw.rileylink.defs.RileyLinkServiceState
import app.aaps.pump.common.hw.rileylink.defs.RileyLinkTargetDevice
import app.aaps.pump.common.hw.rileylink.dialog.RileyLinkStatusActivity
@@ -41,10 +43,9 @@ import app.aaps.pump.medtronic.events.EventMedtronicPumpConfigurationChanged
import app.aaps.pump.medtronic.events.EventMedtronicPumpValuesChanged
import app.aaps.pump.medtronic.util.MedtronicUtil
import dagger.android.support.DaggerFragment
import app.aaps.pump.common.events.EventRileyLinkDeviceStatusChange
import app.aaps.pump.common.extensions.stringResource
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import java.util.Locale
import javax.inject.Inject

class MedtronicFragment : DaggerFragment() {
@@ -193,9 +194,9 @@ class MedtronicFragment : DaggerFragment() {
binding.rlStatus.text =
when {
rileyLinkServiceData.rileyLinkServiceState == RileyLinkServiceState.NotStarted -> rh.gs(resourceId)
rileyLinkServiceData.rileyLinkServiceState.isConnecting -> "{fa-bluetooth-b spin} " + rh.gs(resourceId)
rileyLinkServiceData.rileyLinkServiceState.isError && rileyLinkError == null -> "{fa-bluetooth-b} " + rh.gs(resourceId)
rileyLinkServiceData.rileyLinkServiceState.isError && rileyLinkError != null -> "{fa-bluetooth-b} " + rh.gs(rileyLinkError.getResourceId(RileyLinkTargetDevice.MedtronicPump))
rileyLinkServiceData.rileyLinkServiceState.isConnecting() -> "{fa-bluetooth-b spin} " + rh.gs(resourceId)
rileyLinkServiceData.rileyLinkServiceState.isError() && rileyLinkError == null -> "{fa-bluetooth-b} " + rh.gs(resourceId)
rileyLinkServiceData.rileyLinkServiceState.isError() && rileyLinkError != null -> "{fa-bluetooth-b} " + rh.gs(rileyLinkError.getResourceId(RileyLinkTargetDevice.MedtronicPump))
else -> "{fa-bluetooth-b} " + rh.gs(resourceId)
}
binding.rlStatus.setTextColor(rh.gac(context, if (rileyLinkError != null) app.aaps.core.ui.R.attr.warningColor else app.aaps.core.ui.R.attr.defaultTextColor))
@@ -330,7 +331,7 @@ class MedtronicFragment : DaggerFragment() {
binding.pumpStateBattery.text = "{fa-battery-" + medtronicPumpStatus.batteryRemaining / 25 + "} "
} else {
binding.pumpStateBattery.text =
"{fa-battery-" + medtronicPumpStatus.batteryRemaining / 25 + "} " + medtronicPumpStatus.batteryRemaining + "%" + String.format(" (%.2f V)", medtronicPumpStatus.batteryVoltage)
"{fa-battery-" + medtronicPumpStatus.batteryRemaining / 25 + "} " + medtronicPumpStatus.batteryRemaining + "%" + String.format(Locale.getDefault(), " (%.2f V)", medtronicPumpStatus.batteryVoltage)
}
warnColors.setColorInverse(binding.pumpStateBattery, medtronicPumpStatus.batteryRemaining.toDouble(), 25, 10)

Original file line number Diff line number Diff line change
@@ -279,9 +279,9 @@ class MedtronicPumpPlugin @Inject constructor(
if (displayConnectionMessages) aapsLogger.debug(LTag.PUMP, "MedtronicPumpPlugin::isBusy")
if (isServiceSet) {
if (isBusy) return true
if (this.busyTimestamps.size > 0) {
if (this.busyTimestamps.isNotEmpty()) {
clearBusyQueue()
return this.busyTimestamps.size > 0
return this.busyTimestamps.isNotEmpty()
}
}
return false
@@ -342,7 +342,7 @@ class MedtronicPumpPlugin @Inject constructor(
aapsLogger.debug(LTag.PUMP, "RileyLink unreachable.")
return false
}
return rileyLinkMedtronicService?.deviceCommunicationManager?.isDeviceReachable != true
return rileyLinkMedtronicService?.deviceCommunicationManager?.isDeviceReachable() != true
}

private fun refreshAnyStatusThatNeedsToBeRefreshed() {
@@ -978,7 +978,7 @@ class MedtronicPumpPlugin @Inject constructor(
return 0L
}
lastPumpEntryTime
} catch (ex: Exception) {
} catch (_x: Exception) {
aapsLogger.warn(LTag.PUMP, "Saved LastPumpHistoryEntry was invalid.")
0L
}
@@ -1220,7 +1220,7 @@ class MedtronicPumpPlugin @Inject constructor(
serviceTaskExecutor.startTask(ResetRileyLinkConfigurationTask(injector))
}

null -> {
null -> { // do nothing

}
}
Original file line number Diff line number Diff line change
@@ -53,14 +53,13 @@ import javax.inject.Singleton
* functionality added.
*/
@Singleton
class MedtronicCommunicationManager // This empty constructor must be kept, otherwise dagger injection might break!
@Inject constructor() : RileyLinkCommunicationManager<PumpMessage?>() {

@Inject lateinit var medtronicPumpStatus: MedtronicPumpStatus
@Inject lateinit var medtronicPumpPlugin: MedtronicPumpPlugin
@Inject lateinit var medtronicConverter: MedtronicConverter
@Inject lateinit var medtronicUtil: MedtronicUtil
@Inject lateinit var medtronicPumpHistoryDecoder: MedtronicPumpHistoryDecoder
class MedtronicCommunicationManager @Inject constructor(
private val medtronicPumpStatus: MedtronicPumpStatus,
private val medtronicPumpPlugin: MedtronicPumpPlugin,
private val medtronicConverter: MedtronicConverter,
private val medtronicUtil: MedtronicUtil,
private val medtronicPumpHistoryDecoder: MedtronicPumpHistoryDecoder,
) : RileyLinkCommunicationManager<PumpMessage>() {

companion object {

@@ -129,19 +128,18 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
// check connection
val pumpMsgContent = createPumpMessageContent(RLMessageType.ReadSimpleData) // simple
val rfSpyResponse = rfspy.transmitThenReceive(
RadioPacket(injector, pumpMsgContent), 0.toByte(), 200.toByte(),
0.toByte(), 0.toByte(), 25000, 0.toByte()
RadioPacket(rileyLinkUtil, pumpMsgContent), 0.toByte(), 200.toByte(), 0.toByte(), 0.toByte(), 25000, 0.toByte()
)
aapsLogger.info(LTag.PUMPCOMM, "wakeup: raw response is " + ByteUtil.shortHexString(rfSpyResponse.raw))
if (rfSpyResponse.wasTimeout()) {
aapsLogger.info(LTag.PUMPCOMM, "wakeup: raw response is " + ByteUtil.shortHexString(rfSpyResponse?.raw))
if (rfSpyResponse?.wasTimeout() == true) {
aapsLogger.error(LTag.PUMPCOMM, "isDeviceReachable. Failed to find pump (timeout).")
} else if (rfSpyResponse.looksLikeRadioPacket()) {
} else if (rfSpyResponse?.looksLikeRadioPacket() == true) {
val radioResponse = RadioResponse(injector)
try {
radioResponse.init(rfSpyResponse.raw)
if (radioResponse.isValid) {
val pumpResponse = createResponseMessage(radioResponse.payload)
if (!pumpResponse.isValid) {
if (radioResponse.isValid()) {
val pumpResponse = createResponseMessage(radioResponse.getPayload())
if (!pumpResponse.isValid()) {
aapsLogger.warn(
LTag.PUMPCOMM, String.format(
Locale.ENGLISH, "Response is invalid ! [interrupted=%b, timeout=%b]", rfSpyResponse.wasInterrupted(),
@@ -181,14 +179,14 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
+ ByteUtil.shortHexString(rfSpyResponse.raw)
)
}
} catch (e: RileyLinkCommunicationException) {
} catch (_: RileyLinkCommunicationException) {
aapsLogger.warn(
LTag.PUMPCOMM, "isDeviceReachable. Failed to decode radio response: "
+ ByteUtil.shortHexString(rfSpyResponse.raw)
)
}
} else {
aapsLogger.warn(LTag.PUMPCOMM, "isDeviceReachable. Unknown response: " + ByteUtil.shortHexString(rfSpyResponse.raw))
aapsLogger.warn(LTag.PUMPCOMM, "isDeviceReachable. Unknown response: " + ByteUtil.shortHexString(rfSpyResponse?.raw))
}
return false
}
@@ -279,7 +277,7 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
firstResponse = runCommandWithArgs(getHistoryMsg)
failed = false
break
} catch (e: RileyLinkCommunicationException) {
} catch (_: RileyLinkCommunicationException) {
aapsLogger.error(LTag.PUMPCOMM, String.format(Locale.ENGLISH, "First call for PumpHistory failed (retry=%d)", retries))
failed = true
}
@@ -345,7 +343,7 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
try {
nextMsg = sendAndListen(ackMsg)
break
} catch (e: RileyLinkCommunicationException) {
} catch (_: RileyLinkCommunicationException) {
aapsLogger.error(LTag.PUMPCOMM, String.format(Locale.ENGLISH, "Problem acknowledging frame response. (retry=%d)", retries))
}
}
@@ -432,11 +430,6 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
return sendAndListen(msg, 4000) // 2000
}

// All pump communications go through this function.
@Throws(RileyLinkCommunicationException::class) private /*override*/ fun sendAndListen(msg: PumpMessage, timeout_ms: Int): PumpMessage {
return super.sendAndListen(msg, timeout_ms)!!
}

private inline fun <reified T> sendAndGetResponseWithCheck(
commandType: MedtronicCommandType,
bodyData: ByteArray? = null,
@@ -485,7 +478,7 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
}

private fun checkResponseContent(response: PumpMessage, method: String, expectedLength: Int): String? {
if (!response.isValid) {
if (!response.isValid()) {
val responseData = String.format("%s: Invalid response.", method)
aapsLogger.warn(LTag.PUMPCOMM, responseData)
return responseData
Original file line number Diff line number Diff line change
@@ -2,10 +2,10 @@ package app.aaps.pump.medtronic.comm.ui

import app.aaps.core.interfaces.logging.AAPSLogger
import app.aaps.core.interfaces.logging.LTag
import dagger.android.HasAndroidInjector
import app.aaps.pump.medtronic.comm.MedtronicCommunicationManager
import app.aaps.pump.medtronic.defs.MedtronicCommandType
import app.aaps.pump.medtronic.util.MedtronicUtil
import dagger.android.HasAndroidInjector
import javax.inject.Inject

/**
@@ -40,6 +40,6 @@ class MedtronicUIComm @Inject constructor(
}

val invalidResponsesCount: Int
get() = medtronicCommunicationManager.notConnectedCount
get() = medtronicCommunicationManager.getNotConnectedCount()

}
Original file line number Diff line number Diff line change
@@ -211,7 +211,7 @@ class MedtronicHistoryData @Inject constructor(
var dt: LocalDateTime? = null
try {
dt = DateTimeUtil.toLocalDateTime(pheLast.atechDateTime)
} catch (ex: Exception) {
} catch (_: Exception) {
aapsLogger.error("Problem decoding date from last record: $pheLast")
}
if (dt != null) {
Original file line number Diff line number Diff line change
@@ -9,10 +9,7 @@ import org.joda.time.LocalDateTime
class RLHistoryItemMedtronic(private val medtronicCommandType: MedtronicCommandType) :
RLHistoryItem(LocalDateTime(), RLHistoryItemSource.MedtronicCommand, RileyLinkTargetDevice.MedtronicPump) {

override fun getDescription(rh: ResourceHelper): String {
return if (RLHistoryItemSource.MedtronicCommand == source) {
medtronicCommandType.name
} else super.getDescription(rh)
}

override fun getDescription(rh: ResourceHelper): String =
if (RLHistoryItemSource.MedtronicCommand == source) medtronicCommandType.name
else super.getDescription(rh)
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import app.aaps.core.interfaces.resources.ResourceHelper
import app.aaps.core.interfaces.rx.bus.RxBus
import app.aaps.core.interfaces.sharedPreferences.SP
import app.aaps.pump.common.data.PumpStatus
import app.aaps.pump.common.events.EventRileyLinkDeviceStatusChange
import app.aaps.pump.common.hw.rileylink.RileyLinkUtil
import app.aaps.pump.common.hw.rileylink.data.RLHistoryItem
import app.aaps.pump.common.hw.rileylink.defs.RileyLinkTargetDevice
@@ -14,7 +15,6 @@ import app.aaps.pump.medtronic.defs.BasalProfileStatus
import app.aaps.pump.medtronic.defs.BatteryType
import app.aaps.pump.medtronic.defs.MedtronicDeviceType
import app.aaps.pump.medtronic.util.MedtronicConst
import app.aaps.pump.common.events.EventRileyLinkDeviceStatusChange
import java.util.Calendar
import java.util.GregorianCalendar
import javax.inject.Inject
@@ -26,13 +26,13 @@ import javax.inject.Singleton
@Singleton
class MedtronicPumpStatus @Inject constructor(
private val rh: ResourceHelper,
private val sp: SP,
sp: SP,
private val rxBus: RxBus,
private val rileyLinkUtil: RileyLinkUtil
) : PumpStatus(PumpType.MEDTRONIC_522_722) {

var errorDescription: String? = null
lateinit var serialNumber: String //? = null
var serialNumber: String = sp.getString(MedtronicConst.Prefs.PumpSerial, "000000")
var pumpFrequency: String? = null
var maxBolus: Double? = null
var maxBasal: Double? = null
@@ -53,18 +53,14 @@ class MedtronicPumpStatus @Inject constructor(
var basalProfileStatus = BasalProfileStatus.NotInitialized
var batteryType = BatteryType.None

override fun initSettings() {
init {
activeProfileName = "STD"
reservoirRemainingUnits = 75.0
batteryRemaining = 75
if (medtronicPumpMap.isEmpty()) createMedtronicPumpMap()
if (medtronicDeviceTypeMap.isEmpty()) createMedtronicDeviceTypeMap()
lastConnection = sp.getLong(MedtronicConst.Statistics.LastGoodPumpCommunicationTime, 0L)
lastDataTime = lastConnection
val serial = sp.getStringOrNull(MedtronicConst.Prefs.PumpSerial, null)
if (serial != null) {
serialNumber = serial
}
}

private fun createMedtronicDeviceTypeMap() {
@@ -105,7 +101,7 @@ class MedtronicPumpStatus @Inject constructor(
}

// Battery type
private var batteryTypeByDescMap: MutableMap<String, BatteryType?> = HashMap()
private var batteryTypeByDescMap: MutableMap<String, BatteryType> = HashMap()

fun getBatteryTypeByDescription(batteryTypeStr: String?): BatteryType {
if (batteryTypeByDescMap.isEmpty()) {
@@ -116,8 +112,7 @@ class MedtronicPumpStatus @Inject constructor(
return batteryTypeByDescMap[batteryTypeStr] ?: BatteryType.None
}

override val errorInfo: String
get() = if (errorDescription == null) "-" else errorDescription!!
override val errorInfo: String get() = errorDescription ?: "-"

val tbrRemainingTime: Int?
get() {
@@ -136,8 +131,4 @@ class MedtronicPumpStatus @Inject constructor(
val timeMinutes = (tempBasalEnd!! - System.currentTimeMillis()) / (1000 * 60)
return timeMinutes.toInt()
}

init {
initSettings()
}
}
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ class RileyLinkMedtronicService : RileyLinkService() {

/* private functions */ // PumpInterface - REMOVE
val isInitialized: Boolean
get() = rileyLinkServiceData.rileyLinkServiceState.isReady
get() = rileyLinkServiceData.rileyLinkServiceState.isReady()

override fun verifyConfiguration(forceRileyLinkAddressRenewal: Boolean): Boolean {
return try {
@@ -251,7 +251,7 @@ class RileyLinkMedtronicService : RileyLinkService() {
rileyLinkAddressChanged = false
}
if (encodingChanged) {
changeRileyLinkEncoding(encodingType)
changeRileyLinkEncoding(encodingType!!)
encodingChanged = false
}
}
@@ -269,7 +269,7 @@ class RileyLinkMedtronicService : RileyLinkService() {
val value = sp.getString(key, defaultValue)
valueDouble = try {
value.toDouble()
} catch (ex: Exception) {
} catch (_: Exception) {
aapsLogger.error("Error parsing setting: %s, value found %s", key, value)
defaultValueDouble
}
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import org.mockito.Mock

open class MedtronicTestBase : TestBaseWithProfile() {

var rileyLinkUtil = RileyLinkUtil()
var rileyLinkUtil = RileyLinkUtil(aapsLogger)

@Mock lateinit var pumpSync: PumpSync
@Mock lateinit var pumpSyncStorage: PumpSyncStorage
Original file line number Diff line number Diff line change
@@ -663,7 +663,7 @@ public double getReservoirLevel() {
@Override
public int getBatteryLevel() {
if (aapsOmnipodErosManager.isShowRileyLinkBatteryLevel()) {
return Optional.ofNullable(rileyLinkServiceData.batteryLevel).orElse(0);
return Optional.ofNullable(rileyLinkServiceData.getBatteryLevel()).orElse(0);
}

return 0;
@@ -1118,7 +1118,7 @@ private <T> T executeCommand(OmnipodCommandType commandType, Supplier<T> supplie
try {
aapsLogger.debug(LTag.PUMP, "Executing command: {}", commandType);

rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItemOmnipod(injector, commandType));
rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItemOmnipod(commandType));

return supplier.get();
} finally {
Loading