From 4539b5a9e5645f23d5ef347ff439f09fb8d74761 Mon Sep 17 00:00:00 2001 From: Johannes Mockenhaupt Date: Thu, 26 Oct 2017 01:02:40 +0200 Subject: [PATCH] KeepAliveReceiver: add alarm for missed readings. --- .../receivers/KeepAliveReceiver.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 43db18e403..967da79455 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -21,6 +21,8 @@ import java.util.Date; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.plugins.Overview.Notification; @@ -35,21 +37,37 @@ public class KeepAliveReceiver extends BroadcastReceiver { PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire(); + checkBg(); + checkPump(); + log.debug("KeepAlive received"); + wl.release(); + } + + private void checkBg() { + BgReading bgReading = DatabaseHelper.lastBg(); + if (bgReading != null && bgReading.date + 25 * 60 * 1000 < System.currentTimeMillis()) { + Notification n = new Notification(Notification.PUMP_UNREACHABLE, "Missed BG readings", Notification.URGENT); + n.soundId = R.raw.alarm; + MainApp.bus().post(new EventNewNotification(n)); + } + } + + private void checkPump() { final PumpInterface pump = MainApp.getConfigBuilder(); final Profile profile = MainApp.getConfigBuilder().getProfile(); if (pump != null && profile != null && profile.getBasal() != null) { - boolean isBasalOutdated = false; - boolean isStatusOutdated = false; - Date lastConnection = pump.lastDataTime(); - if (lastConnection.getTime() + 15 * 60 * 1000L < System.currentTimeMillis()) - isStatusOutdated = true; - if (Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep) - isBasalOutdated = true; + + boolean isStatusOutdated = lastConnection.getTime() + 15 * 60 * 1000L < System.currentTimeMillis(); + boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep; SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); if (isStatusOutdated && lastConnection.getTime() + 25 * 60 * 1000 < System.currentTimeMillis()) { + // TODO the alarm will trigger every 5m until the problem is resolved. That can get annoying quiet quickly if + // fixing the problem takes longer (or is not immediately possible because the pump was forgotten)? + // suppress this for another 25m if the message was dismissed? + // The alarm sound is played back as regular media, that means it might be muted if sound level is at 0 Notification n = new Notification(Notification.PUMP_UNREACHABLE, "Pump unreachable", Notification.URGENT); n.soundId = R.raw.alarm; MainApp.bus().post(new EventNewNotification(n)); @@ -79,9 +97,6 @@ public class KeepAliveReceiver extends BroadcastReceiver { t.start(); } } - - log.debug("KeepAlive received"); - wl.release(); } public void setAlarm(Context context) {