From 87fb969d9f62b0a75177ff2798977f5720d58844 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 26 Jun 2017 17:21:39 +0200 Subject: [PATCH] ns openaps status --- .../androidaps/Services/DataService.java | 2 +- .../data}/NSDeviceStatus.java | 94 +++++++++++++++++-- .../plugins/Overview/OverviewFragment.java | 13 ++- 3 files changed, 99 insertions(+), 10 deletions(-) rename app/src/main/java/info/nightscout/{utils => androidaps/plugins/NSClientInternal/data}/NSDeviceStatus.java (67%) 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 79653ebfc0..40cbbf3c52 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -36,7 +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.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; import info.nightscout.utils.SP; diff --git a/app/src/main/java/info/nightscout/utils/NSDeviceStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java similarity index 67% rename from app/src/main/java/info/nightscout/utils/NSDeviceStatus.java rename to app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java index bbbd2fac37..035a59c145 100644 --- a/app/src/main/java/info/nightscout/utils/NSDeviceStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSDeviceStatus.java @@ -1,7 +1,6 @@ -package info.nightscout.utils; +package info.nightscout.androidaps.plugins.NSClientInternal.data; import android.text.Html; -import android.text.Spannable; import android.text.Spanned; import org.json.JSONException; @@ -9,11 +8,10 @@ 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; +import info.nightscout.utils.DateUtil; +import info.nightscout.utils.Round; +import info.nightscout.utils.SP; /** * Created by mike on 25.06.2017. @@ -87,6 +85,7 @@ public class NSDeviceStatus { public NSDeviceStatus setData(JSONObject obj) { this.data = obj; updatePumpData(obj); + updateOpenApsData(obj); return this; } @@ -127,10 +126,10 @@ public class NSDeviceStatus { return Html.fromHtml(""); StringBuilder string = new StringBuilder(); - // test wanring level + // test warning level int level = Levels.INFO; long now = System.currentTimeMillis(); - if (deviceStatusPumpData.clock + NSSettingsStatus.getInstance().extendedPumpSettings("urgentClock") < now) + if (deviceStatusPumpData.clock + NSSettingsStatus.getInstance().extendedPumpSettings("urgentClock") * 60 * 1000L < now) level = Levels.URGENT; else if (deviceStatusPumpData.reservoir < NSSettingsStatus.getInstance().extendedPumpSettings("urgentRes")) level = Levels.URGENT; @@ -235,4 +234,83 @@ public class NSDeviceStatus { } } + + // ********* OpenAPS data *********** + + static DeviceStatusOpenAPSData deviceStatusOpenAPSData = new DeviceStatusOpenAPSData(); + + static class DeviceStatusOpenAPSData { + long clockSuggested = 0L; + long clockEnacted = 0L; + + JSONObject suggested = null; + JSONObject enacted = null; + } + + public void updateOpenApsData(JSONObject object) { + try { + JSONObject openaps = object.has("openaps") ? object.getJSONObject("openaps") : new JSONObject(); + JSONObject suggested = openaps.has("suggested") ? openaps.getJSONObject("suggested") : new JSONObject(); + JSONObject enacted = openaps.has("enacted") ? openaps.getJSONObject("enacted") : new JSONObject(); + + long clock = 0L; + if (suggested.has("timestamp")) + clock = DateUtil.fromISODateString(suggested.getString("timestamp")).getTime(); + // check if this is new data + if (clock != 0 && clock > deviceStatusOpenAPSData.clockSuggested) { + deviceStatusOpenAPSData.suggested = suggested; + deviceStatusOpenAPSData.clockSuggested = clock; + } + + clock = 0L; + if (enacted.has("timestamp")) + clock = DateUtil.fromISODateString(enacted.getString("timestamp")).getTime(); + // check if this is new data + if (clock != 0 && clock > deviceStatusOpenAPSData.clockEnacted) { + deviceStatusOpenAPSData.enacted = enacted; + deviceStatusOpenAPSData.clockEnacted = clock; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Spanned getOpenApsStatus() { + StringBuilder string = new StringBuilder(); + // test warning level + int level = Levels.INFO; + long now = System.currentTimeMillis(); + if (deviceStatusOpenAPSData.clockSuggested != 0 && deviceStatusOpenAPSData.clockSuggested + SP.getInt(R.string.key_nsalarm_urgent_staledatavalue, 16) * 60 * 1000L < now) + level = Levels.URGENT; + else if (deviceStatusOpenAPSData.clockSuggested != 0 && deviceStatusOpenAPSData.clockSuggested + SP.getInt(R.string.key_nsalarm_staledatavalue, 16) * 60 * 1000L < now) + level = Levels.WARN; + + string.append(""); + if (level == Levels.WARN) string.append("yellow\">"); + if (level == Levels.URGENT) string.append("red\">"); + + if (deviceStatusOpenAPSData.clockSuggested != 0) { + string.append(DateUtil.minAgo(deviceStatusOpenAPSData.clockSuggested)).append(" "); + } + string.append(""); // color + + return Html.fromHtml(string.toString()); + } + + public Spanned getExtendedOpenApsStatus() { + StringBuilder string = new StringBuilder(); + + try { + if (deviceStatusOpenAPSData.enacted != null && deviceStatusOpenAPSData.clockEnacted != deviceStatusOpenAPSData.clockSuggested) + string.append("").append(DateUtil.minAgo(deviceStatusOpenAPSData.clockEnacted)).append(" ").append(deviceStatusOpenAPSData.enacted.getString("reason")).append("
"); + if (deviceStatusOpenAPSData.suggested != null) + string.append("").append(DateUtil.minAgo(deviceStatusOpenAPSData.clockSuggested)).append(" ").append(deviceStatusOpenAPSData.suggested.getString("reason")).append("
"); + return Html.fromHtml(string.toString()); + } catch (JSONException e) { + e.printStackTrace(); + } + return Html.fromHtml(""); + } + } 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 3ea3a901fc..aa3c63a79e 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,7 @@ 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.androidaps.plugins.NSClientInternal.data.NSDeviceStatus; import info.nightscout.utils.NSUpload; import info.nightscout.utils.OKDialog; import info.nightscout.utils.Profiler; @@ -1173,6 +1173,17 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, }); } + // OpenAPS status from ns + if (openapsDeviceStatusView != null) { + openapsDeviceStatusView.setText(NSDeviceStatus.getInstance().getOpenApsStatus()); + openapsDeviceStatusView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + OKDialog.show(getActivity(), MainApp.sResources.getString(R.string.openaps), NSDeviceStatus.getInstance().getExtendedOpenApsStatus(), null); + } + }); + } + // ****** GRAPH ******* //log.debug("updateGUI checkpoint 1");