Implement GetRecordPacket (initial)

This commit is contained in:
jbr7rr 2023-03-25 16:55:06 +01:00
parent db409f5b7f
commit cafdd6cf8f
4 changed files with 106 additions and 1 deletions

View file

@ -55,6 +55,7 @@ class MedtrumPump @Inject constructor(
var patchId = 0L var patchId = 0L
var lastTimeReceivedFromPump = 0L // Time in seconds! var lastTimeReceivedFromPump = 0L // Time in seconds!
var lastKnownSequenceNumber = 0
// Pump history // Pump history
@ -97,13 +98,17 @@ class MedtrumPump @Inject constructor(
lastBasalType = basalType lastBasalType = basalType
lastBasalRate = basalRate lastBasalRate = basalRate
lastBasalSequence = basalSequence lastBasalSequence = basalSequence
lastKnownSequenceNumber = basalSequence
lastBasalPatchId = basalPatchId lastBasalPatchId = basalPatchId
lastBasalStartTime = basalStartTime lastBasalStartTime = basalStartTime
// TODO Handle history
} }
fun handleStopStatusUpdate(stopSequence: Int, stopPatchId: Int) { fun handleStopStatusUpdate(stopSequence: Int, stopPatchId: Int) {
aapsLogger.debug(LTag.PUMP, "handleStopStatusUpdate: stopSequence: $stopSequence stopPatchId: $stopPatchId") aapsLogger.debug(LTag.PUMP, "handleStopStatusUpdate: stopSequence: $stopSequence stopPatchId: $stopPatchId")
lastStopSequence = stopSequence lastStopSequence = stopSequence
lastKnownSequenceNumber = stopSequence
lastStopPatchId = stopPatchId lastStopPatchId = stopPatchId
// TODO Handle history
} }
} }

View file

@ -19,5 +19,6 @@ enum class CommandType(val code: Byte) {
STOP_PATCH(31), STOP_PATCH(31),
READ_BOLUS_STATE(34), READ_BOLUS_STATE(34),
SET_PATCH(35), SET_PATCH(35),
SET_BOLUS_MOTOR(36) SET_BOLUS_MOTOR(36),
GET_RECORD(99)
} }

View file

@ -0,0 +1,53 @@
package info.nightscout.pump.medtrum.comm.packets
import dagger.android.HasAndroidInjector
import info.nightscout.pump.medtrum.MedtrumPump
import info.nightscout.pump.medtrum.comm.enums.CommandType.GET_RECORD
import info.nightscout.pump.medtrum.extension.toByteArray
import info.nightscout.pump.medtrum.extension.toInt
import info.nightscout.pump.medtrum.extension.toLong
import javax.inject.Inject
class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int) : MedtrumPacket(injector) {
@Inject lateinit var medtrumPump: MedtrumPump
companion object {
private const val RESP_RECORD_HEADER_START = 6
private const val RESP_RECORD_HEADER_END = RESP_RECORD_HEADER_START + 1
private const val RESP_RECORD_UNKNOWN_START = RESP_RECORD_HEADER_END
private const val RESP_RECORD_UNKNOWN_END = RESP_RECORD_UNKNOWN_START + 1
private const val RESP_RECORD_TYPE_START = RESP_RECORD_UNKNOWN_END
private const val RESP_RECORD_TYPE_END = RESP_RECORD_TYPE_START + 1
private const val RESP_RECORD_SERIAL_START = RESP_RECORD_TYPE_END
private const val RESP_RECORD_SERIAL_END = RESP_RECORD_SERIAL_START + 4
private const val RESP_RECORD_PATCHID_START = RESP_RECORD_SERIAL_END
private const val RESP_RECORD_PATCHID_END = RESP_RECORD_PATCHID_START + 2
private const val RESP_RECORD_DATA_START = RESP_RECORD_PATCHID_END
}
init {
opCode = GET_RECORD.code
expectedMinRespLength = RESP_RECORD_DATA_START
}
override fun getRequest(): ByteArray {
return byteArrayOf(opCode) + recordIndex.toByteArray(2) + medtrumPump.patchId.toByteArray(2)
}
override fun handleResponse(data: ByteArray): Boolean {
val success = super.handleResponse(data)
if (success) {
val recordHeader = data.copyOfRange(RESP_RECORD_HEADER_START, RESP_RECORD_HEADER_END).toInt()
val recordUnknown = data.copyOfRange(RESP_RECORD_UNKNOWN_START, RESP_RECORD_UNKNOWN_END).toInt()
val recordType = data.copyOfRange(RESP_RECORD_TYPE_START, RESP_RECORD_TYPE_END).toInt()
val recordSerial = data.copyOfRange(RESP_RECORD_SERIAL_START, RESP_RECORD_SERIAL_END).toLong()
val recordPatchId = data.copyOfRange(RESP_RECORD_PATCHID_START, RESP_RECORD_PATCHID_END).toInt()
// TODO Handle history records
}
return success
}
}

View file

@ -0,0 +1,46 @@
package info.nightscout.pump.medtrum.comm.packets
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.pump.medtrum.MedtrumTestBase
import info.nightscout.pump.medtrum.extension.toByteArray
import org.junit.jupiter.api.Test
import org.junit.Assert.*
class GetRecordPacketTest : MedtrumTestBase() {
/** Test packet specific behavoir */
private val packetInjector = HasAndroidInjector {
AndroidInjector {
if (it is GetRecordPacket) {
it.aapsLogger = aapsLogger
it.medtrumPump = medtrumPump
}
}
}
@Test fun getRequestGivenPacketWhenCalledThenReturnOpCode() {
// Inputs
val recordIndex = 4
medtrumPump.patchId = 146
// Call
val packet = GetRecordPacket(packetInjector, recordIndex)
val result = packet.getRequest()
// Expected values
val expected = byteArrayOf(99, 4, 0, -110, 0)
assertEquals(expected.contentToString(), result.contentToString())
}
@Test fun handleResponseGivenPacketWhenValuesSetThenReturnCorrectValues() {
assertTrue(false)
// TODO: Implement history and test
}
@Test fun handleResponseGivenResponseWhenMessageTooShortThenResultFalse() {
assertTrue(false)
// TODO: Implement history and test
}
}