Merge pull request #921 from 0pen-dash/avereha/fix-dst

dash: fix DST and timeOnPod
This commit is contained in:
Milos Kozak 2021-11-14 17:01:19 +01:00 committed by GitHub
commit c4f19b5a85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 30 deletions

View file

@ -1193,7 +1193,7 @@ class OmnipodDashPumpPlugin @Inject constructor(
private fun handleTimeChange(): PumpEnactResult {
return profileFunction.getProfile()?.let {
setNewBasalProfile(it, OmnipodCommandType.SET_TIME)
} ?: PumpEnactResult(injector).success(true).enacted(false).comment("No profile active")
} ?: PumpEnactResult(injector).success(false).enacted(false).comment("No profile active")
}
private fun updateAlertConfiguration(): PumpEnactResult {

View file

@ -23,8 +23,6 @@ interface OmnipodDashManager {
fun suspendDelivery(hasBasalBeepEnabled: Boolean): Observable<PodEvent>
fun setTime(): Observable<PodEvent>
fun setTempBasal(rate: Double, durationInMinutes: Short, tempBasalBeeps: Boolean): Observable<PodEvent>
fun stopTempBasal(hasTempBasalBeepEnabled: Boolean): Observable<PodEvent>

View file

@ -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<PodEvent> {
// TODO
logger.error(LTag.PUMPCOMM, "NOT IMPLEMENTED: setTime()")
return Observable.empty()
}
private fun observeSendProgramTempBasalCommand(rate: Double, durationInMinutes: Short, tempBasalBeeps: Boolean): Observable<PodEvent> {
return Observable.defer {
bleManager.sendCommand(

View file

@ -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,

View file

@ -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

View file

@ -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