Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open aps show proper debug log #3238

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DetermineBasalResult @Inject constructor(val injector: HasAndroidInjector)
targetBG = result.targetBG ?: 0.0
deliverAt = result.deliverAt ?: 0L
variableSens = result.variable_sens
scriptDebug = result.consoleError
scriptDebug = result.consoleLog
}

override val carbsRequiredText: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.aaps.plugins.aps

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
Expand Down Expand Up @@ -136,7 +139,7 @@ class OpenAPSFragment : DaggerFragment(), MenuProvider {
binding.iobdata.text = rh.gs(R.string.array_of_elements, lastAPSResult.iobData?.size) + "\n" + lastAPSResult.iob?.dataClassToHtml()
binding.profile.text = lastAPSResult.oapsProfile?.dataClassToHtml()
binding.mealdata.text = lastAPSResult.mealData?.dataClassToHtml()
binding.scriptdebugdata.text = lastAPSResult.scriptDebug?.joinToString("\n")
binding.scriptdebugdata.text = lastAPSResult.scriptDebug?.let { buildSpannableFromEntries(it) }
binding.constraints.text = lastAPSResult.inputConstraints?.getReasons()
binding.autosensdata.text = lastAPSResult.autosensResult?.dataClassToHtml()
binding.lastrun.text = dateUtil.dateAndTimeString(openAPSPlugin.lastAPSRun)
Expand Down Expand Up @@ -188,4 +191,21 @@ class OpenAPSFragment : DaggerFragment(), MenuProvider {

private fun String.bold(): String = "<b>$this</b>"
private val br = "<br>"

private fun buildSpannableFromEntries(entries: List<String>): SpannableStringBuilder {
val builder = SpannableStringBuilder()
entries.forEach { entry ->
val start = builder.length
if (entry.startsWith("ERROR: ")) {
// Strip the "ERROR: " prefix before displaying
val strippedEntry = entry.removePrefix("ERROR: ")
builder.append("$strippedEntry\n")
val end = builder.length
builder.setSpan(ForegroundColorSpan(Color.RED), start, end, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE)
} else {
builder.append("$entry\n")
}
}
return builder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class DetermineBasalAMA @Inject constructor(
private val consoleError = mutableListOf<String>()
private val consoleLog = mutableListOf<String>()

private fun consoleLog(msg: String) {
consoleLog.add(msg)
}

private fun consoleError(msg: String) {
consoleError.add(msg)
consoleLog("ERROR: $msg")
}

private fun Double.toFixed2(): String = DecimalFormat("0.00#").format(round(this, 2))
private fun Double.toFixed3(): String = DecimalFormat("0.000#").format(round(this, 3))

Expand Down Expand Up @@ -58,7 +67,7 @@ class DetermineBasalAMA @Inject constructor(
fun reason(rT: RT, msg: String) {
if (rT.reason.toString().isNotEmpty()) rT.reason.append(". ")
rT.reason.append(msg)
consoleError.add(msg)
consoleError(msg)
}

private fun getMaxSafeBasal(profile: OapsProfile): Double =
Expand Down Expand Up @@ -116,7 +125,7 @@ class DetermineBasalAMA @Inject constructor(
)
val basal = round_basal(profile.current_basal * autosens_data.ratio)
if (basal != profile.current_basal) {
consoleError.add("Adjusting basal from ${profile.current_basal} to $basal")
consoleError("Adjusting basal from ${profile.current_basal} to $basal")
}

val bg = glucose_status.glucose
Expand All @@ -142,15 +151,15 @@ class DetermineBasalAMA @Inject constructor(
// adjust min, max, and target BG for sensitivity, such that 50% increase in ISF raises target from 100 to 120
if (profile.autosens_adjust_targets) {
if (profile.temptargetSet) {
consoleError.add("Temp Target set, not adjusting with autosens")
consoleError("Temp Target set, not adjusting with autosens")
} else {
min_bg = round((min_bg - 60) / autosens_data.ratio, 0) + 60
max_bg = round((max_bg - 60) / autosens_data.ratio, 0) + 60
val new_target_bg = round((target_bg - 60) / autosens_data.ratio) + 60.0
if (target_bg == new_target_bg) {
consoleError.add("target_bg unchanged: $new_target_bg")
consoleError("target_bg unchanged: $new_target_bg")
} else {
consoleError.add("Adjusting target_bg from $target_bg to $new_target_bg")
consoleError("Adjusting target_bg from $target_bg to $new_target_bg")
}
target_bg = new_target_bg
}
Expand All @@ -171,7 +180,7 @@ class DetermineBasalAMA @Inject constructor(

val sens = round(profile.sens / autosens_data.ratio, 1)
if (sens != profile.sens) {
consoleError.add("Adjusting sens from ${profile.sens} to $sens")
consoleError("Adjusting sens from ${profile.sens} to $sens")
}

//calculate BG impact: the amount BG "should" be rising or falling based on insulin activity alone
Expand Down Expand Up @@ -240,8 +249,8 @@ class DetermineBasalAMA @Inject constructor(
//5m data points = g * (1U/10g) * (40mg/dL/1U) / (mg/dL/5m)
cid = meal_data.mealCOB * (sens / profile.carb_ratio) / ci
val acid: Double = meal_data.mealCOB * (sens / profile.carb_ratio) / aci
consoleError.add("Carb Impact: $ci mg/dL per 5m; CI Duration: ${Math.round(10 * cid / 6) / 10} hours")
consoleError.add("Accel. Carb Impact: $aci mg/dL per 5m; ACI Duration: ${Math.round(10 * acid / 6) / 10} hours")
consoleError("Carb Impact: $ci mg/dL per 5m; CI Duration: ${Math.round(10 * cid / 6) / 10} hours")
consoleError("Accel. Carb Impact: $aci mg/dL per 5m; ACI Duration: ${Math.round(10 * acid / 6) / 10} hours")
var minPredBG = 999.0
var maxPredBG = bg
//var eventualPredBG = bg
Expand Down
Loading