diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt index 5b86577182..3f6bb023a7 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.kt @@ -61,6 +61,8 @@ import org.joda.time.LocalDateTime import java.util.* import javax.inject.Inject import javax.inject.Singleton +import kotlin.math.abs +import kotlin.math.floor /** * Created by andy on 23.04.18. @@ -125,7 +127,7 @@ class MedtronicPumpPlugin @Inject constructor( rileyLinkMedtronicService = mLocalBinder.serviceInstance isServiceSet = true rileyLinkMedtronicService.verifyConfiguration() - Thread(Runnable { + Thread { for (i in 0..19) { SystemClock.sleep(5000) aapsLogger.debug(LTag.PUMP, "Starting Medtronic-RileyLink service") @@ -133,7 +135,7 @@ class MedtronicPumpPlugin @Inject constructor( break } } - }).start() + }.start() } } super.onStart() @@ -156,7 +158,7 @@ class MedtronicPumpPlugin @Inject constructor( medtronicPumpStatus.previousConnection = medtronicPumpStatus.lastConnection //if (rileyLinkMedtronicService != null) rileyLinkMedtronicService.verifyConfiguration(); - aapsLogger.debug(LTag.PUMP, "initPumpStatusData: " + medtronicPumpStatus) + aapsLogger.debug(LTag.PUMP, "initPumpStatusData: $medtronicPumpStatus") // this is only thing that can change, by being configured pumpDescription.maxTempAbsolute = if (medtronicPumpStatus.maxBasal != null) medtronicPumpStatus.maxBasal!! else 35.0 @@ -192,13 +194,14 @@ class MedtronicPumpPlugin @Inject constructor( override fun onStartCustomActions() { // check status every minute (if any status needs refresh we send readStatus command) - Thread(Runnable { + Thread { do { SystemClock.sleep(60000) if (this.isInitialized) { val statusRefresh = workWithStatusRefresh( - StatusRefreshAction.GetData, null, null)!! - if (doWeHaveAnyStatusNeededRefereshing(statusRefresh)) { + StatusRefreshAction.GetData, null, null + )!! + if (doWeHaveAnyStatusNeededRefreshing(statusRefresh)) { if (!commandQueue.statusInQueue()) { commandQueue.readStatus("Scheduled Status Refresh", null) } @@ -206,7 +209,7 @@ class MedtronicPumpPlugin @Inject constructor( clearBusyQueue() } } while (serviceRunning) - }).start() + }.start() } override val serviceClass: Class<*> @@ -333,7 +336,7 @@ class MedtronicPumpPlugin @Inject constructor( private fun refreshAnyStatusThatNeedsToBeRefreshed() { val statusRefresh = workWithStatusRefresh(StatusRefreshAction.GetData, null, null)!! - if (!doWeHaveAnyStatusNeededRefereshing(statusRefresh)) { + if (!doWeHaveAnyStatusNeededRefreshing(statusRefresh)) { return } var resetTime = false @@ -387,7 +390,7 @@ class MedtronicPumpPlugin @Inject constructor( if (resetTime) medtronicPumpStatus.setLastCommunicationToNow() } - private fun doWeHaveAnyStatusNeededRefereshing(statusRefresh: Map): Boolean { + private fun doWeHaveAnyStatusNeededRefreshing(statusRefresh: Map): Boolean { for ((_, value) in statusRefresh) { if (value > 0 && System.currentTimeMillis() > value) { return true @@ -445,7 +448,7 @@ class MedtronicPumpPlugin @Inject constructor( basalProfiles val errorCount = rileyLinkMedtronicService.medtronicUIComm.invalidResponsesCount if (errorCount >= 5) { - aapsLogger.error("Number of error counts was 5 or more. Starting tunning.") + aapsLogger.error("Number of error counts was 5 or more. Starting tuning.") setRefreshButtonEnabled(true) serviceTaskExecutor.startTask(WakeAndTuneTask(injector)) return true @@ -471,7 +474,7 @@ class MedtronicPumpPlugin @Inject constructor( @Synchronized override fun isThisProfileSet(profile: Profile): Boolean { - aapsLogger.debug(LTag.PUMP, "isThisProfileSet: basalInitalized=" + medtronicPumpStatus.basalProfileStatus) + aapsLogger.debug(LTag.PUMP, "isThisProfileSet: basalInitialized=" + medtronicPumpStatus.basalProfileStatus) if (!isInitialized) return true if (medtronicPumpStatus.basalProfileStatus === BasalProfileStatus.NotInitialized) { // this shouldn't happen, but if there was problem we try again @@ -561,7 +564,7 @@ class MedtronicPumpPlugin @Inject constructor( clock = medtronicUtil.pumpTime } if (clock == null) return - val timeDiff = Math.abs(clock.timeDifference) + val timeDiff = abs(clock.timeDifference) if (timeDiff > 20) { if (clock.localDeviceTime.year <= 2015 || timeDiff <= 24 * 60 * 60) { aapsLogger.info(LTag.PUMP, String.format(Locale.ENGLISH, "MedtronicPumpPlugin::checkTimeAndOptionallySetTime - Time difference is %d s. Set time on pump.", timeDiff)) @@ -597,12 +600,12 @@ class MedtronicPumpPlugin @Inject constructor( bolusDeliveryType = BolusDeliveryType.DeliveryPrepared if (isPumpNotReachable) { aapsLogger.debug(LTag.PUMP, "MedtronicPumpPlugin::deliverBolus - Pump Unreachable.") - return setNotReachable(true, false) + return setNotReachable(isBolus = true, success = false) } medtronicUtil.dismissNotification(MedtronicNotificationType.PumpUnreachable, rxBus) if (bolusDeliveryType == BolusDeliveryType.CancelDelivery) { // LOG.debug("MedtronicPumpPlugin::deliverBolus - Delivery Canceled."); - return setNotReachable(true, true) + return setNotReachable(isBolus = true, success = true) } // LOG.debug("MedtronicPumpPlugin::deliverBolus - Starting wait period."); @@ -610,7 +613,7 @@ class MedtronicPumpPlugin @Inject constructor( SystemClock.sleep(sleepTime.toLong()) return if (bolusDeliveryType == BolusDeliveryType.CancelDelivery) { // LOG.debug("MedtronicPumpPlugin::deliverBolus - Delivery Canceled, before wait period."); - setNotReachable(true, true) + setNotReachable(isBolus = true, success = true) } else try { bolusDeliveryType = BolusDeliveryType.Delivering @@ -629,10 +632,10 @@ class MedtronicPumpPlugin @Inject constructor( } else { if (bolusDeliveryType == BolusDeliveryType.CancelDelivery) { // LOG.debug("MedtronicPumpPlugin::deliverBolus - Delivery Canceled after Bolus started."); - Thread(Runnable { + Thread { SystemClock.sleep(2000) runAlarm(context, rh.gs(R.string.medtronic_cmd_cancel_bolus_not_supported), rh.gs(R.string.medtronic_warning), R.raw.boluserror) - }).start() + }.start() } val now = System.currentTimeMillis() detailedBolusInfo.timestamp = now @@ -662,6 +665,7 @@ class MedtronicPumpPlugin @Inject constructor( // LOG.debug("MedtronicPumpPlugin::deliverBolus - End wait period. Start delivery"); } + @Suppress("SameParameterValue") private fun setNotReachable(isBolus: Boolean, success: Boolean): PumpEnactResult { setRefreshButtonEnabled(true) if (isBolus) { @@ -786,12 +790,12 @@ class MedtronicPumpPlugin @Inject constructor( var item : PumpDbEntryTBR? = null if (tbrs.size==1) { - item = tbrs.get(0); + item = tbrs[0] } else { for (tbr in tbrs) { if (tbr.date == medtronicPumpStatus.runningTBRWithTemp!!.date) { item = tbr - break; + break } } } @@ -909,7 +913,7 @@ class MedtronicPumpPlugin @Inject constructor( } else { // all other reads if (debugHistory) aapsLogger.debug(LTag.PUMP, logPrefix + "readPumpHistoryLogic(): lastPumpHistoryEntry: not null - " + medtronicUtil.gsonInstance.toJson(lastPumpHistoryEntry)) medtronicHistoryData.setIsInInit(false) - // we need to read 35 minutes in the past so that we can repair any TBR or Bolus values if neeeded + // we need to read 35 minutes in the past so that we can repair any TBR or Bolus values if needed targetDate = LocalDateTime(DateTimeUtil.getMillisFromATDWithAddedMinutes(lastPumpHistoryEntry!!.atechDateTime, -35)) } @@ -930,7 +934,7 @@ class MedtronicPumpPlugin @Inject constructor( medtronicHistoryData.addNewHistory(historyResult) medtronicHistoryData.filterNewEntries() - // determine if first run, if yes detrmine how much of update do we need + // determine if first run, if yes determine how much of update do we need // - first run: // - get last history entry // - if not there download 1.5 days of data @@ -965,12 +969,11 @@ class MedtronicPumpPlugin @Inject constructor( } } - private fun scheduleNextRefresh(refreshType: MedtronicStatusRefreshType?, additionalTimeInMinutes: Int = 0) { + private fun scheduleNextRefresh(refreshType: MedtronicStatusRefreshType, additionalTimeInMinutes: Int = 0) { when (refreshType) { MedtronicStatusRefreshType.RemainingInsulin -> { val remaining = medtronicPumpStatus.reservoirRemainingUnits - val min: Int - min = if (remaining > 50) 4 * 60 else if (remaining > 20) 60 else 15 + val min: Int = if (remaining > 50) 4 * 60 else if (remaining > 20) 60 else 15 workWithStatusRefresh(StatusRefreshAction.Add, refreshType, getTimeInFutureFromMinutes(min)) } @@ -1049,7 +1052,7 @@ class MedtronicPumpPlugin @Inject constructor( return PumpEnactResult(injector).success(true).enacted(false) } } else { - aapsLogger.warn(LTag.PUMP, logPrefix + "cancelTempBasal - Could not read currect TBR, canceling operation.") + aapsLogger.warn(LTag.PUMP, logPrefix + "cancelTempBasal - Could not read current TBR, canceling operation.") finishAction("TBR") return PumpEnactResult(injector).success(false).enacted(false) .comment(R.string.medtronic_cmd_cant_read_tbr) @@ -1082,7 +1085,7 @@ class MedtronicPumpPlugin @Inject constructor( runningTBR.pumpType, runningTBR.serialNumber) - val differenceTimeMin = Math.floor(differenceTime / (60.0 * 1000.0)) + val differenceTimeMin = floor(differenceTime / (60.0 * 1000.0)) aapsLogger.debug(LTag.PUMP, "canceling running TBR - syncTemporaryBasalWithPumpId [date=${runningTBR.date}, " + "pumpId=${runningTBR.pumpId}, rate=${runningTBR.rate} U, duration=${differenceTimeMin.toInt()}, " + @@ -1160,7 +1163,7 @@ class MedtronicPumpPlugin @Inject constructor( stringBuilder.append(profileEntry.rate) } } - return if (stringBuilder.length == 0) null else stringBuilder.toString() + return if (stringBuilder.isEmpty()) null else stringBuilder.toString() } private fun convertProfileToMedtronicProfile(profile: Profile): BasalProfile { @@ -1186,7 +1189,7 @@ class MedtronicPumpPlugin @Inject constructor( override fun getCustomActions(): List? { if (customActions == null) { - customActions = Arrays.asList(customActionWakeUpAndTune, // + customActions = listOf(customActionWakeUpAndTune, // customActionClearBolusBlock, // customActionResetRLConfig) } @@ -1194,8 +1197,7 @@ class MedtronicPumpPlugin @Inject constructor( } override fun executeCustomAction(customActionType: CustomActionType) { - val mcat = customActionType as? MedtronicCustomActionType - when (mcat) { + when (customActionType as? MedtronicCustomActionType) { MedtronicCustomActionType.WakeUpAndTune -> { if (rileyLinkMedtronicService.verifyConfiguration()) { serviceTaskExecutor.startTask(WakeAndTuneTask(injector)) @@ -1229,6 +1231,7 @@ class MedtronicPumpPlugin @Inject constructor( return sp.getBoolean(R.string.key_set_neutral_temps, true) } + @Suppress("SameParameterValue") private fun setEnableCustomAction(customAction: MedtronicCustomActionType, isEnabled: Boolean) { if (customAction === MedtronicCustomActionType.ClearBolusBlock) { customActionClearBolusBlock.isEnabled = isEnabled diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryEntry.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryEntry.kt index 8776906d95..39ab042651 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryEntry.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/history/MedtronicHistoryEntry.kt @@ -164,7 +164,7 @@ abstract class MedtronicHistoryEntry : MedtronicHistoryEntryInterface { sb.append("]") return sb.toString() } - if (head!=null && head.size != 0) { + if (head.isNotEmpty()) { sb.append(", head=") sb.append(ByteUtil.shortHexString(head)) } diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt index edd3de2c35..d5036a412d 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt @@ -474,7 +474,7 @@ class MedtronicHistoryData @Inject constructor( for (bolus in entryList) { val bolusDTO = bolus.decodedData["Object"] as BolusDTO - var type: DetailedBolusInfo.BolusType = DetailedBolusInfo.BolusType.NORMAL + //var type: DetailedBolusInfo.BolusType = DetailedBolusInfo.BolusType.NORMAL var multiwave = false if (bolusDTO.bolusType == PumpBolusType.Extended) { @@ -502,7 +502,7 @@ class MedtronicHistoryData @Inject constructor( temporaryId = entryWithTempId.temporaryId pumpSyncStorage.removeBolusWithTemporaryId(temporaryId) boluses.remove(entryWithTempId) - type = entryWithTempId.bolusType + //type = entryWithTempId.bolusType } } @@ -584,7 +584,7 @@ class MedtronicHistoryData @Inject constructor( } else { if (oneMoreEntryFromHistory != null) { val tbrPrev = oneMoreEntryFromHistory.getDecodedDataEntry("Object") as TempBasalPair - if (tbrPrev.isZeroTBR) { // if we had Zere TBR in last previous TBR, then we need to limit it, so we need to process it too + if (tbrPrev.isZeroTBR) { // if we had Zero TBR in last previous TBR, then we need to limit it, so we need to process it too entryList.add(0, oneMoreEntryFromHistory) } } @@ -592,7 +592,7 @@ class MedtronicHistoryData @Inject constructor( val tbrRecords = pumpSyncStorage.getTBRs() - val processList: MutableList = createTBRProcessList(entryList); + val processList: MutableList = createTBRProcessList(entryList) if (processList.isNotEmpty()) { for (tempBasalProcessDTO in processList) { @@ -735,7 +735,7 @@ class MedtronicHistoryData @Inject constructor( for (tempBasalProcessDTO in processList) { if (previousItem!=null) { - var pheEnd = PumpHistoryEntry() + val pheEnd = PumpHistoryEntry() pheEnd.atechDateTime = DateTimeUtil.getATDWithAddedSeconds(tempBasalProcessDTO.itemOne.atechDateTime, -2) pheEnd.addDecodedData("Object", TempBasalPair(0.0, false, 0)) diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt index b4b3b6cefb..9f03c29619 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/dto/TempBasalProcessDTO.kt @@ -1,10 +1,8 @@ package info.nightscout.androidaps.plugins.pump.medtronic.data.dto import info.nightscout.androidaps.logging.AAPSLogger -import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry -import java.lang.StringBuilder class TempBasalProcessDTO constructor(var itemOne: PumpHistoryEntry, var aapsLogger: AAPSLogger, @@ -66,17 +64,15 @@ class TempBasalProcessDTO constructor(var itemOne: PumpHistoryEntry, if (itemTwo!=null) { stringBuilder.append(" - ") - stringBuilder.append(itemTwo!!.DT) + stringBuilder.append(itemTwo?.DT) } - var dur = durationAsSeconds - stringBuilder.append(" " + durationAsSeconds + " s (" + durationAsSeconds/60 + ")") if (itemTwoTbr!=null) { - stringBuilder.append(" " + itemOneTbr!!.insulinRate + " / " + itemTwoTbr!!.insulinRate) + stringBuilder.append(" " + itemOneTbr?.insulinRate + " / " + itemTwoTbr?.insulinRate) } else { - stringBuilder.append(" " + itemOneTbr!!.insulinRate) + stringBuilder.append(" " + itemOneTbr?.insulinRate) } return stringBuilder.toString() @@ -86,12 +82,6 @@ class TempBasalProcessDTO constructor(var itemOne: PumpHistoryEntry, return "ItemOne: $itemOne, ItemTwo: $itemTwo, Duration: $durationAsSeconds, ObjectType: $objectType" } - enum class Operation { - None, - Add, - Edit - } - enum class ObjectType { TemporaryBasal, Suspend, diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt index 2c79549a0d..d7a06ad405 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManagerImpl.kt @@ -244,7 +244,7 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() { return time?.let { return Duration.between(ZonedDateTime.now(), it) - } ?: null + } } override val timeZoneUpdated: Long?