From 47a9d4b9c84e34332a205b9aa653c3118b3b7ce8 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 26 Jun 2017 12:44:03 +0200 Subject: [PATCH] ns pump status --- .../androidaps/Services/DataService.java | 18 +- .../broadcasts/BroadcastStatus.java | 4 +- .../{NSStatus.java => NSSettingsStatus.java} | 106 +++++++- .../services/NSClientService.java | 13 +- .../plugins/Overview/Notification.java | 6 +- .../plugins/Overview/OverviewFragment.java | 17 ++ .../java/info/nightscout/utils/DateUtil.java | 7 + .../info/nightscout/utils/NSDeviceStatus.java | 238 ++++++++++++++++++ .../java/info/nightscout/utils/OKDialog.java | 25 ++ .../res/layout/overview_fragment_tablet.xml | 103 ++++++++ app/src/main/res/values/strings.xml | 3 + 11 files changed, 510 insertions(+), 30 deletions(-) rename app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/{NSStatus.java => NSSettingsStatus.java} (63%) create mode 100644 app/src/main/java/info/nightscout/utils/NSDeviceStatus.java 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 4d0685a00f..79653ebfc0 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -12,8 +12,6 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; - import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -25,7 +23,7 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesPlugin import info.nightscout.androidaps.plugins.NSClientInternal.data.NSMbg; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; import info.nightscout.androidaps.data.ProfileStore; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.Overview.Notification; import info.nightscout.androidaps.plugins.Overview.OverviewPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; @@ -38,6 +36,7 @@ import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.receivers.DataReceiver; +import info.nightscout.utils.NSDeviceStatus; import info.nightscout.utils.SP; @@ -259,11 +258,11 @@ public class DataService extends IntentService { if (bundles.containsKey("status")) { try { JSONObject statusJson = new JSONObject(bundles.getString("status")); - NSStatus.getInstance().setData(statusJson); + NSSettingsStatus.getInstance().setData(statusJson); if (Config.logIncommingData) log.debug("Received status: " + statusJson.toString()); - Double targetHigh = NSStatus.getInstance().getThreshold("bgTargetTop"); - Double targetlow = NSStatus.getInstance().getThreshold("bgTargetBottom"); + Double targetHigh = NSSettingsStatus.getInstance().getThreshold("bgTargetTop"); + Double targetlow = NSSettingsStatus.getInstance().getThreshold("bgTargetBottom"); if (targetHigh != null) OverviewPlugin.bgTargetHigh = targetHigh; if (targetlow != null) @@ -276,8 +275,8 @@ public class DataService extends IntentService { if (intent.getAction().equals(Intents.ACTION_NEW_DEVICESTATUS)) { try { if (bundles.containsKey("devicestatus")) { - String devicestatusesstring = bundles.getString("devicestatus"); JSONObject devicestatusJson = new JSONObject(bundles.getString("devicestatus")); + NSDeviceStatus.getInstance().setData(devicestatusJson); if (devicestatusJson.has("pump")) { // Objectives 0 ObjectivesPlugin.pumpStatusIsAvailableInNS = true; @@ -287,8 +286,9 @@ public class DataService extends IntentService { if (bundles.containsKey("devicestatuses")) { String devicestatusesstring = bundles.getString("devicestatuses"); JSONArray jsonArray = new JSONArray(devicestatusesstring); - if (jsonArray.length() > 0) { - JSONObject devicestatusJson = jsonArray.getJSONObject(0); + 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; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java index b3bd3cfac2..20055aa108 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastStatus.java @@ -13,7 +13,7 @@ import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.Services.Intents; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientService; /** @@ -22,7 +22,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.services.NSClientServ public class BroadcastStatus { private static Logger log = LoggerFactory.getLogger(BroadcastStatus.class); - public static void handleNewStatus(NSStatus status, Context context, boolean isDelta) { + public static void handleNewStatus(NSSettingsStatus status, Context context, boolean isDelta) { Bundle bundle = new Bundle(); try { bundle.putString("nsclientversionname", MainApp.instance().getPackageManager().getPackageInfo(MainApp.instance().getPackageName(), 0).versionName); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java similarity index 63% rename from app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java index e89dee8db5..3201d62f6e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSSettingsStatus.java @@ -97,21 +97,21 @@ import java.util.Date; "activeProfile": "2016 +30%" } */ -public class NSStatus { - private static NSStatus instance = null; +public class NSSettingsStatus { + private static NSSettingsStatus instance = null; - public static NSStatus getInstance() { + public static NSSettingsStatus getInstance() { if (instance == null) - instance = new NSStatus(); + instance = new NSSettingsStatus(); return instance; } private JSONObject data = null; - public NSStatus() { + public NSSettingsStatus() { } - public NSStatus setData(JSONObject obj) { + public NSSettingsStatus setData(JSONObject obj) { this.data = obj; return this; } @@ -152,8 +152,17 @@ public class NSStatus { return getStringOrNull("settings"); } - public String getExtendedSettings() { - return getStringOrNull("extendedSettings"); + public JSONObject getExtendedSettings() { + try { + String extended = getStringOrNull("extendedSettings"); + if (extended != null) + return new JSONObject(extended); + + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } public String getActiveProfile() { @@ -254,10 +263,89 @@ public class NSStatus { return ret; } - ; + // ***** PUMP STATUS ****** public JSONObject getData() { return data; } + /* + , warnClock: sbx.extendedSettings.warnClock || 30 + , urgentClock: sbx.extendedSettings.urgentClock || 60 + , warnRes: sbx.extendedSettings.warnRes || 10 + , urgentRes: sbx.extendedSettings.urgentRes || 5 + , warnBattV: sbx.extendedSettings.warnBattV || 1.35 + , urgentBattV: sbx.extendedSettings.urgentBattV || 1.3 + , warnBattP: sbx.extendedSettings.warnBattP || 30 + , urgentBattP: sbx.extendedSettings.urgentBattP || 20 + , enableAlerts: sbx.extendedSettings.enableAlerts || false + + */ + + public double extendedPumpSettings(String setting) { + try { + JSONObject pump = extentendedPumpSettings(); + switch (setting) { + case "warnClock": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "urgentClock": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "warnRes": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "urgentRes": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "warnBattV": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "urgentBattV": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "warnBattP": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + case "urgentBattP": + return pump != null && pump.has(setting) ? pump.getDouble(setting) : 30; + } + } catch (JSONException e) { + e.printStackTrace(); + } + return 0d; + } + + @Nullable + public JSONObject extentendedPumpSettings() { + try { + JSONObject extended = getExtendedSettings(); + if (extended.has("pump")) { + JSONObject pump = extended.getJSONObject("pump"); + return pump; + } + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + public boolean pumpExtentendedSettingsEnabledAlerts() { + try { + JSONObject pump = extentendedPumpSettings(); + if (pump != null && pump.has("enableAlerts")) { + return pump.getBoolean("enableAlerts"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return false; + } + + public String pumpExtentendedSettingsFields() { + try { + JSONObject pump = extentendedPumpSettings(); + if (pump != null && pump.has("fields")) { + return pump.getString("fields"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return ""; + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java index ed308e2ff0..21f863fe10 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java @@ -51,7 +51,7 @@ import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastT import info.nightscout.androidaps.plugins.NSClientInternal.broadcasts.BroadcastUrgentAlarm; import info.nightscout.androidaps.plugins.NSClientInternal.data.AlarmAck; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSgv; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientRestart; @@ -432,17 +432,17 @@ public class NSClientService extends Service { if (data.has("status")) { JSONObject status = data.getJSONObject("status"); - NSStatus nsStatus = NSStatus.getInstance().setData(status); + NSSettingsStatus nsSettingsStatus = NSSettingsStatus.getInstance().setData(status); if (!status.has("versionNum")) { if (status.getInt("versionNum") < Config.SUPPORTEDNSVERSION) { MainApp.bus().post(new EventNSClientNewLog("ERROR", "Unsupported Nightscout version !!!!")); } } else { - nightscoutVersionName = nsStatus.getVersion(); - nightscoutVersionCode = nsStatus.getVersionNum(); + nightscoutVersionName = nsSettingsStatus.getVersion(); + nightscoutVersionCode = nsSettingsStatus.getVersionNum(); } - BroadcastStatus.handleNewStatus(nsStatus, MainApp.instance().getApplicationContext(), isDelta); + BroadcastStatus.handleNewStatus(nsSettingsStatus, MainApp.instance().getApplicationContext(), isDelta); /* Other received data to 2016/02/10 { @@ -516,8 +516,7 @@ public class NSClientService extends Service { // remove from upload queue if Ack is failing UploadQueue.removeID(jsonStatus); } - // send only last record - BroadcastDeviceStatus.handleNewDeviceStatus(devicestatuses.getJSONObject(devicestatuses.length() - 1), MainApp.instance().getApplicationContext(), isDelta); + BroadcastDeviceStatus.handleNewDeviceStatus(devicestatuses, MainApp.instance().getApplicationContext(), isDelta); } } if (data.has("mbgs")) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index bfa1f6e3f5..6b15a9e17d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -6,7 +6,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSAlarm; -import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.utils.SP; /** @@ -122,7 +122,7 @@ public class Notification { BgReading bgReading = MainApp.getDbHelper().lastBg(); if (bgReading == null) return false; - Double threshold = NSStatus.getInstance().getThreshold("bgTargetTop"); + Double threshold = NSSettingsStatus.getInstance().getThreshold("bgTargetTop"); if (threshold == null) return false; if (bgReading.value <= threshold) @@ -134,7 +134,7 @@ public class Notification { BgReading bgReading = MainApp.getDbHelper().lastBg(); if (bgReading == null) return false; - Double threshold = NSStatus.getInstance().getThreshold("bgTargetBottom"); + Double threshold = NSSettingsStatus.getInstance().getThreshold("bgTargetBottom"); if (threshold == null) return false; if (bgReading.value >= threshold) 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 51b8b67c42..b432cf38d7 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 @@ -117,7 +117,9 @@ import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; +import info.nightscout.utils.NSDeviceStatus; import info.nightscout.utils.NSUpload; +import info.nightscout.utils.OKDialog; import info.nightscout.utils.Profiler; import info.nightscout.utils.Round; import info.nightscout.utils.SP; @@ -147,6 +149,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, TextView apsModeView; TextView tempTargetView; TextView pumpStatusView; + TextView pumpDeviceStatusView; + TextView openapsDeviceStatusView; LinearLayout loopStatusLayout; LinearLayout pumpStatusLayout; GraphView bgGraph; @@ -236,6 +240,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, extendedBolusView = (TextView) view.findViewById(R.id.overview_extendedbolus); activeProfileView = (TextView) view.findViewById(R.id.overview_activeprofile); pumpStatusView = (TextView) view.findViewById(R.id.overview_pumpstatus); + pumpDeviceStatusView = (TextView) view.findViewById(R.id.overview_pump); + openapsDeviceStatusView = (TextView) view.findViewById(R.id.overview_openaps); loopStatusLayout = (LinearLayout) view.findViewById(R.id.overview_looplayout); pumpStatusLayout = (LinearLayout) view.findViewById(R.id.overview_pumpstatuslayout); @@ -1156,6 +1162,17 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, getActivity().findViewById(R.id.overview_showprediction_label).setVisibility(View.GONE); } + // pump status from ns + if (pumpDeviceStatusView != null) { + pumpDeviceStatusView.setText(NSDeviceStatus.getInstance().getPumpStatus()); + pumpStatusView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + OKDialog.show(getActivity(), MainApp.sResources.getString(R.string.pump), NSDeviceStatus.getInstance().getExtendedPumpStatus(), null); + } + }); + } + // ****** GRAPH ******* //log.debug("updateGUI checkpoint 1"); diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index c4efa45f93..a4ee25eb22 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -12,6 +12,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; /** * The Class DateUtil. A simple wrapper around SimpleDateFormat to ease the handling of iso date string <-> date obj @@ -111,4 +112,10 @@ public class DateUtil { public static String dateAndTimeString(long mills) { return dateString(mills) + " " + timeString(mills); } + + public static String minAgo(long time) { + int mins = (int) ((System.currentTimeMillis() - time) / 1000 / 60); + return String.format(MainApp.sResources.getString(R.string.minago), mins); + } + } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/utils/NSDeviceStatus.java b/app/src/main/java/info/nightscout/utils/NSDeviceStatus.java new file mode 100644 index 0000000000..bbbd2fac37 --- /dev/null +++ b/app/src/main/java/info/nightscout/utils/NSDeviceStatus.java @@ -0,0 +1,238 @@ +package info.nightscout.utils; + +import android.text.Html; +import android.text.Spannable; +import android.text.Spanned; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Iterator; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; + +import static android.R.attr.value; + +/** + * Created by mike on 25.06.2017. + */ + +/* +{ + "_id": "594fdcec327b83c81b6b8c0f", + "device": "openaps://Sony D5803", + "pump": { + "battery": { + "percent": 100 + }, + "status": { + "status": "normal", + "timestamp": "2017-06-25T15:50:14Z" + }, + "extended": { + "Version": "1.5-ac98852-2017.06.25", + "PumpIOB": 1.13, + "LastBolus": "25. 6. 2017 17:25:00", + "LastBolusAmount": 0.3, + "BaseBasalRate": 0.4, + "ActiveProfile": "2016 +30%" + }, + "reservoir": 109, + "clock": "2017-06-25T15:55:10Z" + }, + "openaps": { + "suggested": { + "temp": "absolute", + "bg": 115.9, + "tick": "+5", + "eventualBG": 105, + "snoozeBG": 105, + "predBGs": { + "IOB": [116, 114, 112, 110, 109, 107, 106, 105, 105, 104, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 107] + }, + "COB": 0, + "IOB": -0.035, + "reason": "COB: 0, Dev: -18, BGI: 0.43, ISF: 216, Target: 99; Eventual BG 105 > 99 but Min. Delta -2.60 < Exp. Delta 0.1; setting current basal of 0.4 as temp. Suggested rate is same as profile rate, no temp basal is active, doing nothing", + "timestamp": "2017-06-25T15:55:10Z" + }, + "iob": { + "iob": -0.035, + "basaliob": -0.035, + "activity": -0.0004, + "time": "2017-06-25T15:55:10Z" + } + }, + "uploaderBattery": 93, + "created_at": "2017-06-25T15:55:10Z", + "NSCLIENT_ID": 1498406118857 +} + */ +public class NSDeviceStatus { + + private static NSDeviceStatus instance = null; + + public static NSDeviceStatus getInstance() { + if (instance == null) + instance = new NSDeviceStatus(); + return instance; + } + + private JSONObject data = null; + + public NSDeviceStatus() { + } + + public NSDeviceStatus setData(JSONObject obj) { + this.data = obj; + updatePumpData(obj); + return this; + } + + public String getDevice() { + try { + if (data.has("device")) { + return data.getString("device"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return ""; + } + + public static class Levels { + static int URGENT = 2; + static int WARN = 1; + static int INFO = 0; + int LOW = -1; + int LOWEST = -2; + static int NONE = -3; + } + + // ***** PUMP DATA ****** + + static DeviceStatusPumpData deviceStatusPumpData = null; + + public Spanned getExtendedPumpStatus() { + if (deviceStatusPumpData.extended != null) + return deviceStatusPumpData.extended; + return Html.fromHtml(""); + } + + public Spanned getPumpStatus() { + //String[] ALL_STATUS_FIELDS = {"reservoir", "battery", "clock", "status", "device"}; + + if (deviceStatusPumpData == null) + return Html.fromHtml(""); + + StringBuilder string = new StringBuilder(); + // test wanring level + int level = Levels.INFO; + long now = System.currentTimeMillis(); + if (deviceStatusPumpData.clock + NSSettingsStatus.getInstance().extendedPumpSettings("urgentClock") < now) + level = Levels.URGENT; + else if (deviceStatusPumpData.reservoir < NSSettingsStatus.getInstance().extendedPumpSettings("urgentRes")) + level = Levels.URGENT; + else if (deviceStatusPumpData.isPercent && deviceStatusPumpData.percent < NSSettingsStatus.getInstance().extendedPumpSettings("urgentBattP")) + level = Levels.URGENT; + else if (!deviceStatusPumpData.isPercent && deviceStatusPumpData.voltage < NSSettingsStatus.getInstance().extendedPumpSettings("urgentBattV")) + level = Levels.URGENT; + else if (deviceStatusPumpData.clock + NSSettingsStatus.getInstance().extendedPumpSettings("warnClock") < now) + level = Levels.WARN; + else if (deviceStatusPumpData.reservoir < NSSettingsStatus.getInstance().extendedPumpSettings("warnRes")) + level = Levels.WARN; + else if (deviceStatusPumpData.isPercent && deviceStatusPumpData.percent < NSSettingsStatus.getInstance().extendedPumpSettings("warnBattP")) + level = Levels.WARN; + else if (!deviceStatusPumpData.isPercent && deviceStatusPumpData.voltage < NSSettingsStatus.getInstance().extendedPumpSettings("warnBattV")) + level = Levels.WARN; + + string.append(""); + if (level == Levels.WARN) string.append("yellow\">"); + if (level == Levels.URGENT) string.append("red\">"); + + String fields = NSSettingsStatus.getInstance().pumpExtentendedSettingsFields(); + + if (fields.contains("reservoir")) { + string.append((int) deviceStatusPumpData.reservoir).append("U "); + } + + if (fields.contains("battery") && deviceStatusPumpData.isPercent) { + string.append(deviceStatusPumpData.percent).append("% "); + } + if (fields.contains("battery") && !deviceStatusPumpData.isPercent) { + string.append(Round.roundTo(deviceStatusPumpData.voltage, 0.001d)).append(" "); + } + + if (fields.contains("clock")) { + string.append(DateUtil.minAgo(deviceStatusPumpData.clock)).append(" "); + } + + if (fields.contains("status")) { + string.append(deviceStatusPumpData.status).append(" "); + } + + if (fields.contains("device")) { + string.append(getDevice()).append(" "); + } + + + string.append(""); // color + + return Html.fromHtml(string.toString()); + } + + static class DeviceStatusPumpData { + long clock = 0L; + boolean isPercent = false; + int percent = 0; + double voltage = 0; + + String status = "N/A"; + double reservoir = 0d; + + Spanned extended = null; + } + + public void updatePumpData(JSONObject object) { + try { + JSONObject pump = data != null && data.has("pump") ? data.getJSONObject("pump") : new JSONObject(); + + long clock = 0L; + if (pump.has("clock")) + clock = DateUtil.fromISODateString(pump.getString("clock")).getTime(); + // check if this is new data + if (clock == 0 || deviceStatusPumpData != null && clock < deviceStatusPumpData.clock) + return; + // create new status and process data + deviceStatusPumpData = new DeviceStatusPumpData(); + deviceStatusPumpData.clock = clock; + if (pump.has("status") && pump.getJSONObject("status").has("status")) + deviceStatusPumpData.status = pump.getJSONObject("status").getString("status"); + if (pump.has("reservoir")) + deviceStatusPumpData.reservoir = pump.getDouble("reservoir"); + if (pump.has("battery") && pump.getJSONObject("battery").has("percent")) { + deviceStatusPumpData.isPercent = true; + deviceStatusPumpData.percent = pump.getJSONObject("battery").getInt("percent"); + } else if (pump.has("battery") && pump.getJSONObject("battery").has("voltage")) { + deviceStatusPumpData.isPercent = false; + deviceStatusPumpData.voltage = pump.getJSONObject("battery").getDouble("voltage"); + } + if (pump.has("extended")) { + JSONObject extendedJson = pump.getJSONObject("extended"); + StringBuilder exteneded = new StringBuilder(); + Iterator keys = extendedJson.keys(); + while (keys.hasNext()) { + String key = (String) keys.next(); + String value = extendedJson.getString(key); + exteneded.append("").append(key).append(": ").append(value).append("
"); + } + deviceStatusPumpData.extended = Html.fromHtml(exteneded.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/app/src/main/java/info/nightscout/utils/OKDialog.java b/app/src/main/java/info/nightscout/utils/OKDialog.java index 19fbe7c62a..72e0ddd8d2 100644 --- a/app/src/main/java/info/nightscout/utils/OKDialog.java +++ b/app/src/main/java/info/nightscout/utils/OKDialog.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.view.ContextThemeWrapper; +import android.text.Spanned; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,4 +42,28 @@ public class OKDialog { log.debug("show_dialog exception: " + e); } } + + public static void show(final Activity activity, String title, Spanned message, final Runnable runnable) { + try { + AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme)); + builder.setTitle(title); + builder.setMessage(message); + builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + if (runnable != null) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + activity.runOnUiThread(runnable); + } + } + }); + + builder.create().show(); + } catch (Exception e) { + log.debug("show_dialog exception: " + e); + } + } } diff --git a/app/src/main/res/layout/overview_fragment_tablet.xml b/app/src/main/res/layout/overview_fragment_tablet.xml index 6f71242ce5..933371b337 100644 --- a/app/src/main/res/layout/overview_fragment_tablet.xml +++ b/app/src/main/res/layout/overview_fragment_tablet.xml @@ -400,6 +400,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Urgent stale data Stale data threshold [min] Urgent stale data threshold [min] + Pump + OpenAPS + Device