From a508b0bbc5caef0dc004b58afc86594aa9a6702a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 21 Feb 2017 13:33:52 +0100 Subject: [PATCH 1/3] do not postpone status upload --- .../ConfigBuilder/ConfigBuilderPlugin.java | 26 +------------------ .../DanaR/Services/ExecutionService.java | 2 +- .../Services/ExecutionService.java | 2 +- .../androidaps/plugins/Loop/LoopPlugin.java | 2 +- .../plugins/Overview/OverviewFragment.java | 2 +- 5 files changed, 5 insertions(+), 29 deletions(-) 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 8c6d9f5c35..9d82370f4a 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 @@ -81,9 +81,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain static ArrayList pluginList; - private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); - private static ScheduledFuture scheduledPost = null; - PowerManager.WakeLock mWakeLock; public ConfigBuilderPlugin() { @@ -815,11 +812,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain return maxIobAfterConstrain; } - @Subscribe - public void onStatusEvent(final EventNewBG ev) { - uploadDeviceStatus(120); - } - public void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) { try { Context context = MainApp.instance().getApplicationContext(); @@ -923,7 +915,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } } - public void doUploadDeviceStatus() { + public void uploadDeviceStatus() { DeviceStatus deviceStatus = new DeviceStatus(); try { LoopPlugin.LastRun lastRun = LoopPlugin.lastRun; @@ -981,22 +973,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } } - static public void uploadDeviceStatus(int sec) { - class PostRunnable implements Runnable { - public void run() { - MainApp.getConfigBuilder().doUploadDeviceStatus(); - scheduledPost = null; - } - } - // prepare task for execution - // cancel waiting task to prevent sending multiple posts - if (scheduledPost != null) - scheduledPost.cancel(false); - Runnable task = new PostRunnable(); - scheduledPost = worker.schedule(task, sec, TimeUnit.SECONDS); - log.debug("Scheduling devicestatus upload in " + sec + " sec"); - } - public void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) { JSONObject data = new JSONObject(); try { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java index 323b139bda..a6e7a2b278 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaR/Services/ExecutionService.java @@ -341,7 +341,7 @@ public class ExecutionService extends Service { danaRPump.lastConnection = now; MainApp.bus().post(new EventDanaRNewStatus()); MainApp.bus().post(new EventInitializationChanged()); - MainApp.getConfigBuilder().uploadDeviceStatus(15); + MainApp.getConfigBuilder().uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning ) { log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits); Notification reportFail = new Notification(Notification.APPROACHING_DAILY_LIMIT, MainApp.sResources.getString(R.string.approachingdailylimit), Notification.URGENT); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java index 1a0267dd67..2d8e04109d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/DanaRKorean/Services/ExecutionService.java @@ -332,7 +332,7 @@ public class ExecutionService extends Service { danaRKoreanPump.lastConnection = now; MainApp.bus().post(new EventDanaRNewStatus()); MainApp.bus().post(new EventInitializationChanged()); - MainApp.getConfigBuilder().uploadDeviceStatus(15); + MainApp.getConfigBuilder().uploadDeviceStatus(); if (danaRKoreanPump.dailyTotalUnits > danaRKoreanPump.maxDailyTotalUnits * Constants.dailyLimitWarning ) { log.debug("Approaching daily limit: " + danaRKoreanPump.dailyTotalUnits + "/" + danaRKoreanPump.maxDailyTotalUnits); Notification reportFail = new Notification(Notification.APPROACHING_DAILY_LIMIT, MainApp.sResources.getString(R.string.approachingdailylimit), Notification.URGENT); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java index 59ddcfc3b8..a357d519b9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/LoopPlugin.java @@ -228,7 +228,7 @@ public class LoopPlugin implements PluginBase { } MainApp.bus().post(new EventLoopUpdateGui()); - MainApp.getConfigBuilder().uploadDeviceStatus(60); + MainApp.getConfigBuilder().uploadDeviceStatus(); } finally { if (Config.logFunctionCalls) log.debug("invoke end"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index ac9035b7fd..ea3d8d551a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -272,7 +272,7 @@ public class OverviewFragment extends Fragment { finalLastRun.setByPump = applyResult; finalLastRun.lastEnact = new Date(); finalLastRun.lastOpenModeAccept = new Date(); - MainApp.getConfigBuilder().uploadDeviceStatus(15); + MainApp.getConfigBuilder().uploadDeviceStatus(); ObjectivesPlugin objectivesPlugin = (ObjectivesPlugin) MainApp.getSpecificPlugin(ObjectivesPlugin.class); if (objectivesPlugin != null) { objectivesPlugin.manualEnacts++; From 4b6276c0b17e56290efb4d3cb131bad307e200fb Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 21 Feb 2017 13:45:33 +0100 Subject: [PATCH 2/3] uploader battery always in json --- .../plugins/ConfigBuilder/ConfigBuilderPlugin.java | 10 ++++------ .../java/info/nightscout/utils/BatteryLevel.java | 14 ++++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) 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 9d82370f4a..f3df77fe64 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 @@ -960,12 +960,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } int batteryLevel = BatteryLevel.getBatteryLevel(); - if (batteryLevel != BatteryLevel.lastUploadedLevel) { - JSONObject uploaderBattery = new JSONObject(); - uploaderBattery.put("uploaderBattery", batteryLevel); - deviceStatus.uploaderBattery = uploaderBattery; - BatteryLevel.lastUploadedLevel = batteryLevel; - } + JSONObject uploaderBattery = new JSONObject(); + uploaderBattery.put("uploaderBattery", batteryLevel); + deviceStatus.uploaderBattery = uploaderBattery; + deviceStatus.created_at = DateUtil.toISOString(new Date()); deviceStatus.sendToNSClient(); } catch (JSONException e) { diff --git a/app/src/main/java/info/nightscout/utils/BatteryLevel.java b/app/src/main/java/info/nightscout/utils/BatteryLevel.java index 44f6b6e0e5..ea55dd9e69 100644 --- a/app/src/main/java/info/nightscout/utils/BatteryLevel.java +++ b/app/src/main/java/info/nightscout/utils/BatteryLevel.java @@ -4,6 +4,9 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.MainApp; /** @@ -11,18 +14,21 @@ import info.nightscout.androidaps.MainApp; */ public class BatteryLevel { + private static Logger log = LoggerFactory.getLogger(BatteryLevel.class); static public int lastUploadedLevel = 0; static public int getBatteryLevel() { + int batteryLevel = 0; Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); if (batteryIntent != null) { int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); - if (level == -1 || scale == -1) { - return 50; + if (level != -1 && scale != -1) { + batteryLevel = (int) (((float) level / (float) scale) * 100.0f); } - return (int) (((float) level / (float) scale) * 100.0f); - } else return 50; + } + log.debug("Battery level: " + batteryLevel); + return batteryLevel; } } From efc1988374646d0493f45b754ad2ae4b993b7072 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 21 Feb 2017 14:12:28 +0100 Subject: [PATCH 3/3] uploader battery fix --- .../androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java | 4 +--- .../info/nightscout/androidaps/plugins/Loop/DeviceStatus.java | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) 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 f3df77fe64..362e5727c9 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 @@ -960,9 +960,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } int batteryLevel = BatteryLevel.getBatteryLevel(); - JSONObject uploaderBattery = new JSONObject(); - uploaderBattery.put("uploaderBattery", batteryLevel); - deviceStatus.uploaderBattery = uploaderBattery; + deviceStatus.uploaderBattery = batteryLevel; deviceStatus.created_at = DateUtil.toISOString(new Date()); deviceStatus.sendToNSClient(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java index 0d4f5b423f..0d72464b73 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Loop/DeviceStatus.java @@ -378,7 +378,7 @@ public class DeviceStatus { public JSONObject enacted = null; public JSONObject suggested = null; public JSONObject iob = null; - public JSONObject uploaderBattery = null; + public int uploaderBattery = 0; public String created_at = null; public JSONObject mongoRecord () { @@ -392,9 +392,9 @@ public class DeviceStatus { if (enacted != null) openaps.put("enacted", enacted); if (suggested != null) openaps.put("suggested", suggested); if (iob != null) openaps.put("iob", iob); - if (uploaderBattery != null) openaps.put("uploaderBattery", uploaderBattery); record.put("openaps", openaps); } + if (uploaderBattery != 0) record.put("uploaderBattery", uploaderBattery); if (created_at != null) record.put("created_at" , created_at); } catch (JSONException e) { e.printStackTrace();