AndroidAPS/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java

150 lines
7 KiB
Java
Raw Normal View History

2016-07-07 00:56:31 +02:00
package info.nightscout.androidaps.receivers;
/**
* Created by mike on 07.07.2016.
*/
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-08-02 15:43:21 +02:00
import java.util.Date;
2017-12-02 10:21:42 +01:00
import info.nightscout.androidaps.Config;
2016-07-07 00:56:31 +02:00
import info.nightscout.androidaps.Constants;
2016-07-18 12:02:33 +02:00
import info.nightscout.androidaps.MainApp;
2017-11-28 21:48:46 +01:00
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading;
import info.nightscout.androidaps.db.DatabaseHelper;
2017-01-28 23:45:14 +01:00
import info.nightscout.androidaps.interfaces.PumpInterface;
2017-06-02 10:25:49 +02:00
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
2017-11-28 21:48:46 +01:00
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.utils.SP;
2016-07-07 00:56:31 +02:00
public class KeepAliveReceiver extends BroadcastReceiver {
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
2017-11-28 21:48:46 +01:00
public static final long STATUS_UPDATE_FREQUENCY = 15 * 60 * 1000L;
// TODO consider moving this into an Alarms plugin that works offline and can be configured
// (e.g. override silent mode at night only)
private static int missedReadingsThreshold() {
return SP.getInt(MainApp.sResources.getString(R.string.key_missed_bg_readings_threshold), 30) * 60 * 1000;
}
private static int pumpUnreachableThreshold() {
return SP.getInt(MainApp.sResources.getString(R.string.key_pump_unreachable_threshold), 30) * 60 * 1000;
}
2016-07-07 00:56:31 +02:00
@Override
2016-07-07 10:34:20 +02:00
public void onReceive(Context context, Intent rIntent) {
2016-07-07 00:56:31 +02:00
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
2017-11-28 21:48:46 +01:00
shortenSnoozeInterval();
checkBg();
checkPump();
2017-01-29 11:11:07 +01:00
2017-11-28 21:48:46 +01:00
log.debug("KeepAlive received");
wl.release();
}
private void checkBg() {
BgReading bgReading = DatabaseHelper.lastBg();
if (SP.getBoolean(MainApp.sResources.getString(R.string.key_enable_missed_bg_readings_alert), false)
&& bgReading != null && bgReading.date + missedReadingsThreshold() < System.currentTimeMillis()
&& SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) {
Notification n = new Notification(Notification.BG_READINGS_MISSED, MainApp.sResources.getString(R.string.missed_bg_readings), Notification.URGENT);
n.soundId = R.raw.alarm;
SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + missedReadingsThreshold());
MainApp.bus().post(new EventNewNotification(n));
}
}
private void checkPump() {
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
2017-06-02 10:25:49 +02:00
final Profile profile = MainApp.getConfigBuilder().getProfile();
if (pump != null && profile != null && profile.getBasal() != null) {
2017-02-19 19:15:14 +01:00
Date lastConnection = pump.lastDataTime();
2017-11-28 21:48:46 +01:00
boolean isStatusOutdated = lastConnection.getTime() + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis();
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
boolean alarmTimeoutExpired = lastConnection.getTime() + pumpUnreachableThreshold() < System.currentTimeMillis();
boolean nextAlarmOccurrenceReached = SP.getLong("nextPumpDisconnectedAlarm", 0l) < System.currentTimeMillis();
2017-12-02 10:21:42 +01:00
if (Config.APS && SP.getBoolean(MainApp.sResources.getString(R.string.key_enable_pump_unreachable_alert), true)
2017-11-28 21:48:46 +01:00
&& isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached && !ConfigBuilderPlugin.getActiveLoop().isDisconnected()) {
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT);
n.soundId = R.raw.alarm;
SP.putLong("nextPumpDisconnectedAlarm", System.currentTimeMillis() + pumpUnreachableThreshold());
MainApp.bus().post(new EventNewNotification(n));
}
2017-01-29 11:11:07 +01:00
if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
2017-11-11 14:05:29 +01:00
MainApp.getConfigBuilder().getCommandQueue().setProfile(profile, null);
} else if (isStatusOutdated && !pump.isBusy()) {
2017-11-11 14:05:29 +01:00
MainApp.getConfigBuilder().getCommandQueue().readStatus("KeepAlive. Status outdated.", null);
} else if (isBasalOutdated && !pump.isBusy()) {
2017-11-11 14:05:29 +01:00
MainApp.getConfigBuilder().getCommandQueue().readStatus("KeepAlive. Basal outdated.", null);
2017-01-29 11:11:07 +01:00
}
}
2016-07-07 00:56:31 +02:00
}
2017-11-28 21:48:46 +01:00
//called by MainApp at first app start
2016-07-07 00:56:31 +02:00
public void setAlarm(Context context) {
2017-11-28 21:48:46 +01:00
shortenSnoozeInterval();
presnoozeAlarms();
2016-07-07 00:56:31 +02:00
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, KeepAliveReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
try {
pi.send();
} catch (PendingIntent.CanceledException e) {
}
am.cancel(pi);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
}
2017-11-28 21:48:46 +01:00
/*Presnoozes the alarms with 5 minutes if no snooze exists.
* Call only at startup!
*/
public void presnoozeAlarms() {
if (SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) {
SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + 5 * 60 * 1000);
}
if (SP.getLong("nextPumpDisconnectedAlarm", 0l) < System.currentTimeMillis()) {
SP.putLong("nextPumpDisconnectedAlarm", System.currentTimeMillis() + 5 * 60 * 1000);
}
}
2016-07-07 00:56:31 +02:00
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, KeepAliveReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
2017-11-28 21:48:46 +01:00
static void shortenSnoozeInterval() {
//shortens alarm times in case of setting changes or future data
long nextMissedReadingsAlarm = SP.getLong("nextMissedReadingsAlarm", 0L);
nextMissedReadingsAlarm = Math.min(System.currentTimeMillis() + missedReadingsThreshold(), nextMissedReadingsAlarm);
SP.putLong("nextMissedReadingsAlarm", nextMissedReadingsAlarm);
long nextPumpDisconnectedAlarm = SP.getLong("nextPumpDisconnectedAlarm", 0L);
nextPumpDisconnectedAlarm = Math.min(System.currentTimeMillis() + pumpUnreachableThreshold(), nextPumpDisconnectedAlarm);
SP.putLong("nextPumpDisconnectedAlarm", nextPumpDisconnectedAlarm);
}
2017-12-02 10:21:42 +01:00
}