From f544f19a02082237193630448f4a6da6921adf97 Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Sat, 29 Jun 2019 21:02:51 +0100 Subject: [PATCH] - Small fix in case that saved date is invalid (shouldn't be) --- .../overview/notifications/Notification.java | 20 ++++++++-------- .../pump/common/utils/DateTimeUtil.java | 2 +- .../pump/medtronic/MedtronicPumpPlugin.java | 24 ++++++++++++++++++- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.java index f3b0368f86..3b9bb44e27 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/Notification.java @@ -1,4 +1,3 @@ - package info.nightscout.androidaps.plugins.general.overview.notifications; import java.util.Date; @@ -86,6 +85,7 @@ public class Notification { public NSAlarm nsAlarm = null; public Integer soundId = null; + public Notification() { } @@ -199,10 +199,10 @@ public class Notification { return false; } - public static boolean isAlarmForStaleData(){ + public static boolean isAlarmForStaleData() { long snoozedTo = SP.getLong("snoozedTo", 0L); - if(snoozedTo != 0L){ - if(System.currentTimeMillis() < SP.getLong("snoozedTo", 0L)) { + if (snoozedTo != 0L) { + if (System.currentTimeMillis() < SP.getLong("snoozedTo", 0L)) { //log.debug("Alarm is snoozed for next "+(SP.getLong("snoozedTo", 0L)-System.currentTimeMillis())/1000+" seconds"); return false; } @@ -218,12 +218,12 @@ public class Notification { //log.debug("bgReadingAgoMin value is:"+bgReadingAgoMin); //log.debug("Stale alarm snoozed to: "+(System.currentTimeMillis() - snoozedTo)/60000L); Double threshold = NSSettingsStatus.getInstance().getThreshold("alarmTimeagoWarnMins"); - //log.debug("OpenAPS Alerts enabled: "+openAPSEnabledAlerts); - // if no thresshold from Ns get it loccally - if(threshold == null) threshold = SP.getDouble(R.string.key_nsalarm_staledatavalue,15D); - // No threshold of OpenAPS Alarm so using the one for BG - // Added OpenAPSEnabledAlerts to alarm check - if((bgReadingAgoMin > threshold && SP.getBoolean(R.string.key_nsalarm_staledata, false))||(bgReadingAgoMin > threshold && openAPSEnabledAlerts)){ + //log.debug("OpenAPS Alerts enabled: "+openAPSEnabledAlerts); + // if no thresshold from Ns get it loccally + if (threshold == null) threshold = SP.getDouble(R.string.key_nsalarm_staledatavalue, 15D); + // No threshold of OpenAPS Alarm so using the one for BG + // Added OpenAPSEnabledAlerts to alarm check + if ((bgReadingAgoMin > threshold && SP.getBoolean(R.string.key_nsalarm_staledata, false)) || (bgReadingAgoMin > threshold && openAPSEnabledAlerts)) { return true; } //snoozing for threshold diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java index 7f22509d09..e4f097e7db 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/common/utils/DateTimeUtil.java @@ -49,7 +49,7 @@ public class DateTimeUtil { return new LocalDateTime(year, month, dayOfMonth, hourOfDay, minute, second); } catch (Exception ex) { if (L.isEnabled(L.PUMPCOMM)) - LOG.error("Error creating LocalDateTime from values [atechDateTime=%d, year=%d, month=%d, day=%d, hour=%d, minute=%d, second=%d]", atechDateTime, year, month, dayOfMonth, hourOfDay, minute, second); + LOG.error("Error creating LocalDateTime from values [atechDateTime={}, year={}, month={}, day={}, hour={}, minute={}, second={}]. Exception: {}", atechDateTime, year, month, dayOfMonth, hourOfDay, minute, second, ex.getMessage()); //return null; throw ex; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java index be49f1ec6a..1158892cea 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/medtronic/MedtronicPumpPlugin.java @@ -14,7 +14,9 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -1102,7 +1104,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter if (isLoggingEnabled()) LOG.debug(getLogPrefix() + "readPumpHistoryLogic(): lastPumpHistoryEntry: null"); - Long lastPumpHistoryEntryTime = SP.getLong(MedtronicConst.Statistics.LastPumpHistoryEntry, 0L); + Long lastPumpHistoryEntryTime = getLastPumpEntryTime(); LocalDateTime timeMinus36h = new LocalDateTime(); timeMinus36h = timeMinus36h.minusHours(36); @@ -1197,6 +1199,26 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter } + private Long getLastPumpEntryTime() { + Long lastPumpEntryTime = SP.getLong(MedtronicConst.Statistics.LastPumpHistoryEntry, 0L); + + try { + LocalDateTime localDateTime = DateTimeUtil.toLocalDateTime(lastPumpEntryTime); + + if (localDateTime.getYear() != (new GregorianCalendar().get(Calendar.YEAR))) { + LOG.warn("Saved LastPumpHistoryEntry was invalid. Year was not the same."); + return 0L; + } + + return lastPumpEntryTime; + + } catch (Exception ex) { + LOG.warn("Saved LastPumpHistoryEntry was invalid."); + return 0L; + } + + } + private void scheduleNextRefresh(MedtronicStatusRefreshType refreshType) { scheduleNextRefresh(refreshType, 0);