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) {