Only update time when diff

This commit is contained in:
jbr7rr 2023-06-27 20:19:49 +02:00
parent 345ae0f9b2
commit 93dee4eb3c
3 changed files with 21 additions and 15 deletions

View file

@ -411,11 +411,15 @@ import kotlin.math.round
}
override fun timezoneOrDSTChanged(timeChangeType: TimeChangeType) {
medtrumPump.needTimeUpdate = true
medtrumPump.needCheckTimeUpdate = true
if (isInitialized()) {
commandQueue.updateTime(object : Callback() {
override fun run() {
medtrumService?.timeUpdateNotification(this.result.success)
if (this.result.success == false) {
aapsLogger.error(LTag.PUMP, "Medtrum time update failed")
// Only notify here on failure (connection may be failed), service will handle success
medtrumService?.timeUpdateNotification(false)
}
}
})
}
@ -463,7 +467,7 @@ import kotlin.math.round
result.comment = "pump not initialized"
return result
}
val connectionOK = medtrumService?.updateTime() ?: false
val connectionOK = medtrumService?.updateTimeIfNeeded() ?: false
return PumpEnactResult(injector).success(connectionOK)
}
}

View file

@ -206,7 +206,7 @@ class MedtrumPump @Inject constructor(
val pumpSN: Long
get() = _pumpSN
var needTimeUpdate = false
var needCheckTimeUpdate = false
var lastTimeReceivedFromPump = 0L // Time in ms!
var suspendTime = 0L // Time in ms!
var patchAge = 0L // Time in seconds?! // As reported by pump, not used (yet)

View file

@ -190,19 +190,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
fun readPumpStatus() {
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingpumpstatus)))
// Update time if needed
if (medtrumPump.needTimeUpdate || medtrumPump.pumpTimeZoneOffset != dateUtil.getTimeZoneOffsetMinutes(dateUtil.now())) {
aapsLogger.warn(LTag.PUMPCOMM, "Pump time update from readPumpStatus")
timeUpdateNotification(updateTime(false))
}
// Update history
updateTimeIfNeeded(false)
loadEvents()
}
fun timeUpdateNotification(updateSuccess: Boolean) {
if (updateSuccess) {
aapsLogger.debug(LTag.PUMPCOMM, "Pump time updated")
medtrumPump.needTimeUpdate = false
uiInteraction.addNotification(
Notification.INSIGHT_DATE_TIME_UPDATED, // :---)
rh.gs(info.nightscout.core.ui.R.string.pump_time_updated),
@ -218,12 +212,20 @@ class MedtrumService : DaggerService(), BLECommCallback {
}
}
fun updateTime(needLoadHistory: Boolean = true): Boolean {
var result = sendPacketAndGetResponse(SetTimePacket(injector))
if (result) result = sendPacketAndGetResponse(SetTimeZonePacket(injector))
fun updateTimeIfNeeded(needLoadHistory: Boolean = true): Boolean {
// Note we only check timeZone here, time is updated each connection attempt if needed, because the pump requires it to be checked
// But we dont check timeZone each time, therefore we do it here (if needed)
var result = true
if (medtrumPump.pumpTimeZoneOffset != dateUtil.getTimeZoneOffsetMinutes(dateUtil.now())) {
result = sendPacketAndGetResponse(SetTimePacket(injector))
if (result) result = sendPacketAndGetResponse(SetTimeZonePacket(injector))
timeUpdateNotification(result)
}
// Do this here, because TBR can be cancelled due to time change by connect flow
if (needLoadHistory) {
if (result) result = loadEvents()
}
if (result) medtrumPump.needCheckTimeUpdate = false
return result
}
@ -832,7 +834,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
// Succes!
responseHandled = true
responseSuccess = true
medtrumPump.needTimeUpdate = false
medtrumPump.needCheckTimeUpdate = false
timeUpdateNotification(true)
toState(SynchronizeState())
} else if (mPacket?.failed == true) {