From a7efa317be097e784734e72d48a9791698b1d801 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 28 Jul 2018 16:26:36 +0200 Subject: [PATCH] DATA logging & refactor --- .../info/nightscout/androidaps/Config.java | 4 + .../info/nightscout/androidaps/Constants.java | 4 + .../nightscout/androidaps/MainActivity.java | 4 +- .../androidaps/Services/DataService.java | 123 ++++-------------- .../ConfigBuilder/ConfigBuilderPlugin.java | 9 -- .../androidaps/plugins/Food/FoodService.java | 22 +++- .../NSClientInternal/data/NSDeviceStatus.java | 44 ++++++- .../data/NSSettingsStatus.java | 78 ++++++++++- .../plugins/Treatments/Treatment.java | 2 - .../plugins/Treatments/TreatmentService.java | 68 ++++++---- .../Treatments/TreatmentsFragment.java | 2 - .../plugins/Treatments/TreatmentsPlugin.java | 16 ++- .../fragments/ProfileViewerDialog.java | 2 - .../fragments/TreatmentsBolusFragment.java | 2 - .../TreatmentsExtendedBolusesFragment.java | 2 - 15 files changed, 222 insertions(+), 160 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index 3612ee8898..4ee5371cd1 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -40,6 +40,10 @@ public class Config { public static final boolean logOverview = true; public static final boolean logNotification = true; public static final boolean logAlarm = false; + public static final boolean logDataService = true; + public static final boolean logDataNS = true; + public static final boolean logDataFood = true; + public static final boolean logDataTreatments = true; // DanaR specific public static final boolean logDanaBTComm = true; diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 0b9c0a408e..6e54954cc6 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -76,4 +76,8 @@ public class Constants { public static final String OVERVIEW = "OVERVIEW"; public static final String NOTIFICATION = "NOTIFICATION"; public static final String ALARM = "ALARM"; + public static final String DATASERVICE = "DATASERVICE"; + public static final String DATANS = "DATANS"; + public static final String DATAFOOD = "DATAFOOD"; + public static final String DATATREATMENTS = "DATATREATMENTS"; } diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index ea8948abdd..1742f3349f 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -44,8 +44,8 @@ import info.nightscout.androidaps.events.EventFeatureRunning; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Food.FoodPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.setupwizard.SetupWizardActivity; import info.nightscout.androidaps.tabs.TabPageAdapter; @@ -400,7 +400,7 @@ public class MainActivity extends AppCompatActivity { builder.setIcon(R.mipmap.blueowl); String message = "Build: " + BuildConfig.BUILDVERSION + "\n"; message += "Flavor: " + BuildConfig.FLAVOR + BuildConfig.BUILD_TYPE + "\n"; - message += MainApp.gs(R.string.configbuilder_nightscoutversion_label) + " " + ConfigBuilderPlugin.nightscoutVersionName; + message += MainApp.gs(R.string.configbuilder_nightscoutversion_label) + " " + NSSettingsStatus.getInstance().nightscoutVersionName; if (MainApp.engineeringMode) message += "\n" + MainApp.gs(R.string.engineering_mode_enabled); message += MainApp.gs(R.string.about_link_urls); diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index a254f9bf5f..6efc770246 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -2,7 +2,6 @@ package info.nightscout.androidaps.Services; import android.app.IntentService; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Bundle; import android.provider.Telephony; @@ -13,18 +12,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventNsFood; import info.nightscout.androidaps.events.EventNsTreatment; -import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; -import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; -import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.ProfileNS.NSProfilePlugin; @@ -43,7 +39,7 @@ import info.nightscout.utils.SP; public class DataService extends IntentService { - private static Logger log = LoggerFactory.getLogger(DataService.class); + private Logger log = LoggerFactory.getLogger(Constants.DATASERVICE); public DataService() { super("DataService"); @@ -52,8 +48,10 @@ public class DataService extends IntentService { @Override protected void onHandleIntent(final Intent intent) { - if (Config.logFunctionCalls) + if (Config.logDataService) { + log.debug("onHandleIntent " + intent); log.debug("onHandleIntent " + BundleLogger.log(intent.getExtras())); + } boolean acceptNSData = !SP.getBoolean(R.string.key_ns_upload_only, false); Bundle bundles = intent.getExtras(); @@ -78,15 +76,23 @@ public class DataService extends IntentService { } else if (Intents.ACTION_NEW_PROFILE.equals(action)) { // always handle Profile if NSProfile is enabled without looking at nsUploadOnly NSProfilePlugin.getPlugin().handleNewData(intent); + } else if (Intents.ACTION_NEW_DEVICESTATUS.equals(action)) { + NSDeviceStatus.getInstance().handleNewData(intent); + } else if (Intents.ACTION_NEW_STATUS.equals(action)) { + NSSettingsStatus.getInstance().handleNewData(intent); + } else if (Intents.ACTION_NEW_FOOD.equals(action)) { + EventNsFood evt = new EventNsFood(EventNsFood.ADD, bundles); + MainApp.bus().post(evt); + } else if (Intents.ACTION_CHANGED_FOOD.equals(action)) { + EventNsFood evt = new EventNsFood(EventNsFood.UPDATE, bundles); + MainApp.bus().post(evt); + } else if (Intents.ACTION_REMOVED_FOOD.equals(action)) { + EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles); + MainApp.bus().post(evt); } else if (acceptNSData && (Intents.ACTION_NEW_TREATMENT.equals(action) || Intents.ACTION_CHANGED_TREATMENT.equals(action) || Intents.ACTION_REMOVED_TREATMENT.equals(action) || - Intents.ACTION_NEW_STATUS.equals(action) || - Intents.ACTION_NEW_DEVICESTATUS.equals(action) || - Intents.ACTION_NEW_FOOD.equals(action) || - Intents.ACTION_CHANGED_FOOD.equals(action) || - Intents.ACTION_REMOVED_FOOD.equals(action) || Intents.ACTION_NEW_CAL.equals(action) || Intents.ACTION_NEW_MBG.equals(action)) ) { @@ -95,7 +101,7 @@ public class DataService extends IntentService { SmsCommunicatorPlugin.getPlugin().handleNewData(intent); } - if (Config.logFunctionCalls) + if (Config.logDataService) log.debug("onHandleIntent exit " + intent); DataReceiver.completeWakefulIntent(intent); } @@ -122,79 +128,6 @@ public class DataService extends IntentService { log.debug("Got intent: " + intent.getAction()); - if (intent.getAction().equals(Intents.ACTION_NEW_STATUS)) { - if (bundles.containsKey("nsclientversioncode")) { - ConfigBuilderPlugin.nightscoutVersionCode = bundles.getInt("nightscoutversioncode"); // for ver 1.2.3 contains 10203 - ConfigBuilderPlugin.nightscoutVersionName = bundles.getString("nightscoutversionname"); - ConfigBuilderPlugin.nsClientVersionCode = bundles.getInt("nsclientversioncode"); // for ver 1.17 contains 117 - ConfigBuilderPlugin.nsClientVersionName = bundles.getString("nsclientversionname"); - log.debug("Got versions: NSClient: " + ConfigBuilderPlugin.nsClientVersionName + " Nightscout: " + ConfigBuilderPlugin.nightscoutVersionName); - try { - if (ConfigBuilderPlugin.nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) { - Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); - MainApp.bus().post(new EventNewNotification(notification)); - } else { - MainApp.bus().post(new EventDismissNotification(Notification.OLD_NSCLIENT)); - } - } catch (PackageManager.NameNotFoundException e) { - log.error("Unhandled exception", e); - } - if (ConfigBuilderPlugin.nightscoutVersionCode < Config.SUPPORTEDNSVERSION) { - Notification notification = new Notification(Notification.OLD_NS, MainApp.gs(R.string.unsupportednsversion), Notification.NORMAL); - MainApp.bus().post(new EventNewNotification(notification)); - } else { - MainApp.bus().post(new EventDismissNotification(Notification.OLD_NS)); - } - } else { - Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); - MainApp.bus().post(new EventNewNotification(notification)); - } - if (bundles.containsKey("status")) { - try { - JSONObject statusJson = new JSONObject(bundles.getString("status")); - NSSettingsStatus.getInstance().setData(statusJson); - if (Config.logIncommingData) - log.debug("Received status: " + statusJson.toString()); - Double targetHigh = NSSettingsStatus.getInstance().getThreshold("bgTargetTop"); - Double targetlow = NSSettingsStatus.getInstance().getThreshold("bgTargetBottom"); - if (targetHigh != null) - OverviewPlugin.bgTargetHigh = targetHigh; - if (targetlow != null) - OverviewPlugin.bgTargetLow = targetlow; - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - } - } - if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) { - try { - if (bundles.containsKey("devicestatus")) { - JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus")); - NSDeviceStatus.getInstance().setData(devicestatusJson); - if (devicestatusJson.has("pump")) { - // Objectives 0 - ObjectivesPlugin.pumpStatusIsAvailableInNS = true; - ObjectivesPlugin.saveProgress(); - } - } - if (bundles.containsKey("devicestatuses")) { - String devicestatusesstring = bundles.getString("devicestatuses"); - JSONArray jsonArray = new JSONArray(devicestatusesstring); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject devicestatusJson = jsonArray.getJSONObject(i); - NSDeviceStatus.getInstance().setData(devicestatusJson); - if (devicestatusJson.has("pump")) { - // Objectives 0 - ObjectivesPlugin.pumpStatusIsAvailableInNS = true; - ObjectivesPlugin.saveProgress(); - } - } - } - } catch (Exception e) { - log.error("Unhandled exception", e); - } - } - if (intent.getAction().equals(Intents.ACTION_NEW_TREATMENT) || intent.getAction().equals(Intents.ACTION_CHANGED_TREATMENT)) { try { if (bundles.containsKey("treatment")) { @@ -219,7 +152,7 @@ public class DataService extends IntentService { if (bundles.containsKey("treatment")) { String trstring = bundles.getString("treatment"); JSONObject json = new JSONObject(trstring); - handleTreatmentFromNS(json); + handleRemovedTreatmentFromNS(json); } if (bundles.containsKey("treatments")) { @@ -227,7 +160,7 @@ public class DataService extends IntentService { JSONArray jsonArray = new JSONArray(trstring); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json = jsonArray.getJSONObject(i); - handleTreatmentFromNS(json); + handleRemovedTreatmentFromNS(json); } } } catch (JSONException e) { @@ -255,21 +188,9 @@ public class DataService extends IntentService { log.error("Unhandled exception", e); } } - - if (intent.getAction().equals(Intents.ACTION_NEW_FOOD) - || intent.getAction().equals(Intents.ACTION_CHANGED_FOOD)) { - int mode = Intents.ACTION_NEW_FOOD.equals(intent.getAction()) ? EventNsFood.ADD : EventNsFood.UPDATE; - EventNsFood evt = new EventNsFood(mode, bundles); - MainApp.bus().post(evt); - } - - if (intent.getAction().equals(Intents.ACTION_REMOVED_FOOD)) { - EventNsFood evt = new EventNsFood(EventNsFood.REMOVE, bundles); - MainApp.bus().post(evt); - } } - private void handleTreatmentFromNS(JSONObject json) { + private void handleRemovedTreatmentFromNS(JSONObject json) { // new DB model EventNsTreatment evtTreatment = new EventNsTreatment(EventNsTreatment.REMOVE, json); MainApp.bus().post(evtTreatment); 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 886eeb3db7..eb35a3cc60 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 @@ -70,15 +70,9 @@ public class ConfigBuilderPlugin extends PluginBase { private static ProfileInterface activeProfile; private static TreatmentsInterface activeTreatments; private static APSInterface activeAPS; - private static LoopPlugin activeLoop; private static InsulinInterface activeInsulin; private static SensitivityInterface activeSensitivity; - static public String nightscoutVersionName = ""; - static public Integer nightscoutVersionCode = 0; - static public String nsClientVersionName = ""; - static public Integer nsClientVersionCode = 0; - private static ArrayList pluginList; private static CommandQueue commandQueue = new CommandQueue(); @@ -337,9 +331,6 @@ public class ConfigBuilderPlugin extends PluginBase { } this.setFragmentVisiblities(((PluginBase) activePump).getName(), pluginsInCategory, PluginType.PUMP); - // PluginType.LOOP - activeLoop = this.determineActivePlugin(PluginType.LOOP); - // PluginType.TREATMENT activeTreatments = this.determineActivePlugin(PluginType.TREATMENT); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java index 44b96850ae..f1e4159248 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Food/FoodService.java @@ -27,6 +27,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.ICallback; @@ -39,7 +41,7 @@ import info.nightscout.androidaps.events.EventNsFood; */ public class FoodService extends OrmLiteBaseService { - private static Logger log = LoggerFactory.getLogger(FoodService.class); + private Logger log = LoggerFactory.getLogger(Constants.DATAFOOD); private static final ScheduledExecutorService foodEventWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledFoodEventPost = null; @@ -110,7 +112,8 @@ public class FoodService extends OrmLiteBaseService { public void onCreate() { super.onCreate(); try { - log.info("onCreate"); + if (Config.logDataFood) + log.info("onCreate"); TableUtils.createTableIfNotExists(this.getConnectionSource(), Food.class); } catch (SQLException e) { log.error("Can't create database", e); @@ -122,7 +125,8 @@ public class FoodService extends OrmLiteBaseService { if (oldVersion == 7 && newVersion == 8) { log.debug("Upgrading database from v7 to v8"); } else { - log.info("onUpgrade"); + if (Config.logDataFood) + log.info("onUpgrade"); // this.resetFood(); } } @@ -161,7 +165,8 @@ public class FoodService extends OrmLiteBaseService { class PostRunnable implements Runnable { public void run() { - log.debug("Firing EventFoodChange"); + if (Config.logDataFood) + log.debug("Firing EventFoodChange"); MainApp.bus().post(event); callback.setPost(null); } @@ -271,7 +276,8 @@ public class FoodService extends OrmLiteBaseService { public void deleteByNSId(String _id) throws SQLException { Food stored = this.findByNSId(_id); if (stored != null) { - log.debug("FOOD: Removing Food record from database: " + stored.toString()); + if (Config.logDataFood) + log.debug("Removing Food record from database: " + stored.toString()); this.delete(stored); } } @@ -324,7 +330,8 @@ public class FoodService extends OrmLiteBaseService { public void createOrUpdate(Food food) { try { this.getDao().createOrUpdate(food); - log.debug("FOOD: Created or Updated: " + food.toString()); + if (Config.logDataFood) + log.debug("Created or Updated: " + food.toString()); } catch (SQLException e) { log.error("Unable to createOrUpdate Food", e); } @@ -334,7 +341,8 @@ public class FoodService extends OrmLiteBaseService { public void create(Food food) { try { this.getDao().create(food); - log.debug("FOOD: New record: " + food.toString()); + if (Config.logDataFood) + log.debug("New record: " + food.toString()); } catch (SQLException e) { log.error("Unable to create Food", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java index 29d1b33467..e39f717af2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java @@ -1,8 +1,11 @@ package info.nightscout.androidaps.plugins.NSClientInternal.data; +import android.content.Intent; +import android.os.Bundle; import android.text.Html; import android.text.Spanned; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -12,8 +15,12 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin; import info.nightscout.androidaps.plugins.Loop.APSResult; +import info.nightscout.utils.BundleLogger; import info.nightscout.utils.DateUtil; import info.nightscout.utils.Round; import info.nightscout.utils.SP; @@ -73,7 +80,7 @@ import info.nightscout.utils.SP; } */ public class NSDeviceStatus { - private static Logger log = LoggerFactory.getLogger(NSDeviceStatus.class); + private Logger log = LoggerFactory.getLogger(Constants.DATANS); private static NSDeviceStatus instance = null; @@ -88,6 +95,41 @@ public class NSDeviceStatus { public NSDeviceStatus() { } + public void handleNewData(Intent intent) { + Bundle bundle = intent.getExtras(); + if (bundle == null) return; + + if (Config.logDataNS) + log.debug("Got NS devicestatus: " + BundleLogger.log(bundle)); + + try { + if (bundle.containsKey("devicestatus")) { + JSONObject devicestatusJson = new JSONObject(bundle.getString("devicestatus")); + setData(devicestatusJson); + if (devicestatusJson.has("pump")) { + // Objectives 0 + ObjectivesPlugin.pumpStatusIsAvailableInNS = true; + ObjectivesPlugin.saveProgress(); + } + } + if (bundle.containsKey("devicestatuses")) { + String devicestatusesstring = bundle.getString("devicestatuses"); + JSONArray jsonArray = new JSONArray(devicestatusesstring); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject devicestatusJson = jsonArray.getJSONObject(i); + setData(devicestatusJson); + if (devicestatusJson.has("pump")) { + // Objectives 0 + ObjectivesPlugin.pumpStatusIsAvailableInNS = true; + ObjectivesPlugin.saveProgress(); + } + } + } + } catch (Exception e) { + log.error("Unhandled exception", e); + } + } + public NSDeviceStatus setData(JSONObject obj) { this.data = obj; updatePumpData(obj); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java index 8d09a05146..d5d52fd68a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java @@ -1,5 +1,8 @@ package info.nightscout.androidaps.plugins.NSClientInternal.data; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Bundle; import android.support.annotation.Nullable; import org.json.JSONException; @@ -10,6 +13,16 @@ import org.slf4j.LoggerFactory; import java.util.Date; import java.util.Objects; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.androidaps.plugins.Overview.notifications.Notification; +import info.nightscout.utils.BundleLogger; + /* { "status": "ok", @@ -101,7 +114,7 @@ import java.util.Objects; } */ public class NSSettingsStatus { - private static Logger log = LoggerFactory.getLogger(NSSettingsStatus.class); + private Logger log = LoggerFactory.getLogger(Constants.DATANS); private static NSSettingsStatus instance = null; @@ -111,6 +124,8 @@ public class NSSettingsStatus { return instance; } + public String nightscoutVersionName = ""; + private JSONObject data = null; public NSSettingsStatus() { @@ -121,6 +136,59 @@ public class NSSettingsStatus { return this; } + public void handleNewData(Intent intent) { + Bundle bundle = intent.getExtras(); + if (bundle == null) return; + + if (Config.logDataNS) + log.debug("Got NS status: " + BundleLogger.log(bundle)); + + if (bundle.containsKey("nsclientversioncode")) { + + Integer nightscoutVersionCode = bundle.getInt("nightscoutversioncode"); + nightscoutVersionName = bundle.getString("nightscoutversionname"); + Integer nsClientVersionCode = bundle.getInt("nsclientversioncode"); + String nsClientVersionName = bundle.getString("nsclientversionname"); + if (Config.logDataNS) + log.debug("Got versions: NSClient: " + nsClientVersionName + " Nightscout: " + nightscoutVersionName); + try { + if (nsClientVersionCode < MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionCode) { + Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(notification)); + } else { + MainApp.bus().post(new EventDismissNotification(Notification.OLD_NSCLIENT)); + } + } catch (PackageManager.NameNotFoundException e) { + log.error("Unhandled exception", e); + } + if (nightscoutVersionCode < Config.SUPPORTEDNSVERSION) { + Notification notification = new Notification(Notification.OLD_NS, MainApp.gs(R.string.unsupportednsversion), Notification.NORMAL); + MainApp.bus().post(new EventNewNotification(notification)); + } else { + MainApp.bus().post(new EventDismissNotification(Notification.OLD_NS)); + } + } else { + Notification notification = new Notification(Notification.OLD_NSCLIENT, MainApp.gs(R.string.unsupportedclientver), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(notification)); + } + if (bundle.containsKey("status")) { + try { + JSONObject statusJson = new JSONObject(bundle.getString("status")); + setData(statusJson); + if (Config.logDataNS) + log.debug("Received status: " + statusJson.toString()); + Double targetHigh = getThreshold("bgTargetTop"); + Double targetlow = getThreshold("bgTargetBottom"); + if (targetHigh != null) + OverviewPlugin.bgTargetHigh = targetHigh; + if (targetlow != null) + OverviewPlugin.bgTargetLow = targetlow; + } catch (JSONException e) { + log.error("Unhandled exception", e); + } + } + } + public String getName() { return getStringOrNull("name"); } @@ -134,7 +202,7 @@ public class NSSettingsStatus { } public Date getServerTime() { - return getDateOrNull("versionNum"); + return getDateOrNull("serverTime"); } public boolean getApiEnabled() { @@ -202,13 +270,11 @@ public class NSSettingsStatus { if (settingsO.has("thresholds")) { JSONObject tObject = settingsO.getJSONObject("thresholds"); if (tObject.has(what)) { - Double result = tObject.getDouble(what); - return result; + return tObject.getDouble(what); } } if (settingsO.has("alarmTimeagoWarnMins") && Objects.equals(what, "alarmTimeagoWarnMins")) { - Double result = settingsO.getDouble(what); - return result; + return settingsO.getDouble(what); } } } catch (JSONException e) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java index 73423c0eab..401d808e60 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/Treatment.java @@ -29,8 +29,6 @@ import info.nightscout.utils.JsonHelper; @DatabaseTable(tableName = Treatment.TABLE_TREATMENTS) public class Treatment implements DataPointWithLabelInterface { - private static Logger log = LoggerFactory.getLogger(Treatment.class); - public static final String TABLE_TREATMENTS = "Treatments"; @DatabaseField(id = true) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java index e701986e89..b219522102 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentService.java @@ -29,6 +29,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import info.nightscout.androidaps.Config; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.ICallback; @@ -46,7 +48,7 @@ import info.nightscout.utils.JsonHelper; */ public class TreatmentService extends OrmLiteBaseService { - private static Logger log = LoggerFactory.getLogger(TreatmentService.class); + private static Logger log = LoggerFactory.getLogger(Constants.DATATREATMENTS); private static final ScheduledExecutorService treatmentEventWorker = Executors.newSingleThreadScheduledExecutor(); private static ScheduledFuture scheduledTreatmentEventPost = null; @@ -102,7 +104,8 @@ public class TreatmentService extends OrmLiteBaseService { public void onCreate() { super.onCreate(); try { - log.info("onCreate"); + if (Config.logDataTreatments) + log.info("onCreate"); TableUtils.createTableIfNotExists(this.getConnectionSource(), Treatment.class); } catch (SQLException e) { log.error("Can't create database", e); @@ -121,7 +124,8 @@ public class TreatmentService extends OrmLiteBaseService { throw new RuntimeException(e); } } else { - log.info("onUpgrade"); + if (Config.logDataTreatments) + log.info("onUpgrade"); // this.resetFood(); } } @@ -161,10 +165,14 @@ public class TreatmentService extends OrmLiteBaseService { class PostRunnable implements Runnable { public void run() { - log.debug("Firing EventFoodChange"); + if (Config.logDataTreatments) + log.debug("Firing EventReloadTreatmentData"); MainApp.bus().post(event); - if (DatabaseHelper.earliestDataChange != null) + if (DatabaseHelper.earliestDataChange != null) { + if (Config.logDataTreatments) + log.debug("Firing EventNewHistoryData"); MainApp.bus().post(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); + } DatabaseHelper.earliestDataChange = null; callback.setPost(null); } @@ -252,13 +260,14 @@ public class TreatmentService extends OrmLiteBaseService { if (existingTreatment != null) { boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); boolean sameSource = existingTreatment.source == treatment.source; - if(!equalRePumpHistory) { + if (!equalRePumpHistory) { // another treatment exists. Update it with the treatment coming from the pump - log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + if (Config.logDataTreatments) + log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); long oldDate = existingTreatment.date; //preserve carbs - if(existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0){ + if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { treatment.carbs = existingTreatment.carbs; } @@ -278,13 +287,14 @@ public class TreatmentService extends OrmLiteBaseService { boolean equalRePumpHistory = existingTreatment.equalsRePumpHistory(treatment); boolean sameSource = existingTreatment.source == treatment.source; long oldDate = existingTreatment.date; - log.debug("TREATMENT: Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); + if (Config.logDataTreatments) + log.debug("Pump record already found in database: " + existingTreatment.toString() + " wanting to add " + treatment.toString()); //preserve carbs - if(existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0){ + if (existingTreatment.isValid && existingTreatment.carbs > 0 && treatment.carbs == 0) { treatment.carbs = existingTreatment.carbs; } - + getDao().delete(existingTreatment); // need to delete/create because date may change too existingTreatment.copyFrom(treatment); getDao().create(existingTreatment); @@ -294,7 +304,8 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(equalRePumpHistory || sameSource, false); } getDao().create(treatment); - log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + if (Config.logDataTreatments) + log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); return new UpdateReturn(true, true); @@ -308,7 +319,8 @@ public class TreatmentService extends OrmLiteBaseService { getDao().delete(old); // need to delete/create because date may change too old.copyFrom(treatment); getDao().create(old); - log.debug("TREATMENT: Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString()); + if (Config.logDataTreatments) + log.debug("Updating record by date from: " + Source.getString(treatment.source) + " " + old.toString()); if (historyChange) { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); @@ -328,7 +340,8 @@ public class TreatmentService extends OrmLiteBaseService { getDao().delete(old); // need to delete/create because date may change too old.copyFrom(treatment); getDao().create(old); - log.debug("TREATMENT: Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); + if (Config.logDataTreatments) + log.debug("Updating record by _id from: " + Source.getString(treatment.source) + " " + old.toString()); if (historyChange) { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); @@ -339,14 +352,16 @@ public class TreatmentService extends OrmLiteBaseService { } } getDao().create(treatment); - log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + if (Config.logDataTreatments) + log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); return new UpdateReturn(true, true); } if (treatment.source == Source.USER) { getDao().create(treatment); - log.debug("TREATMENT: New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); + if (Config.logDataTreatments) + log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); scheduleTreatmentChange(treatment); return new UpdateReturn(true, true); @@ -357,8 +372,10 @@ public class TreatmentService extends OrmLiteBaseService { return new UpdateReturn(false, false); } - /** Returns the record for the given id, null if none, throws RuntimeException - * if multiple records with the same pump id exist. */ + /** + * Returns the record for the given id, null if none, throws RuntimeException + * if multiple records with the same pump id exist. + */ @Nullable public Treatment getPumpRecordById(long pumpId) { try { @@ -368,9 +385,12 @@ public class TreatmentService extends OrmLiteBaseService { PreparedQuery preparedQuery = queryBuilder.prepare(); List result = getDao().query(preparedQuery); switch (result.size()) { - case 0: return null; - case 1: return result.get(0); - default: throw new RuntimeException("Multiple records with the same pump id found: " + result.toString()); + case 0: + return null; + case 1: + return result.get(0); + default: + throw new RuntimeException("Multiple records with the same pump id found: " + result.toString()); } } catch (SQLException e) { throw new RuntimeException(e); @@ -393,7 +413,8 @@ public class TreatmentService extends OrmLiteBaseService { private void deleteByNSId(String _id) { Treatment stored = findByNSId(_id); if (stored != null) { - log.debug("TREATMENT: Removing Treatment record from database: " + stored.toString()); + if (Config.logDataTreatments) + log.debug("Removing Treatment record from database: " + stored.toString()); delete(stored); DatabaseHelper.updateEarliestDataChange(stored.date); scheduleTreatmentChange(null); @@ -480,10 +501,11 @@ public class TreatmentService extends OrmLiteBaseService { } public class UpdateReturn { - public UpdateReturn(boolean success, boolean newRecord){ + public UpdateReturn(boolean success, boolean newRecord) { this.success = success; this.newRecord = newRecord; } + boolean newRecord; boolean success; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java index ea75dfad6a..550522313d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java @@ -27,8 +27,6 @@ import info.nightscout.androidaps.plugins.Treatments.fragments.TreatmentsTempora import info.nightscout.utils.FabricPrivacy; public class TreatmentsFragment extends SubscriberFragment implements View.OnClickListener { - private static Logger log = LoggerFactory.getLogger(TreatmentsFragment.class); - TextView treatmentsTab; TextView extendedBolusesTab; TextView tempBasalsTab; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index b701f66afc..0a1ed1c2d7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -55,7 +56,7 @@ import info.nightscout.utils.T; * Created by mike on 05.08.2016. */ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface { - private static Logger log = LoggerFactory.getLogger(TreatmentsPlugin.class); + private Logger log = LoggerFactory.getLogger(Constants.DATATREATMENTS); private static TreatmentsPlugin treatmentsPlugin; @@ -109,6 +110,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } private void initializeTreatmentData() { + if (Config.logDataTreatments) + log.debug("initializeTreatmentData"); double dia = Constants.defaultDIA; if (MainApp.getConfigBuilder() != null && MainApp.getConfigBuilder().getProfile() != null) dia = MainApp.getConfigBuilder().getProfile().getDia(); @@ -120,6 +123,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } private void initializeTempBasalData() { + if (Config.logDataTreatments) + log.debug("initializeTempBasalData"); double dia = Constants.defaultDIA; if (MainApp.getConfigBuilder() != null && MainApp.getConfigBuilder().getProfile() != null) dia = MainApp.getConfigBuilder().getProfile().getDia(); @@ -132,6 +137,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } private void initializeExtendedBolusData() { + if (Config.logDataTreatments) + log.debug("initializeExtendedBolusData"); double dia = Constants.defaultDIA; if (MainApp.getConfigBuilder() != null && MainApp.getConfigBuilder().getProfile() != null) dia = MainApp.getConfigBuilder().getProfile().getDia(); @@ -144,6 +151,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } private void initializeTempTargetData() { + if (Config.logDataTreatments) + log.debug("initializeTempTargetData"); synchronized (tempTargets) { long fromMills = System.currentTimeMillis() - 60 * 60 * 1000L * 24; tempTargets.reset().add(MainApp.getDbHelper().getTemptargetsDataFromTime(fromMills, false)); @@ -151,6 +160,8 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } private void initializeProfileSwitchData() { + if (Config.logDataTreatments) + log.debug("initializeProfileSwitchData"); synchronized (profiles) { profiles.reset().add(MainApp.getDbHelper().getProfileSwitchData(false)); } @@ -294,6 +305,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface last = t.date; } } + if (Config.logDataTreatments) log.debug("Last bolus time: " + new Date(last).toLocaleString()); return last; } @@ -322,6 +334,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Subscribe public void onStatusEvent(final EventReloadTreatmentData ev) { + if (Config.logDataTreatments) log.debug("EventReloadTreatmentData"); initializeTreatmentData(); initializeExtendedBolusData(); @@ -332,6 +345,7 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Subscribe @SuppressWarnings("unused") public void onStatusEvent(final EventReloadTempBasalData ev) { + if (Config.logDataTreatments) log.debug("EventReloadTempBasalData"); initializeTempBasalData(); updateTotalIOBTempBasals(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java index 5f758c8a06..c2263d81b5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/ProfileViewerDialog.java @@ -31,8 +31,6 @@ public class ProfileViewerDialog extends DialogFragment { private long time; - private static Logger log = LoggerFactory.getLogger(ProfileViewDialog.class); - @BindView(R.id.profileview_noprofile) TextView noProfile; @BindView(R.id.profileview_invalidprofile) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java index 0faceb2382..ee62733596 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java @@ -46,8 +46,6 @@ import info.nightscout.utils.SP; import static info.nightscout.utils.DateUtil.now; public class TreatmentsBolusFragment extends SubscriberFragment implements View.OnClickListener { - private static Logger log = LoggerFactory.getLogger(TreatmentsBolusFragment.class); - RecyclerView recyclerView; LinearLayoutManager llm; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java index 05e18bfdc3..389965c6fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java @@ -39,8 +39,6 @@ import info.nightscout.utils.NSUpload; public class TreatmentsExtendedBolusesFragment extends SubscriberFragment { - private static Logger log = LoggerFactory.getLogger(TreatmentsExtendedBolusesFragment.class); - RecyclerView recyclerView; LinearLayoutManager llm;