From d84632a8a482a91f2289bfc0f562aa046cb44a08 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 20 Feb 2017 08:32:10 +0100 Subject: [PATCH] uploader battery in device status --- .../ConfigBuilder/ConfigBuilderPlugin.java | 9 ++++++ .../androidaps/plugins/Loop/DeviceStatus.java | 2 ++ .../info/nightscout/utils/BatteryLevel.java | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 app/src/main/java/info/nightscout/utils/BatteryLevel.java 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 7f4a60113a..a10c182ad4 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 @@ -56,6 +56,7 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotificati import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.NSClientInternal.data.DbLogger; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSProfile; +import info.nightscout.utils.BatteryLevel; import info.nightscout.utils.DateUtil; /** @@ -976,6 +977,14 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain deviceStatus.pump = getJSONStatus(); } } + + int batteryLevel = BatteryLevel.getBatteryLevel(); + if (batteryLevel != BatteryLevel.lastUploadedLevel) { + JSONObject uploaderBattery = new JSONObject(); + uploaderBattery.put("uploaderBattery", batteryLevel); + deviceStatus.uploaderBattery = uploaderBattery; + BatteryLevel.lastUploadedLevel = batteryLevel; + } deviceStatus.created_at = DateUtil.toISOString(new Date()); deviceStatus.sendToNSClient(); } catch (JSONException e) { 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 77e1d322c5..0d4f5b423f 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,6 +378,7 @@ public class DeviceStatus { public JSONObject enacted = null; public JSONObject suggested = null; public JSONObject iob = null; + public JSONObject uploaderBattery = null; public String created_at = null; public JSONObject mongoRecord () { @@ -391,6 +392,7 @@ 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 (created_at != null) record.put("created_at" , created_at); diff --git a/app/src/main/java/info/nightscout/utils/BatteryLevel.java b/app/src/main/java/info/nightscout/utils/BatteryLevel.java new file mode 100644 index 0000000000..44f6b6e0e5 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/BatteryLevel.java @@ -0,0 +1,28 @@ +package info.nightscout.utils; + +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; + +import info.nightscout.androidaps.MainApp; + +/** + * Created by mike on 20.02.2017. + */ + +public class BatteryLevel { + static public int lastUploadedLevel = 0; + + static public int getBatteryLevel() { + 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; + } + return (int) (((float) level / (float) scale) * 100.0f); + } else return 50; + } + +}