Revert more local alerts
This commit is contained in:
parent
a7e2224d2e
commit
ee6f29dd75
|
@ -13,7 +13,6 @@ import android.content.SharedPreferences;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import org.mozilla.javascript.ast.Loop;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -21,78 +20,39 @@ import java.util.Date;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
import info.nightscout.androidaps.Constants;
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
|
||||||
import info.nightscout.androidaps.data.Profile;
|
|
||||||
import info.nightscout.androidaps.db.BgReading;
|
|
||||||
import info.nightscout.androidaps.db.DatabaseHelper;
|
|
||||||
import info.nightscout.androidaps.interfaces.PumpInterface;
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
||||||
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
import info.nightscout.androidaps.data.Profile;
|
||||||
import info.nightscout.androidaps.plugins.Overview.Notification;
|
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
|
||||||
import info.nightscout.utils.SP;
|
|
||||||
|
|
||||||
public class KeepAliveReceiver extends BroadcastReceiver {
|
public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
public static final long STATUS_UPDATE_FREQUENCY = 15 * 60 * 1000L;
|
|
||||||
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
|
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
|
||||||
|
|
||||||
// TODO consider moving this into an Alarms plugin that works offline and can be configured
|
|
||||||
// (e.g. override silent mode at night only)
|
|
||||||
public static final int ALARM_FREQUENCY = 25 * 60 * 1000;
|
|
||||||
private static long nextPumpDisconnectedAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
|
||||||
private static long nextMissedReadingsAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent rIntent) {
|
public void onReceive(Context context, Intent rIntent) {
|
||||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
|
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
|
||||||
wl.acquire();
|
wl.acquire();
|
||||||
|
|
||||||
checkBg();
|
|
||||||
checkPump();
|
|
||||||
|
|
||||||
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 + ALARM_FREQUENCY < System.currentTimeMillis()
|
|
||||||
&& nextMissedReadingsAlarm < 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;
|
|
||||||
nextMissedReadingsAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
|
||||||
MainApp.bus().post(new EventNewNotification(n));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkPump() {
|
|
||||||
final PumpInterface pump = MainApp.getConfigBuilder();
|
final PumpInterface pump = MainApp.getConfigBuilder();
|
||||||
final Profile profile = MainApp.getConfigBuilder().getProfile();
|
final Profile profile = MainApp.getConfigBuilder().getProfile();
|
||||||
// TODO suspended?
|
if (pump != null && profile != null && profile.getBasal() != null) {
|
||||||
if (pump != null && profile != null && profile.getBasal() != null && !LoopPlugin.getPlugin().isSuspended()) {
|
boolean isBasalOutdated = false;
|
||||||
|
boolean isStatusOutdated = false;
|
||||||
|
|
||||||
Date lastConnection = pump.lastDataTime();
|
Date lastConnection = pump.lastDataTime();
|
||||||
|
if (lastConnection.getTime() + 30 * 60 * 1000L < System.currentTimeMillis())
|
||||||
boolean isStatusOutdated = lastConnection.getTime() + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis();
|
isStatusOutdated = true;
|
||||||
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
|
if (Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep)
|
||||||
|
isBasalOutdated = true;
|
||||||
boolean alarmTimeoutExpired = lastConnection.getTime() + ALARM_FREQUENCY < System.currentTimeMillis();
|
|
||||||
boolean nextAlarmOccurrenceReached = nextPumpDisconnectedAlarm < System.currentTimeMillis();
|
|
||||||
|
|
||||||
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
||||||
if (SP.getBoolean(MainApp.sResources.getString(R.string.key_enable_pump_unreachable_alert), false)
|
if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
|
||||||
&& isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached) {
|
|
||||||
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT);
|
|
||||||
n.soundId = R.raw.alarm;
|
|
||||||
nextPumpDisconnectedAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
|
||||||
MainApp.bus().post(new EventNewNotification(n));
|
|
||||||
} else if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
|
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
pump.setNewBasalProfile(profile);
|
pump.setNewBasalProfile(profile);
|
||||||
}
|
}
|
||||||
}, "pump-update-basal-profile");
|
});
|
||||||
t.start();
|
t.start();
|
||||||
} else if (isStatusOutdated && !pump.isBusy()) {
|
} else if (isStatusOutdated && !pump.isBusy()) {
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@ -100,7 +60,7 @@ public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
public void run() {
|
public void run() {
|
||||||
pump.refreshDataFromPump("KeepAlive. Status outdated.");
|
pump.refreshDataFromPump("KeepAlive. Status outdated.");
|
||||||
}
|
}
|
||||||
}, "pump-refresh");
|
});
|
||||||
t.start();
|
t.start();
|
||||||
} else if (isBasalOutdated && !pump.isBusy()) {
|
} else if (isBasalOutdated && !pump.isBusy()) {
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@ -108,10 +68,13 @@ public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
public void run() {
|
public void run() {
|
||||||
pump.refreshDataFromPump("KeepAlive. Basal outdated.");
|
pump.refreshDataFromPump("KeepAlive. Basal outdated.");
|
||||||
}
|
}
|
||||||
}, "pump-refresh");
|
});
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.debug("KeepAlive received");
|
||||||
|
wl.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlarm(Context context) {
|
public void setAlarm(Context context) {
|
||||||
|
|
Loading…
Reference in a new issue