diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt index 718df1597b..4f3f682892 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManager.kt @@ -23,8 +23,6 @@ interface OmnipodDashManager { fun suspendDelivery(hasBasalBeepEnabled: Boolean): Observable - fun setTime(): Observable - fun setTempBasal(rate: Double, durationInMinutes: Short, tempBasalBeeps: Boolean): Observable fun stopTempBasal(hasTempBasalBeepEnabled: Boolean): Observable diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt index 2a88cc2d0d..0d62eb9a68 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/OmnipodDashManagerImpl.kt @@ -196,7 +196,7 @@ class OmnipodDashManagerImpl @Inject constructor( DefaultStatusResponse::class ) }.doOnComplete { - podStateManager.timeZone = TimeZone.getDefault() + podStateManager.updateTimeZone() } } @@ -506,12 +506,6 @@ class OmnipodDashManagerImpl @Inject constructor( ).interceptPodEvents() } - override fun setTime(): Observable { - // TODO - logger.error(LTag.PUMPCOMM, "NOT IMPLEMENTED: setTime()") - return Observable.empty() - } - private fun observeSendProgramTempBasalCommand(rate: Double, durationInMinutes: Short, tempBasalBeeps: Boolean): Observable { return Observable.defer { bleManager.sendCommand( diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt index 6927e63195..fa38444317 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/pod/state/OmnipodDashPodStateManager.kt @@ -36,7 +36,8 @@ interface OmnipodDashPodStateManager { val successfulConnectionAttemptsAfterRetries: Int val failedConnectionsAfterRetries: Int - var timeZone: TimeZone + val timeZoneId: String? + val timeZoneUpdated: Long? val sameTimeZone: Boolean // The TimeZone is the same on the phone and on the pod val lastUpdatedSystem: Long // System.currentTimeMillis() val lastStatusResponseReceived: Long @@ -89,6 +90,7 @@ interface OmnipodDashPodStateManager { fun connectionSuccessRatio(): Float fun incrementSuccessfulConnectionAttemptsAfterRetries() fun incrementFailedConnectionsAfterRetries() + fun updateTimeZone() fun createActiveCommand( historyId: String, 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 fe63757d17..2c79549a0d 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 @@ -23,6 +23,8 @@ import io.reactivex.Single import java.io.Serializable import java.time.Duration import java.time.Instant +import java.time.ZoneId +import java.time.ZoneOffset import java.time.ZonedDateTime import java.util.* import javax.inject.Inject @@ -131,27 +133,18 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( podState.failedConnectionsAfterRetries++ } - override var timeZone: TimeZone - get() = TimeZone.getTimeZone(podState.timeZone) - set(tz) { - podState.timeZone = tz.toZoneId().normalized().id - store() - } + override val timeZoneId: String? + get() = podState.timeZone override val sameTimeZone: Boolean get() { val now = System.currentTimeMillis() val currentTimezone = TimeZone.getDefault() val currentOffset = currentTimezone.getOffset(now) - val podOffset = timeZone.getOffset(now) + val podOffset = podState.timeZoneOffset logger.debug( LTag.PUMPCOMM, - "sameTimeZone currentTimezone=${ - currentTimezone.getDisplayName( - true, - TimeZone.SHORT - ) - } " + + "sameTimeZone " + "currentOffset=$currentOffset " + "podOffset=$podOffset" ) @@ -238,8 +231,9 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( get() { val minutesSinceActivation = podState.minutesSinceActivation val activationTime = podState.activationTime - if ((activationTime != null) && (minutesSinceActivation != null)) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(activationTime), timeZone.toZoneId()) + val timeZoneOffset = podState.timeZoneOffset + if ((activationTime != null) && (minutesSinceActivation != null) && (timeZoneOffset != null)) { + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(activationTime), ZoneId.ofOffset("", ZoneOffset.ofTotalSeconds(timeZoneOffset / 1000))) .plusMinutes(minutesSinceActivation.toLong()) .plus(Duration.ofMillis(System.currentTimeMillis() - lastUpdatedSystem)) } @@ -248,9 +242,25 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( override val timeDrift: Duration? get() { - return Duration.between(ZonedDateTime.now(), time) + return time?.let { + return Duration.between(ZonedDateTime.now(), it) + } ?: null } + override val timeZoneUpdated: Long? + get() { + return podState.timeZoneUpdated + } + + override fun updateTimeZone() { + val timeZone = TimeZone.getDefault() + val now = System.currentTimeMillis() + + podState.timeZoneOffset = timeZone.getOffset(now) + podState.timeZone = timeZone.id + podState.timeZoneUpdated = now + } + override val expiry: ZonedDateTime? get() { val podLifeInHours = podLifeInHours @@ -700,7 +710,9 @@ class OmnipodDashPodStateManagerImpl @Inject constructor( var bluetoothAddress: String? = null var ltk: ByteArray? = null var eapAkaSequenceNumber: Long = 1 - var timeZone: String = "" // TimeZone ID (e.g. "Europe/Amsterdam") + var timeZone: String? = null // TimeZone ID (e.g. "Europe/Amsterdam") + var timeZoneOffset: Int? = null + var timeZoneUpdated: Long? = null var alarmSynced: Boolean = false var bleVersion: SoftwareVersion? = null diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt index 698381365e..5c2902ab91 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/ui/OmnipodDashOverviewFragment.kt @@ -306,12 +306,20 @@ class OmnipodDashOverviewFragment : DaggerFragment() { podStateManager.bluetoothVersion.toString() ) - // Update time on Pod + val timeZone = podStateManager.timeZoneId?.let { timeZoneId -> + podStateManager.timeZoneUpdated?.let { timeZoneUpdated -> + val tz = TimeZone.getTimeZone(timeZoneId) + val inDST = tz.inDaylightTime(Date(timeZoneUpdated)) + val locale = resources.configuration.locales.get(0) + tz.getDisplayName(inDST, TimeZone.SHORT, locale) + } ?: PLACEHOLDER + } ?: PLACEHOLDER + podInfoBinding.timeOnPod.text = podStateManager.time?.let { rh.gs( R.string.omnipod_common_time_with_timezone, dateUtil.dateAndTimeString(it.toEpochSecond() * 1000), - podStateManager.timeZone.getDisplayName(true, TimeZone.SHORT) + timeZone ) } ?: PLACEHOLDER