DanaRS tests

This commit is contained in:
Milos Kozak 2020-03-01 16:18:19 +01:00
parent 44edc7ce96
commit 67a5c33f8d
13 changed files with 90 additions and 124 deletions

View file

@ -17,7 +17,7 @@ public class DanaRS_Packet {
protected static final int TYPE_START = 0;
protected static final int OPCODE_START = 1;
protected static final int DATA_START = 2;
public static final int DATA_START = 2;
private boolean received;
public boolean failed;

View file

@ -27,7 +27,7 @@ class DanaRS_Packet_Basal_Get_Basal_Rate(
override fun handleMessage(data: ByteArray) {
var dataIndex = DATA_START
var dataSize = 2
danaRPump.maxBasal = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
danaRPump.maxBasal = byteArrayToInt(getBytes(data, DATA_START, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 1
danaRPump.basalStep = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0

View file

@ -16,9 +16,7 @@ class DanaRS_Packet_Basal_Get_Profile_Number(
}
override fun handleMessage(data: ByteArray) {
val dataIndex = DATA_START
val dataSize = 1
danaRPump.activeProfile = byteArrayToInt(getBytes(data, dataIndex, dataSize))
danaRPump.activeProfile = byteArrayToInt(getBytes(data, DATA_START, 1))
aapsLogger.debug(LTag.PUMPCOMM, "Active profile: " + danaRPump.activeProfile)
}

View file

@ -18,25 +18,15 @@ class DanaRS_Packet_Basal_Get_Temporary_Basal_State(
}
override fun handleMessage(data: ByteArray) {
var dataIndex = DATA_START
var dataSize = 1
val error = byteArrayToInt(getBytes(data, dataIndex, dataSize))
if (error == 1) failed = true
dataIndex += dataSize
dataSize = 1
danaRPump.isTempBasalInProgress = byteArrayToInt(getBytes(data, dataIndex, dataSize)) == 0x01
val isAPSTempBasalInProgress = byteArrayToInt(getBytes(data, dataIndex, dataSize)) == 0x02
dataIndex += dataSize
dataSize = 1
danaRPump.tempBasalPercent = byteArrayToInt(getBytes(data, dataIndex, dataSize))
val error = byteArrayToInt(getBytes(data, DATA_START, 1))
danaRPump.isTempBasalInProgress = byteArrayToInt(getBytes(data, DATA_START + 1, 1)) == 0x01
val isAPSTempBasalInProgress = byteArrayToInt(getBytes(data, DATA_START + 2, 1)) == 0x02
danaRPump.tempBasalPercent = byteArrayToInt(getBytes(data, DATA_START + 3, 1))
if (danaRPump.tempBasalPercent > 200) danaRPump.tempBasalPercent = (danaRPump.tempBasalPercent - 200) * 10
dataIndex += dataSize
dataSize = 1
val durationHour = byteArrayToInt(getBytes(data, dataIndex, dataSize))
val durationHour = byteArrayToInt(getBytes(data, DATA_START + 4, 1))
if (durationHour == 150) danaRPump.tempBasalTotalSec = 15 * 60 else if (durationHour == 160) danaRPump.tempBasalTotalSec = 30 * 60 else danaRPump.tempBasalTotalSec = durationHour * 60 * 60
dataIndex += dataSize
dataSize = 2
val runningMin = byteArrayToInt(getBytes(data, dataIndex, dataSize))
val runningMin = byteArrayToInt(getBytes(data, DATA_START + 5, 2))
if (error != 0) failed = true
val tempBasalRemainingMin = (danaRPump.tempBasalTotalSec - runningMin * 60) / 60
val tempBasalStart = if (danaRPump.isTempBasalInProgress) getDateFromTempBasalSecAgo(runningMin * 60) else 0
aapsLogger.debug(LTag.PUMPCOMM, "Error code: $error")

View file

@ -16,25 +16,15 @@ class DanaRS_Packet_Bolus_Get_Dual_Bolus(
}
override fun handleMessage(data: ByteArray) {
var dataIndex = DATA_START
var dataSize = 1
val error = byteArrayToInt(getBytes(data, dataIndex, dataSize))
dataIndex += dataSize
dataSize = 2
danaRPump.bolusStep = byteArrayToInt(getBytes(data, dataIndex, dataSize)).toDouble()
dataIndex += dataSize
dataSize = 2
danaRPump.extendedBolusAbsoluteRate = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 2
danaRPump.maxBolus = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 1
val bolusIncrement = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
val error = byteArrayToInt(getBytes(data, DATA_START, 1))
danaRPump.bolusStep = byteArrayToInt(getBytes(data, DATA_START + 1, 2)) / 100.0
danaRPump.extendedBolusAbsoluteRate = byteArrayToInt(getBytes(data, DATA_START + 3, 2)) / 100.0
danaRPump.maxBolus = byteArrayToInt(getBytes(data, DATA_START + 5, 2)) / 100.0
val bolusIncrement = byteArrayToInt(getBytes(data, DATA_START + 7, 1)) / 100.0
failed = error != 0
aapsLogger.debug(LTag.PUMPCOMM, "Result: $error")
aapsLogger.debug(LTag.PUMPCOMM, "Bolus step: " + danaRPump.bolusStep + " U")
aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus running: " + danaRPump.extendedBolusAbsoluteRate + " U/h")
aapsLogger.debug(LTag.PUMPCOMM, "Bolus step: ${danaRPump.bolusStep} U")
aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus running: ${danaRPump.extendedBolusAbsoluteRate} U/h")
aapsLogger.debug(LTag.PUMPCOMM, "Max bolus: " + danaRPump.maxBolus + " U")
aapsLogger.debug(LTag.PUMPCOMM, "bolusIncrement: $bolusIncrement U")
}

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
package info
import org.junit.Rule
import org.mockito.Mockito

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import info.TestBase
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump
import info.nightscout.androidaps.utils.sharedPreferences.SP
@ -22,13 +23,24 @@ open class DanaRSTestBase : TestBase() {
}
fun createArray(length: Int, fillWith: Double): Array<Double> {
val ret = Array(length) {0.0}
val ret = Array(length) { 0.0 }
for (i in 0 until length) {
ret[i] = fillWith
}
return ret
}
fun putIntToArray(array: ByteArray, position: Int, value: Int): ByteArray {
array[DanaRS_Packet.DATA_START + position] = (value and 0xFF).toByte()
array[DanaRS_Packet.DATA_START + position + 1] = ((value and 0xFF00) shr 8).toByte()
return array
}
fun putByteToArray(array: ByteArray, position: Int, value: Byte): ByteArray {
array[DanaRS_Packet.DATA_START + position] = value
return array
}
@Before
fun setup() {
danaRPump = DanaRPump(aapsLogger, sp)

View file

@ -20,10 +20,13 @@ class DanaRS_Packet_Basal_Get_Basal_RateTest : DanaRSTestBase() {
val packet = DanaRS_Packet_Basal_Get_Basal_Rate(aapsLogger, rxBus, resourceHelper, danaRPump)
// test message decoding
// rate is 0.01
packet.handleMessage(createArray(100, 1.toByte()))
Assert.assertEquals(false, packet.failed)
packet.handleMessage(createArray(100, 5.toByte()))
Assert.assertEquals(true, packet.failed)
val array = ByteArray(100)
putIntToArray(array, 0, (1.0 * 100).toInt())
putByteToArray(array, 2, (0.05 * 100).toByte())
packet.handleMessage(array)
Assert.assertEquals(1.0, danaRPump.maxBasal, 0.0)
Assert.assertEquals(0.05, danaRPump.basalStep, 0.0)
Assert.assertTrue(packet.failed)
Assert.assertEquals("BASAL__GET_BASAL_RATE", packet.friendlyName)
}
}

View file

@ -1,7 +1,5 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet.byteArrayToInt
import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet.getBytes
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@ -15,11 +13,11 @@ class DanaRS_Packet_Basal_Get_Profile_Basal_RateTest : DanaRSTestBase() {
@Test fun runTest() {
val testPacket = DanaRS_Packet_Basal_Get_Profile_Basal_Rate(aapsLogger, danaRPump, 1)
// test if pumpProfile array is set right
val basal01 = byteArrayToInt(getBytes(createArray(50, 1.toByte()), 2, 2)) / 100.0
val basal05 = byteArrayToInt(getBytes(createArray(50, 5.toByte()), 2, 2)) / 100.0
val basal12 = byteArrayToInt(getBytes(createArray(50, 12.toByte()), 2, 2)) / 100.0
val basal01 = DanaRS_Packet.byteArrayToInt(DanaRS_Packet.getBytes(createArray(50, 1.toByte()), 2, 2)) / 100.0
val basal05 = DanaRS_Packet.byteArrayToInt(DanaRS_Packet.getBytes(createArray(50, 5.toByte()), 2, 2)) / 100.0
val basal12 = DanaRS_Packet.byteArrayToInt(DanaRS_Packet.getBytes(createArray(50, 12.toByte()), 2, 2)) / 100.0
// basal rate > 1U/hr
val basal120 = byteArrayToInt(getBytes(createArray(50, 120.toByte()), 2, 2)) / 100.0
val basal120 = DanaRS_Packet.byteArrayToInt(DanaRS_Packet.getBytes(createArray(50, 120.toByte()), 2, 2)) / 100.0
val params = testPacket.requestParams
assertEquals(1.toByte(), params[0])
testPacket.handleMessage(createArray(50, 0.toByte()))

View file

@ -12,10 +12,11 @@ class DanaRS_Packet_Basal_Get_Profile_NumberTest : DanaRSTestBase() {
@Test fun runTest() {
val packet = DanaRS_Packet_Basal_Get_Profile_Number(aapsLogger, danaRPump)
// test message decoding
packet.handleMessage(byteArrayOf(0.toByte(), 0.toByte(), 0.toByte()))
Assert.assertEquals(false, packet.failed)
// if data.length > 4 should return fail
val array = ByteArray(100)
putByteToArray(array, 0, 1.toByte())
packet.handleMessage(array)
Assert.assertEquals(1, danaRPump.activeProfile)
Assert.assertEquals("BASAL__GET_PROFILE_NUMBER", packet.friendlyName)
}
}

View file

@ -13,10 +13,18 @@ class DanaRS_Packet_Basal_Get_Temporary_Basal_StateTest : DanaRSTestBase() {
@Test fun runTest() {
val packet = DanaRS_Packet_Basal_Get_Temporary_Basal_State(aapsLogger, danaRPump)
// test message decoding
packet.handleMessage(createArray(50, 0.toByte()))
Assert.assertEquals(false, packet.failed)
packet.handleMessage(createArray(50, 1.toByte()))
Assert.assertEquals(true, packet.failed)
val array = ByteArray(100)
putByteToArray(array, 0, 1.toByte())
putByteToArray(array, 1, 1.toByte())
putByteToArray(array, 2, 2.toByte())
putByteToArray(array, 3, 230.toByte())
putByteToArray(array, 4, 150.toByte())
putIntToArray(array, 5, 1)
packet.handleMessage(array)
Assert.assertTrue(packet.failed)
Assert.assertTrue(danaRPump.isTempBasalInProgress)
Assert.assertEquals(300, danaRPump.tempBasalPercent)
Assert.assertEquals(15 * 60, danaRPump.tempBasalTotalSec)
Assert.assertEquals("BASAL__TEMPORARY_BASAL_STATE", packet.friendlyName)
}
}

View file

@ -1,65 +0,0 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump;
import info.nightscout.androidaps.utils.SP;
import static org.junit.Assert.assertEquals;
/**
* Created by Rumen on 06.08.2018.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, SP.class, L.class})
public class DanaRS_Packet_Bolus_Get_Dual_BolusTest {
@Test
public void runTest() {
AAPSMocker.mockMainApp();
AAPSMocker.mockApplicationContext();
AAPSMocker.mockSP();
AAPSMocker.mockL();
DanaRS_Packet_Bolus_Get_Dual_Bolus packet = new DanaRS_Packet_Bolus_Get_Dual_Bolus();
// test params
assertEquals(null, packet.getRequestParams());
// test message decoding
double testValue = 0d;
packet.handleMessage(createArray(10, (byte) testValue));
assertEquals(testValue != 0d, packet.failed);
testValue = 1d;
packet.handleMessage(createArray(10, (byte) testValue));
// is pump.bolustep set to 1
DanaRPump pump = DanaRPump.getInstance();
assertEquals((((byte)testValue & 0x000000FF) << 8) + ((byte) testValue & 0x000000FF), pump.bolusStep, 0);
assertEquals(testValue != 0d, packet.failed);
assertEquals("BOLUS__GET_DUAL_BOLUS", packet.getFriendlyName());
}
byte[] createArray(int length, byte fillWith){
byte[] ret = new byte[length];
for(int i = 0; i<length; i++){
ret[i] = fillWith;
}
return ret;
}
double[] createArray(int length, double fillWith){
double[] ret = new double[length];
for(int i = 0; i<length; i++){
ret[i] = fillWith;
}
return ret;
}
}

View file

@ -0,0 +1,31 @@
package info.nightscout.androidaps.plugins.pump.danaRS.comm
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest()
class DanaRS_Packet_Bolus_Get_Dual_BolusTest : DanaRSTestBase() {
@Test fun runTest() {
val packet = DanaRS_Packet_Bolus_Get_Dual_Bolus(aapsLogger, danaRPump)
// test params
Assert.assertEquals(null, packet.requestParams)
val array = ByteArray(20)
putByteToArray(array, 0, 1)
putIntToArray(array, 1, (1.0 * 100).toInt())
putIntToArray(array, 3, (0.55 * 100).toInt())
putIntToArray(array, 5, (40.0 * 100).toInt())
packet.handleMessage(array)
Assert.assertTrue(packet.failed)
Assert.assertEquals(1.0, danaRPump.bolusStep, 0.0)
Assert.assertEquals(0.55, danaRPump.extendedBolusAbsoluteRate, 0.0)
Assert.assertEquals(40.0, danaRPump.maxBolus, 0.0)
Assert.assertEquals("BOLUS__GET_DUAL_BOLUS", packet.friendlyName)
}
}