APS logging

This commit is contained in:
Milos Kozak 2018-07-29 11:10:06 +02:00
parent 5ef9bc7948
commit ce9e5b9165
20 changed files with 298 additions and 354 deletions

View file

@ -24,7 +24,6 @@ public class Config {
public static boolean logFunctionCalls = true; public static boolean logFunctionCalls = true;
public static boolean logAPSResult = true;
public static boolean logPrefsChange = true; public static boolean logPrefsChange = true;
public static boolean logConfigBuilder = true; public static boolean logConfigBuilder = true;
public static boolean logCongigBuilderActions = true; public static boolean logCongigBuilderActions = true;
@ -44,5 +43,6 @@ public class Config {
public static boolean logPump = true; public static boolean logPump = true;
public static boolean logPumpComm = true; public static boolean logPumpComm = true;
public static boolean logPumpBtComm = false; public static boolean logPumpBtComm = false;
public static boolean logAps = true;
} }

View file

@ -84,4 +84,5 @@ public class Constants {
public static final String PUMPQUEUE = "PUMPQUEUE"; public static final String PUMPQUEUE = "PUMPQUEUE";
public static final String PUMPCOMM = "PUMPCOMM"; public static final String PUMPCOMM = "PUMPCOMM";
public static final String PUMPBTCOMM = "PUMPBTCOMM"; public static final String PUMPBTCOMM = "PUMPBTCOMM";
public static final String APS = "APS";
} }

View file

@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
@ -25,7 +26,7 @@ import info.nightscout.utils.DecimalFormatter;
* Created by mike on 09.06.2016. * Created by mike on 09.06.2016.
*/ */
public class APSResult { public class APSResult {
private static Logger log = LoggerFactory.getLogger(APSResult.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
public long date = 0; public long date = 0;
public String reason; public String reason;

View file

@ -5,6 +5,8 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
/* /*
{ {
"_id": "576cfd15217b0bed77d63641", "_id": "576cfd15217b0bed77d63641",
@ -363,7 +365,7 @@ import org.slf4j.LoggerFactory;
*/ */
public class DeviceStatus { public class DeviceStatus {
private static Logger log = LoggerFactory.getLogger(DeviceStatus.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
public String device = null; public String device = null;
public JSONObject pump = null; public JSONObject pump = null;
@ -373,12 +375,12 @@ public class DeviceStatus {
public int uploaderBattery = 0; public int uploaderBattery = 0;
public String created_at = null; public String created_at = null;
public JSONObject mongoRecord () { public JSONObject mongoRecord() {
JSONObject record = new JSONObject(); JSONObject record = new JSONObject();
try { try {
if (device != null) record.put("device" , device); if (device != null) record.put("device", device);
if (pump != null) record.put("pump" , pump); if (pump != null) record.put("pump", pump);
if (suggested != null) { if (suggested != null) {
JSONObject openaps = new JSONObject(); JSONObject openaps = new JSONObject();
if (enacted != null) openaps.put("enacted", enacted); if (enacted != null) openaps.put("enacted", enacted);
@ -387,7 +389,7 @@ public class DeviceStatus {
record.put("openaps", openaps); record.put("openaps", openaps);
} }
if (uploaderBattery != 0) record.put("uploaderBattery", uploaderBattery); if (uploaderBattery != 0) record.put("uploaderBattery", uploaderBattery);
if (created_at != null) record.put("created_at" , created_at); if (created_at != null) record.put("created_at", created_at);
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }

View file

@ -28,8 +28,6 @@ import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
public class LoopFragment extends SubscriberFragment { public class LoopFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(LoopFragment.class);
@BindView(R.id.loop_run) @BindView(R.id.loop_run)
Button runNowButton; Button runNowButton;
@BindView(R.id.loop_lastrun) @BindView(R.id.loop_lastrun)

View file

@ -43,24 +43,24 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.events.EventAutosensC
import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui; import info.nightscout.androidaps.plugins.Loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui; import info.nightscout.androidaps.plugins.Loop.events.EventLoopUpdateGui;
import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification; import info.nightscout.androidaps.plugins.Loop.events.EventNewOpenLoopNotification;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.queue.commands.Command;
import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.FabricPrivacy;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
/** /**
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class LoopPlugin extends PluginBase { public class LoopPlugin extends PluginBase {
private static Logger log = LoggerFactory.getLogger(LoopPlugin.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
public static final String CHANNEL_ID = "AndroidAPS-Openloop"; private static final String CHANNEL_ID = "AndroidAPS-Openloop";
long lastBgTriggeredRun = 0; private long lastBgTriggeredRun = 0;
protected static LoopPlugin loopPlugin; private static LoopPlugin loopPlugin;
@NonNull @NonNull
public static LoopPlugin getPlugin() { public static LoopPlugin getPlugin() {
@ -131,15 +131,15 @@ public class LoopPlugin extends PluginBase {
PumpInterface pump = ConfigBuilderPlugin.getActivePump(); PumpInterface pump = ConfigBuilderPlugin.getActivePump();
return pump == null || pump.getPumpDescription().isTempBasalCapable; return pump == null || pump.getPumpDescription().isTempBasalCapable;
} }
/** /**
* This method is triggered once autosens calculation has completed, so the LoopPlugin * This method is triggered once autosens calculation has completed, so the LoopPlugin
* has current data to work with. However, autosens calculation can be triggered by multiple * has current data to work with. However, autosens calculation can be triggered by multiple
* sources and currently only a new BG should trigger a loop run. Hence we return early if * sources and currently only a new BG should trigger a loop run. Hence we return early if
* the event causing the calculation is not EventNewBg. * the event causing the calculation is not EventNewBg.
* * <p>
* Callers of {@link info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin#runCalculation(String, long, boolean, Event)} * Callers of {@link info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin#runCalculation(String, long, boolean, Event)}
* are sources triggering a calculation which triggers this method upon completion. * are sources triggering a calculation which triggers this method upon completion.
*/ */
@Subscribe @Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) { public void onStatusEvent(final EventAutosensCalculationFinished ev) {
@ -248,19 +248,20 @@ public class LoopPlugin extends PluginBase {
return isDisconnected; return isDisconnected;
} }
public synchronized void invoke(String initiator, boolean allowNotification){ public synchronized void invoke(String initiator, boolean allowNotification) {
invoke(initiator, allowNotification, false); invoke(initiator, allowNotification, false);
} }
public synchronized void invoke(String initiator, boolean allowNotification, boolean tempBasalFallback) { public synchronized void invoke(String initiator, boolean allowNotification, boolean tempBasalFallback) {
try { try {
if (Config.logFunctionCalls) if (Config.logAps)
log.debug("invoke from " + initiator); log.debug("invoke from " + initiator);
Constraint<Boolean> loopEnabled = MainApp.getConstraintChecker().isLoopInvokationAllowed(); Constraint<Boolean> loopEnabled = MainApp.getConstraintChecker().isLoopInvokationAllowed();
if (!loopEnabled.value()) { if (!loopEnabled.value()) {
String message = MainApp.gs(R.string.loopdisabled) + "\n" + loopEnabled.getReasons(); String message = MainApp.gs(R.string.loopdisabled) + "\n" + loopEnabled.getReasons();
log.debug(message); if (Config.logAps)
log.debug(message);
MainApp.bus().post(new EventLoopSetLastRunGui(message)); MainApp.bus().post(new EventLoopSetLastRunGui(message));
return; return;
} }
@ -273,7 +274,8 @@ public class LoopPlugin extends PluginBase {
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
if (!MainApp.getConfigBuilder().isProfileValid("Loop")) { if (!MainApp.getConfigBuilder().isProfileValid("Loop")) {
log.debug(MainApp.gs(R.string.noprofileselected)); if (Config.logAps)
log.debug(MainApp.gs(R.string.noprofileselected));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.noprofileselected))); MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.noprofileselected)));
return; return;
} }
@ -303,7 +305,8 @@ public class LoopPlugin extends PluginBase {
// safety check for multiple SMBs // safety check for multiple SMBs
long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime(); long lastBolusTime = TreatmentsPlugin.getPlugin().getLastBolusTime();
if (lastBolusTime != 0 && lastBolusTime + 3 * 60 * 1000 > System.currentTimeMillis()) { if (lastBolusTime != 0 && lastBolusTime + 3 * 60 * 1000 > System.currentTimeMillis()) {
log.debug("SMB requsted but still in 3 min interval"); if (Config.logAps)
log.debug("SMB requsted but still in 3 min interval");
resultAfterConstraints.smb = 0; resultAfterConstraints.smb = 0;
} }
@ -318,13 +321,15 @@ public class LoopPlugin extends PluginBase {
NSUpload.uploadDeviceStatus(); NSUpload.uploadDeviceStatus();
if (isSuspended()) { if (isSuspended()) {
log.debug(MainApp.gs(R.string.loopsuspended)); if (Config.logAps)
log.debug(MainApp.gs(R.string.loopsuspended));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.loopsuspended))); MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.loopsuspended)));
return; return;
} }
if (pump.isSuspended()) { if (pump.isSuspended()) {
log.debug(MainApp.gs(R.string.pumpsuspended)); if (Config.logAps)
log.debug(MainApp.gs(R.string.pumpsuspended));
MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.pumpsuspended))); MainApp.bus().post(new EventLoopSetLastRunGui(MainApp.gs(R.string.pumpsuspended)));
return; return;
} }
@ -411,7 +416,7 @@ public class LoopPlugin extends PluginBase {
MainApp.bus().post(new EventLoopUpdateGui()); MainApp.bus().post(new EventLoopUpdateGui());
} finally { } finally {
if (Config.logFunctionCalls) if (Config.logAps)
log.debug("invoke end"); log.debug("invoke end");
} }
} }

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function; import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJSON; import org.mozilla.javascript.NativeJSON;
@ -35,7 +34,7 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
public class DetermineBasalAdapterAMAJS { public class DetermineBasalAdapterAMAJS {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterAMAJS.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private ScriptReader mScriptReader = null; private ScriptReader mScriptReader = null;
@ -56,23 +55,24 @@ public class DetermineBasalAdapterAMAJS {
private String scriptDebug = ""; private String scriptDebug = "";
public DetermineBasalAdapterAMAJS(ScriptReader scriptReader) throws IOException { public DetermineBasalAdapterAMAJS(ScriptReader scriptReader) {
mScriptReader = scriptReader; mScriptReader = scriptReader;
} }
public DetermineBasalResultAMA invoke() { public DetermineBasalResultAMA invoke() {
if (Config.logAps) {
log.debug(">>> Invoking detemine_basal <<<"); log.debug(">>> Invoking detemine_basal <<<");
log.debug("Glucose status: " + (storedGlucoseStatus = mGlucoseStatus.toString())); log.debug("Glucose status: " + (storedGlucoseStatus = mGlucoseStatus.toString()));
log.debug("IOB data: " + (storedIobData = mIobData.toString())); log.debug("IOB data: " + (storedIobData = mIobData.toString()));
log.debug("Current temp: " + (storedCurrentTemp = mCurrentTemp.toString())); log.debug("Current temp: " + (storedCurrentTemp = mCurrentTemp.toString()));
log.debug("Profile: " + (storedProfile = mProfile.toString())); log.debug("Profile: " + (storedProfile = mProfile.toString()));
log.debug("Meal data: " + (storedMeal_data = mMealData.toString())); log.debug("Meal data: " + (storedMeal_data = mMealData.toString()));
if (mAutosensData != null) if (mAutosensData != null)
log.debug("Autosens data: " + (storedAutosens_data = mAutosensData.toString())); log.debug("Autosens data: " + (storedAutosens_data = mAutosensData.toString()));
else else
log.debug("Autosens data: " + (storedAutosens_data = "undefined")); log.debug("Autosens data: " + (storedAutosens_data = "undefined"));
}
DetermineBasalResultAMA determineBasalResultAMA = null; DetermineBasalResultAMA determineBasalResultAMA = null;
@ -119,7 +119,7 @@ public class DetermineBasalAdapterAMAJS {
// Parse the jsResult object to a JSON-String // Parse the jsResult object to a JSON-String
String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString(); String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();
if (Config.logAPSResult) if (Config.logAps)
log.debug("Result: " + result); log.debug("Result: " + result);
try { try {
determineBasalResultAMA = new DetermineBasalResultAMA(jsResult, new JSONObject(result)); determineBasalResultAMA = new DetermineBasalResultAMA(jsResult, new JSONObject(result));
@ -127,17 +127,13 @@ public class DetermineBasalAdapterAMAJS {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
} else { } else {
log.debug("Problem loading JS Functions"); log.error("Problem loading JS Functions");
} }
} catch (IOException e) { } catch (IOException e) {
log.debug("IOException"); log.error("IOException");
} catch (RhinoException e) { } catch (RhinoException e) {
log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString()); log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString());
} catch (IllegalAccessException e) { } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
log.error(e.toString());
} catch (InstantiationException e) {
log.error(e.toString());
} catch (InvocationTargetException e) {
log.error(e.toString()); log.error(e.toString());
} finally { } finally {
Context.exit(); Context.exit();
@ -214,7 +210,7 @@ public class DetermineBasalAdapterAMAJS {
mProfile.put("temptargetSet", tempTargetSet); mProfile.put("temptargetSet", tempTargetSet);
mProfile.put("autosens_adjust_targets", SP.getBoolean(R.string.key_openapsama_autosens_adjusttargets, true)); mProfile.put("autosens_adjust_targets", SP.getBoolean(R.string.key_openapsama_autosens_adjusttargets, true));
//align with max-absorption model in AMA sensitivity //align with max-absorption model in AMA sensitivity
if(mealData.usedMinCarbsImpact > 0){ if (mealData.usedMinCarbsImpact > 0) {
mProfile.put("min_5m_carbimpact", mealData.usedMinCarbsImpact); mProfile.put("min_5m_carbimpact", mealData.usedMinCarbsImpact);
} else { } else {
mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact)); mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact));
@ -265,31 +261,21 @@ public class DetermineBasalAdapterAMAJS {
} }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) { private Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
if (jsonObject == null) return Undefined.instance; if (jsonObject == null) return Undefined.instance;
Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), (context, scriptable, scriptable1, objects) -> objects[1]);
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param; return param;
} }
public Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) { private Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) {
//Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() { //Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() {
Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), (context, scriptable, scriptable1, objects) -> objects[1]);
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param; return param;
} }
public String readFile(String filename) throws IOException { private String readFile(String filename) throws IOException {
byte[] bytes = mScriptReader.readFile(filename); byte[] bytes = mScriptReader.readFile(filename);
String string = new String(bytes, "UTF-8"); String string = new String(bytes, "UTF-8");
if (string.startsWith("#!/usr/bin/env node")) { if (string.startsWith("#!/usr/bin/env node")) {

View file

@ -6,11 +6,12 @@ import org.mozilla.javascript.NativeObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
public class DetermineBasalResultAMA extends APSResult { public class DetermineBasalResultAMA extends APSResult {
private static Logger log = LoggerFactory.getLogger(DetermineBasalResultAMA.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private double eventualBG; private double eventualBG;
private double snoozeBG; private double snoozeBG;
@ -47,7 +48,7 @@ public class DetermineBasalResultAMA extends APSResult {
bolusRequested = false; bolusRequested = false;
} }
public DetermineBasalResultAMA() { private DetermineBasalResultAMA() {
hasPredictions = true; hasPredictions = true;
} }

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.OpenAPSAMA;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -17,6 +16,7 @@ import org.json.JSONException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
@ -26,7 +26,7 @@ import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.JSONFormatter; import info.nightscout.utils.JSONFormatter;
public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnClickListener { public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(OpenAPSAMAFragment.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
Button run; Button run;
TextView lastRunView; TextView lastRunView;
@ -43,29 +43,23 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
try { View view = inflater.inflate(R.layout.openapsama_fragment, container, false);
View view = inflater.inflate(R.layout.openapsama_fragment, container, false);
run = (Button) view.findViewById(R.id.openapsma_run); run = (Button) view.findViewById(R.id.openapsma_run);
run.setOnClickListener(this); run.setOnClickListener(this);
lastRunView = (TextView) view.findViewById(R.id.openapsma_lastrun); lastRunView = (TextView) view.findViewById(R.id.openapsma_lastrun);
glucoseStatusView = (TextView) view.findViewById(R.id.openapsma_glucosestatus); glucoseStatusView = (TextView) view.findViewById(R.id.openapsma_glucosestatus);
currentTempView = (TextView) view.findViewById(R.id.openapsma_currenttemp); currentTempView = (TextView) view.findViewById(R.id.openapsma_currenttemp);
iobDataView = (TextView) view.findViewById(R.id.openapsma_iobdata); iobDataView = (TextView) view.findViewById(R.id.openapsma_iobdata);
profileView = (TextView) view.findViewById(R.id.openapsma_profile); profileView = (TextView) view.findViewById(R.id.openapsma_profile);
mealDataView = (TextView) view.findViewById(R.id.openapsma_mealdata); mealDataView = (TextView) view.findViewById(R.id.openapsma_mealdata);
autosensDataView = (TextView) view.findViewById(R.id.openapsma_autosensdata); autosensDataView = (TextView) view.findViewById(R.id.openapsma_autosensdata);
scriptdebugView = (TextView) view.findViewById(R.id.openapsma_scriptdebugdata); scriptdebugView = (TextView) view.findViewById(R.id.openapsma_scriptdebugdata);
resultView = (TextView) view.findViewById(R.id.openapsma_result); resultView = (TextView) view.findViewById(R.id.openapsma_result);
requestView = (TextView) view.findViewById(R.id.openapsma_request); requestView = (TextView) view.findViewById(R.id.openapsma_request);
updateGUI(); updateGUI();
return view; return view;
} catch (Exception e) {
FabricPrivacy.logException(e);
}
return null;
} }
@Override @Override
@ -93,35 +87,32 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli
protected void updateGUI() { protected void updateGUI() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override DetermineBasalResultAMA lastAPSResult = OpenAPSAMAPlugin.getPlugin().lastAPSResult;
public void run() { if (lastAPSResult != null) {
DetermineBasalResultAMA lastAPSResult = OpenAPSAMAPlugin.getPlugin().lastAPSResult; resultView.setText(JSONFormatter.format(lastAPSResult.json));
if (lastAPSResult != null) { requestView.setText(lastAPSResult.toSpanned());
resultView.setText(JSONFormatter.format(lastAPSResult.json)); }
requestView.setText(lastAPSResult.toSpanned()); DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS = OpenAPSAMAPlugin.getPlugin().lastDetermineBasalAdapterAMAJS;
} if (determineBasalAdapterAMAJS != null) {
DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS = OpenAPSAMAPlugin.getPlugin().lastDetermineBasalAdapterAMAJS; glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getGlucoseStatusParam()));
if (determineBasalAdapterAMAJS != null) { currentTempView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getCurrentTempParam()));
glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getGlucoseStatusParam())); try {
currentTempView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getCurrentTempParam())); JSONArray iobArray = new JSONArray(determineBasalAdapterAMAJS.getIobDataParam());
try { iobDataView.setText(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0)));
JSONArray iobArray = new JSONArray(determineBasalAdapterAMAJS.getIobDataParam()); } catch (JSONException e) {
iobDataView.setText(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))); log.error("Unhandled exception", e);
} catch (JSONException e) { iobDataView.setText("JSONException");
log.error("Unhandled exception", e);
iobDataView.setText("JSONException");
}
profileView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getProfileParam()));
mealDataView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getMealDataParam()));
scriptdebugView.setText(determineBasalAdapterAMAJS.getScriptDebug());
}
if (OpenAPSAMAPlugin.getPlugin().lastAPSRun != null) {
lastRunView.setText(OpenAPSAMAPlugin.getPlugin().lastAPSRun.toLocaleString());
}
if (OpenAPSAMAPlugin.getPlugin().lastAutosensResult != null) {
autosensDataView.setText(JSONFormatter.format(OpenAPSAMAPlugin.getPlugin().lastAutosensResult.json()));
} }
profileView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getProfileParam()));
mealDataView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getMealDataParam()));
scriptdebugView.setText(determineBasalAdapterAMAJS.getScriptDebug());
}
if (OpenAPSAMAPlugin.getPlugin().lastAPSRun != null) {
lastRunView.setText(OpenAPSAMAPlugin.getPlugin().lastAPSRun.toLocaleString());
}
if (OpenAPSAMAPlugin.getPlugin().lastAutosensResult != null) {
autosensDataView.setText(JSONFormatter.format(OpenAPSAMAPlugin.getPlugin().lastAutosensResult.json()));
} }
}); });
} }
@ -129,20 +120,17 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli
void updateResultGUI(final String text) { void updateResultGUI(final String text) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override resultView.setText(text);
public void run() { glucoseStatusView.setText("");
resultView.setText(text); currentTempView.setText("");
glucoseStatusView.setText(""); iobDataView.setText("");
currentTempView.setText(""); profileView.setText("");
iobDataView.setText(""); mealDataView.setText("");
profileView.setText(""); autosensDataView.setText("");
mealDataView.setText(""); scriptdebugView.setText("");
autosensDataView.setText(""); requestView.setText("");
scriptdebugView.setText(""); lastRunView.setText("");
requestView.setText("");
lastRunView.setText("");
}
}); });
} }
} }

View file

@ -1,14 +1,13 @@
package info.nightscout.androidaps.plugins.OpenAPSAMA; package info.nightscout.androidaps.plugins.OpenAPSAMA;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import org.json.JSONException; import org.json.JSONException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.GlucoseStatus;
@ -23,6 +22,7 @@ import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
@ -39,7 +39,7 @@ import info.nightscout.utils.Round;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
private static Logger log = LoggerFactory.getLogger(OpenAPSAMAPlugin.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private static OpenAPSAMAPlugin openAPSAMAPlugin; private static OpenAPSAMAPlugin openAPSAMAPlugin;
@ -91,15 +91,11 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
@Override @Override
public void invoke(String initiator, boolean tempBasalFallback) { public void invoke(String initiator, boolean tempBasalFallback) {
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback); if (Config.logAps)
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS; DetermineBasalAdapterAMAJS determineBasalAdapterAMAJS;
try { determineBasalAdapterAMAJS = new DetermineBasalAdapterAMAJS(new ScriptReader(MainApp.instance().getBaseContext()));
determineBasalAdapterAMAJS = new DetermineBasalAdapterAMAJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
log.error(e.getMessage(), e);
return;
}
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
@ -107,21 +103,21 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
if (profile == null) { if (profile == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.noprofileselected)); log.debug(MainApp.gs(R.string.noprofileselected));
return; return;
} }
if (!isEnabled(PluginType.APS)) { if (!isEnabled(PluginType.APS)) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_disabled)); log.debug(MainApp.gs(R.string.openapsma_disabled));
return; return;
} }
if (glucoseStatus == null) { if (glucoseStatus == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_noglucosedata)); log.debug(MainApp.gs(R.string.openapsma_noglucosedata));
return; return;
} }
@ -139,11 +135,13 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
Date start = new Date(); Date start = new Date();
Date startPart = new Date(); Date startPart = new Date();
IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile); IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile);
Profiler.log(log, "calculateIobArrayInDia()", startPart); if (Config.logAps)
Profiler.log(log, "calculateIobArrayInDia()", startPart);
startPart = new Date(); startPart = new Date();
MealData mealData = TreatmentsPlugin.getPlugin().getMealData(); MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
Profiler.log(log, "getMealData()", startPart); if (Config.logAps)
Profiler.log(log, "getMealData()", startPart);
double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value();
@ -163,7 +161,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
if (!HardLimits.checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) if (!HardLimits.checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA))
return; return;
if (!HardLimits.checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC)) if (!HardLimits.checkOnlyHardLimits(profile.getIcTimeFromMidnight(Profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC))
return; return;
if (!HardLimits.checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF)) if (!HardLimits.checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF))
return; return;
@ -184,8 +182,10 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
lastAutosensResult = new AutosensResult(); lastAutosensResult = new AutosensResult();
lastAutosensResult.sensResult = "autosens disabled"; lastAutosensResult.sensResult = "autosens disabled";
} }
Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); if (Config.logAps)
Profiler.log(log, "AMA data gathering", start); Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart);
if (Config.logAps)
Profiler.log(log, "AMA data gathering", start);
start = new Date(); start = new Date();
@ -200,7 +200,8 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
DetermineBasalResultAMA determineBasalResultAMA = determineBasalAdapterAMAJS.invoke(); DetermineBasalResultAMA determineBasalResultAMA = determineBasalAdapterAMAJS.invoke();
Profiler.log(log, "AMA calculation", start); if (Config.logAps)
Profiler.log(log, "AMA calculation", start);
// Fix bug determine basal // Fix bug determine basal
if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) if (determineBasalResultAMA.rate == 0d && determineBasalResultAMA.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress())
determineBasalResultAMA.tempBasalRequested = false; determineBasalResultAMA.tempBasalRequested = false;

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.OpenAPSMA;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function; import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJSON; import org.mozilla.javascript.NativeJSON;
@ -29,9 +28,9 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
public class DetermineBasalAdapterMAJS { public class DetermineBasalAdapterMAJS {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private ScriptReader mScriptReader = null; private ScriptReader mScriptReader;
private JSONObject mProfile; private JSONObject mProfile;
private JSONObject mGlucoseStatus; private JSONObject mGlucoseStatus;
private JSONObject mIobData; private JSONObject mIobData;
@ -39,12 +38,12 @@ public class DetermineBasalAdapterMAJS {
private JSONObject mCurrentTemp; private JSONObject mCurrentTemp;
private String storedCurrentTemp = null; private String storedCurrentTemp = null;
public String storedIobData = null; private String storedIobData = null;
private String storedGlucoseStatus = null; private String storedGlucoseStatus = null;
private String storedProfile = null; private String storedProfile = null;
private String storedMeal_data = null; private String storedMeal_data = null;
public DetermineBasalAdapterMAJS(ScriptReader scriptReader) throws IOException { DetermineBasalAdapterMAJS(ScriptReader scriptReader) {
mScriptReader = scriptReader; mScriptReader = scriptReader;
} }
@ -97,7 +96,7 @@ public class DetermineBasalAdapterMAJS {
// Parse the jsResult object to a JSON-String // Parse the jsResult object to a JSON-String
String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString(); String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();
if (Config.logAPSResult) if (Config.logAps)
log.debug("Result: " + result); log.debug("Result: " + result);
try { try {
determineBasalResultMA = new DetermineBasalResultMA(jsResult, new JSONObject(result)); determineBasalResultMA = new DetermineBasalResultMA(jsResult, new JSONObject(result));
@ -108,14 +107,10 @@ public class DetermineBasalAdapterMAJS {
log.debug("Problem loading JS Functions"); log.debug("Problem loading JS Functions");
} }
} catch (IOException e) { } catch (IOException e) {
log.debug("IOException"); log.error("IOException");
} catch (RhinoException e) { } catch (RhinoException e) {
log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString()); log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString());
} catch (IllegalAccessException e) { } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
log.error(e.toString());
} catch (InstantiationException e) {
log.error(e.toString());
} catch (InvocationTargetException e) {
log.error(e.toString()); log.error(e.toString());
} finally { } finally {
Context.exit(); Context.exit();
@ -210,7 +205,7 @@ public class DetermineBasalAdapterMAJS {
mMealData.put("boluses", mealData.boluses); mMealData.put("boluses", mealData.boluses);
} }
public String readFile(String filename) throws IOException { private String readFile(String filename) throws IOException {
byte[] bytes = mScriptReader.readFile(filename); byte[] bytes = mScriptReader.readFile(filename);
String string = new String(bytes, "UTF-8"); String string = new String(bytes, "UTF-8");
if (string.startsWith("#!/usr/bin/env node")) { if (string.startsWith("#!/usr/bin/env node")) {
@ -219,13 +214,8 @@ public class DetermineBasalAdapterMAJS {
return string; return string;
} }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) { private Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), (context, scriptable, scriptable1, objects) -> objects[1]);
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param; return param;
} }

View file

@ -6,17 +6,18 @@ import org.mozilla.javascript.NativeObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
public class DetermineBasalResultMA extends APSResult { public class DetermineBasalResultMA extends APSResult {
private static Logger log = LoggerFactory.getLogger(DetermineBasalResultMA.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
public JSONObject json = new JSONObject(); public JSONObject json = new JSONObject();
public double eventualBG; private double eventualBG;
public double snoozeBG; private double snoozeBG;
public String mealAssist; private String mealAssist;
public DetermineBasalResultMA(NativeObject result, JSONObject j) { DetermineBasalResultMA(NativeObject result, JSONObject j) {
json = j; json = j;
if (result.containsKey("error")) { if (result.containsKey("error")) {
reason = (String) result.get("error"); reason = (String) result.get("error");
@ -49,7 +50,7 @@ public class DetermineBasalResultMA extends APSResult {
} }
} }
public DetermineBasalResultMA() { private DetermineBasalResultMA() {
} }
@Override @Override

View file

@ -4,8 +4,8 @@ import org.mozilla.javascript.ScriptableObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.Config;
import info.nightscout.utils.ToastUtils; import info.nightscout.androidaps.Constants;
/** /**
* Created by adrian on 15/10/17. * Created by adrian on 15/10/17.
@ -14,10 +14,10 @@ import info.nightscout.utils.ToastUtils;
public class LoggerCallback extends ScriptableObject { public class LoggerCallback extends ScriptableObject {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterMAJS.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
static StringBuffer errorBuffer = new StringBuffer(); private static StringBuffer errorBuffer = new StringBuffer();
static StringBuffer logBuffer = new StringBuffer(); private static StringBuffer logBuffer = new StringBuffer();
public LoggerCallback() { public LoggerCallback() {
@ -36,26 +36,27 @@ public class LoggerCallback extends ScriptableObject {
} }
public void jsFunction_log(Object obj1) { public void jsFunction_log(Object obj1) {
log.debug(obj1.toString()); if (Config.logAps)
log.debug(obj1.toString());
logBuffer.append(obj1.toString()); logBuffer.append(obj1.toString());
logBuffer.append(' '); logBuffer.append(' ');
} }
public void jsFunction_error(Object obj1) { public void jsFunction_error(Object obj1) {
log.error(obj1.toString()); if (Config.logAps)
log.error(obj1.toString());
errorBuffer.append(obj1.toString()); errorBuffer.append(obj1.toString());
errorBuffer.append(' '); errorBuffer.append(' ');
} }
public static String getScriptDebug() {
public static String getScriptDebug(){
String ret = ""; String ret = "";
if(errorBuffer.length() > 0){ if (errorBuffer.length() > 0) {
ret += "e:\n" + errorBuffer.toString(); ret += "e:\n" + errorBuffer.toString();
} }
if(ret.length() > 0 && logBuffer.length() > 0) ret += '\n'; if (ret.length() > 0 && logBuffer.length() > 0) ret += '\n';
if(logBuffer.length() > 0){ if (logBuffer.length() > 0) {
ret += "d:\n" + logBuffer.toString(); ret += "d:\n" + logBuffer.toString();
} }
return ret; return ret;

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.OpenAPSMA;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -12,10 +11,6 @@ import android.widget.TextView;
import com.crashlytics.android.answers.CustomEvent; import com.crashlytics.android.answers.CustomEvent;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui;
@ -24,8 +19,6 @@ import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.JSONFormatter; import info.nightscout.utils.JSONFormatter;
public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClickListener { public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(OpenAPSMAFragment.class);
Button run; Button run;
TextView lastRunView; TextView lastRunView;
TextView glucoseStatusView; TextView glucoseStatusView;
@ -87,25 +80,22 @@ public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClic
protected void updateGUI() { protected void updateGUI() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override DetermineBasalResultMA lastAPSResult = OpenAPSMAPlugin.getPlugin().lastAPSResult;
public void run() { if (lastAPSResult != null) {
DetermineBasalResultMA lastAPSResult = OpenAPSMAPlugin.getPlugin().lastAPSResult; resultView.setText(JSONFormatter.format(lastAPSResult.json));
if (lastAPSResult != null) { requestView.setText(lastAPSResult.toSpanned());
resultView.setText(JSONFormatter.format(lastAPSResult.json)); }
requestView.setText(lastAPSResult.toSpanned()); DetermineBasalAdapterMAJS determineBasalAdapterMAJS = OpenAPSMAPlugin.getPlugin().lastDetermineBasalAdapterMAJS;
} if (determineBasalAdapterMAJS != null) {
DetermineBasalAdapterMAJS determineBasalAdapterMAJS = OpenAPSMAPlugin.getPlugin().lastDetermineBasalAdapterMAJS; glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getGlucoseStatusParam()));
if (determineBasalAdapterMAJS != null) { currentTempView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getCurrentTempParam()));
glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getGlucoseStatusParam())); iobDataView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getIobDataParam()));
currentTempView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getCurrentTempParam())); profileView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getProfileParam()));
iobDataView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getIobDataParam())); mealDataView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getMealDataParam()));
profileView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getProfileParam())); }
mealDataView.setText(JSONFormatter.format(determineBasalAdapterMAJS.getMealDataParam())); if (OpenAPSMAPlugin.getPlugin().lastAPSRun != null) {
} lastRunView.setText(OpenAPSMAPlugin.getPlugin().lastAPSRun.toLocaleString());
if (OpenAPSMAPlugin.getPlugin().lastAPSRun != null) {
lastRunView.setText(OpenAPSMAPlugin.getPlugin().lastAPSRun.toLocaleString());
}
} }
}); });
} }
@ -113,18 +103,15 @@ public class OpenAPSMAFragment extends SubscriberFragment implements View.OnClic
private void updateResultGUI(final String text) { private void updateResultGUI(final String text) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override resultView.setText(text);
public void run() { glucoseStatusView.setText("");
resultView.setText(text); currentTempView.setText("");
glucoseStatusView.setText(""); iobDataView.setText("");
currentTempView.setText(""); profileView.setText("");
iobDataView.setText(""); mealDataView.setText("");
profileView.setText(""); requestView.setText("");
mealDataView.setText(""); lastRunView.setText("");
requestView.setText("");
lastRunView.setText("");
}
}); });
} }
} }

View file

@ -4,10 +4,10 @@ import org.json.JSONException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.GlucoseStatus;
@ -39,7 +39,7 @@ import static info.nightscout.utils.HardLimits.verifyHardLimits;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class OpenAPSMAPlugin extends PluginBase implements APSInterface { public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
private static Logger log = LoggerFactory.getLogger(OpenAPSMAPlugin.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private static OpenAPSMAPlugin openAPSMAPlugin; private static OpenAPSMAPlugin openAPSMAPlugin;
@ -90,15 +90,11 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
@Override @Override
public void invoke(String initiator, boolean tempBasalFallback) { public void invoke(String initiator, boolean tempBasalFallback) {
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback); if (Config.logAps)
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterMAJS determineBasalAdapterMAJS = null; DetermineBasalAdapterMAJS determineBasalAdapterMAJS = null;
try { determineBasalAdapterMAJS = new DetermineBasalAdapterMAJS(new ScriptReader(MainApp.instance().getBaseContext()));
determineBasalAdapterMAJS = new DetermineBasalAdapterMAJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
log.error(e.getMessage(), e);
return;
}
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
@ -106,21 +102,21 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
if (profile == null) { if (profile == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.noprofileselected)); log.debug(MainApp.gs(R.string.noprofileselected));
return; return;
} }
if (!isEnabled(PluginType.APS)) { if (!isEnabled(PluginType.APS)) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_disabled)); log.debug(MainApp.gs(R.string.openapsma_disabled));
return; return;
} }
if (glucoseStatus == null) { if (glucoseStatus == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_noglucosedata)); log.debug(MainApp.gs(R.string.openapsma_noglucosedata));
return; return;
} }
@ -147,7 +143,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
MealData mealData = TreatmentsPlugin.getPlugin().getMealData(); MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value();
Profiler.log(log, "MA data gathering", start); if (Config.logAps)
Profiler.log(log, "MA data gathering", start);
minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]); minBg = verifyHardLimits(minBg, "minBg", HardLimits.VERY_HARD_LIMIT_MIN_BG[0], HardLimits.VERY_HARD_LIMIT_MIN_BG[1]);
maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]); maxBg = verifyHardLimits(maxBg, "maxBg", HardLimits.VERY_HARD_LIMIT_MAX_BG[0], HardLimits.VERY_HARD_LIMIT_MAX_BG[1]);
@ -177,7 +174,8 @@ public class OpenAPSMAPlugin extends PluginBase implements APSInterface {
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
Profiler.log(log, "MA calculation", start); if (Config.logAps)
Profiler.log(log, "MA calculation", start);
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();

View file

@ -6,7 +6,7 @@ import info.nightscout.androidaps.events.EventUpdateGui;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class EventOpenAPSUpdateResultGui extends EventUpdateGui { public class EventOpenAPSUpdateResultGui extends EventUpdateGui {
public String text = null; public String text;
public EventOpenAPSUpdateResultGui(String text) { public EventOpenAPSUpdateResultGui(String text) {
this.text = text; this.text = text;

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.OpenAPSSMB;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function; import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeJSON; import org.mozilla.javascript.NativeJSON;
@ -35,10 +34,10 @@ import info.nightscout.utils.SP;
import info.nightscout.utils.SafeParse; import info.nightscout.utils.SafeParse;
public class DetermineBasalAdapterSMBJS { public class DetermineBasalAdapterSMBJS {
private static Logger log = LoggerFactory.getLogger(DetermineBasalAdapterSMBJS.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private ScriptReader mScriptReader = null; private ScriptReader mScriptReader;
private JSONObject mProfile; private JSONObject mProfile;
private JSONObject mGlucoseStatus; private JSONObject mGlucoseStatus;
private JSONArray mIobData; private JSONArray mIobData;
@ -64,7 +63,7 @@ public class DetermineBasalAdapterSMBJS {
* Main code * Main code
*/ */
public DetermineBasalAdapterSMBJS(ScriptReader scriptReader) throws IOException { DetermineBasalAdapterSMBJS(ScriptReader scriptReader) {
mScriptReader = scriptReader; mScriptReader = scriptReader;
} }
@ -72,19 +71,21 @@ public class DetermineBasalAdapterSMBJS {
public DetermineBasalResultSMB invoke() { public DetermineBasalResultSMB invoke() {
log.debug(">>> Invoking detemine_basal <<<"); if (Config.logAps) {
log.debug("Glucose status: " + (storedGlucoseStatus = mGlucoseStatus.toString())); log.debug(">>> Invoking detemine_basal <<<");
log.debug("IOB data: " + (storedIobData = mIobData.toString())); log.debug("Glucose status: " + (storedGlucoseStatus = mGlucoseStatus.toString()));
log.debug("Current temp: " + (storedCurrentTemp = mCurrentTemp.toString())); log.debug("IOB data: " + (storedIobData = mIobData.toString()));
log.debug("Profile: " + (storedProfile = mProfile.toString())); log.debug("Current temp: " + (storedCurrentTemp = mCurrentTemp.toString()));
log.debug("Meal data: " + (storedMeal_data = mMealData.toString())); log.debug("Profile: " + (storedProfile = mProfile.toString()));
if (mAutosensData != null) log.debug("Meal data: " + (storedMeal_data = mMealData.toString()));
log.debug("Autosens data: " + (storedAutosens_data = mAutosensData.toString())); if (mAutosensData != null)
else log.debug("Autosens data: " + (storedAutosens_data = mAutosensData.toString()));
log.debug("Autosens data: " + (storedAutosens_data = "undefined")); else
log.debug("Reservoir data: " + "undefined"); log.debug("Autosens data: " + (storedAutosens_data = "undefined"));
log.debug("MicroBolusAllowed: " + (storedMicroBolusAllowed = "" + mMicrobolusAllowed)); log.debug("Reservoir data: " + "undefined");
log.debug("SMBAlwaysAllowed: " + (storedSMBAlwaysAllowed = "" + mSMBAlwaysAllowed)); log.debug("MicroBolusAllowed: " + (storedMicroBolusAllowed = "" + mMicrobolusAllowed));
log.debug("SMBAlwaysAllowed: " + (storedSMBAlwaysAllowed = "" + mSMBAlwaysAllowed));
}
DetermineBasalResultSMB determineBasalResultSMB = null; DetermineBasalResultSMB determineBasalResultSMB = null;
@ -135,7 +136,7 @@ public class DetermineBasalAdapterSMBJS {
// Parse the jsResult object to a JSON-String // Parse the jsResult object to a JSON-String
String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString(); String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();
if (Config.logAPSResult) if (Config.logAps)
log.debug("Result: " + result); log.debug("Result: " + result);
try { try {
determineBasalResultSMB = new DetermineBasalResultSMB(new JSONObject(result)); determineBasalResultSMB = new DetermineBasalResultSMB(new JSONObject(result));
@ -143,17 +144,13 @@ public class DetermineBasalAdapterSMBJS {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }
} else { } else {
log.debug("Problem loading JS Functions"); log.error("Problem loading JS Functions");
} }
} catch (IOException e) { } catch (IOException e) {
log.debug("IOException"); log.error("IOException");
} catch (RhinoException e) { } catch (RhinoException e) {
log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString()); log.error("RhinoException: (" + e.lineNumber() + "," + e.columnNumber() + ") " + e.toString());
} catch (IllegalAccessException e) { } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
log.error(e.toString());
} catch (InstantiationException e) {
log.error(e.toString());
} catch (InvocationTargetException e) {
log.error(e.toString()); log.error(e.toString());
} finally { } finally {
Context.exit(); Context.exit();
@ -244,7 +241,7 @@ public class DetermineBasalAdapterSMBJS {
mProfile.put("maxCOB", SMBDefaults.maxCOB); mProfile.put("maxCOB", SMBDefaults.maxCOB);
mProfile.put("skip_neutral_temps", SMBDefaults.skip_neutral_temps); mProfile.put("skip_neutral_temps", SMBDefaults.skip_neutral_temps);
//align with max-absorption model in AMA sensitivity //align with max-absorption model in AMA sensitivity
if(mealData.usedMinCarbsImpact > 0){ if (mealData.usedMinCarbsImpact > 0) {
mProfile.put("min_5m_carbimpact", mealData.usedMinCarbsImpact); mProfile.put("min_5m_carbimpact", mealData.usedMinCarbsImpact);
} else { } else {
mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact)); mProfile.put("min_5m_carbimpact", SP.getDouble(R.string.key_openapsama_min_5m_carbimpact, SMBDefaults.min_5m_carbimpact));
@ -319,31 +316,21 @@ public class DetermineBasalAdapterSMBJS {
} }
public Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) { private Object makeParam(JSONObject jsonObject, Context rhino, Scriptable scope) {
if (jsonObject == null) return Undefined.instance; if (jsonObject == null) return Undefined.instance;
Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonObject.toString(), (context, scriptable, scriptable1, objects) -> objects[1]);
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param; return param;
} }
public Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) { private Object makeParamArray(JSONArray jsonArray, Context rhino, Scriptable scope) {
//Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() { //Object param = NativeJSON.parse(rhino, scope, "{myarray: " + jsonArray.toString() + " }", new Callable() {
Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), new Callable() { Object param = NativeJSON.parse(rhino, scope, jsonArray.toString(), (context, scriptable, scriptable1, objects) -> objects[1]);
@Override
public Object call(Context context, Scriptable scriptable, Scriptable scriptable1, Object[] objects) {
return objects[1];
}
});
return param; return param;
} }
public String readFile(String filename) throws IOException { private String readFile(String filename) throws IOException {
byte[] bytes = mScriptReader.readFile(filename); byte[] bytes = mScriptReader.readFile(filename);
String string = new String(bytes, "UTF-8"); String string = new String(bytes, "UTF-8");
if (string.startsWith("#!/usr/bin/env node")) { if (string.startsWith("#!/usr/bin/env node")) {

View file

@ -5,11 +5,12 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
public class DetermineBasalResultSMB extends APSResult { public class DetermineBasalResultSMB extends APSResult {
private static final Logger log = LoggerFactory.getLogger(DetermineBasalResultSMB.class); private static final Logger log = LoggerFactory.getLogger(Constants.APS);
private double eventualBG; private double eventualBG;
private double snoozeBG; private double snoozeBG;

View file

@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.Common.SubscriberFragment;
@ -28,7 +29,7 @@ import info.nightscout.utils.FabricPrivacy;
import info.nightscout.utils.JSONFormatter; import info.nightscout.utils.JSONFormatter;
public class OpenAPSSMBFragment extends SubscriberFragment { public class OpenAPSSMBFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(OpenAPSSMBFragment.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
@BindView(R.id.openapsma_run) @BindView(R.id.openapsma_run)
Button run; Button run;
@ -84,40 +85,37 @@ public class OpenAPSSMBFragment extends SubscriberFragment {
protected void updateGUI() { protected void updateGUI() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override synchronized (OpenAPSSMBFragment.this) {
public void run() { if (!isBound()) return;
synchronized (OpenAPSSMBFragment.this) { OpenAPSSMBPlugin plugin = OpenAPSSMBPlugin.getPlugin();
if (!isBound()) return; DetermineBasalResultSMB lastAPSResult = plugin.lastAPSResult;
OpenAPSSMBPlugin plugin = OpenAPSSMBPlugin.getPlugin(); if (lastAPSResult != null) {
DetermineBasalResultSMB lastAPSResult = plugin.lastAPSResult; resultView.setText(JSONFormatter.format(lastAPSResult.json));
if (lastAPSResult != null) { requestView.setText(lastAPSResult.toSpanned());
resultView.setText(JSONFormatter.format(lastAPSResult.json)); }
requestView.setText(lastAPSResult.toSpanned()); DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = plugin.lastDetermineBasalAdapterSMBJS;
} if (determineBasalAdapterSMBJS != null) {
DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = plugin.lastDetermineBasalAdapterSMBJS; glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam()).toString().trim());
if (determineBasalAdapterSMBJS != null) { currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam()).toString().trim());
glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam()).toString().trim()); try {
currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam()).toString().trim()); JSONArray iobArray = new JSONArray(determineBasalAdapterSMBJS.getIobDataParam());
try { iobDataView.setText((String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))).trim());
JSONArray iobArray = new JSONArray(determineBasalAdapterSMBJS.getIobDataParam()); } catch (JSONException e) {
iobDataView.setText((String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))).trim()); log.error("Unhandled exception", e);
} catch (JSONException e) { iobDataView.setText("JSONException see log for details");
log.error("Unhandled exception", e);
iobDataView.setText("JSONException see log for details");
}
profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam()).toString().trim());
mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam()).toString().trim());
scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug().trim());
if (lastAPSResult != null && lastAPSResult.inputConstraints != null)
constraintsView.setText(lastAPSResult.inputConstraints.getReasons().trim());
}
if (plugin.lastAPSRun != null) {
lastRunView.setText(plugin.lastAPSRun.toLocaleString().trim());
}
if (plugin.lastAutosensResult != null) {
autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json()).toString().trim());
} }
profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam()).toString().trim());
mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam()).toString().trim());
scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug().trim());
if (lastAPSResult != null && lastAPSResult.inputConstraints != null)
constraintsView.setText(lastAPSResult.inputConstraints.getReasons().trim());
}
if (plugin.lastAPSRun != null) {
lastRunView.setText(plugin.lastAPSRun.toLocaleString().trim());
}
if (plugin.lastAutosensResult != null) {
autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json()).toString().trim());
} }
} }
}); });
@ -126,22 +124,19 @@ public class OpenAPSSMBFragment extends SubscriberFragment {
void updateResultGUI(final String text) { void updateResultGUI(final String text) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override synchronized (OpenAPSSMBFragment.this) {
public void run() { if (isBound()) {
synchronized (OpenAPSSMBFragment.this) { resultView.setText(text);
if (isBound()) { glucoseStatusView.setText("");
resultView.setText(text); currentTempView.setText("");
glucoseStatusView.setText(""); iobDataView.setText("");
currentTempView.setText(""); profileView.setText("");
iobDataView.setText(""); mealDataView.setText("");
profileView.setText(""); autosensDataView.setText("");
mealDataView.setText(""); scriptdebugView.setText("");
autosensDataView.setText(""); requestView.setText("");
scriptdebugView.setText(""); lastRunView.setText("");
requestView.setText("");
lastRunView.setText("");
}
} }
} }
}); });

View file

@ -1,14 +1,13 @@
package info.nightscout.androidaps.plugins.OpenAPSSMB; package info.nightscout.androidaps.plugins.OpenAPSSMB;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import org.json.JSONException; import org.json.JSONException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.GlucoseStatus;
@ -24,16 +23,17 @@ import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensResult;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.Loop.APSResult; import info.nightscout.androidaps.plugins.Loop.APSResult;
import info.nightscout.androidaps.plugins.Loop.ScriptReader; import info.nightscout.androidaps.plugins.Loop.ScriptReader;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateGui;
import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateResultGui; import info.nightscout.androidaps.plugins.OpenAPSMA.events.EventOpenAPSUpdateResultGui;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.HardLimits; import info.nightscout.utils.HardLimits;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.utils.Profiler; import info.nightscout.utils.Profiler;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
import info.nightscout.utils.ToastUtils; import info.nightscout.utils.ToastUtils;
@ -42,7 +42,7 @@ import info.nightscout.utils.ToastUtils;
* Created by mike on 05.08.2016. * Created by mike on 05.08.2016.
*/ */
public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
private static Logger log = LoggerFactory.getLogger(OpenAPSSMBPlugin.class); private static Logger log = LoggerFactory.getLogger(Constants.APS);
private static OpenAPSSMBPlugin openAPSSMBPlugin; private static OpenAPSSMBPlugin openAPSSMBPlugin;
@ -94,15 +94,11 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
@Override @Override
public void invoke(String initiator, boolean tempBasalFallback) { public void invoke(String initiator, boolean tempBasalFallback) {
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback); if (Config.logAps)
log.debug("invoke from " + initiator + " tempBasalFallback: " + tempBasalFallback);
lastAPSResult = null; lastAPSResult = null;
DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = null; DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS;
try { determineBasalAdapterSMBJS = new DetermineBasalAdapterSMBJS(new ScriptReader(MainApp.instance().getBaseContext()));
determineBasalAdapterSMBJS = new DetermineBasalAdapterSMBJS(new ScriptReader(MainApp.instance().getBaseContext()));
} catch (IOException e) {
log.error(e.getMessage(), e);
return;
}
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData(); GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
@ -110,21 +106,21 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
if (profile == null) { if (profile == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.noprofileselected)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.noprofileselected)); log.debug(MainApp.gs(R.string.noprofileselected));
return; return;
} }
if (!isEnabled(PluginType.APS)) { if (!isEnabled(PluginType.APS)) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_disabled)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_disabled)); log.debug(MainApp.gs(R.string.openapsma_disabled));
return; return;
} }
if (glucoseStatus == null) { if (glucoseStatus == null) {
MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata))); MainApp.bus().post(new EventOpenAPSUpdateResultGui(MainApp.gs(R.string.openapsma_noglucosedata)));
if (Config.logAPSResult) if (Config.logAps)
log.debug(MainApp.gs(R.string.openapsma_noglucosedata)); log.debug(MainApp.gs(R.string.openapsma_noglucosedata));
return; return;
} }
@ -146,11 +142,13 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
Date start = new Date(); Date start = new Date();
Date startPart = new Date(); Date startPart = new Date();
IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(profile); IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(profile);
Profiler.log(log, "calculateIobArrayInDia()", startPart); if (Config.logAps)
Profiler.log(log, "calculateIobArrayInDia()", startPart);
startPart = new Date(); startPart = new Date();
MealData mealData = TreatmentsPlugin.getPlugin().getMealData(); MealData mealData = TreatmentsPlugin.getPlugin().getMealData();
Profiler.log(log, "getMealData()", startPart); if (Config.logAps)
Profiler.log(log, "getMealData()", startPart);
double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value(); double maxIob = MainApp.getConstraintChecker().getMaxIOBAllowed().value();
@ -170,7 +168,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA)) if (!checkOnlyHardLimits(profile.getDia(), "dia", HardLimits.MINDIA, HardLimits.MAXDIA))
return; return;
if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC)) if (!checkOnlyHardLimits(profile.getIcTimeFromMidnight(Profile.secondsFromMidnight()), "carbratio", HardLimits.MINIC, HardLimits.MAXIC))
return; return;
if (!checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF)) if (!checkOnlyHardLimits(Profile.toMgdl(profile.getIsf(), units), "sens", HardLimits.MINISF, HardLimits.MAXISF))
return; return;
@ -200,8 +198,10 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering); MainApp.getConstraintChecker().isAdvancedFilteringEnabled(advancedFiltering);
inputConstraints.copyReasons(advancedFiltering); inputConstraints.copyReasons(advancedFiltering);
Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart); if (Config.logAps)
Profiler.log(log, "SMB data gathering", start); Profiler.log(log, "detectSensitivityandCarbAbsorption()", startPart);
if (Config.logAps)
Profiler.log(log, "SMB data gathering", start);
start = new Date(); start = new Date();
try { try {
@ -219,7 +219,8 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke(); DetermineBasalResultSMB determineBasalResultSMB = determineBasalAdapterSMBJS.invoke();
Profiler.log(log, "SMB calculation", start); if (Config.logAps)
Profiler.log(log, "SMB calculation", start);
// TODO still needed with oref1? // TODO still needed with oref1?
// Fix bug determine basal // Fix bug determine basal
if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress()) if (determineBasalResultSMB.rate == 0d && determineBasalResultSMB.duration == 0 && !TreatmentsPlugin.getPlugin().isTempBasalInProgress())