MedtronicPumpPlugin -> kt

This commit is contained in:
Milos Kozak 2022-07-01 12:38:05 +02:00
parent 88072ea752
commit ad89d54319

View file

@ -201,9 +201,7 @@ class MedtronicPumpPlugin @Inject constructor(
do {
SystemClock.sleep(60000)
if (this.isInitialized) {
val statusRefresh = workWithStatusRefresh(
StatusRefreshAction.GetData, null, null
)!!
val statusRefresh = synchronized(statusRefreshMap) { HashMap(statusRefreshMap) }
if (doWeHaveAnyStatusNeededRefreshing(statusRefresh)) {
if (!commandQueue.statusInQueue()) {
commandQueue.readStatus(rh.gs(R.string.scheduled_status_refresh), null)
@ -316,10 +314,7 @@ class MedtronicPumpPlugin @Inject constructor(
}
private fun refreshAnyStatusThatNeedsToBeRefreshed() {
val statusRefresh = workWithStatusRefresh(
StatusRefreshAction.GetData, null,
null
)!!
val statusRefresh = synchronized(statusRefreshMap) { HashMap(statusRefreshMap) }
if (!doWeHaveAnyStatusNeededRefreshing(statusRefresh)) {
return
}
@ -659,19 +654,9 @@ class MedtronicPumpPlugin @Inject constructor(
@Suppress("SameParameterValue")
private fun setNotReachable(isBolus: Boolean, success: Boolean): PumpEnactResult {
setRefreshButtonEnabled(true)
if (isBolus) {
bolusDeliveryType = BolusDeliveryType.Idle
}
return if (success) {
PumpEnactResult(injector) //
.success(true) //
.enacted(false)
} else {
PumpEnactResult(injector) //
.success(false) //
.enacted(false) //
.comment(R.string.medtronic_pump_status_pump_unreachable)
}
if (isBolus) bolusDeliveryType = BolusDeliveryType.Idle
return if (success) PumpEnactResult(injector).success(true).enacted(false)
else PumpEnactResult(injector).success(false).enacted(false).comment(R.string.medtronic_pump_status_pump_unreachable)
}
override fun stopBolusDelivering() {
@ -688,7 +673,7 @@ class MedtronicPumpPlugin @Inject constructor(
}
// if enforceNew===true current temp basal is canceled and new TBR set (duration is prolonged),
// if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed
// if false and the same rate is requested enacted=false and success=true is returned and TBR is not changed
@Synchronized
override fun setTempBasalAbsolute(absoluteRate: Double, durationInMinutes: Int, profile: Profile, enforceNew: Boolean, tbrType: TemporaryBasalType): PumpEnactResult {
setRefreshButtonEnabled(false)
@ -975,38 +960,15 @@ class MedtronicPumpPlugin @Inject constructor(
MedtronicStatusRefreshType.RemainingInsulin -> {
val remaining = medtronicPumpStatus.reservoirRemainingUnits
val min: Int = if (remaining > 50) 4 * 60 else if (remaining > 20) 60 else 15
workWithStatusRefresh(StatusRefreshAction.Add, refreshType, getTimeInFutureFromMinutes(min))
synchronized(statusRefreshMap) { statusRefreshMap[refreshType] = getTimeInFutureFromMinutes(min) }
}
MedtronicStatusRefreshType.PumpTime, MedtronicStatusRefreshType.Configuration, MedtronicStatusRefreshType.BatteryStatus, MedtronicStatusRefreshType.PumpHistory -> {
workWithStatusRefresh(
StatusRefreshAction.Add, refreshType,
getTimeInFutureFromMinutes(refreshType.refreshTime + additionalTimeInMinutes)
)
synchronized(statusRefreshMap) { statusRefreshMap[refreshType] = getTimeInFutureFromMinutes(refreshType.refreshTime + additionalTimeInMinutes) }
}
}
}
private enum class StatusRefreshAction {
Add, //
GetData
}
@Synchronized
private fun workWithStatusRefresh(action: StatusRefreshAction, statusRefreshType: MedtronicStatusRefreshType?, time: Long?): Map<MedtronicStatusRefreshType, Long>? {
return when (action) {
StatusRefreshAction.Add -> {
statusRefreshMap[statusRefreshType!!] = time!!
null
}
StatusRefreshAction.GetData -> {
HashMap(statusRefreshMap)
}
}
}
private fun getTimeInFutureFromMinutes(minutes: Int): Long {
return System.currentTimeMillis() + getTimeInMs(minutes)
}