remove Joda

This commit is contained in:
Andrei Vereha 2021-07-11 21:19:57 +02:00
parent f289cd50c5
commit d1f99bfb71
5 changed files with 60 additions and 80 deletions

View file

@ -113,7 +113,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
pumpSync.syncTemporaryBasalWithPumpId(
timestamp = System.currentTimeMillis(),
rate = 0.0,
duration = T.mins(PodConstants.MAX_POD_LIFETIME.standardMinutes).msecs(),
duration = T.mins(PodConstants.MAX_POD_LIFETIME.toMinutes()).msecs(),
isAbsolute = true,
type = PumpSync.TemporaryBasalType.PUMP_SUSPEND,
pumpId = Random.Default.nextLong(), // we don't use this, just make sure it's unique
@ -146,7 +146,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
rxBus.send(EventNewNotification(notification))
} else {
rxBus.send(EventDismissNotification(Notification.OMNIPOD_POD_SUSPENDED))
if (!TimeZone.getDefault().equals(podStateManager.timeZone)) {
if (!podStateManager.sameTimeZone) {
val notification =
Notification(
Notification.OMNIPOD_TIME_OUT_OF_SYNC,
@ -277,7 +277,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
pumpSync.syncTemporaryBasalWithPumpId(
timestamp = System.currentTimeMillis(),
rate = 0.0,
duration = T.mins(PodConstants.MAX_POD_LIFETIME.standardMinutes).msecs(),
duration = T.mins(PodConstants.MAX_POD_LIFETIME.toMinutes()).msecs(),
isAbsolute = true,
type = PumpSync.TemporaryBasalType.PUMP_SUSPEND,
pumpId = Random.Default.nextLong(), // we don't use this, just make sure it's unique
@ -371,7 +371,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
.map {
pumpSyncTempBasal(
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PodConstants.MAX_POD_LIFETIME.toMinutes(),
PumpSync.TemporaryBasalType.PUMP_SUSPEND
)
rxBus.send(EventTempBasalChange())
@ -961,7 +961,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
.map {
pumpSyncTempBasal(
0.0,
PodConstants.MAX_POD_LIFETIME.standardMinutes,
PodConstants.MAX_POD_LIFETIME.toMinutes(),
PumpSync.TemporaryBasalType.PUMP_SUSPEND
)
}
@ -1116,6 +1116,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
rxBus.send(EventDismissNotification(Notification.OMNIPOD_POD_SUSPENDED))
}
rxBus.send(EventDismissNotification(Notification.OMNIPOD_TBR_ALERTS))
rxBus.send(EventDismissNotification(Notification.OMNIPOD_TIME_OUT_OF_SYNC))
}
OmnipodCommandType.SET_BASAL_PROFILE -> {
@ -1137,6 +1138,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
rxBus.send(EventDismissNotification(Notification.OMNIPOD_POD_SUSPENDED))
rxBus.send(EventDismissNotification(Notification.FAILED_UPDATE_PROFILE))
rxBus.send(EventDismissNotification(Notification.OMNIPOD_TBR_ALERTS))
rxBus.send(EventDismissNotification(Notification.OMNIPOD_TIME_OUT_OF_SYNC))
}
}

View file

@ -1,9 +1,9 @@
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.definition
import org.joda.time.Duration
import java.time.Duration
class PodConstants {
companion object {
val MAX_POD_LIFETIME = Duration.standardHours(80)
val MAX_POD_LIFETIME = Duration.ofMinutes(80)
}
}

View file

@ -11,9 +11,8 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.response.
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Single
import org.joda.time.DateTime
import org.joda.time.Duration
import java.io.Serializable
import java.time.ZonedDateTime
import java.util.*
sealed class CommandConfirmationFromState
@ -34,11 +33,12 @@ interface OmnipodDashPodStateManager {
var bluetoothConnectionState: BluetoothConnectionState
var timeZone: TimeZone
val sameTimeZone: Boolean // The TimeZone is the same on the phone and on the pod
val lastUpdatedSystem: Long // System.currentTimeMillis()
val lastStatusResponseReceived: Long
val time: DateTime?
val timeDrift: Duration?
val expiry: DateTime?
val time: ZonedDateTime?
val timeDrift: java.time.Duration?
val expiry: ZonedDateTime?
val messageSequenceNumber: Short
val sequenceNumberOfLastProgrammingCommand: Short?

View file

@ -20,9 +20,10 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Single
import org.joda.time.DateTime
import org.joda.time.Duration
import java.io.Serializable
import java.time.Duration
import java.time.Instant
import java.time.ZonedDateTime
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
@ -104,6 +105,12 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
store()
}
override val sameTimeZone: Boolean
get() {
val now = System.currentTimeMillis()
return TimeZone.getDefault().getOffset(now) == timeZone.getOffset(now)
}
override val bluetoothVersion: SoftwareVersion?
get() = podState.bleVersion
@ -180,30 +187,31 @@ class OmnipodDashPodStateManagerImpl @Inject constructor(
override val lastStatusResponseReceived: Long
get() = podState.lastStatusResponseReceived
override val time: DateTime?
override val time: ZonedDateTime?
get() {
val minutesSinceActivation = podState.minutesSinceActivation
val activationTime = podState.activationTime
if ((activationTime != null) && (minutesSinceActivation != null)) {
return DateTime(activationTime)
.plusMinutes(minutesSinceActivation.toInt())
.plus(Duration(podState.lastUpdatedSystem, System.currentTimeMillis()))
return ZonedDateTime.from(Instant.ofEpochMilli(activationTime))
.plusMinutes(minutesSinceActivation.toLong())
.plus(Duration.ofMillis(System.currentTimeMillis() - lastUpdatedSystem))
}
return null
}
override val timeDrift: Duration?
get() {
return Duration(DateTime.now(), time)
return Duration.between(ZonedDateTime.now(), time)
}
override val expiry: DateTime?
override val expiry: ZonedDateTime?
// TODO: Consider storing expiry datetime in pod state saving continuously recalculating to the same value
get() {
val podLifeInHours = podLifeInHours
val activationTime = podState.activationTime
if (podLifeInHours != null && activationTime != null) {
return DateTime(podState.activationTime).plusHours(podLifeInHours.toInt())
return return ZonedDateTime.from(Instant.ofEpochMilli(activationTime))
.plusHours(podLifeInHours.toLong())
}
return null
}

View file

@ -44,8 +44,8 @@ import info.nightscout.androidaps.utils.ui.UIRunnable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime
import org.joda.time.Duration
import java.time.Duration
import java.time.ZonedDateTime
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList
@ -269,27 +269,34 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
// Update time on Pod
podInfoBinding.timeOnPod.text = podStateManager.time?.let {
readableZonedTime(it)
resourceHelper.gs(
R.string.omnipod_common_time_with_timezone,
dateUtil.dateAndTimeString(it.toEpochSecond()),
podStateManager.timeZone.getDisplayName(true, TimeZone.SHORT)
)
} ?: PLACEHOLDER
podInfoBinding.timeOnPod.setTextColor(
podStateManager.timeDrift?.let {
if (it.abs().isLongerThan(Duration.standardMinutes(MAX_TIME_DEVIATION_MINUTES))) {
Color.RED
} else {
when {
!podStateManager.sameTimeZone ->
Color.MAGENTA
podStateManager.timeDrift?.abs()?.minus(
Duration.ofMinutes(MAX_TIME_DEVIATION_MINUTES)
)?.isNegative ?: false ->
Color.YELLOW
else ->
Color.WHITE
}
} ?: Color.WHITE
}
)
// Update Pod expiry time
val expiresAt = podStateManager.expiry
podInfoBinding.podExpiryDate.text = expiresAt?.let {
readableZonedTime(it)
dateUtil.dateAndTimeString(it.toEpochSecond())
}
?: PLACEHOLDER
podInfoBinding.podExpiryDate.setTextColor(
if (expiresAt != null && DateTime.now().isAfter(expiresAt))
if (expiresAt != null && ZonedDateTime.now().isAfter(expiresAt))
Color.RED
else
Color.WHITE
@ -368,14 +375,14 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
private fun updateLastConnection() {
if (podStateManager.isUniqueIdSet) {
podInfoBinding.lastConnection.text = readableDuration(
Duration(
podStateManager.lastUpdatedSystem,
System
.currentTimeMillis()
Duration.ofMillis(
System.currentTimeMillis() -
podStateManager.lastUpdatedSystem,
)
)
val lastConnectionColor =
if (omnipodDashPumpPlugin.isUnreachableAlertTimeoutExceeded(getPumpUnreachableTimeout().millis)) {
if (omnipodDashPumpPlugin.isUnreachableAlertTimeoutExceeded(getPumpUnreachableTimeout().toMillis())) {
Color.RED
} else {
Color.WHITE
@ -441,7 +448,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
R.string.omnipod_common_overview_last_bolus_value,
omnipodDashPumpPlugin.model().determineCorrectBolusSize(requestedBolus),
resourceHelper.gs(R.string.insulin_unit_shortname),
readableDuration(Duration(it.createdRealtime, SystemClock.elapsedRealtime()))
readableDuration(Duration.ofMillis(SystemClock.elapsedRealtime() - it.createdRealtime))
)
text += " (uncertain) "
textColor = Color.RED
@ -461,7 +468,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
R.string.omnipod_common_overview_last_bolus_value,
omnipodDashPumpPlugin.model().determineCorrectBolusSize(bolusSize),
resourceHelper.gs(R.string.insulin_unit_shortname),
readableDuration(Duration(it.startTime, System.currentTimeMillis()))
readableDuration(Duration.ofMillis(System.currentTimeMillis() - it.startTime))
)
if (!it.deliveryComplete) {
textColor = Color.YELLOW
@ -597,47 +604,10 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
}
}
// private fun getTimeZone(): DateTimeZone {
// // return getSafe(() -> podState.getTimeZone());
// return podStateManager.timeZone
// }
private fun getTimeZone(): TimeZone {
// Return timezone ID (e.g "Europe/Amsterdam")
return podStateManager.timeZone
}
private fun readableZonedTime(time: DateTime): String {
val timeAsJavaData = time.toLocalDateTime().toDate()
return dateUtil.dateAndTimeString(timeAsJavaData.time)
// // TODO: Handle timeZone ID
// val timeZone = getTimeZone()
// if (timeZone == "") {
// // No timezone defined, use local time (default)
// return dateUtil.dateAndTimeString(timeAsJavaData.time)
// }
// else {
// // Get full timezoned time
// val isDaylightTime = timeZone.inDaylightTime(timeAsJavaData)
// val locale = resources.configuration.locales.get(0)
// val timeZoneDisplayName =
// timeZone.getDisplayName(isDaylightTime, TimeZone.SHORT, locale) + " " + timeZone.getDisplayName(
// isDaylightTime,
// TimeZone.LONG,
// locale
// )
// return resourceHelper.gs(
// R.string.omnipod_common_time_with_timezone,
// dateUtil.dateAndTimeString(timeAsJavaData.time),
// timeZoneDisplayName
// )
// }
}
private fun readableDuration(duration: Duration): String {
val hours = duration.standardHours.toInt()
val minutes = duration.standardMinutes.toInt()
val seconds = duration.standardSeconds.toInt()
val hours = duration.toHours().toInt()
val minutes = duration.toMinutes().toInt()
val seconds = duration.seconds
when {
seconds < 10 -> {
return resourceHelper.gs(R.string.omnipod_common_moments_ago)
@ -680,7 +650,7 @@ class OmnipodDashOverviewFragment : DaggerFragment() {
// FIXME ideally we should just have access to LocalAlertUtils here
private fun getPumpUnreachableTimeout(): Duration {
return Duration.standardMinutes(
return Duration.ofMinutes(
sp.getInt(
R.string.key_pump_unreachable_threshold_minutes,
Constants.DEFAULT_PUMP_UNREACHABLE_THRESHOLD_MINUTES