AndroidAPS/app/src/main/java/info/nightscout/utils/LocalAlertUtils.java

107 lines
5.4 KiB
Java
Raw Normal View History

2017-12-17 01:37:27 +01:00
package info.nightscout.utils;
2018-02-18 22:55:04 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2017-12-17 01:37:27 +01:00
import java.util.Date;
import info.nightscout.androidaps.Config;
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.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
2017-12-17 01:37:27 +01:00
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
/**
* Created by adrian on 17/12/17.
*/
public class LocalAlertUtils {
2018-02-18 22:55:04 +01:00
private static Logger log = LoggerFactory.getLogger(LocalAlertUtils.class);
public static long missedReadingsThreshold() {
2018-05-02 13:42:50 +02:00
return T.mins(SP.getInt(MainApp.gs(R.string.key_missed_bg_readings_threshold), 30)).msecs();
2017-12-17 01:37:27 +01:00
}
private static long pumpUnreachableThreshold() {
2018-05-02 13:42:50 +02:00
return T.mins(SP.getInt(MainApp.gs(R.string.key_pump_unreachable_threshold), 30)).msecs();
2017-12-17 01:37:27 +01:00
}
public static void checkPumpUnreachableAlarm(Date lastConnection, boolean isStatusOutdated) {
boolean alarmTimeoutExpired = lastConnection.getTime() + pumpUnreachableThreshold() < System.currentTimeMillis();
2018-02-18 22:55:04 +01:00
boolean nextAlarmOccurrenceReached = SP.getLong("nextPumpDisconnectedAlarm", 0L) < System.currentTimeMillis();
2017-12-17 01:37:27 +01:00
2018-05-02 13:42:50 +02:00
if (Config.APS && SP.getBoolean(MainApp.gs(R.string.key_enable_pump_unreachable_alert), true)
&& isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached && !LoopPlugin.getPlugin().isDisconnected()) {
2018-02-18 22:55:04 +01:00
log.debug("Generating pump unreachable alarm. lastConnection: " + DateUtil.dateAndTimeString(lastConnection) + " isStatusOutdated: " + isStatusOutdated);
2018-05-02 13:42:50 +02:00
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.gs(R.string.pump_unreachable), Notification.URGENT);
2017-12-17 01:37:27 +01:00
n.soundId = R.raw.alarm;
SP.putLong("nextPumpDisconnectedAlarm", System.currentTimeMillis() + pumpUnreachableThreshold());
MainApp.bus().post(new EventNewNotification(n));
if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) {
NSUpload.uploadError(n.text);
}
2017-12-17 01:37:27 +01:00
}
if (!isStatusOutdated && !alarmTimeoutExpired)
MainApp.bus().post(new EventDismissNotification(Notification.PUMP_UNREACHABLE));
2017-12-17 01:37:27 +01:00
}
/*Presnoozes the alarms with 5 minutes if no snooze exists.
* Call only at startup!
*/
2017-12-17 01:37:27 +01:00
public static 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);
}
}
public 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);
}
public static void notifyPumpStatusRead() {
2017-12-17 01:37:27 +01:00
//TODO: persist the actual time the pump is read and simplify the whole logic when to alarm
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
final Profile profile = MainApp.getConfigBuilder().getProfile();
2018-03-18 15:02:21 +01:00
if (pump != null && profile != null) {
2017-12-17 01:37:27 +01:00
Date lastConnection = pump.lastDataTime();
long earliestAlarmTime = lastConnection.getTime() + pumpUnreachableThreshold();
if (SP.getLong("nextPumpDisconnectedAlarm", 0l) < earliestAlarmTime) {
SP.putLong("nextPumpDisconnectedAlarm", earliestAlarmTime);
}
}
}
public static void checkStaleBGAlert() {
BgReading bgReading = DatabaseHelper.lastBg();
2018-05-02 13:42:50 +02:00
if (SP.getBoolean(MainApp.gs(R.string.key_enable_missed_bg_readings_alert), false)
2017-12-17 01:37:27 +01:00
&& bgReading != null && bgReading.date + missedReadingsThreshold() < System.currentTimeMillis()
&& SP.getLong("nextMissedReadingsAlarm", 0l) < System.currentTimeMillis()) {
2018-05-02 13:42:50 +02:00
Notification n = new Notification(Notification.BG_READINGS_MISSED, MainApp.gs(R.string.missed_bg_readings), Notification.URGENT);
2017-12-17 01:37:27 +01:00
n.soundId = R.raw.alarm;
SP.putLong("nextMissedReadingsAlarm", System.currentTimeMillis() + missedReadingsThreshold());
MainApp.bus().post(new EventNewNotification(n));
if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) {
NSUpload.uploadError(n.text);
}
2017-12-17 01:37:27 +01:00
}
}
}