From f74955d062d3ded3e98a0ca58cf0c647a079d3c1 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 2 Jun 2021 16:54:58 +0200 Subject: [PATCH 1/4] Improve APS result logging --- .../androidaps/plugins/aps/loop/APSResult.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.kt b/core/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.kt index 39202e1a4b..6c7633e693 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/aps/loop/APSResult.kt @@ -30,6 +30,7 @@ import kotlin.math.max /** * Created by mike on 09.06.2016. */ +@Suppress("LeakingThis") open class APSResult @Inject constructor(val injector: HasAndroidInjector) { @Inject lateinit var aapsLogger: AAPSLogger @@ -42,7 +43,7 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { @Inject lateinit var dateUtil: DateUtil var date: Long = 0 - var reason: String? = null + var reason: String = "" var rate = 0.0 var percent = 0 var usePercent = false @@ -102,16 +103,16 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { val pump = activePlugin.activePump if (isChangeRequested) { // rate - var ret: String = if (rate == 0.0 && duration == 0) "${resourceHelper.gs(R.string.canceltemp)}\n" + var ret: String = if (rate == 0.0 && duration == 0) "${resourceHelper.gs(R.string.canceltemp)} " else if (rate == -1.0) "${resourceHelper.gs(R.string.let_temp_basal_run)}\n" - else if (usePercent) "${resourceHelper.gs(R.string.rate)}: ${DecimalFormatter.to2Decimal(percent.toDouble())}% (${DecimalFormatter.to2Decimal(percent * pump.baseBasalRate / 100.0)} U/h)\n" + - "${resourceHelper.gs(R.string.duration)}: ${DecimalFormatter.to2Decimal(duration.toDouble())} min\n" - else "${resourceHelper.gs(R.string.rate)}: ${DecimalFormatter.to2Decimal(rate)} U/h (${DecimalFormatter.to2Decimal(rate / pump.baseBasalRate * 100)}%)" + - "${resourceHelper.gs(R.string.duration)}: ${DecimalFormatter.to2Decimal(duration.toDouble())} min\n" + else if (usePercent) "${resourceHelper.gs(R.string.rate)}: ${DecimalFormatter.to2Decimal(percent.toDouble())}% (${DecimalFormatter.to2Decimal(percent * pump.baseBasalRate / 100.0)} U/h) " + + "${resourceHelper.gs(R.string.duration)}: ${DecimalFormatter.to2Decimal(duration.toDouble())} min " + else "${resourceHelper.gs(R.string.rate)}: ${DecimalFormatter.to2Decimal(rate)} U/h (${DecimalFormatter.to2Decimal(rate / pump.baseBasalRate * 100)}%) " + + "${resourceHelper.gs(R.string.duration)}: ${DecimalFormatter.to2Decimal(duration.toDouble())} min " // smb - if (smb != 0.0) ret += "SMB: ${DecimalFormatter.toPumpSupportedBolus(smb, activePlugin.activePump, resourceHelper)}\n" + if (smb != 0.0) ret += "SMB: ${DecimalFormatter.toPumpSupportedBolus(smb, activePlugin.activePump, resourceHelper)} " if (isCarbsRequired) { - ret += "$carbsRequiredText\n" + ret += "$carbsRequiredText " } // reason @@ -140,7 +141,7 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { } // reason - ret += "" + resourceHelper.gs(R.string.reason) + ": " + reason!!.replace("<", "<").replace(">", ">") + ret += "" + resourceHelper.gs(R.string.reason) + ": " + reason.replace("<", "<").replace(">", ">") return fromHtml(ret) } return if (isCarbsRequired) { @@ -156,7 +157,7 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { protected fun doClone(newResult: APSResult) { newResult.date = date - newResult.reason = if (reason != null) reason else null + newResult.reason = reason newResult.rate = rate newResult.duration = duration newResult.tempBasalRequested = tempBasalRequested @@ -332,12 +333,12 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { aapsLogger.debug(LTag.APS, "FALSE: Temp equal") return false } - // always report zerotemp + // always report zero temp if (percent == 0) { aapsLogger.debug(LTag.APS, "TRUE: Zero temp") return true } - // always report hightemp + // always report high temp if (pump.pumpDescription.tempBasalStyle == PumpDescription.PERCENT) { val pumpLimit = pump.pumpDescription.pumpType.tbrSettings?.maxDose ?: 0.0 if (percent.toDouble() == pumpLimit) { @@ -368,12 +369,12 @@ open class APSResult @Inject constructor(val injector: HasAndroidInjector) { aapsLogger.debug(LTag.APS, "FALSE: Temp equal") return false } - // always report zerotemp + // always report zero temp if (rate == 0.0) { aapsLogger.debug(LTag.APS, "TRUE: Zero temp") return true } - // always report hightemp + // always report high temp if (pump.pumpDescription.tempBasalStyle == PumpDescription.ABSOLUTE) { val pumpLimit = pump.pumpDescription.pumpType.tbrSettings?.maxDose ?: 0.0 if (rate == pumpLimit) { From ec73c4b0cbe74eef57c05c82089d5a3fd4829252 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 2 Jun 2021 16:55:19 +0200 Subject: [PATCH 2/4] Insight vibrate permission --- insight/src/main/AndroidManifest.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/insight/src/main/AndroidManifest.xml b/insight/src/main/AndroidManifest.xml index feecfc8067..f00fc7fba9 100644 --- a/insight/src/main/AndroidManifest.xml +++ b/insight/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + Date: Wed, 2 Jun 2021 16:56:13 +0200 Subject: [PATCH 3/4] immutable flag for PendingIntent --- .../nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt | 8 ++++---- .../general/overview/notifications/NotificationStore.kt | 2 +- .../PersistentNotificationPlugin.kt | 4 ++-- .../utils/androidNotification/NotificationHolderImpl.kt | 2 +- .../plugins/pump/insight/InsightAlertService.java | 6 +++--- .../complications/ComplicationTapBroadcastReceiver.java | 4 ++-- .../info/nightscout/androidaps/data/ListenerService.java | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt index b2a13d8386..ba1f0425c6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt @@ -311,15 +311,15 @@ open class LoopPlugin @Inject constructor( if (sp.getBoolean(R.string.key_enable_carbs_required_alert_local, true) && sp.getBoolean(R.string.key_raise_notifications_as_android_notifications, true)) { val intentAction5m = Intent(context, CarbSuggestionReceiver::class.java) intentAction5m.putExtra("ignoreDuration", 5) - val pendingIntent5m = PendingIntent.getBroadcast(context, 1, intentAction5m, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent5m = PendingIntent.getBroadcast(context, 1, intentAction5m, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) val actionIgnore5m = NotificationCompat.Action(R.drawable.ic_notif_aaps, resourceHelper.gs(R.string.ignore5m, "Ignore 5m"), pendingIntent5m) val intentAction15m = Intent(context, CarbSuggestionReceiver::class.java) intentAction15m.putExtra("ignoreDuration", 15) - val pendingIntent15m = PendingIntent.getBroadcast(context, 1, intentAction15m, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent15m = PendingIntent.getBroadcast(context, 1, intentAction15m, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) val actionIgnore15m = NotificationCompat.Action(R.drawable.ic_notif_aaps, resourceHelper.gs(R.string.ignore15m, "Ignore 15m"), pendingIntent15m) val intentAction30m = Intent(context, CarbSuggestionReceiver::class.java) intentAction30m.putExtra("ignoreDuration", 30) - val pendingIntent30m = PendingIntent.getBroadcast(context, 1, intentAction30m, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent30m = PendingIntent.getBroadcast(context, 1, intentAction30m, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) val actionIgnore30m = NotificationCompat.Action(R.drawable.ic_notif_aaps, resourceHelper.gs(R.string.ignore30m, "Ignore 30m"), pendingIntent30m) val builder = NotificationCompat.Builder(context, CHANNEL_ID) builder.setSmallIcon(R.drawable.notif_icon) @@ -441,7 +441,7 @@ open class LoopPlugin @Inject constructor( stackBuilder.addParentStack(MainActivity::class.java) // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent) - val resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT) + val resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) builder.setContentIntent(resultPendingIntent) builder.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000)) val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt index 2ac420ade8..b273ac952e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationStore.kt @@ -128,7 +128,7 @@ class NotificationStore @Inject constructor( private fun deleteIntent(id: Int): PendingIntent { val intent = Intent(context, DismissNotificationService::class.java) intent.putExtra("alertID", id) - return PendingIntent.getService(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT) + return PendingIntent.getService(context, id, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) } fun createNotificationChannel() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt index 7a7edb678b..ceac37b723 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.kt @@ -171,7 +171,7 @@ class PersistentNotificationPlugin @Inject constructor( val msgReadPendingIntent = PendingIntent.getBroadcast(context, notificationHolder.notificationID, msgReadIntent, - PendingIntent.FLAG_UPDATE_CURRENT) + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) val msgReplyIntent = Intent() .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) .setAction(REPLY_ACTION) @@ -181,7 +181,7 @@ class PersistentNotificationPlugin @Inject constructor( context, notificationHolder.notificationID, msgReplyIntent, - PendingIntent.FLAG_UPDATE_CURRENT) + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) // Build a RemoteInput for receiving voice input from devices val remoteInput = RemoteInput.Builder(EXTRA_VOICE_REPLY).build() // Create the UnreadConversation diff --git a/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt b/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt index dfe9ed21c4..4fda9e5fd8 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/androidNotification/NotificationHolderImpl.kt @@ -40,6 +40,6 @@ class NotificationHolderImpl @Inject constructor( override fun openAppIntent(context: Context): PendingIntent? = TaskStackBuilder.create(context).run { addParentStack(MainActivity::class.java) addNextIntent(Intent(context, MainActivity::class.java)) - getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT) + getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) } } diff --git a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java index a134c904be..8278203eae 100644 --- a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java +++ b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/InsightAlertService.java @@ -302,17 +302,17 @@ public class InsightAlertService extends DaggerService implements InsightConnect notificationBuilder.setContentText(HtmlHelper.INSTANCE.fromHtml(description).toString()); Intent fullScreenIntent = new Intent(this, InsightAlertActivity.class); - PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, true); switch (alert.getAlertStatus()) { case ACTIVE: Intent muteIntent = new Intent(this, InsightAlertService.class).putExtra("command", "mute"); - PendingIntent mutePendingIntent = PendingIntent.getService(this, 1, muteIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent mutePendingIntent = PendingIntent.getService(this, 1, muteIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.addAction(0, resourceHelper.gs(R.string.mute_alert), mutePendingIntent); case SNOOZED: Intent confirmIntent = new Intent(this, InsightAlertService.class).putExtra("command", "confirm"); - PendingIntent confirmPendingIntent = PendingIntent.getService(this, 2, confirmIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent confirmPendingIntent = PendingIntent.getService(this, 2, confirmIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.addAction(0, resourceHelper.gs(R.string.confirm), confirmPendingIntent); } diff --git a/wear/src/main/java/info/nightscout/androidaps/complications/ComplicationTapBroadcastReceiver.java b/wear/src/main/java/info/nightscout/androidaps/complications/ComplicationTapBroadcastReceiver.java index 33d302dd6f..8e791b0fbb 100644 --- a/wear/src/main/java/info/nightscout/androidaps/complications/ComplicationTapBroadcastReceiver.java +++ b/wear/src/main/java/info/nightscout/androidaps/complications/ComplicationTapBroadcastReceiver.java @@ -142,7 +142,7 @@ public class ComplicationTapBroadcastReceiver extends BroadcastReceiver { // Pass complicationId as the requestCode to ensure that different complications get // different intents. return PendingIntent.getBroadcast( - context, complicationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); + context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); } /** @@ -161,7 +161,7 @@ public class ComplicationTapBroadcastReceiver extends BroadcastReceiver { // Pass complicationId as the requestCode to ensure that different complications get // different intents. return PendingIntent.getBroadcast( - context, complicationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); + context, complicationId, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); } } diff --git a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java index e58d886f0e..78803079b8 100644 --- a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java +++ b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java @@ -589,7 +589,7 @@ public class ListenerService extends WearableListenerService implements GoogleAp intent.putExtras(params); PendingIntent resultPendingIntent = - PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); builder = builder.setContentIntent(resultPendingIntent); From c0dba456d78ebfaf7d2ec658071740fbd71cdbe0 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 2 Jun 2021 22:40:30 +0200 Subject: [PATCH 4/4] fix lst bolus query and smb pocessing --- .../androidaps/plugins/aps/loop/LoopPlugin.kt | 26 +++++++++++-- .../androidaps/queue/CommandQueue.kt | 10 +++-- .../queue/commands/CommandSMBBolus.kt | 1 + .../plugins/pump/PumpSyncImplementation.kt | 2 +- .../comm/DanaRSPacketAPSHistoryEvents.kt | 39 +++++++++---------- .../androidaps/database/daos/BolusDao.kt | 8 ++-- 6 files changed, 53 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt index ba1f0425c6..40a32ddcc2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.kt @@ -166,11 +166,13 @@ open class LoopPlugin @Inject constructor( } override val isSuspended: Boolean - get() = repository.getOfflineEventActiveAt(dateUtil.now()).blockingGet() is ValueWrapper.Existing + get() = repository.getOfflineEventActiveAt(dateUtil.now()).blockingGet() is ValueWrapper.Existing override var enabled: Boolean get() = isEnabled() - set(value) { setPluginEnabled(PluginType.LOOP, value)} + set(value) { + setPluginEnabled(PluginType.LOOP, value) + } val isLGS: Boolean get() { @@ -209,6 +211,17 @@ open class LoopPlugin @Inject constructor( invoke(initiator, allowNotification, false) } + @Synchronized + fun isEmptyQueue(): Boolean { + val maxMinutes = 2L + val start = dateUtil.now() + while (start + T.mins(maxMinutes).msecs() > dateUtil.now()) { + if (commandQueue.size() == 0 && commandQueue.performing() == null) return true + SystemClock.sleep(100) + } + return false + } + @Synchronized operator fun invoke(initiator: String, allowNotification: Boolean, tempBasalFallback: Boolean) { try { @@ -247,6 +260,12 @@ open class LoopPlugin @Inject constructor( return } else rxBus.send(EventLoopInvoked()) + if (!isEmptyQueue()) { + aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.pumpbusy)) + rxBus.send(EventLoopSetLastRunGui(resourceHelper.gs(R.string.pumpbusy))) + return + } + // Prepare for pumps using % basals if (pump.pumpDescription.tempBasalStyle == PumpDescription.PERCENT && allowPercentage()) { apsResult.usePercent = true @@ -357,8 +376,7 @@ open class LoopPlugin @Inject constructor( } } if (resultAfterConstraints.isChangeRequested - && !commandQueue.bolusInQueue() - && !commandQueue.isRunning(Command.CommandType.BOLUS)) { + && !commandQueue.bolusInQueue()) { val waiting = PumpEnactResult(injector) waiting.queued = true if (resultAfterConstraints.tempBasalRequested) lastRun.tbrSetByPump = waiting diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.kt b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.kt index cdaba94dc6..10c0f8e983 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.kt @@ -202,11 +202,11 @@ open class CommandQueue @Inject constructor( @Synchronized override fun bolusInQueue(): Boolean { if (isRunning(CommandType.BOLUS)) return true + if (isRunning(CommandType.SMB_BOLUS)) return true synchronized(queue) { for (i in queue.indices) { - if (queue[i].commandType == CommandType.BOLUS) { - return true - } + if (queue[i].commandType == CommandType.BOLUS) return true + if (queue[i].commandType == CommandType.SMB_BOLUS) return true } } return false @@ -243,13 +243,15 @@ open class CommandQueue @Inject constructor( } var type = if (detailedBolusInfo.bolusType == DetailedBolusInfo.BolusType.SMB) CommandType.SMB_BOLUS else CommandType.BOLUS if (type == CommandType.SMB_BOLUS) { - if (isRunning(CommandType.BOLUS) || isRunning(CommandType.SMB_BOLUS) || bolusInQueue()) { + if (bolusInQueue()) { aapsLogger.debug(LTag.PUMPQUEUE, "Rejecting SMB since a bolus is queue/running") + callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run() return false } val lastBolusTime = repository.getLastBolusRecord()?.timestamp ?: 0L if (detailedBolusInfo.lastKnownBolusTime < lastBolusTime) { aapsLogger.debug(LTag.PUMPQUEUE, "Rejecting bolus, another bolus was issued since request time") + callback?.result(PumpEnactResult(injector).enacted(false).success(false))?.run() return false } removeAll(CommandType.SMB_BOLUS) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSMBBolus.kt b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSMBBolus.kt index d43493059b..eaac269a71 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSMBBolus.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/commands/CommandSMBBolus.kt @@ -23,6 +23,7 @@ class CommandSMBBolus( override fun execute() { val r: PumpEnactResult val lastBolusTime = repository.getLastBolusRecord()?.timestamp ?: 0L + aapsLogger.debug(LTag.PUMPQUEUE, "Last bolus: $lastBolusTime ${dateUtil.dateAndTimeAndSecondsString(lastBolusTime)}") if (lastBolusTime != 0L && lastBolusTime + T.mins(3).msecs() > dateUtil.now()) { aapsLogger.debug(LTag.PUMPQUEUE, "SMB requested but still in 3 min interval") r = PumpEnactResult(injector).enacted(false).success(false).comment("SMB requested but still in 3 min interval") diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt index 4ea4d4d712..dfe8a94a42 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt @@ -70,7 +70,7 @@ class PumpSyncImplementation @Inject constructor( if (type.description != storedType || serialNumber != storedSerial) rxBus.send(EventNewNotification(Notification(Notification.WRONG_PUMP_DATA, resourceHelper.gs(R.string.wrong_pump_data), Notification.URGENT))) - aapsLogger.error(LTag.PUMP, "Ignoring pump history record Allowed: ${dateUtil.dateAndTimeAndSecondsString(storedTimestamp)} $storedType $storedSerial Received: $timestamp ${dateUtil.dateAndTimeAndSecondsString(timestamp)}${type.description} $serialNumber") + aapsLogger.error(LTag.PUMP, "Ignoring pump history record Allowed: ${dateUtil.dateAndTimeAndSecondsString(storedTimestamp)} $storedType $storedSerial Received: $timestamp ${dateUtil.dateAndTimeAndSecondsString(timestamp)} ${type.description} $serialNumber") return false } diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRSPacketAPSHistoryEvents.kt b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRSPacketAPSHistoryEvents.kt index ddf8e3b606..4c26d38dc1 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRSPacketAPSHistoryEvents.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/comm/DanaRSPacketAPSHistoryEvents.kt @@ -118,14 +118,13 @@ open class DanaRSPacketAPSHistoryEvents( val param1 = intFromBuffMsbLsb(data, 7, 2) val param2 = intFromBuffMsbLsb(data, 9, 2) val pumpId: Long - var id = 0 if (!danaPump.usingUTC) { datetime = dateTimeSecFromBuff(data, 1) // 6 bytes pumpId = datetime } else { datetime = intFromBuffMsbLsb(data, 3, 4) * 1000L - id = intFromBuffMsbLsb(data, 0, 2) // range only 1-2000 - pumpId = datetime shl 16 + id + val id = intFromBuffMsbLsb(data, 0, 2) // range only 1-2000 + pumpId = datetime * 2 + id } val status: String when (recordCode) { @@ -140,7 +139,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Ratio: " + param1 + "% Duration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Ratio: " + param1 + "% Duration: " + param2 + "min") status = "TEMPSTART " + dateUtil.timeString(datetime) } @@ -150,7 +149,7 @@ open class DanaRSPacketAPSHistoryEvents( endPumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") status = "TEMPSTOP " + dateUtil.timeString(datetime) } @@ -163,7 +162,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U Duration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U Duration: " + param2 + "min") status = "EXTENDEDSTART " + dateUtil.timeString(datetime) } @@ -173,7 +172,7 @@ open class DanaRSPacketAPSHistoryEvents( endPumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Delivered: " + param1 / 100.0 + "U RealDuration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Delivered: " + param1 / 100.0 + "U RealDuration: " + param2 + "min") status = "EXTENDEDSTOP " + dateUtil.timeString(datetime) } @@ -186,7 +185,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Bolus: " + param1 / 100.0 + "U ") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Bolus: " + param1 / 100.0 + "U ") status = "BOLUS " + dateUtil.timeString(datetime) } @@ -199,7 +198,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALBOLUS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Bolus: " + param1 / 100.0 + "U Duration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALBOLUS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Bolus: " + param1 / 100.0 + "U Duration: " + param2 + "min") status = "DUALBOLUS " + dateUtil.timeString(datetime) } @@ -212,7 +211,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALEXTENDEDSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U Duration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALEXTENDEDSTART (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U Duration: " + param2 + "min") status = "DUALEXTENDEDSTART " + dateUtil.timeString(datetime) } @@ -222,17 +221,17 @@ open class DanaRSPacketAPSHistoryEvents( endPumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALEXTENDEDSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Delivered: " + param1 / 100.0 + "U RealDuration: " + param2 + "min") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT DUALEXTENDEDSTOP (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Delivered: " + param1 / 100.0 + "U RealDuration: " + param2 + "min") status = "DUALEXTENDEDSTOP " + dateUtil.timeString(datetime) } DanaPump.SUSPENDON -> { - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "EVENT SUSPENDON (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "EVENT SUSPENDON (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") status = "SUSPENDON " + dateUtil.timeString(datetime) } DanaPump.SUSPENDOFF -> { - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "EVENT SUSPENDOFF (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "EVENT SUSPENDOFF (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")") status = "SUSPENDOFF " + dateUtil.timeString(datetime) } @@ -245,18 +244,18 @@ open class DanaRSPacketAPSHistoryEvents( pumpType = danaPump.pumpType(), pumpSerial = danaPump.serialNumber ) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT REFILL (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT REFILL (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") } status = "REFILL " + dateUtil.timeString(datetime) } DanaPump.PRIME -> { - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "EVENT PRIME (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "EVENT PRIME (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") status = "PRIME " + dateUtil.timeString(datetime) } DanaPump.PROFILECHANGE -> { - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "EVENT PROFILECHANGE (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " No: " + param1 + " CurrentRate: " + param2 / 100.0 + "U/h") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "EVENT PROFILECHANGE (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " No: " + param1 + " CurrentRate: " + param2 / 100.0 + "U/h") status = "PROFILECHANGE " + dateUtil.timeString(datetime) } @@ -267,7 +266,7 @@ open class DanaRSPacketAPSHistoryEvents( pumpId = pumpId, pumpType = PumpType.DANA_RS, pumpSerial = danaPump.serialNumber) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT CARBS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Carbs: " + param1 + "g") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT CARBS (" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Carbs: " + param1 + "g") status = "CARBS " + dateUtil.timeString(datetime) } @@ -280,19 +279,19 @@ open class DanaRSPacketAPSHistoryEvents( pumpType = danaPump.pumpType(), pumpSerial = danaPump.serialNumber ) - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + (if (newRecord) "**NEW** " else "") + "EVENT PRIMECANNULA(" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + (if (newRecord) "**NEW** " else "") + "EVENT PRIMECANNULA(" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Amount: " + param1 / 100.0 + "U") } status = "PRIMECANNULA " + dateUtil.timeString(datetime) } DanaPump.TIMECHANGE -> { val oldDateTime = intFromBuffMsbLsb(data, 7, 4) * 1000L - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "EVENT TIMECHANGE(" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Previous: " + dateUtil.dateAndTimeString(oldDateTime)) + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "EVENT TIMECHANGE(" + recordCode + ") " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Previous: " + dateUtil.dateAndTimeString(oldDateTime)) status = "TIMECHANGE " + dateUtil.timeString(datetime) } else -> { - aapsLogger.debug(LTag.PUMPCOMM, "[" + id + "] " + "Event: " + recordCode + " " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Param1: " + param1 + " Param2: " + param2) + aapsLogger.debug(LTag.PUMPCOMM, "[" + pumpId + "] " + "Event: " + recordCode + " " + dateUtil.dateAndTimeString(datetime) + " (" + datetime + ")" + " Param1: " + param1 + " Param2: " + param2) status = "UNKNOWN " + dateUtil.timeString(datetime) } } diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt index 2483cb04cc..31169c9c93 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/BolusDao.kt @@ -33,16 +33,16 @@ internal interface BolusDao : TraceableDao { @Query("SELECT * FROM $TABLE_BOLUSES WHERE temporaryId = :temporaryId AND pumpType = :pumpType AND pumpSerial = :pumpSerial AND referenceId IS NULL") fun findByPumpTempIds(temporaryId: Long, pumpType: InterfaceIDs.PumpType, pumpSerial: String): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY id ASC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecord(exclude: Bolus.Type = Bolus.Type.PRIMING): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY id ASC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecordMaybe(exclude: Bolus.Type = Bolus.Type.PRIMING): Maybe - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type == :only AND referenceId IS NULL ORDER BY id ASC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type == :only AND referenceId IS NULL ORDER BY timestamp DESC LIMIT 1") fun getLastBolusRecordOfType(only: Bolus.Type): Bolus? - @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY id ASC LIMIT 1") + @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND type <> :exclude AND referenceId IS NULL ORDER BY timestamp ASC LIMIT 1") fun getOldestBolusRecord(exclude: Bolus.Type = Bolus.Type.PRIMING): Bolus? @Query("SELECT * FROM $TABLE_BOLUSES WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC")