From f0ac7c4c2517dae7050c49e22dbd5be7c9ebe55a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 13 Feb 2021 22:44:01 +0100 Subject: [PATCH] LoggerCallback -> kt --- .../plugins/aps/logger/LoggerCallback.java | 63 ------------------- .../plugins/aps/logger/LoggerCallback.kt | 60 ++++++++++++++++++ .../DetermineBasalAdapterAMAJS.java | 2 +- .../DetermineBasalAdapterSMBJS.java | 2 +- 4 files changed, 62 insertions(+), 65 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java deleted file mode 100644 index 2fa1816ad5..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.java +++ /dev/null @@ -1,63 +0,0 @@ -package info.nightscout.androidaps.plugins.aps.logger; - -import org.mozilla.javascript.ScriptableObject; - -import javax.inject.Inject; - -import info.nightscout.androidaps.db.StaticInjector; -import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.LTag; - -/** - * Created by adrian on 15/10/17. - */ - - -public class LoggerCallback extends ScriptableObject { - - @Inject - AAPSLogger aapsLogger; - - private static StringBuffer errorBuffer = new StringBuffer(); - private static StringBuffer logBuffer = new StringBuffer(); - - - public LoggerCallback() { - //empty constructor needed for Rhino - errorBuffer = new StringBuffer(); - logBuffer = new StringBuffer(); - StaticInjector.Companion.getInstance().androidInjector().inject(this); - } - - @Override - public String getClassName() { - return "LoggerCallback"; - } - - public void jsConstructor() { - //empty constructor on JS site; could work as setter - } - - public void jsFunction_log(Object obj1) { - aapsLogger.debug(LTag.APS, obj1.toString().trim()); - logBuffer.append(obj1.toString()); - } - - public void jsFunction_error(Object obj1) { - aapsLogger.error(LTag.APS, obj1.toString().trim()); - errorBuffer.append(obj1.toString()); - } - - - public static String getScriptDebug() { - String ret = ""; - if (errorBuffer.length() > 0) { - ret += "e:\n" + errorBuffer.toString(); - } - if (ret.length() > 0 && logBuffer.length() > 0) ret += '\n'; - if (logBuffer.length() > 0) { - ret += "d:\n" + logBuffer.toString(); - } - return ret; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.kt new file mode 100644 index 0000000000..82aefa9b7e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/logger/LoggerCallback.kt @@ -0,0 +1,60 @@ +package info.nightscout.androidaps.plugins.aps.logger + +import info.nightscout.androidaps.db.StaticInjector.Companion.getInstance +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag +import org.mozilla.javascript.ScriptableObject +import javax.inject.Inject + +@Suppress("unused", "FunctionName") +class LoggerCallback : ScriptableObject() { + + @Inject lateinit var aapsLogger: AAPSLogger + + override fun getClassName(): String = "LoggerCallback" + + fun jsConstructor() { + //empty constructor on JS site; could work as setter + } + + fun jsFunction_log(obj1: Any) { + aapsLogger.debug(LTag.APS, obj1.toString().trim { it <= ' ' }) + logBuffer.append(obj1.toString()) + } + + fun jsFunction_error(obj1: Any) { + aapsLogger.error(LTag.APS, obj1.toString().trim { it <= ' ' }) + errorBuffer.append(obj1.toString()) + } + + companion object { + + private var errorBuffer = StringBuffer() + private var logBuffer = StringBuffer() + val scriptDebug: String + get() { + var ret = "" + if (errorBuffer.isNotEmpty()) { + ret += """ + e: + $errorBuffer + """.trimIndent() + } + if (ret.isNotEmpty() && logBuffer.isNotEmpty()) ret += '\n' + if (logBuffer.isNotEmpty()) { + ret += """ + d: + $logBuffer + """.trimIndent() + } + return ret + } + } + + init { + //empty constructor needed for Rhino + errorBuffer = StringBuffer() + logBuffer = StringBuffer() + getInstance().androidInjector().inject(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java index d8af22b203..b52cfdd753 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/DetermineBasalAdapterAMAJS.java @@ -129,7 +129,7 @@ public class DetermineBasalAdapterAMAJS { setTempBasalFunctionsObj}; NativeObject jsResult = (NativeObject) determineBasalJS.call(rhino, scope, scope, params); - scriptDebug = LoggerCallback.getScriptDebug(); + scriptDebug = LoggerCallback.Companion.getScriptDebug(); // Parse the jsResult object to a JSON-String String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java index 5793c7125f..96649420c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/DetermineBasalAdapterSMBJS.java @@ -156,7 +156,7 @@ public class DetermineBasalAdapterSMBJS { NativeObject jsResult = (NativeObject) determineBasalJS.call(rhino, scope, scope, params); - scriptDebug = LoggerCallback.getScriptDebug(); + scriptDebug = LoggerCallback.Companion.getScriptDebug(); // Parse the jsResult object to a JSON-String String result = NativeJSON.stringify(rhino, scope, jsResult, null, null).toString();