Merge branch 'dev' of git://github.com/MilosKozak/AndroidAps into refactor-header-area
This commit is contained in:
commit
a4137a2c3b
|
@ -25,8 +25,6 @@ public class Constants {
|
|||
public static final int hoursToKeepInDatabase = 72;
|
||||
public static final int daysToKeepHistoryInDatabase = 30;
|
||||
|
||||
public static final long keepAliveMsecs = 5 * 60 * 1000L;
|
||||
|
||||
// SMS COMMUNICATOR
|
||||
public static final long remoteBolusMinDistance = 15 * 60 * 1000L;
|
||||
|
||||
|
|
|
@ -313,9 +313,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
|
|||
return true;
|
||||
case R.id.nav_exit:
|
||||
log.debug("Exiting");
|
||||
MainApp.instance().stopKeepAliveService();
|
||||
RxBus.INSTANCE.send(new EventAppExit());
|
||||
MainApp.closeDbHelper();
|
||||
finish();
|
||||
System.runFinalization();
|
||||
System.exit(0);
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package info.nightscout.androidaps;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.Resources;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.annotation.ColorRes;
|
||||
import androidx.annotation.PluralsRes;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import com.crashlytics.android.Crashlytics;
|
||||
|
@ -99,7 +101,6 @@ import static info.nightscout.androidaps.plugins.constraints.versionChecker.Vers
|
|||
|
||||
public class MainApp extends Application {
|
||||
private static Logger log = LoggerFactory.getLogger(L.CORE);
|
||||
private static KeepAliveReceiver keepAliveReceiver;
|
||||
|
||||
private static MainApp sInstance;
|
||||
public static Resources sResources;
|
||||
|
@ -112,9 +113,7 @@ public class MainApp extends Application {
|
|||
private static ArrayList<PluginBase> pluginsList = null;
|
||||
|
||||
private static DataReceiver dataReceiver = new DataReceiver();
|
||||
private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
|
||||
private LocalBroadcastManager lbm;
|
||||
BroadcastReceiver btReceiver;
|
||||
private static NSAlarmReceiver alarmReceiver = new NSAlarmReceiver();
|
||||
TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver;
|
||||
|
||||
public static boolean devBranch;
|
||||
|
@ -168,7 +167,6 @@ public class MainApp extends Application {
|
|||
|
||||
//trigger here to see the new version on app start after an update
|
||||
triggerCheckVersion();
|
||||
//setBTReceiver();
|
||||
|
||||
if (pluginsList == null) {
|
||||
pluginsList = new ArrayList<>();
|
||||
|
@ -240,10 +238,10 @@ public class MainApp extends Application {
|
|||
new Thread(() -> {
|
||||
SystemClock.sleep(5000);
|
||||
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("Initialization", null);
|
||||
startKeepAliveService();
|
||||
}).start();
|
||||
}
|
||||
|
||||
new Thread(() -> KeepAliveReceiver.setAlarm(this)).start();
|
||||
doMigrations();
|
||||
}
|
||||
|
||||
|
@ -273,7 +271,7 @@ public class MainApp extends Application {
|
|||
|
||||
|
||||
private void registerLocalBroadcastReceiver() {
|
||||
lbm = LocalBroadcastManager.getInstance(this);
|
||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
|
||||
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT));
|
||||
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_CHANGED_TREATMENT));
|
||||
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_REMOVED_TREATMENT));
|
||||
|
@ -288,33 +286,21 @@ public class MainApp extends Application {
|
|||
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_CAL));
|
||||
|
||||
//register alarms
|
||||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_ALARM));
|
||||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_ANNOUNCEMENT));
|
||||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_CLEAR_ALARM));
|
||||
lbm.registerReceiver(alarmReciever, new IntentFilter(Intents.ACTION_URGENT_ALARM));
|
||||
lbm.registerReceiver(alarmReceiver, new IntentFilter(Intents.ACTION_ALARM));
|
||||
lbm.registerReceiver(alarmReceiver, new IntentFilter(Intents.ACTION_ANNOUNCEMENT));
|
||||
lbm.registerReceiver(alarmReceiver, new IntentFilter(Intents.ACTION_CLEAR_ALARM));
|
||||
lbm.registerReceiver(alarmReceiver, new IntentFilter(Intents.ACTION_URGENT_ALARM));
|
||||
|
||||
this.timeDateOrTZChangeReceiver = new TimeDateOrTZChangeReceiver();
|
||||
this.timeDateOrTZChangeReceiver.registerBroadcasts(this);
|
||||
|
||||
}
|
||||
|
||||
private void startKeepAliveService() {
|
||||
if (keepAliveReceiver == null) {
|
||||
keepAliveReceiver = new KeepAliveReceiver();
|
||||
keepAliveReceiver.setAlarm(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopKeepAliveService() {
|
||||
if (keepAliveReceiver != null)
|
||||
KeepAliveReceiver.cancelAlarm(this);
|
||||
}
|
||||
|
||||
public static String gs(int id) {
|
||||
public static String gs(@StringRes int id) {
|
||||
return sResources.getString(id);
|
||||
}
|
||||
|
||||
public static String gs(int id, Object... args) {
|
||||
public static String gs(@StringRes int id, Object... args) {
|
||||
return sResources.getString(id, args);
|
||||
}
|
||||
|
||||
|
@ -322,8 +308,8 @@ public class MainApp extends Application {
|
|||
return sResources.getQuantityString(id, quantity, args);
|
||||
}
|
||||
|
||||
public static int gc(int id) {
|
||||
return sResources.getColor(id);
|
||||
public static int gc(@ColorRes int id) {
|
||||
return ContextCompat.getColor(instance(), id);
|
||||
}
|
||||
|
||||
public static MainApp instance() {
|
||||
|
@ -334,13 +320,6 @@ public class MainApp extends Application {
|
|||
return sDatabaseHelper;
|
||||
}
|
||||
|
||||
public static void closeDbHelper() {
|
||||
if (sDatabaseHelper != null) {
|
||||
sDatabaseHelper.close();
|
||||
sDatabaseHelper = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static FirebaseAnalytics getFirebaseAnalytics() {
|
||||
return mFirebaseAnalytics;
|
||||
}
|
||||
|
@ -443,20 +422,12 @@ public class MainApp extends Application {
|
|||
public void onTerminate() {
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("onTerminate");
|
||||
super.onTerminate();
|
||||
if (sDatabaseHelper != null) {
|
||||
sDatabaseHelper.close();
|
||||
sDatabaseHelper = null;
|
||||
}
|
||||
|
||||
if (btReceiver != null) {
|
||||
unregisterReceiver(btReceiver);
|
||||
}
|
||||
|
||||
if (timeDateOrTZChangeReceiver != null) {
|
||||
if (timeDateOrTZChangeReceiver != null)
|
||||
unregisterReceiver(timeDateOrTZChangeReceiver);
|
||||
}
|
||||
unregisterActivityLifecycleCallbacks(ActivityMonitor.INSTANCE);
|
||||
KeepAliveReceiver.cancelAlarm(this);
|
||||
super.onTerminate();
|
||||
}
|
||||
|
||||
public static int dpToPx(int dp) {
|
||||
|
|
|
@ -194,15 +194,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
|
|||
return newVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the database connections and clear any cached DAOs.
|
||||
*/
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
}
|
||||
|
||||
|
||||
public long size(String database) {
|
||||
return DatabaseUtils.queryNumEntries(getReadableDatabase(), database);
|
||||
}
|
||||
|
|
|
@ -381,13 +381,11 @@ public class DeviceStatus {
|
|||
try {
|
||||
if (device != null) record.put("device", device);
|
||||
if (pump != null) record.put("pump", pump);
|
||||
if (suggested != null) {
|
||||
JSONObject openaps = new JSONObject();
|
||||
if (enacted != null) openaps.put("enacted", enacted);
|
||||
if (suggested != null) openaps.put("suggested", suggested);
|
||||
if (iob != null) openaps.put("iob", iob);
|
||||
record.put("openaps", openaps);
|
||||
}
|
||||
if (uploaderBattery != 0) record.put("uploaderBattery", uploaderBattery);
|
||||
if (created_at != null) record.put("created_at", created_at);
|
||||
} catch (JSONException e) {
|
||||
|
|
|
@ -110,9 +110,7 @@ public class ImportExportPrefs {
|
|||
SP.putBoolean(R.string.key_setupwizard_processed, true);
|
||||
OKDialog.show(context, MainApp.gs(R.string.setting_imported), MainApp.gs(R.string.restartingapp), () -> {
|
||||
log.debug("Exiting");
|
||||
MainApp.instance().stopKeepAliveService();
|
||||
RxBus.INSTANCE.send(new EventAppExit());
|
||||
MainApp.closeDbHelper();
|
||||
if (context instanceof Activity) {
|
||||
((Activity) context).finish();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package info.nightscout.androidaps.plugins.general.nsclient;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
@ -25,6 +24,7 @@ import java.util.Locale;
|
|||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.data.DetailedBolusInfo;
|
||||
import info.nightscout.androidaps.data.IobTotal;
|
||||
import info.nightscout.androidaps.data.Profile;
|
||||
import info.nightscout.androidaps.db.BgReading;
|
||||
import info.nightscout.androidaps.db.CareportalEvent;
|
||||
|
@ -39,6 +39,7 @@ import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus;
|
|||
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
||||
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
|
||||
import info.nightscout.androidaps.utils.BatteryLevel;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
import info.nightscout.androidaps.utils.SP;
|
||||
|
@ -52,7 +53,6 @@ public class NSUpload {
|
|||
|
||||
public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", CareportalEvent.TEMPBASAL);
|
||||
data.put("duration", temporaryBasal.durationInMinutes);
|
||||
|
@ -87,7 +87,6 @@ public class NSUpload {
|
|||
uploadTempBasalStartAbsolute(t, null);
|
||||
}
|
||||
} else {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", CareportalEvent.TEMPBASAL);
|
||||
data.put("duration", temporaryBasal.durationInMinutes);
|
||||
|
@ -107,7 +106,6 @@ public class NSUpload {
|
|||
|
||||
public static void uploadTempBasalEnd(long time, boolean isFakedTempBasal, long pumpId) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", CareportalEvent.TEMPBASAL);
|
||||
data.put("created_at", DateUtil.toISOString(time));
|
||||
|
@ -124,7 +122,6 @@ public class NSUpload {
|
|||
|
||||
public static void uploadExtendedBolus(ExtendedBolus extendedBolus) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", CareportalEvent.COMBOBOLUS);
|
||||
data.put("duration", extendedBolus.durationInMinutes);
|
||||
|
@ -144,7 +141,6 @@ public class NSUpload {
|
|||
|
||||
public static void uploadExtendedBolusEnd(long time, long pumpId) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", CareportalEvent.COMBOBOLUS);
|
||||
data.put("duration", 0);
|
||||
|
@ -205,7 +201,12 @@ public class NSUpload {
|
|||
}
|
||||
} else {
|
||||
if (L.isEnabled(L.NSCLIENT))
|
||||
log.debug("OpenAPS data too old to upload");
|
||||
log.debug("OpenAPS data too old to upload, sending iob only");
|
||||
IobTotal[] iob = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile);
|
||||
if (iob.length > 0) {
|
||||
deviceStatus.iob = iob[0].json();
|
||||
deviceStatus.iob.put("time", DateUtil.toISOString(DateUtil.now()));
|
||||
}
|
||||
}
|
||||
deviceStatus.device = "openaps://" + Build.MANUFACTURER + " " + Build.MODEL;
|
||||
JSONObject pumpstatus = ConfigBuilderPlugin.getPlugin().getActivePump().getJSONStatus(profile, profileName);
|
||||
|
@ -335,7 +336,6 @@ public class NSUpload {
|
|||
|
||||
public static void uploadOpenAPSOffline(double durationInMinutes) {
|
||||
try {
|
||||
Context context = MainApp.instance().getApplicationContext();
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("eventType", "OpenAPS Offline");
|
||||
data.put("duration", durationInMinutes);
|
||||
|
|
|
@ -32,6 +32,7 @@ import info.nightscout.androidaps.db.TemporaryBasal;
|
|||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
|
||||
import info.nightscout.androidaps.plugins.pump.common.bolusInfo.DetailedBolusInfoStorage;
|
||||
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil;
|
||||
import info.nightscout.androidaps.plugins.pump.common.utils.DateTimeUtil;
|
||||
import info.nightscout.androidaps.plugins.pump.common.utils.StringUtil;
|
||||
import info.nightscout.androidaps.plugins.pump.medtronic.comm.history.pump.MedtronicPumpHistoryDecoder;
|
||||
|
@ -613,6 +614,8 @@ public class MedtronicHistoryData {
|
|||
return;
|
||||
}
|
||||
|
||||
filterOutNonInsulinEntries(entriesFromHistory);
|
||||
|
||||
if (doubleBolusDebug)
|
||||
LOG.debug("DoubleBolusDebug: List (after filter): {}, FromDb={}", gson.toJson(entryList),
|
||||
gsonCore.toJson(entriesFromHistory));
|
||||
|
@ -640,6 +643,23 @@ public class MedtronicHistoryData {
|
|||
}
|
||||
|
||||
|
||||
private void filterOutNonInsulinEntries(List<? extends DbObjectBase> entriesFromHistory) {
|
||||
// when we try to pair PumpHistory with AAPS treatments, we need to ignore all non-insulin entries
|
||||
List<DbObjectBase> removeList = new ArrayList<>();
|
||||
|
||||
for (DbObjectBase dbObjectBase : entriesFromHistory) {
|
||||
|
||||
Treatment treatment = (Treatment)dbObjectBase;
|
||||
|
||||
if (RileyLinkUtil.isSame(treatment.insulin, 0d)) {
|
||||
removeList.add(dbObjectBase);
|
||||
}
|
||||
}
|
||||
|
||||
entriesFromHistory.removeAll(removeList);
|
||||
}
|
||||
|
||||
|
||||
private void processTBREntries(List<PumpHistoryEntry> entryList) {
|
||||
|
||||
Collections.reverse(entryList);
|
||||
|
|
|
@ -1,110 +0,0 @@
|
|||
package info.nightscout.androidaps.receivers;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import info.nightscout.androidaps.Constants;
|
||||
import info.nightscout.androidaps.MainApp;
|
||||
import info.nightscout.androidaps.data.Profile;
|
||||
import info.nightscout.androidaps.events.EventProfileNeedsUpdate;
|
||||
import info.nightscout.androidaps.interfaces.PumpInterface;
|
||||
import info.nightscout.androidaps.logging.L;
|
||||
import info.nightscout.androidaps.plugins.bus.RxBus;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
|
||||
import info.nightscout.androidaps.queue.commands.Command;
|
||||
import info.nightscout.androidaps.utils.DateUtil;
|
||||
import info.nightscout.androidaps.utils.FabricPrivacy;
|
||||
import info.nightscout.androidaps.utils.LocalAlertUtils;
|
||||
import info.nightscout.androidaps.utils.T;
|
||||
|
||||
|
||||
/**
|
||||
* Created by mike on 07.07.2016.
|
||||
*/
|
||||
public class KeepAliveReceiver extends BroadcastReceiver {
|
||||
private static Logger log = LoggerFactory.getLogger(L.CORE);
|
||||
public static final long STATUS_UPDATE_FREQUENCY = T.mins(15).msecs();
|
||||
private static long lastReadStatus = 0;
|
||||
private static long lastRun = 0;
|
||||
|
||||
public static void cancelAlarm(Context context) {
|
||||
Intent intent = new Intent(context, KeepAliveReceiver.class);
|
||||
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmManager.cancel(sender);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent rIntent) {
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:KeepAliveReciever");
|
||||
wl.acquire();
|
||||
|
||||
LocalAlertUtils.shortenSnoozeInterval();
|
||||
LocalAlertUtils.checkStaleBGAlert();
|
||||
checkPump();
|
||||
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("KeepAlive received");
|
||||
wl.release();
|
||||
}
|
||||
|
||||
private void checkPump() {
|
||||
final PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
|
||||
final Profile profile = ProfileFunctions.getInstance().getProfile();
|
||||
if (pump != null && profile != null) {
|
||||
long lastConnection = pump.lastDataTime();
|
||||
boolean isStatusOutdated = lastConnection + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis();
|
||||
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
|
||||
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("Last connection: " + DateUtil.dateAndTimeString(lastConnection));
|
||||
// sometimes keepalive broadcast stops
|
||||
// as as workaround test if readStatus was requested before an alarm is generated
|
||||
if (lastReadStatus != 0 && lastReadStatus > System.currentTimeMillis() - T.mins(5).msecs()) {
|
||||
LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated);
|
||||
}
|
||||
|
||||
if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) {
|
||||
RxBus.INSTANCE.send(new EventProfileNeedsUpdate());
|
||||
} else if (isStatusOutdated && !pump.isBusy()) {
|
||||
lastReadStatus = System.currentTimeMillis();
|
||||
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("KeepAlive. Status outdated.", null);
|
||||
} else if (isBasalOutdated && !pump.isBusy()) {
|
||||
lastReadStatus = System.currentTimeMillis();
|
||||
ConfigBuilderPlugin.getPlugin().getCommandQueue().readStatus("KeepAlive. Basal outdated.", null);
|
||||
}
|
||||
}
|
||||
if (lastRun != 0 && System.currentTimeMillis() - lastRun > T.mins(10).msecs()) {
|
||||
log.error("KeepAlive fail");
|
||||
FabricPrivacy.getInstance().logCustom("KeepAliveFail");
|
||||
}
|
||||
lastRun = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
//called by MainApp at first app start
|
||||
public void setAlarm(Context context) {
|
||||
|
||||
LocalAlertUtils.shortenSnoozeInterval();
|
||||
LocalAlertUtils.presnoozeAlarms();
|
||||
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
Intent i = new Intent(context, KeepAliveReceiver.class);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
|
||||
try {
|
||||
pi.send();
|
||||
} catch (PendingIntent.CanceledException e) {
|
||||
}
|
||||
am.cancel(pi);
|
||||
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package info.nightscout.androidaps.receivers
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.PendingIntent.CanceledException
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.PowerManager
|
||||
import android.os.SystemClock
|
||||
import info.nightscout.androidaps.events.EventProfileNeedsUpdate
|
||||
import info.nightscout.androidaps.logging.L
|
||||
import info.nightscout.androidaps.plugins.bus.RxBus.send
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
|
||||
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions
|
||||
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
|
||||
import info.nightscout.androidaps.queue.commands.Command
|
||||
import info.nightscout.androidaps.utils.DateUtil
|
||||
import info.nightscout.androidaps.utils.FabricPrivacy
|
||||
import info.nightscout.androidaps.utils.LocalAlertUtils
|
||||
import info.nightscout.androidaps.utils.T
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.math.abs
|
||||
|
||||
class KeepAliveReceiver : BroadcastReceiver() {
|
||||
private var lastReadStatus: Long = 0
|
||||
private var lastRun: Long = 0
|
||||
private var lastIobUpload: Long = 0
|
||||
|
||||
override fun onReceive(context: Context, rIntent: Intent) {
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("KeepAlive received")
|
||||
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
val wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:KeepAliveReceiver")
|
||||
wl.acquire(T.mins(2).msecs())
|
||||
LocalAlertUtils.shortenSnoozeInterval()
|
||||
LocalAlertUtils.checkStaleBGAlert()
|
||||
checkPump()
|
||||
checkAPS()
|
||||
wl.release()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val log = LoggerFactory.getLogger(L.CORE)
|
||||
|
||||
private val KEEP_ALIVE_MILLISECONDS = T.mins(5).msecs()
|
||||
private val STATUS_UPDATE_FREQUENCY = T.mins(15).msecs()
|
||||
private val IOB_UPDATE_FREQUENCY = T.mins(5).msecs()
|
||||
|
||||
//called by MainApp at first app start
|
||||
@JvmStatic
|
||||
fun setAlarm(context: Context) {
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("KeepAlive scheduled")
|
||||
SystemClock.sleep(5000) // wait for app initialization
|
||||
LocalAlertUtils.shortenSnoozeInterval()
|
||||
LocalAlertUtils.presnoozeAlarms()
|
||||
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
val i = Intent(context, KeepAliveReceiver::class.java)
|
||||
val pi = PendingIntent.getBroadcast(context, 0, i, 0)
|
||||
try {
|
||||
pi.send()
|
||||
} catch (e: CanceledException) {
|
||||
}
|
||||
am.cancel(pi)
|
||||
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), KEEP_ALIVE_MILLISECONDS, pi)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun cancelAlarm(context: Context) {
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("KeepAlive canceled")
|
||||
val intent = Intent(context, KeepAliveReceiver::class.java)
|
||||
val sender = PendingIntent.getBroadcast(context, 0, intent, 0)
|
||||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(sender)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkAPS() {
|
||||
val usedAPS = ConfigBuilderPlugin.getPlugin().activeAPS
|
||||
var shouldUploadStatus = false
|
||||
if (usedAPS == null) shouldUploadStatus = true
|
||||
else if (DateUtil.isOlderThan(usedAPS.lastAPSRun, 5)) shouldUploadStatus = true
|
||||
if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) {
|
||||
lastIobUpload = DateUtil.now()
|
||||
NSUpload.uploadDeviceStatus()
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkPump() {
|
||||
val pump = ConfigBuilderPlugin.getPlugin().activePump
|
||||
val profile = ProfileFunctions.getInstance().profile
|
||||
if (pump != null && profile != null) {
|
||||
val lastConnection = pump.lastDataTime()
|
||||
val isStatusOutdated = lastConnection + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis()
|
||||
val isBasalOutdated = abs(profile.basal - pump.baseBasalRate) > pump.pumpDescription.basalStep
|
||||
if (L.isEnabled(L.CORE))
|
||||
log.debug("Last connection: " + DateUtil.dateAndTimeString(lastConnection))
|
||||
// sometimes keep alive broadcast stops
|
||||
// as as workaround test if readStatus was requested before an alarm is generated
|
||||
if (lastReadStatus != 0L && lastReadStatus > System.currentTimeMillis() - T.mins(5).msecs()) {
|
||||
LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated)
|
||||
}
|
||||
if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getPlugin().commandQueue.isRunning(Command.CommandType.BASALPROFILE)) {
|
||||
send(EventProfileNeedsUpdate())
|
||||
} else if (isStatusOutdated && !pump.isBusy) {
|
||||
lastReadStatus = System.currentTimeMillis()
|
||||
ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("KeepAlive. Status outdated.", null)
|
||||
} else if (isBasalOutdated && !pump.isBusy) {
|
||||
lastReadStatus = System.currentTimeMillis()
|
||||
ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("KeepAlive. Basal outdated.", null)
|
||||
}
|
||||
}
|
||||
if (lastRun != 0L && System.currentTimeMillis() - lastRun > T.mins(10).msecs()) {
|
||||
log.error("KeepAlive fail")
|
||||
FabricPrivacy.getInstance().logCustom("KeepAliveFail")
|
||||
}
|
||||
lastRun = System.currentTimeMillis()
|
||||
}
|
||||
}
|
|
@ -217,6 +217,11 @@ public class DateUtil {
|
|||
return diff < T.mins(2).msecs();
|
||||
}
|
||||
|
||||
public static boolean isOlderThan(long date, long minutes) {
|
||||
long diff = now() - date;
|
||||
return diff > T.mins(minutes).msecs();
|
||||
}
|
||||
|
||||
public static GregorianCalendar gregorianCalendar() {
|
||||
return new GregorianCalendar();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue