Medtrum: Fix setting timezone GMT + 13 and larger

This commit is contained in:
jbr7rr 2023-09-24 10:32:31 +02:00
parent 0731827b38
commit dc71478b69
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.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 {

View file

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