ns openaps status

This commit is contained in:
Milos Kozak 2017-06-26 17:21:39 +02:00
parent 36b83e4c8a
commit 87fb969d9f
3 changed files with 99 additions and 10 deletions

View file

@ -36,7 +36,7 @@ import info.nightscout.androidaps.plugins.SourceMM640g.SourceMM640gPlugin;
import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin; import info.nightscout.androidaps.plugins.SourceNSClient.SourceNSClientPlugin;
import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.androidaps.receivers.DataReceiver;
import info.nightscout.utils.NSDeviceStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;

View file

@ -1,7 +1,6 @@
package info.nightscout.utils; package info.nightscout.androidaps.plugins.NSClientInternal.data;
import android.text.Html; import android.text.Html;
import android.text.Spannable;
import android.text.Spanned; import android.text.Spanned;
import org.json.JSONException; import org.json.JSONException;
@ -9,11 +8,10 @@ import org.json.JSONObject;
import java.util.Iterator; import java.util.Iterator;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSSettingsStatus; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.Round;
import static android.R.attr.value; import info.nightscout.utils.SP;
/** /**
* Created by mike on 25.06.2017. * Created by mike on 25.06.2017.
@ -87,6 +85,7 @@ public class NSDeviceStatus {
public NSDeviceStatus setData(JSONObject obj) { public NSDeviceStatus setData(JSONObject obj) {
this.data = obj; this.data = obj;
updatePumpData(obj); updatePumpData(obj);
updateOpenApsData(obj);
return this; return this;
} }
@ -127,10 +126,10 @@ public class NSDeviceStatus {
return Html.fromHtml(""); return Html.fromHtml("");
StringBuilder string = new StringBuilder(); StringBuilder string = new StringBuilder();
// test wanring level // test warning level
int level = Levels.INFO; int level = Levels.INFO;
long now = System.currentTimeMillis(); 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; level = Levels.URGENT;
else if (deviceStatusPumpData.reservoir < NSSettingsStatus.getInstance().extendedPumpSettings("urgentRes")) else if (deviceStatusPumpData.reservoir < NSSettingsStatus.getInstance().extendedPumpSettings("urgentRes"))
level = Levels.URGENT; 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("<span style=\"color:");
if (level == Levels.INFO) string.append("white\">");
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("</span>"); // color
return Html.fromHtml(string.toString());
}
public Spanned getExtendedOpenApsStatus() {
StringBuilder string = new StringBuilder();
try {
if (deviceStatusOpenAPSData.enacted != null && deviceStatusOpenAPSData.clockEnacted != deviceStatusOpenAPSData.clockSuggested)
string.append("<b>").append(DateUtil.minAgo(deviceStatusOpenAPSData.clockEnacted)).append("</b> ").append(deviceStatusOpenAPSData.enacted.getString("reason")).append("<br>");
if (deviceStatusOpenAPSData.suggested != null)
string.append("<b>").append(DateUtil.minAgo(deviceStatusOpenAPSData.clockSuggested)).append("</b> ").append(deviceStatusOpenAPSData.suggested.getString("reason")).append("<br>");
return Html.fromHtml(string.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return Html.fromHtml("");
}
} }

View file

@ -117,7 +117,7 @@ import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin;
import info.nightscout.utils.BolusWizard; import info.nightscout.utils.BolusWizard;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; 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.NSUpload;
import info.nightscout.utils.OKDialog; import info.nightscout.utils.OKDialog;
import info.nightscout.utils.Profiler; 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 ******* // ****** GRAPH *******
//log.debug("updateGUI checkpoint 1"); //log.debug("updateGUI checkpoint 1");