diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index cde760fc28..b580dc9b38 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -28,6 +28,9 @@ public interface PumpInterface { int setNewBasalProfile(NSProfile profile); boolean isThisProfileSet(NSProfile profile); + Date lastStatusTime(); + void updateStatus(String reason); + double getBaseBasalRate(); // base basal rate, not temp basal double getTempBasalAbsoluteRate(); double getTempBasalRemainingMinutes(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index bbdff616c5..42d0ae2584 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -373,6 +373,16 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return activePump.isThisProfileSet(profile); } + @Override + public Date lastStatusTime() { + return activePump.lastStatusTime(); + } + + @Override + public void updateStatus(String reason) { + activePump.updateStatus(reason); + } + @Override public double getBaseBasalRate() { return activePump.getBaseBasalRate(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java index 287aabca4c..577e1d3bcf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/DanaRPlugin.java @@ -271,6 +271,18 @@ public class DanaRPlugin implements PluginBase, PumpInterface, ConstraintsInterf return true; } + @Override + public Date lastStatusTime() { + return getDanaRPump().lastConnection; + } + + @Override + public void updateStatus(String reason) { + if (!isConnected() && !isConnecting()) { + doConnect(reason); + } + } + @Override public double getBaseBasalRate() { return getDanaRPump().currentBasal; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java index d680327e85..e5b40a60a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/DanaRKoreanPlugin.java @@ -271,6 +271,18 @@ public class DanaRKoreanPlugin implements PluginBase, PumpInterface, Constraints return true; } + @Override + public Date lastStatusTime() { + return getDanaRPump().lastConnection; + } + + @Override + public void updateStatus(String reason) { + if (!isConnected() && !isConnecting()) { + doConnect(reason); + } + } + @Override public double getBaseBasalRate() { return getDanaRPump().currentBasal; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java index 1174f2e17b..cc63428349 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/MDI/MDIPlugin.java @@ -136,6 +136,16 @@ public class MDIPlugin implements PluginBase, PumpInterface { return false; } + @Override + public Date lastStatusTime() { + return new Date(); + } + + @Override + public void updateStatus(String reason) { + // do nothing + } + @Override public double getBaseBasalRate() { return 0d; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java index 2b65b439f1..ac4e1a8c13 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/VirtualPump/VirtualPumpPlugin.java @@ -145,6 +145,16 @@ public class VirtualPumpPlugin implements PluginBase, PumpInterface { return false; } + @Override + public Date lastStatusTime() { + return new Date(); + } + + @Override + public void updateStatus(String reason) { + // do nothing + } + @Override public double getBaseBasalRate() { NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java index 597489250b..d02aa0b797 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.java @@ -7,12 +7,9 @@ package info.nightscout.androidaps.receivers; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.os.IBinder; import android.os.PowerManager; import android.preference.PreferenceManager; @@ -23,16 +20,12 @@ import java.util.Date; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; -import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.DanaR.DanaRFragment; import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin; -import info.nightscout.androidaps.plugins.DanaR.Services.ExecutionService; import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin; import info.nightscout.client.data.NSProfile; -import info.nightscout.utils.ToastUtils; public class KeepAliveReceiver extends BroadcastReceiver { private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class); @@ -43,6 +36,47 @@ public class KeepAliveReceiver extends BroadcastReceiver { PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire(); + + final PumpInterface pump = MainApp.getConfigBuilder(); + final NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); + if (pump != null && profile != null) { + boolean isBasalOutdated = false; + boolean isStatusOutdated = false; + + Date lastConnection = pump.lastStatusTime(); + if (lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime()) + isStatusOutdated = true; + if (Math.abs(profile.getBasal(NSProfile.secondsFromMidnight()) - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep) + isBasalOutdated = true; + + SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); + if (SP.getBoolean("syncprofiletopump", false)) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + pump.setNewBasalProfile(profile); + } + }); + t.start(); + } else if (isBasalOutdated) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + pump.updateStatus("KeepAlive. Basal outdated."); + } + }); + t.start(); + } else if (isStatusOutdated) { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + pump.updateStatus("KeepAlive. Status outdated."); + } + }); + t.start(); + } + } + log.debug("KeepAlive received"); final DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); if (danaRPlugin != null && Config.DANAR && danaRPlugin.isEnabled(PluginBase.PUMP)) { @@ -50,7 +84,7 @@ public class KeepAliveReceiver extends BroadcastReceiver { Thread t = new Thread(new Runnable() { @Override public void run() { - danaRPlugin.doConnect("KeepAlive"); + danaRPlugin.doConnect("KeepAlive. Basal outdated: "); } }); t.start(); @@ -62,21 +96,12 @@ public class KeepAliveReceiver extends BroadcastReceiver { Thread t = new Thread(new Runnable() { @Override public void run() { - danaRKoreanPlugin.doConnect("KeepAlive"); + danaRKoreanPlugin.doConnect("KeepAlive. Basal outdated: "); } }); t.start(); } } - PumpInterface pump = MainApp.getConfigBuilder(); - NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile(); - if (pump != null && profile != null) { - SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); - if (SP.getBoolean("syncprofiletopump", false)) { - pump.setNewBasalProfile(profile); - } - } - wl.release(); }