From cafdd6cf8fb6ca30f2c4795a45a383522f39f82a Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Sat, 25 Mar 2023 16:55:06 +0100 Subject: [PATCH] Implement GetRecordPacket (initial) --- .../nightscout/pump/medtrum/MedtrumPump.kt | 5 ++ .../pump/medtrum/comm/enums/CommandType.kt | 3 +- .../medtrum/comm/packets/GetRecordPacket.kt | 53 +++++++++++++++++++ .../comm/packets/GetRecordPacketTest.kt | 46 ++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt create mode 100644 pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacketTest.kt 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 0435ff973c..d36f30c91a 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 @@ -55,6 +55,7 @@ class MedtrumPump @Inject constructor( var patchId = 0L var lastTimeReceivedFromPump = 0L // Time in seconds! + var lastKnownSequenceNumber = 0 // Pump history @@ -97,13 +98,17 @@ class MedtrumPump @Inject constructor( lastBasalType = basalType lastBasalRate = basalRate lastBasalSequence = basalSequence + lastKnownSequenceNumber = basalSequence lastBasalPatchId = basalPatchId lastBasalStartTime = basalStartTime + // TODO Handle history } fun handleStopStatusUpdate(stopSequence: Int, stopPatchId: Int) { aapsLogger.debug(LTag.PUMP, "handleStopStatusUpdate: stopSequence: $stopSequence stopPatchId: $stopPatchId") lastStopSequence = stopSequence + lastKnownSequenceNumber = stopSequence lastStopPatchId = stopPatchId + // TODO Handle history } } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt index 8b6119c0b4..b38941ac91 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/enums/CommandType.kt @@ -19,5 +19,6 @@ enum class CommandType(val code: Byte) { STOP_PATCH(31), READ_BOLUS_STATE(34), SET_PATCH(35), - SET_BOLUS_MOTOR(36) + SET_BOLUS_MOTOR(36), + GET_RECORD(99) } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt new file mode 100644 index 0000000000..9aa9c2e2be --- /dev/null +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt @@ -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 + } +} \ No newline at end of file diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacketTest.kt new file mode 100644 index 0000000000..2816996dad --- /dev/null +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacketTest.kt @@ -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 + } +} \ No newline at end of file