show predictions in nsclient mode

This commit is contained in:
Milos Kozak 2018-06-27 20:13:25 +02:00
parent f7f4385d5d
commit 29032e658f
5 changed files with 57 additions and 31 deletions

View file

@ -10,7 +10,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.MainApp;
@ -28,7 +27,7 @@ import info.nightscout.utils.DecimalFormatter;
public class APSResult {
private static Logger log = LoggerFactory.getLogger(APSResult.class);
public Date date;
public long date = 0;
public String reason;
public double rate;
public int duration;
@ -133,8 +132,8 @@ public class APSResult {
public List<BgReading> getPredictions() {
List<BgReading> array = new ArrayList<>();
try {
long startTime = date.getTime();
if (json.has("predBGs")) {
long startTime = date;
if (json != null && json.has("predBGs")) {
JSONObject predBGs = json.getJSONObject("predBGs");
if (predBGs.has("IOB")) {
JSONArray iob = predBGs.getJSONArray("IOB");
@ -196,8 +195,8 @@ public class APSResult {
public long getLatestPredictionsTime() {
long latest = 0;
try {
long startTime = date != null ? date.getTime() : 0;
if (json.has("predBGs")) {
long startTime = date;
if (json != null && json.has("predBGs")) {
JSONObject predBGs = json.getJSONObject("predBGs");
if (predBGs.has("IOB")) {
JSONArray iob = predBGs.getJSONArray("IOB");

View file

@ -13,6 +13,7 @@ import java.util.Iterator;
import java.util.Map;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.Round;
import info.nightscout.utils.SP;
@ -374,7 +375,7 @@ public class NSDeviceStatus {
public String getUploaderStatus() {
Iterator iter = uploaders.entrySet().iterator();
int minBattery = 100;
while(iter.hasNext()) {
while (iter.hasNext()) {
Map.Entry pair = (Map.Entry) iter.next();
Uploader uploader = (Uploader) pair.getValue();
if (minBattery > uploader.battery)
@ -388,7 +389,7 @@ public class NSDeviceStatus {
StringBuilder string = new StringBuilder();
Iterator iter = uploaders.entrySet().iterator();
while(iter.hasNext()) {
while (iter.hasNext()) {
Map.Entry pair = (Map.Entry) iter.next();
Uploader uploader = (Uploader) pair.getValue();
String device = (String) pair.getKey();
@ -398,4 +399,11 @@ public class NSDeviceStatus {
return Html.fromHtml(string.toString());
}
public static APSResult getAPSResult() {
APSResult result = new APSResult();
result.json = deviceStatusOpenAPSData.suggested;
result.date = deviceStatusOpenAPSData.clockSuggested;
return result;
}
}

View file

@ -6,19 +6,18 @@ import org.mozilla.javascript.NativeObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil;
public class DetermineBasalResultAMA extends APSResult {
private static Logger log = LoggerFactory.getLogger(DetermineBasalResultAMA.class);
public double eventualBG;
public double snoozeBG;
private double eventualBG;
private double snoozeBG;
public DetermineBasalResultAMA(NativeObject result, JSONObject j) {
DetermineBasalResultAMA(NativeObject result, JSONObject j) {
this();
date = new Date();
date = DateUtil.now();
json = j;
if (result.containsKey("error")) {
reason = result.get("error").toString();

View file

@ -5,22 +5,20 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil;
public class DetermineBasalResultSMB extends APSResult {
private static final Logger log = LoggerFactory.getLogger(DetermineBasalResultSMB.class);
public double eventualBG;
public double snoozeBG;
public double insulinReq;
public double carbsReq;
private double eventualBG;
private double snoozeBG;
//public double insulinReq;
//public double carbsReq;
public DetermineBasalResultSMB(JSONObject result) {
DetermineBasalResultSMB(JSONObject result) {
this();
date = new Date();
date = DateUtil.now();
json = result;
try {
if (result.has("error")) {
@ -31,8 +29,8 @@ public class DetermineBasalResultSMB extends APSResult {
reason = result.getString("reason");
if (result.has("eventualBG")) eventualBG = result.getDouble("eventualBG");
if (result.has("snoozeBG")) snoozeBG = result.getDouble("snoozeBG");
if (result.has("insulinReq")) insulinReq = result.getDouble("insulinReq");
if (result.has("carbsReq")) carbsReq = result.getDouble("carbsReq");
//if (result.has("insulinReq")) insulinReq = result.getDouble("insulinReq");
//if (result.has("carbsReq")) carbsReq = result.getDouble("carbsReq");
if (result.has("rate") && result.has("duration")) {
tempBasalRequested = true;
@ -64,7 +62,7 @@ public class DetermineBasalResultSMB extends APSResult {
}
}
public DetermineBasalResultSMB() {
private DetermineBasalResultSMB() {
hasPredictions = true;
}

View file

@ -90,6 +90,7 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.CobInfo;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventIobCalculationProgress;
import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
import info.nightscout.androidaps.plugins.NSClientInternal.data.NSDeviceStatus;
@ -343,7 +344,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
@Override
public void onClick(View v) {
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
final boolean predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
boolean predictionsAvailable;
if (Config.APS)
predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
else if (Config.NSCLIENT)
predictionsAvailable = true;
else
predictionsAvailable = false;
MenuItem item;
CharSequence title;
@ -1355,7 +1362,15 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
cobView.setText(cobText);
}
final boolean predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
boolean predictionsAvailable;
if (Config.APS)
predictionsAvailable = finalLastRun != null && finalLastRun.request.hasPredictions;
else if (Config.NSCLIENT)
predictionsAvailable = true;
else
predictionsAvailable = false;
final boolean finalPredictionsAvailable = predictionsAvailable;
// pump status from ns
if (pumpDeviceStatusView != null) {
pumpDeviceStatusView.setText(NSDeviceStatus.getInstance().getPumpStatus());
@ -1398,8 +1413,15 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
final long toTime;
final long fromTime;
final long endTime;
if (predictionsAvailable && SP.getBoolean("showprediction", false)) {
int predHours = (int) (Math.ceil(finalLastRun.constraintsProcessed.getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000));
APSResult apsResult;
if (Config.APS)
apsResult = finalLastRun.constraintsProcessed;
else
apsResult = NSDeviceStatus.getAPSResult();
if (finalPredictionsAvailable && SP.getBoolean("showprediction", false)) {
int predHours = (int) (Math.ceil(apsResult.getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000));
predHours = Math.min(2, predHours);
predHours = Math.max(0, predHours);
hoursToFetch = rangeToDisplay - predHours;
@ -1425,9 +1447,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
graphData.addInRangeArea(fromTime, endTime, lowLine, highLine);
// **** BG ****
if (predictionsAvailable && SP.getBoolean("showprediction", false))
if (finalPredictionsAvailable && SP.getBoolean("showprediction", false))
graphData.addBgReadings(fromTime, toTime, lowLine, highLine,
finalLastRun.constraintsProcessed.getPredictions());
apsResult.getPredictions());
else
graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null);