diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.kt index c7b55e93ae..7dff52b472 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.kt @@ -127,6 +127,7 @@ open class Notification { const val NSCLIENT_VERSION_DOES_NOT_MATCH = 73 const val VERSION_EXPIRE = 74 const val INVALID_PROFILE_NOT_ACCEPTED = 75 + const val MDT_INVALID_HISTORY_DATA = 76 const val USER_MESSAGE = 1000 diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.kt index 3478cfce7c..ffe626d696 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/comm/MedtronicCommunicationManager.kt @@ -579,7 +579,11 @@ class MedtronicCommunicationManager // This empty constructor must be kept, oth fun getTemporaryBasal(): TempBasalPair? { return sendAndGetResponseWithCheck(MedtronicCommandType.ReadTemporaryBasal) { _, _, rawContent -> - TempBasalPair(aapsLogger, rawContent) + if (rawContent.size >= 5) TempBasalPair(aapsLogger, rawContent) + else { + aapsLogger.debug(LTag.PUMPBTCOMM, "Received invalid TempBasal response" + ByteUtil.getHex(rawContent)) + null + } } } diff --git a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt index d5036a412d..c2525f9d31 100644 --- a/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt +++ b/medtronic/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryData.kt @@ -8,12 +8,16 @@ import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.PumpSync import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification +import info.nightscout.androidaps.plugins.general.overview.notifications.Notification import info.nightscout.androidaps.plugins.pump.common.defs.PumpType import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntry import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryBolus import info.nightscout.androidaps.plugins.pump.common.sync.PumpDbEntryTBR import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil +import info.nightscout.androidaps.plugins.pump.medtronic.R import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.MedtronicPumpHistoryDecoder import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntryType @@ -24,6 +28,7 @@ import info.nightscout.androidaps.plugins.pump.medtronic.defs.PumpBolusType import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicConst import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil +import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.apache.commons.lang3.StringUtils import org.joda.time.LocalDateTime @@ -40,12 +45,13 @@ import javax.inject.Singleton // all times that time changed (TZ, DST, etc.). Data needs to be returned in batches (time_changed batches, so that we can // handle it. It would help to assign sort_ids to items (from oldest (1) to newest (x) // -@Suppress("DEPRECATION") @Singleton class MedtronicHistoryData @Inject constructor( val injector: HasAndroidInjector, val aapsLogger: AAPSLogger, val sp: SP, + val rh: ResourceHelper, + val rxBus: RxBus, val activePlugin: ActivePlugin, val medtronicUtil: MedtronicUtil, val medtronicPumpHistoryDecoder: MedtronicPumpHistoryDecoder, @@ -207,7 +213,7 @@ class MedtronicHistoryData @Inject constructor( allPumpIds.remove(pumpHistoryEntry.pumpId) } } - allHistory.removeAll(removeList) + allHistory.removeAll(removeList.toSet()) this.sort(allHistory) aapsLogger.debug(LTag.PUMP, String.format(Locale.ENGLISH, "All History records [afterFilterCount=%d, removedItemsCount=%d, newItemsCount=%d]", allHistory.size, removeList.size, newHistory.size)) @@ -659,17 +665,23 @@ class MedtronicHistoryData @Inject constructor( "pumpId=${tempBasalProcessDTO.pumpId}, rate=${tbrEntry.insulinRate} U, " + "duration=${tempBasalProcessDTO.durationAsSeconds} s, pumpSerial=${medtronicPumpStatus.serialNumber}]") - val result = pumpSync.syncTemporaryBasalWithPumpId( - tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), - tbrEntry.insulinRate, - tempBasalProcessDTO.durationAsSeconds * 1000L, - !tbrEntry.isPercent, - PumpSync.TemporaryBasalType.NORMAL, - tempBasalProcessDTO.pumpId, - medtronicPumpStatus.pumpType, - medtronicPumpStatus.serialNumber) + if (tempBasalProcessDTO.durationAsSeconds == 0) { + rxBus.send(EventNewNotification(Notification(Notification.MDT_INVALID_HISTORY_DATA, rh.gs(R.string.invalid_history_data), Notification.URGENT))) + aapsLogger.debug(LTag.PUMP, "syncTemporaryBasalWithPumpId - Skipped") + } else { + val result = pumpSync.syncTemporaryBasalWithPumpId( + tryToGetByLocalTime(tempBasalProcessDTO.atechDateTime), + tbrEntry.insulinRate, + tempBasalProcessDTO.durationAsSeconds * 1000L, + !tbrEntry.isPercent, + PumpSync.TemporaryBasalType.NORMAL, + tempBasalProcessDTO.pumpId, + medtronicPumpStatus.pumpType, + medtronicPumpStatus.serialNumber + ) - aapsLogger.debug(LTag.PUMP, "syncTemporaryBasalWithPumpId - Result: $result") + aapsLogger.debug(LTag.PUMP, "syncTemporaryBasalWithPumpId - Result: $result") + } if (medtronicPumpStatus.runningTBR != null) { if (!isTBRActive(medtronicPumpStatus.runningTBR!!)) { diff --git a/medtronic/src/main/res/values/strings.xml b/medtronic/src/main/res/values/strings.xml index 56d216e9cf..5ec63bcdfd 100644 --- a/medtronic/src/main/res/values/strings.xml +++ b/medtronic/src/main/res/values/strings.xml @@ -122,5 +122,6 @@ If enabled, it will cancel a temporary basal before the end of each hour. This method can help stop some pumps beeping/vibrating on the hour. %1$.1f U/h (%2$d min remaining) ^\\d{6} + Invalid pump history data detected. Open new issue and provide logs. \ No newline at end of file diff --git a/medtronic/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryDataUTest.kt b/medtronic/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryDataUTest.kt index 4f9fef20c1..f9c3545be2 100644 --- a/medtronic/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryDataUTest.kt +++ b/medtronic/src/test/java/info/nightscout/androidaps/plugins/pump/medtronic/data/MedtronicHistoryDataUTest.kt @@ -11,12 +11,14 @@ import info.nightscout.androidaps.TestBase import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.PumpSync import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.pump.common.sync.PumpSyncStorage import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.MedtronicPumpHistoryDecoder import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.PumpHistoryEntry import info.nightscout.androidaps.plugins.pump.medtronic.data.dto.TempBasalPair import info.nightscout.androidaps.plugins.pump.medtronic.driver.MedtronicPumpStatus import info.nightscout.androidaps.plugins.pump.medtronic.util.MedtronicUtil +import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP import org.hamcrest.Matchers.notNullValue import org.junit.Assert.* @@ -35,6 +37,8 @@ class MedtronicHistoryDataUTest : TestBase() { @Mock lateinit var pumpSync: PumpSync @Mock lateinit var pumpSyncStorage: PumpSyncStorage @Mock lateinit var sp: SP + @Mock lateinit var rh: ResourceHelper + @Mock lateinit var rxBus: RxBus private val packetInjector = HasAndroidInjector { @@ -48,7 +52,7 @@ class MedtronicHistoryDataUTest : TestBase() { @Test fun createTBRProcessList() { - var unitToTest = MedtronicHistoryData(packetInjector, aapsLogger, sp, activePlugin, + var unitToTest = MedtronicHistoryData(packetInjector, aapsLogger, sp, rh, rxBus, activePlugin, medtronicUtil, medtronicPumpHistoryDecoder, medtronicPumpStatus, pumpSync,