diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.kt index 8c50099756..6d1d4a2bc4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgHistoryAll.kt @@ -25,8 +25,8 @@ open class MsgHistoryAll( val datetimewihtsec = dateTimeSecFromBuff(bytes, 1) // 6 bytes val dailyBasal = intFromBuff(bytes, 4, 2) * 0.01 val dailyBolus = intFromBuff(bytes, 6, 2) * 0.01 - val paramByte5 = intFromBuff(bytes, 4, 1).toByte() - val paramByte6 = intFromBuff(bytes, 5, 1).toByte() + //val paramByte5 = intFromBuff(bytes, 4, 1).toByte() + //val paramByte6 = intFromBuff(bytes, 5, 1).toByte() val paramByte7 = intFromBuff(bytes, 6, 1).toByte() val paramByte8 = intFromBuff(bytes, 7, 1).toByte() val value = intFromBuff(bytes, 8, 2).toDouble() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgSettingPumpTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgSettingPumpTime.kt index df356ca744..07b3825b84 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgSettingPumpTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgSettingPumpTime.kt @@ -4,6 +4,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump import info.nightscout.androidaps.utils.DateUtil +import org.joda.time.DateTime import java.util.* class MsgSettingPumpTime( @@ -17,14 +18,14 @@ class MsgSettingPumpTime( } override fun handleMessage(bytes: ByteArray) { - val time = Date( - 100 + intFromBuff(bytes, 5, 1), - intFromBuff(bytes, 4, 1) - 1, + val time = DateTime( + 2000 + intFromBuff(bytes, 5, 1), + intFromBuff(bytes, 4, 1), intFromBuff(bytes, 3, 1), intFromBuff(bytes, 2, 1), intFromBuff(bytes, 1, 1), intFromBuff(bytes, 0, 1) - ).time + ).millis aapsLogger.debug(LTag.PUMPCOMM, "Pump time: " + DateUtil.dateAndTimeString(time) + " Phone time: " + Date()) danaRPump.pumpTime = time } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt index 395d90b6ae..dca14ce099 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_Information.kt @@ -5,6 +5,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump import info.nightscout.androidaps.utils.DateUtil +import org.joda.time.DateTime import java.util.* class DanaRS_Packet_Bolus_Get_Step_Bolus_Information( @@ -18,32 +19,15 @@ class DanaRS_Packet_Bolus_Get_Step_Bolus_Information( } override fun handleMessage(data: ByteArray) { - var dataIndex = DATA_START - var dataSize = 1 - val error = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 1 - val bolusType = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 2 - danaRPump.initialBolusAmount = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 - val lbt = Date() // it doesn't provide day only hour+min, workaround: expecting today - dataIndex += dataSize - dataSize = 1 - lbt.hours = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 1 - lbt.minutes = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - danaRPump.lastBolusTime = lbt.time - dataIndex += dataSize - dataSize = 2 - danaRPump.lastBolusAmount = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 - dataIndex += dataSize - dataSize = 2 - danaRPump.maxBolus = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 - dataIndex += dataSize - dataSize = 1 - danaRPump.bolusStep = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 + val error = intFromBuff(data, 0, 1) + val bolusType = intFromBuff(data, 1, 1) + danaRPump.initialBolusAmount = intFromBuff(data, 2, 2) / 100.0 + val hours = intFromBuff(data, 4, 1) + val minutes = intFromBuff(data, 5, 1) + danaRPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis + danaRPump.lastBolusAmount = intFromBuff(data, 6, 2) / 100.0 + danaRPump.maxBolus = intFromBuff(data, 8, 2) / 100.0 + danaRPump.bolusStep = intFromBuff(data, 10, 1) / 100.0 failed = error != 0 aapsLogger.debug(LTag.PUMPCOMM, "Result: $error") aapsLogger.debug(LTag.PUMPCOMM, "BolusType: $bolusType") diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_Information.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_Information.kt index 875b91f397..eb7d617d3b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_Information.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_Information.kt @@ -1,11 +1,11 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm -import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump +import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.utils.DateUtil -import java.util.* +import org.joda.time.DateTime class DanaRS_Packet_General_Get_More_Information( private val aapsLogger: AAPSLogger, @@ -22,37 +22,21 @@ class DanaRS_Packet_General_Get_More_Information( failed = true return } - var dataIndex = DATA_START - var dataSize = 2 - danaRPump.iob = byteArrayToInt(getBytes(data, dataIndex, dataSize)).toDouble() - dataIndex += dataSize - dataSize = 2 - danaRPump.dailyTotalUnits = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 - dataIndex += dataSize - dataSize = 1 - danaRPump.isExtendedInProgress = byteArrayToInt(getBytes(data, dataIndex, dataSize)) == 0x01 - dataIndex += dataSize - dataSize = 2 - danaRPump.extendedBolusRemainingMinutes = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 2 - //val remainRate = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 - val lastBolusTime = Date() // it doesn't provide day only hour+min, workaround: expecting today - dataIndex += dataSize - dataSize = 1 - lastBolusTime.hours = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 1 - lastBolusTime.minutes = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - dataIndex += dataSize - dataSize = 2 - danaRPump.lastBolusAmount = byteArrayToInt(getBytes(data, dataIndex, dataSize)).toDouble() + danaRPump.iob = intFromBuff(data, 0, 2) / 100.0 + danaRPump.dailyTotalUnits = intFromBuff(data, 2, 2) / 100.0 + danaRPump.isExtendedInProgress = intFromBuff(data, 4, 1) == 0x01 + danaRPump.extendedBolusRemainingMinutes = intFromBuff(data, 5, 2) + // val remainRate = intFromBuff(data, 7, 2) / 100.0 + val hours = intFromBuff(data, 9, 1) + val minutes = intFromBuff(data, 10, 1) + danaRPump.lastBolusTime = DateTime.now().withHourOfDay(hours).withMinuteOfHour(minutes).millis + danaRPump.lastBolusAmount = intFromBuff(data, 11, 2) / 100.0 // On DanaRS DailyUnits can't be more than 160 if (danaRPump.dailyTotalUnits > 160) failed = true aapsLogger.debug(LTag.PUMPCOMM, "Daily total units: " + danaRPump.dailyTotalUnits.toString() + " U") aapsLogger.debug(LTag.PUMPCOMM, "Is extended in progress: " + danaRPump.isExtendedInProgress) aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus remaining minutes: " + danaRPump.extendedBolusRemainingMinutes) - aapsLogger.debug(LTag.PUMPCOMM, "Last bolus time: " + DateUtil.dateAndTimeAndSecondsString(lastBolusTime.time)) + aapsLogger.debug(LTag.PUMPCOMM, "Last bolus time: " + DateUtil.dateAndTimeAndSecondsString(danaRPump.lastBolusTime)) aapsLogger.debug(LTag.PUMPCOMM, "Last bolus amount: " + danaRPump.lastBolusAmount) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.kt index e8da961028..4273f6ca0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_.kt @@ -8,6 +8,7 @@ import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes import info.nightscout.androidaps.plugins.pump.danaR.events.EventDanaRSyncStatus import info.nightscout.androidaps.utils.DateUtil +import org.joda.time.DateTime import java.util.* abstract class DanaRS_Packet_History_( @@ -25,6 +26,7 @@ abstract class DanaRS_Packet_History_( var done = false var totalCount = 0 + val danaRHistoryRecord = DanaRHistoryRecord() init { val cal = GregorianCalendar() @@ -79,14 +81,13 @@ abstract class DanaRS_Packet_History_( val historySecond = byteArrayToInt(getBytes(data, DATA_START + 6, 1)) val paramByte7 = historySecond.toByte() val dailyBolus: Double = ((data[DATA_START + 6].toInt() and 0xFF shl 8) + (data[DATA_START + 7].toInt() and 0xFF)) * 0.01 - val date = Date(100 + historyYear, historyMonth - 1, historyDay) - val datetime = Date(100 + historyYear, historyMonth - 1, historyDay, historyHour, historyMinute) - val datetimewihtsec = Date(100 + historyYear, historyMonth - 1, historyDay, historyHour, historyMinute, historySecond) + val date = DateTime(2000 + historyYear, historyMonth, historyDay, 0, 0) + val datetime = DateTime(2000 + historyYear, historyMonth, historyDay, historyHour, historyMinute) + val datetimewihtsec = DateTime(2000 + historyYear, historyMonth, historyDay, historyHour, historyMinute, historySecond) val historyCode = byteArrayToInt(getBytes(data, DATA_START + 7, 1)) val paramByte8 = historyCode.toByte() val value: Int = (data[DATA_START + 8].toInt() and 0xFF shl 8) + (data[DATA_START + 9].toInt() and 0xFF) - aapsLogger.debug(LTag.PUMPCOMM, "History packet: " + recordCode + " Date: " + DateUtil.dateAndTimeString(datetimewihtsec) + " Code: " + historyCode + " Value: " + value) - val danaRHistoryRecord = DanaRHistoryRecord() + aapsLogger.debug(LTag.PUMPCOMM, "History packet: " + recordCode + " Date: " + DateUtil.dateAndTimeString(datetimewihtsec.millis) + " Code: " + historyCode + " Value: " + value) danaRHistoryRecord.setBytes(data) // danaRHistoryRecord.recordCode is different from DanaR codes // set in switch for every type @@ -94,7 +95,7 @@ abstract class DanaRS_Packet_History_( when (recordCode) { 0x02 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_BOLUS - danaRHistoryRecord.recordDate = datetime.time + danaRHistoryRecord.recordDate = datetime.millis when (0xF0 and paramByte8.toInt()) { 0xA0 -> { danaRHistoryRecord.bolusType = "DS" @@ -125,7 +126,7 @@ abstract class DanaRS_Packet_History_( 0x03 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_DAILY messageType += "dailyinsulin" - danaRHistoryRecord.recordDate = date.time + danaRHistoryRecord.recordDate = date.millis danaRHistoryRecord.recordDailyBasal = dailyBasal danaRHistoryRecord.recordDailyBolus = dailyBolus } @@ -133,49 +134,49 @@ abstract class DanaRS_Packet_History_( 0x04 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_PRIME messageType += "prime" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value * 0.01 } 0x05 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_REFILL messageType += "refill" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value * 0.01 } 0x0b -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_BASALHOUR messageType += "basal hour" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value * 0.01 } 0x99 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_TEMP_BASAL messageType += "tb" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value * 0.01 } 0x06 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_GLUCOSE messageType += "glucose" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value.toDouble() } 0x07 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_CARBO messageType += "carbo" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis danaRHistoryRecord.recordValue = value.toDouble() } 0x0a -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_ALARM messageType += "alarm" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis var strAlarm = "None" when (paramByte8.toInt()) { 67 -> strAlarm = "Check" @@ -190,7 +191,7 @@ abstract class DanaRS_Packet_History_( 0x09 -> { danaRHistoryRecord.recordCode = RecordTypes.RECORD_TYPE_SUSPEND messageType += "suspend" - danaRHistoryRecord.recordDate = datetimewihtsec.time + danaRHistoryRecord.recordDate = datetimewihtsec.millis var strRecordValue = "Off" if (paramByte8.toInt() == 79) strRecordValue = "On" danaRHistoryRecord.stringRecordValue = strRecordValue diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_Time.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_Time.kt index b582a0b492..4db309afe0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_Time.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_Time.kt @@ -1,11 +1,11 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm -import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump +import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.utils.DateUtil -import java.util.* +import org.joda.time.DateTime class DanaRS_Packet_Option_Get_Pump_Time( private val aapsLogger: AAPSLogger, @@ -36,10 +36,9 @@ class DanaRS_Packet_Option_Get_Pump_Time( dataIndex += dataSize dataSize = 1 val sec = byteArrayToInt(getBytes(data, dataIndex, dataSize)) - val time = Date(100 + year, month - 1, day, hour, min, sec) - danaRPump.pumpTime = time.time - failed = year == month && month == day && day == hour && hour == min && min == sec && sec == 1 - aapsLogger.debug(LTag.PUMPCOMM, "Pump time " + DateUtil.dateAndTimeString(time)) + val time = DateTime(2000 + year, month, day, hour, min, sec) + danaRPump.pumpTime = time.millis + aapsLogger.debug(LTag.PUMPCOMM, "Pump time " + DateUtil.dateAndTimeString(time.millis)) } override fun handleMessageNotReceived() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.kt index 54a38af3de..73cf49c6df 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_Time.kt @@ -1,10 +1,10 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm -import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.utils.DateUtil -import java.util.* +import org.joda.time.DateTime class DanaRS_Packet_Option_Set_Pump_Time( private val aapsLogger: AAPSLogger, @@ -19,14 +19,14 @@ class DanaRS_Packet_Option_Set_Pump_Time( } override fun getRequestParams(): ByteArray { - val date = Date(time) + val date = DateTime(time) val request = ByteArray(6) - request[0] = (date.year - 100 and 0xff).toByte() - request[1] = (date.month + 1 and 0xff).toByte() - request[2] = (date.date and 0xff).toByte() - request[3] = (date.hours and 0xff).toByte() - request[4] = (date.minutes and 0xff).toByte() - request[5] = (date.seconds and 0xff).toByte() + request[0] = (date.year - 2000 and 0xff).toByte() + request[1] = (date.monthOfYear and 0xff).toByte() + request[2] = (date.dayOfMonth and 0xff).toByte() + request[3] = (date.hourOfDay and 0xff).toByte() + request[4] = (date.minuteOfHour and 0xff).toByte() + request[5] = (date.secondOfMinute and 0xff).toByte() return request } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgSetAPSTempBasalStart_v2.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgSetAPSTempBasalStart_v2.kt index 95d2b56b77..2ff8c669ef 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgSetAPSTempBasalStart_v2.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/comm/MsgSetAPSTempBasalStart_v2.kt @@ -4,6 +4,7 @@ import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.pump.danaR.comm.MessageBase +@Suppress("UNUSED_PARAMETER") class MsgSetAPSTempBasalStart_v2( private val aapsLogger: AAPSLogger, private var percent: Int, diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_InformationTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_InformationTest.kt index 86239a8ea1..c3ac9a12d6 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_InformationTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Bolus_Get_Step_Bolus_InformationTest.kt @@ -5,6 +5,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner +import java.util.* @RunWith(PowerMockRunner::class) @PrepareForTest() @@ -12,14 +13,26 @@ class DanaRS_Packet_Bolus_Get_Step_Bolus_InformationTest : DanaRSTestBase() { @Test fun runTest() { val packet = DanaRS_Packet_Bolus_Get_Step_Bolus_Information(aapsLogger, danaRPump) - Assert.assertEquals(null, packet.requestParams) - // test message decoding - packet.handleMessage(createArray(34, 0.toByte())) - Assert.assertEquals(false, packet.failed) - packet.handleMessage(createArray(34, 1.toByte())) - val valueRequested: Int = (1 and 0x000000FF shl 8) + (1 and 0x000000FF) - Assert.assertEquals(valueRequested / 100.0, danaRPump.lastBolusAmount, 0.0) - Assert.assertEquals(true, packet.failed) + + val array = createArray(13, 0.toByte()) // 11 + 2 + putByteToArray(array, 0, 2) // error 2 + putByteToArray(array, 1, 3) // bolus type 3 + putIntToArray(array, 2, 600) // initial bolus amount 6 + putByteToArray(array, 4, 13) // 13h + putByteToArray(array, 5, 20) // 20min + putIntToArray(array, 6, 1250) // last bolus amount 12.5 + putIntToArray(array, 8, 2500) // max bolus 25 + putByteToArray(array, 10, 100) // bolus step 1 + + packet.handleMessage(array) + Assert.assertTrue(packet.failed) + Assert.assertEquals(6.0, danaRPump.initialBolusAmount, 0.01) + val lastBolus = Date(danaRPump.lastBolusTime) + Assert.assertEquals(13, lastBolus.hours) + Assert.assertEquals(20, lastBolus.minutes) + Assert.assertEquals(12.5, danaRPump.lastBolusAmount, 0.01) + Assert.assertEquals(25.0, danaRPump.maxBolus, 0.01) + Assert.assertEquals(1.0, danaRPump.bolusStep, 0.01) Assert.assertEquals("BOLUS__GET_STEP_BOLUS_INFORMATION", packet.friendlyName) } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_InformationTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_InformationTest.kt index 327aa1c8a6..12ff721a2b 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_InformationTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_General_Get_More_InformationTest.kt @@ -3,25 +3,39 @@ 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 +import java.util.* @RunWith(PowerMockRunner::class) -@PrepareForTest() class DanaRS_Packet_General_Get_More_InformationTest : DanaRSTestBase() { @Test fun runTest() { var packet = DanaRS_Packet_General_Get_More_Information(aapsLogger, danaRPump) - Assert.assertEquals(null, packet.requestParams) - // test message decoding - // test for the length message - packet.handleMessage(createArray(13, 0.toByte())) - Assert.assertEquals(true, packet.failed) + + packet.handleMessage(createArray(14, 0.toByte())) + Assert.assertTrue(packet.failed) + packet = DanaRS_Packet_General_Get_More_Information(aapsLogger, danaRPump) - packet.handleMessage(createArray(15, 0.toByte())) - Assert.assertEquals(false, packet.failed) - packet.handleMessage(createArray(15, 161.toByte())) - Assert.assertEquals(true, packet.failed) + val array = createArray(15, 0.toByte()) // 13 + 2 + putIntToArray(array, 0, 600) // iob 6 + putIntToArray(array, 2, 1250) // daily units 12.5 + putByteToArray(array, 4, 1) // is extended in progress + putIntToArray(array, 5, 150) // extended remaining minutes 150 + putByteToArray(array, 9, 15) // hours 15 + putByteToArray(array, 10, 25) // minutes 25 + putIntToArray(array, 11, 170) // last bolus manout 1.70 + + packet.handleMessage(array) + Assert.assertFalse(packet.failed) + Assert.assertEquals(6.0, danaRPump.iob, 0.01) + Assert.assertEquals(12.5, danaRPump.dailyTotalUnits, 0.01) + Assert.assertTrue(danaRPump.isExtendedInProgress) + Assert.assertEquals(150, danaRPump.extendedBolusRemainingMinutes) + val lastBolus = Date(danaRPump.lastBolusTime) + Assert.assertEquals(15, lastBolus.hours) + Assert.assertEquals(25, lastBolus.minutes) + Assert.assertEquals(1.7, danaRPump.lastBolusAmount, 0.01) + Assert.assertEquals("REVIEW__GET_MORE_INFORMATION", packet.friendlyName) } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_AlarmTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_AlarmTest.kt index 743d3f66e7..bc10832af9 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_AlarmTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_History_AlarmTest.kt @@ -1,17 +1,52 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.db.DatabaseHelper +import info.nightscout.androidaps.plugins.pump.danaR.comm.RecordTypes import org.junit.Assert +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner +import java.util.* @RunWith(PowerMockRunner::class) -@PrepareForTest() +@PrepareForTest(MainApp::class) class DanaRS_Packet_History_AlarmTest : DanaRSTestBase() { - @Test fun runTest() { - val packet = DanaRS_Packet_History_Alarm(aapsLogger, rxBus, System.currentTimeMillis()) + @Mock lateinit var databaseHelper: DatabaseHelper + + @Before + fun mock() { + PowerMockito.mockStatic(MainApp::class.java) + `when`(MainApp.getDbHelper()).thenReturn(databaseHelper) + } + + @Test + fun runTest() { + val packet = DanaRS_Packet_History_Alarm(aapsLogger, rxBus, 0) + + val array = createArray(12, 0.toByte()) // 10 + 2 + putByteToArray(array, 0, 0x0A) // record code alarm + putByteToArray(array, 1, 19) // year 2019 + putByteToArray(array, 2, 2) // month february + putByteToArray(array, 3, 4) // day 4 + putByteToArray(array, 4, 20) // hour 20 + putByteToArray(array, 5, 11) // min 11 + putByteToArray(array, 6, 35) // second 35 + putByteToArray(array, 7, 79) // occlusion + putByteToArray(array, 8, 1) // value + putByteToArray(array, 9, 100) // value + + packet.handleMessage(array) + Assert.assertEquals(RecordTypes.RECORD_TYPE_ALARM, packet.danaRHistoryRecord.recordCode) + Assert.assertEquals(Date(119, 1, 4, 20, 11, 35).time, packet.danaRHistoryRecord.recordDate) + Assert.assertEquals("Occlusion", packet.danaRHistoryRecord.recordAlarm) + Assert.assertEquals(3.56, packet.danaRHistoryRecord.recordValue, 0.01) Assert.assertEquals("REVIEW__ALARM", packet.friendlyName) } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_TimeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_TimeTest.kt index 8f74812e9e..2ac915644f 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_TimeTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Get_Pump_TimeTest.kt @@ -1,25 +1,26 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm +import org.joda.time.DateTime 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_Option_Get_Pump_TimeTest : DanaRSTestBase() { @Test fun runTest() { val packet = DanaRS_Packet_Option_Get_Pump_Time(aapsLogger, danaRPump) - // test params - Assert.assertEquals(null, packet.requestParams) - // test message decoding - packet.handleMessage(createArray(8, 0.toByte())) - Assert.assertEquals(false, packet.failed) - // this should fail - packet.handleMessage(createArray(8, 1.toByte())) - Assert.assertEquals(true, packet.failed) + val array = createArray(8, 0.toByte()) // 6 + 2 + putByteToArray(array, 0, 19) // year 2019 + putByteToArray(array, 1, 2) // month february + putByteToArray(array, 2, 4) // day 4 + putByteToArray(array, 3, 20) // hour 20 + putByteToArray(array, 4, 11) // min 11 + putByteToArray(array, 5, 35) // second 35 + + packet.handleMessage(array) + Assert.assertEquals(DateTime(2019, 2, 4, 20, 11, 35).millis, danaRPump.pumpTime) Assert.assertEquals("OPTION__GET_PUMP_TIME", packet.friendlyName) } } \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_TimeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_TimeTest.kt index 5fd034e5b6..bf34d76510 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_TimeTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/pump/danaRS/comm/DanaRS_Packet_Option_Set_Pump_TimeTest.kt @@ -1,22 +1,25 @@ package info.nightscout.androidaps.plugins.pump.danaRS.comm -import info.nightscout.androidaps.utils.DateUtil 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 import java.util.* @RunWith(PowerMockRunner::class) -@PrepareForTest() class DanaRS_Packet_Option_Set_Pump_TimeTest : DanaRSTestBase() { @Test fun runTest() { - val packet = DanaRS_Packet_Option_Set_Pump_Time(aapsLogger, DateUtil.now()) + val date = Date() + val packet = DanaRS_Packet_Option_Set_Pump_Time(aapsLogger, date.time) // test params val params = packet.requestParams - Assert.assertEquals((Date().date and 0xff).toByte(), params[2]) + Assert.assertEquals((date.year - 100 and 0xff).toByte(), params[0]) // 2019 -> 19 + Assert.assertEquals((date.month + 1 and 0xff).toByte(), params[1]) + Assert.assertEquals((date.date and 0xff).toByte(), params[2]) + Assert.assertEquals((date.hours and 0xff).toByte(), params[3]) + Assert.assertEquals((date.minutes and 0xff).toByte(), params[4]) + Assert.assertEquals((date.seconds and 0xff).toByte(), params[5]) // test message decoding packet.handleMessage(createArray(3, 0.toByte())) Assert.assertEquals(false, packet.failed)