From dc71478b699d0a25176d13fcccbf7ab001241cdf Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Sun, 24 Sep 2023 10:32:31 +0200 Subject: [PATCH] Medtrum: Fix setting timezone GMT + 13 and larger --- .../medtrum/comm/packets/SetTimeZonePacket.kt | 19 +++++++++++++++---- .../comm/packets/SetTimeZonePacketTest.kt | 5 ++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt index cbddb8c75d..ccdf9d8d25 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacket.kt @@ -5,7 +5,9 @@ import info.nightscout.pump.medtrum.MedtrumPump import info.nightscout.pump.medtrum.comm.enums.CommandType.SET_TIME_ZONE import info.nightscout.pump.medtrum.extension.toByteArray import info.nightscout.pump.medtrum.util.MedtrumTimeUtil +import info.nightscout.rx.logging.LTag import info.nightscout.shared.utils.DateUtil +import info.nightscout.shared.utils.T import javax.inject.Inject class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { @@ -13,7 +15,7 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) @Inject lateinit var dateUtil: DateUtil @Inject lateinit var medtrumPump: MedtrumPump - private var offsetMinutes: Int = 0 + private val offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) init { opCode = SET_TIME_ZONE.code @@ -21,9 +23,18 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) override fun getRequest(): ByteArray { val time = MedtrumTimeUtil().getCurrentTimePumpSeconds() - offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) - if (offsetMinutes < 0) offsetMinutes += 65536 - return byteArrayOf(opCode) + offsetMinutes.toByteArray(2) + time.toByteArray(4) + var calcOffset = offsetMinutes + aapsLogger.debug(LTag.PUMPCOMM, "Requested offset: $calcOffset minutes") + // Workaround for bug where it fails to set timezone > GMT + 12 + // if offset is > 12 hours, subtract 24 hours + if (calcOffset > T.hours(12).mins()) + { + calcOffset -= T.hours(24).mins().toInt() + aapsLogger.debug(LTag.PUMPCOMM, "Modifying requested offset to: $calcOffset minutes") + } + // Pump expects this for negative offsets + if (calcOffset < 0) calcOffset += 65536 + return byteArrayOf(opCode) + calcOffset.toByteArray(2) + time.toByteArray(4) } override fun handleResponse(data: ByteArray): Boolean { diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacketTest.kt index 4ef3ba9fae..127fac1af8 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/SetTimeZonePacketTest.kt @@ -43,15 +43,14 @@ class SetTimeZonePacketTest : MedtrumTestBase() { @Test fun handleResponseGivenPacketWhenValuesSetThenReturnCorrectValues() { // Inputs val response = byteArrayOf(7, 12, 4, 0, 0, 0, -78) + val offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) // Call val packet = SetTimeZonePacket(packetInjector) val result = packet.handleResponse(response) // Expected values - val expectedOffsetMinutes = 0 - Assertions.assertTrue(result) - Assertions.assertEquals(expectedOffsetMinutes, medtrumPump.pumpTimeZoneOffset) + Assertions.assertEquals(offsetMinutes, medtrumPump.pumpTimeZoneOffset) } }