From 93dee4eb3c64fa9b9dfec9681c201a29dfcef765 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Tue, 27 Jun 2023 20:19:49 +0200 Subject: [PATCH] Only update time when diff --- .../nightscout/pump/medtrum/MedtrumPlugin.kt | 10 +++++--- .../nightscout/pump/medtrum/MedtrumPump.kt | 2 +- .../pump/medtrum/services/MedtrumService.kt | 24 ++++++++++--------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt index 8700b0d8c6..8fa3a198cf 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt @@ -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) } } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt index d64bce6135..cfd5293f22 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt @@ -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) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index b3d57110b3..b84b3c2590 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -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) {