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 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;
|
2018-07-30 14:21:11 +02:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
|
2018-04-11 15:42:51 +02:00
|
|
|
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
|
2018-07-28 23:57:25 +02:00
|
|
|
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
|
2018-04-19 23:42:37 +02:00
|
|
|
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);
|
|
|
|
|
2018-04-19 23:42:37 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-19 23:42:37 +02: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
|
|
|
}
|
|
|
|
|
2018-08-17 15:11:43 +02:00
|
|
|
public static void checkPumpUnreachableAlarm(long lastConnection, boolean isStatusOutdated) {
|
|
|
|
boolean alarmTimeoutExpired = lastConnection + 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)
|
2018-04-11 15:42:51 +02:00
|
|
|
&& 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));
|
2018-02-13 00:34:24 +01:00
|
|
|
if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) {
|
|
|
|
NSUpload.uploadError(n.text);
|
|
|
|
}
|
2017-12-17 01:37:27 +01:00
|
|
|
}
|
2018-04-19 23:42:37 +02: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.
|
2018-04-19 23:42:37 +02:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2018-04-19 23:42:37 +02:00
|
|
|
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
|
|
|
|
|
2018-09-17 17:43:42 +02:00
|
|
|
final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
|
2018-07-30 14:21:11 +02:00
|
|
|
final Profile profile = ProfileFunctions.getInstance().getProfile();
|
2018-03-18 15:02:21 +01:00
|
|
|
if (pump != null && profile != null) {
|
2018-08-17 15:11:43 +02:00
|
|
|
long lastConnection = pump.lastDataTime();
|
|
|
|
long earliestAlarmTime = lastConnection + pumpUnreachableThreshold();
|
2017-12-17 01:37:27 +01:00
|
|
|
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));
|
2018-02-13 00:34:24 +01:00
|
|
|
if (SP.getBoolean(R.string.key_ns_create_announcements_from_errors, true)) {
|
|
|
|
NSUpload.uploadError(n.text);
|
|
|
|
}
|
2017-12-17 01:37:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|