Skip to content

Commit

Permalink
- refactorings kotlin !!
Browse files Browse the repository at this point in the history
- moved 2 filed into pump-common module (newly created)
  • Loading branch information
andyrozman committed May 24, 2021
1 parent fb5ff40 commit e24af39
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 148 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ dependencies {
implementation project(':danars')
implementation project(':danar')
implementation project(':insight')
implementation project(':pump-common')
implementation project(':rileylink')
implementation project(':medtronic')
implementation project(':omnipod-common')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,36 @@ open class AppModule {
pumpSync: PumpSync,
sp: SP,
aapsLogger: AAPSLogger
): PumpSyncStorage {
return PumpSyncStorage(pumpSync, sp, aapsLogger)
): info.nightscout.androidaps.plugins.pump.common.sync.PumpSyncStorage {
return info.nightscout.androidaps.plugins.pump.common.sync.PumpSyncStorage(pumpSync, sp, aapsLogger)
}

@Module
interface AppBindings {

@Binds fun bindContext(mainApp: MainApp): Context
@Binds fun bindInjector(mainApp: MainApp): HasAndroidInjector
@Binds fun bindActivePluginProvider(pluginStore: PluginStore): ActivePlugin
@Binds fun bindCommandQueueProvider(commandQueue: CommandQueue): CommandQueueProvider
@Binds fun bindConfigInterface(config: ConfigImpl): Config
@Binds fun bindConfigBuilderInterface(configBuilderPlugin: ConfigBuilderPlugin): ConfigBuilder
@Binds
fun bindConfigBuilderInterface(configBuilderPlugin: ConfigBuilderPlugin): ConfigBuilder
@Binds fun bindTreatmentsInterface(treatmentsPlugin: TreatmentsPlugin): TreatmentsInterface
@Binds fun bindDatabaseHelperInterface(databaseHelperProvider: DatabaseHelperProvider): DatabaseHelperInterface
@Binds fun bindNotificationHolderInterface(notificationHolder: NotificationHolderImpl): NotificationHolder
@Binds fun bindImportExportPrefsInterface(importExportPrefs: ImportExportPrefsImpl): ImportExportPrefs
@Binds fun bindIconsProviderInterface(iconsProvider: IconsProviderImplementation): IconsProvider
@Binds
fun bindDatabaseHelperInterface(databaseHelperProvider: DatabaseHelperProvider): DatabaseHelperInterface
@Binds
fun bindNotificationHolderInterface(notificationHolder: NotificationHolderImpl): NotificationHolder
@Binds
fun bindImportExportPrefsInterface(importExportPrefs: ImportExportPrefsImpl): ImportExportPrefs
@Binds
fun bindIconsProviderInterface(iconsProvider: IconsProviderImplementation): IconsProvider
@Binds fun bindLoopInterface(loopPlugin: LoopPlugin): LoopInterface
@Binds fun bindIobCobCalculatorInterface(iobCobCalculatorPlugin: IobCobCalculatorPlugin): IobCobCalculator
@Binds fun bindSmsCommunicatorInterface(smsCommunicatorPlugin: SmsCommunicatorPlugin): SmsCommunicator
@Binds fun bindDataSyncSelector(dataSyncSelectorImplementation: DataSyncSelectorImplementation): DataSyncSelector
@Binds
fun bindIobCobCalculatorInterface(iobCobCalculatorPlugin: IobCobCalculatorPlugin): IobCobCalculator
@Binds
fun bindSmsCommunicatorInterface(smsCommunicatorPlugin: SmsCommunicatorPlugin): SmsCommunicator
@Binds
fun bindDataSyncSelector(dataSyncSelectorImplementation: DataSyncSelectorImplementation): DataSyncSelector
@Binds fun bindPumpSync(pumpSyncImplementation: PumpSyncImplementation): PumpSync

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum class PumpHistoryEntryGroup(val resourceId: Int) {
}

companion object {

private var translatedList: MutableList<PumpHistoryEntryGroup>? = null

private fun doTranslation(resourceHelper: ResourceHelper) {
Expand All @@ -43,9 +44,9 @@ enum class PumpHistoryEntryGroup(val resourceId: Int) {
}

@JvmStatic
fun getTranslatedList(resourceHelper: ResourceHelper): List<PumpHistoryEntryGroup>? {
fun getTranslatedList(resourceHelper: ResourceHelper): List<PumpHistoryEntryGroup> {
if (translatedList == null) doTranslation(resourceHelper)
return translatedList
return translatedList!!
}
}

Expand Down
1 change: 1 addition & 0 deletions medtronic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ android {

dependencies {
implementation project(':core')
implementation project(':pump-common')
implementation project(':rileylink')
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class MedtronicPumpPlugin @Inject constructor(
dateUtil: DateUtil,
aapsSchedulers: AapsSchedulers,
pumpSync: PumpSync,
pumpSyncStorage: PumpSyncStorage
) : PumpPluginAbstract(PluginDescription() //
pumpSyncStorage: info.nightscout.androidaps.plugins.pump.common.sync.PumpSyncStorage
) : info.nightscout.androidaps.plugins.pump.common.PumpPluginAbstract(PluginDescription() //
.mainType(PluginType.PUMP) //
.fragmentClass(MedtronicFragment::class.java.name) //
.pluginIcon(R.drawable.ic_veo_128)
Expand All @@ -101,7 +101,7 @@ class MedtronicPumpPlugin @Inject constructor(
.description(R.string.description_pump_medtronic), //
PumpType.MEDTRONIC_522_722, // we default to most basic model, correct model from config is loaded later
injector, resourceHelper, aapsLogger, commandQueue, rxBus, activePlugin, sp, context, fabricPrivacy, dateUtil, aapsSchedulers, pumpSync, pumpSyncStorage
), Pump, RileyLinkPumpDevice, PumpSyncEntriesCreator {
), Pump, RileyLinkPumpDevice, info.nightscout.androidaps.plugins.pump.common.sync.PumpSyncEntriesCreator {

private lateinit var rileyLinkMedtronicService: RileyLinkMedtronicService

Expand Down Expand Up @@ -199,7 +199,7 @@ class MedtronicPumpPlugin @Inject constructor(
SystemClock.sleep(60000)
if (this.isInitialized) {
val statusRefresh = workWithStatusRefresh(
StatusRefreshAction.GetData, null, null)
StatusRefreshAction.GetData, null, null)!!
if (doWeHaveAnyStatusNeededRefereshing(statusRefresh)) {
if (!commandQueue.statusInQueue()) {
commandQueue.readStatus("Scheduled Status Refresh", null)
Expand All @@ -214,7 +214,7 @@ class MedtronicPumpPlugin @Inject constructor(
override val serviceClass: Class<*>
get() = RileyLinkMedtronicService::class.java

override val pumpStatusData: PumpStatus
override val pumpStatusData: info.nightscout.androidaps.plugins.pump.common.data.PumpStatus
get() = medtronicPumpStatus

override fun deviceID(): String {
Expand Down Expand Up @@ -334,7 +334,7 @@ class MedtronicPumpPlugin @Inject constructor(

private fun refreshAnyStatusThatNeedsToBeRefreshed() {
val statusRefresh = workWithStatusRefresh(StatusRefreshAction.GetData, null,
null)
null)!!
if (!doWeHaveAnyStatusNeededRefereshing(statusRefresh)) {
return
}
Expand All @@ -354,7 +354,7 @@ class MedtronicPumpPlugin @Inject constructor(

// execute
val refreshTypesNeededToReschedule: MutableSet<MedtronicStatusRefreshType> = mutableSetOf()
for ((key, value) in statusRefresh!!) {
for ((key, value) in statusRefresh) {
if (value > 0 && System.currentTimeMillis() > value) {
when (key) {
MedtronicStatusRefreshType.PumpHistory -> {
Expand Down Expand Up @@ -389,8 +389,8 @@ class MedtronicPumpPlugin @Inject constructor(
if (resetTime) medtronicPumpStatus.setLastCommunicationToNow()
}

private fun doWeHaveAnyStatusNeededRefereshing(statusRefresh: Map<MedtronicStatusRefreshType, Long>?): Boolean {
for ((_, value) in statusRefresh!!) {
private fun doWeHaveAnyStatusNeededRefereshing(statusRefresh: Map<MedtronicStatusRefreshType, Long>): Boolean {
for ((_, value) in statusRefresh) {
if (value > 0 && System.currentTimeMillis() > value) {
return true
}
Expand Down Expand Up @@ -583,10 +583,10 @@ class MedtronicPumpPlugin @Inject constructor(
scheduleNextRefresh(MedtronicStatusRefreshType.PumpTime, 0)
}

override fun deliverBolus(detailedBolusInfo: DetailedBolusInfo?): PumpEnactResult {
override fun deliverBolus(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult {
aapsLogger.info(LTag.PUMP, "MedtronicPumpPlugin::deliverBolus - " + BolusDeliveryType.DeliveryPrepared)
setRefreshButtonEnabled(false)
if (detailedBolusInfo!!.insulin > medtronicPumpStatus.reservoirRemainingUnits) {
if (detailedBolusInfo.insulin > medtronicPumpStatus.reservoirRemainingUnits) {
return PumpEnactResult(injector) //
.success(false) //
.enacted(false) //
Expand Down Expand Up @@ -621,7 +621,12 @@ class MedtronicPumpPlugin @Inject constructor(
setRefreshButtonEnabled(true)

// LOG.debug("MedtronicPumpPlugin::deliverBolus - Response: {}", response);
if (response!!) {
return if (response == null || !response) {
PumpEnactResult(injector) //
.success(bolusDeliveryType == BolusDeliveryType.CancelDelivery) //
.enacted(false) //
.comment(R.string.medtronic_cmd_bolus_could_not_be_delivered)
} else {
if (bolusDeliveryType == BolusDeliveryType.CancelDelivery) {
// LOG.debug("MedtronicPumpPlugin::deliverBolus - Delivery Canceled after Bolus started.");
Thread(Runnable {
Expand All @@ -648,11 +653,6 @@ class MedtronicPumpPlugin @Inject constructor(
.enacted(true) //
.bolusDelivered(detailedBolusInfo.insulin) //
.carbsDelivered(detailedBolusInfo.carbs)
} else {
PumpEnactResult(injector) //
.success(bolusDeliveryType == BolusDeliveryType.CancelDelivery) //
.enacted(false) //
.comment(R.string.medtronic_cmd_bolus_could_not_be_delivered)
}
} finally {
finishAction("Bolus")
Expand Down Expand Up @@ -739,13 +739,13 @@ class MedtronicPumpPlugin @Inject constructor(
// CANCEL
val responseTask2 = rileyLinkMedtronicService.medtronicUIComm.executeCommand(MedtronicCommandType.CancelTBR)
val response = responseTask2.result as Boolean?
if (response!!) {
aapsLogger.info(LTag.PUMP, logPrefix + "setTempBasalAbsolute - Current TBR cancelled.")
} else {
if (response == null || !response) {
aapsLogger.error(logPrefix + "setTempBasalAbsolute - Cancel TBR failed.")
finishAction("TBR")
return PumpEnactResult(injector).success(false).enacted(false)
.comment(R.string.medtronic_cmd_cant_cancel_tbr_stop_op)
} else {
aapsLogger.info(LTag.PUMP, logPrefix + "setTempBasalAbsolute - Current TBR cancelled.")
}
}

Expand All @@ -763,7 +763,7 @@ class MedtronicPumpPlugin @Inject constructor(
medtronicPumpStatus.tempBasalAmount = absoluteRate
medtronicPumpStatus.tempBasalLength = durationInMinutes

val tempData = PumpDbEntryTBR(absoluteRate, true, durationInMinutes, tbrType)
val tempData = info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR(absoluteRate, true, durationInMinutes, tbrType)

pumpSyncStorage.addTemporaryBasalRateWithTempId(tempData, true, this)

Expand Down Expand Up @@ -956,10 +956,13 @@ class MedtronicPumpPlugin @Inject constructor(
val tbr = responseTask.result as TempBasalPair?

// we sometimes get rate returned even if TBR is no longer running
if (tbr!!.durationMinutes == 0) {
tbr.insulinRate = 0.0
}
tbr
if (tbr != null) {
if (tbr.durationMinutes == 0) {
tbr.insulinRate = 0.0
}
tbr
} else
null
} else {
null
}
Expand Down Expand Up @@ -992,7 +995,11 @@ class MedtronicPumpPlugin @Inject constructor(
val responseTask2 = rileyLinkMedtronicService.medtronicUIComm.executeCommand(MedtronicCommandType.CancelTBR)
val response = responseTask2.result as Boolean?
finishAction("TBR")
return if (response!!) {
return if (response == null || !response) {
aapsLogger.info(LTag.PUMP, logPrefix + "cancelTempBasal - Cancel TBR failed.")
PumpEnactResult(injector).success(false).enacted(false) //
.comment(R.string.medtronic_cmd_cant_cancel_tbr)
} else {
aapsLogger.info(LTag.PUMP, logPrefix + "cancelTempBasal - Cancel TBR successful.")

val runningTBR = medtronicPumpStatus.runningTBR
Expand Down Expand Up @@ -1022,19 +1029,13 @@ class MedtronicPumpPlugin @Inject constructor(
}
}

//pumpSyncStorage.addTemporaryBasalRateWithTempId(tempData, true, this)

PumpEnactResult(injector).success(true).enacted(true) //
.isTempCancel(true)
} else {
aapsLogger.info(LTag.PUMP, logPrefix + "cancelTempBasal - Cancel TBR failed.")
PumpEnactResult(injector).success(response).enacted(response) //
.comment(R.string.medtronic_cmd_cant_cancel_tbr)
}
}

override fun manufacturer(): ManufacturerType {
return pumpDescription.pumpType.manufacturer!!
return ManufacturerType.Medtronic
}

override fun model(): PumpType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
medtronicUtil.medtronicPumpModel = pumpModel!!
medtronicUtil.isModelSet = true
}
aapsLogger.debug(LTag.PUMPCOMM, String.format(Locale.ENGLISH, "isDeviceReachable. PumpModel is %s - Valid: %b (rssi=%d)", pumpModel!!.name, valid,
aapsLogger.debug(LTag.PUMPCOMM, String.format(Locale.ENGLISH, "isDeviceReachable. PumpModel is %s - Valid: %b (rssi=%d)", medtronicUtil.medtronicPumpModel, valid,
radioResponse.rssi))
if (valid) {
if (state === PumpDeviceState.PumpUnreachable)
Expand Down Expand Up @@ -313,7 +313,9 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth
aapsLogger.error(LTag.PUMPCOMM, String.format(Locale.ENGLISH, "Problem acknowledging frame response. (retry=%d)", retries))
}
}
if (nextMsg != null) currentResponse = GetHistoryPageCarelinkMessageBody(nextMsg.messageBody!!.txData) else {
if (nextMsg != null)
currentResponse = GetHistoryPageCarelinkMessageBody(nextMsg.messageBody!!.txData)
else {
aapsLogger.error(LTag.PUMPCOMM, "We couldn't acknowledge frame from pump, aborting operation.")
}
}
Expand Down Expand Up @@ -602,7 +604,7 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth

fun getTemporaryBasal(): TempBasalPair? {
return sendAndGetResponseWithCheck(MedtronicCommandType.ReadTemporaryBasal) { _, _, rawContent ->
TempBasalPair(aapsLogger, rawContent!!)
TempBasalPair(aapsLogger, rawContent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MedtronicPumpHistoryDecoder @Inject constructor(
if (decoded === RecordDecodeStatus.OK || decoded === RecordDecodeStatus.Ignored) {
//Log.i(TAG, "#" + record + " " + decoded.getDescription() + " " + pe);
} else {
aapsLogger.warn(LTag.PUMPBTCOMM, "#" + record + " " + decoded!!.description + " " + pe)
aapsLogger.warn(LTag.PUMPBTCOMM, "#" + record + " " + decoded.description + " " + pe)
}
addToStatistics(pe, decoded, null)
record++
Expand All @@ -130,7 +130,7 @@ class MedtronicPumpHistoryDecoder @Inject constructor(
return outList
}

override fun decodeRecord(record: PumpHistoryEntry): RecordDecodeStatus? {
override fun decodeRecord(record: PumpHistoryEntry): RecordDecodeStatus {
return try {
decodeRecordInternal(record)
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MedtronicUIPostprocessor @Inject constructor(
when (uiTask.commandType) {
MedtronicCommandType.SetBasalProfileSTD -> {
val response = uiTask.result as Boolean?
if (response!!) {
if (response != null && response) {
val basalProfile = uiTask.getParameter(0) as BasalProfile
aapsLogger.debug("D: basal profile returned after set: $basalProfile")

Expand Down
Loading

0 comments on commit e24af39

Please sign in to comment.