Merge pull request #2831 from jbr7rr/medtrum-fixes

Medtrum: Fix setting timezone GMT + 13 and larger
This commit is contained in:
Milos Kozak 2023-09-24 11:01:58 +02:00 committed by GitHub
commit 8e885ef00e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View file

@ -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.comm.enums.CommandType.SET_TIME_ZONE
import info.nightscout.pump.medtrum.extension.toByteArray import info.nightscout.pump.medtrum.extension.toByteArray
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
import info.nightscout.rx.logging.LTag
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
import javax.inject.Inject import javax.inject.Inject
class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) {
@ -13,7 +15,7 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector)
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
@Inject lateinit var medtrumPump: MedtrumPump @Inject lateinit var medtrumPump: MedtrumPump
private var offsetMinutes: Int = 0 private val offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now())
init { init {
opCode = SET_TIME_ZONE.code opCode = SET_TIME_ZONE.code
@ -21,9 +23,18 @@ class SetTimeZonePacket(injector: HasAndroidInjector) : MedtrumPacket(injector)
override fun getRequest(): ByteArray { override fun getRequest(): ByteArray {
val time = MedtrumTimeUtil().getCurrentTimePumpSeconds() val time = MedtrumTimeUtil().getCurrentTimePumpSeconds()
offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now()) var calcOffset = offsetMinutes
if (offsetMinutes < 0) offsetMinutes += 65536 aapsLogger.debug(LTag.PUMPCOMM, "Requested offset: $calcOffset minutes")
return byteArrayOf(opCode) + offsetMinutes.toByteArray(2) + time.toByteArray(4) // 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 { override fun handleResponse(data: ByteArray): Boolean {

View file

@ -43,15 +43,14 @@ class SetTimeZonePacketTest : MedtrumTestBase() {
@Test fun handleResponseGivenPacketWhenValuesSetThenReturnCorrectValues() { @Test fun handleResponseGivenPacketWhenValuesSetThenReturnCorrectValues() {
// Inputs // Inputs
val response = byteArrayOf(7, 12, 4, 0, 0, 0, -78) val response = byteArrayOf(7, 12, 4, 0, 0, 0, -78)
val offsetMinutes = dateUtil.getTimeZoneOffsetMinutes(dateUtil.now())
// Call // Call
val packet = SetTimeZonePacket(packetInjector) val packet = SetTimeZonePacket(packetInjector)
val result = packet.handleResponse(response) val result = packet.handleResponse(response)
// Expected values // Expected values
val expectedOffsetMinutes = 0
Assertions.assertTrue(result) Assertions.assertTrue(result)
Assertions.assertEquals(expectedOffsetMinutes, medtrumPump.pumpTimeZoneOffset) Assertions.assertEquals(offsetMinutes, medtrumPump.pumpTimeZoneOffset)
} }
} }