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

Blaqone flavored AAPS: Dexcom G7 integration, latest BG fix, AutoIFS #8

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<action android:name="it.ct.glicemia.ACTION_GLUCOSE_MEASURED" />
<!-- Receiver from Dexcom -->
<action android:name="com.dexcom.cgm.EXTERNAL_BROADCAST" />
<action android:name="com.dexcom.g7.EXTERNAL_BROADCAST" />
<!-- Receiver from Poctech -->
<action android:name="com.china.poctech.data" />
<!-- Receiver from Tomato -->
Expand Down
327 changes: 315 additions & 12 deletions app/src/main/assets/OpenAPSSMB/determine-basal.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class MainActivity : NoSplashAppCompatActivity() {
val messageSpanned = SpannableString(message)
Linkify.addLinks(messageSpanned, Linkify.WEB_URLS)
MaterialAlertDialogBuilder(this, R.style.DialogTheme)
.setTitle(rh.gs(R.string.app_name) + " " + BuildConfig.VERSION)
.setTitle(rh.gs(R.string.app_name) + " " + BuildConfig.VERSION + "+autoISF2.2.7+G7+BackSmoothing")
.setIcon(iconsProvider.getIcon())
.setMessage(messageSpanned)
.setPositiveButton(rh.gs(R.string.ok), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
val graphData = GraphData(injector, binding.bgGraph, historyBrowserData.overviewData)
val menuChartSettings = overviewMenus.setting
graphData.addInRangeArea(historyBrowserData.overviewData.fromTime, historyBrowserData.overviewData.endTime, defaultValueHelper.determineLowLine(), defaultValueHelper.determineHighLine())
graphData.addBgReadings(menuChartSettings[0][OverviewMenus.CharType.PRE.ordinal], context)
graphData.addBgReadings(menuChartSettings[0][OverviewMenus.CharType.PRE.ordinal], menuChartSettings[0][OverviewMenus.CharType.RAW.ordinal], context)
if (buildHelper.isDev()) graphData.addBucketedData()
graphData.addTreatments(context)
graphData.addEps(context, 0.95)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.database.transactions.InsertAndCancelCurrentTemporaryTargetTransaction
import info.nightscout.androidaps.databinding.DialogCarbsBinding
import info.nightscout.androidaps.extensions.formatColor
import info.nightscout.androidaps.extensions.rawOrSmoothed
import info.nightscout.androidaps.interfaces.*
import info.nightscout.shared.logging.LTag
import info.nightscout.androidaps.logging.UserEntryLogger
Expand Down Expand Up @@ -184,7 +185,7 @@ class CarbsDialog : DialogFragmentWithDate() {
}

iobCobCalculator.ads.actualBg()?.let { bgReading ->
if (bgReading.value < 72)
if (bgReading.rawOrSmoothed(sp) < 72)
binding.hypoTt.isChecked = true
}
binding.hypoTt.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class WizardDialog : DaggerDialogFragment() {
binding.bgInput.step = if (units == GlucoseUnit.MGDL) 1.0 else 0.1

// Set BG if not old
binding.bgInput.value = iobCobCalculator.ads.actualBg()?.valueToUnits(units) ?: 0.0
binding.bgInput.value = iobCobCalculator.ads.actualBg()?.valueToUnits(units, sp) ?: 0.0

binding.ttCheckbox.isEnabled = tempTarget is ValueWrapper.Existing
binding.ttCheckboxIcon.visibility = binding.ttCheckbox.isEnabled.toVisibility()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader:
this.profile.put("enableSMB_with_COB", smbEnabled && sp.getBoolean(R.string.key_enableSMB_with_COB, false))
this.profile.put("enableSMB_with_temptarget", smbEnabled && sp.getBoolean(R.string.key_enableSMB_with_temptarget, false))
this.profile.put("allowSMB_with_high_temptarget", smbEnabled && sp.getBoolean(R.string.key_allowSMB_with_high_temptarget, false))
this.profile.put("enableSMB_always", smbEnabled && sp.getBoolean(R.string.key_enableSMB_always, false) && advancedFiltering)
this.profile.put("enableSMB_after_carbs", smbEnabled && sp.getBoolean(R.string.key_enableSMB_after_carbs, false) && advancedFiltering)

// mod 13: allow SMBalways and enableSMB_after_carbs if selected in preferences
// this.profile.put("enableSMB_always", smbEnabled && sp.getBoolean(R.string.key_enableSMB_always, false) && advancedFiltering)
// this.profile.put("enableSMB_after_carbs", smbEnabled && sp.getBoolean(R.string.key_enableSMB_after_carbs, false) && advancedFiltering)
this.profile.put("enableSMB_always", smbEnabled && sp.getBoolean(R.string.key_enableSMB_always, false)) // && advancedFiltering)
this.profile.put("enableSMB_after_carbs", smbEnabled && sp.getBoolean(R.string.key_enableSMB_after_carbs, false)) // && advancedFiltering)

this.profile.put("maxSMBBasalMinutes", sp.getInt(R.string.key_smbmaxminutes, SMBDefaults.maxSMBBasalMinutes))
this.profile.put("maxUAMSMBBasalMinutes", sp.getInt(R.string.key_uamsmbmaxminutes, SMBDefaults.maxUAMSMBBasalMinutes))
//set the min SMB amount to be the amount set by the pump.
Expand All @@ -216,6 +221,32 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader:
this.profile.put("current_basal", basalRate)
this.profile.put("temptargetSet", tempTargetSet)
this.profile.put("autosens_max", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_autosens_max, "1.2")))

// mod 7e: can I add use autoisf here?
this.profile.put("enable_autoISF", sp.getBoolean(R.string.key_enable_autoISF, false))
// mod 7f: can I add use autoisf with COB here?
this.profile.put("autoISF_max", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_autoISF_max, "1.0")))
this.profile.put("autoISF_min", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_autoISF_min, "1.0")))
this.profile.put("bgAccel_ISF_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_bgAccel_ISF_weight, "0.0")))
this.profile.put("bgBrake_ISF_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_bgBrake_ISF_weight, "0.0")))
// mod 14f: for pp_ISF without meal
this.profile.put("enable_pp_ISF_always", sp.getBoolean(R.string.key_enable_postprandial_ISF_always, false))
this.profile.put("pp_ISF_hours", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_pp_ISF_hours, "3.0")))
this.profile.put("pp_ISF_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_pp_ISF_weight, "0.0")))
this.profile.put("delta_ISFrange_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_delta_ISFrange_weight, "0.0")))
this.profile.put("lower_ISFrange_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_lower_ISFrange_weight, "0.0")))
this.profile.put("higher_ISFrange_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_higher_ISFrange_weight, "0.0")))
this.profile.put("enable_dura_ISF_with_COB", sp.getBoolean(R.string.key_enable_dura_ISF_with_COB, false))
// mod 7d: can I add autosens_min here?
this.profile.put("dura_ISF_weight", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_dura_ISF_weight, "0.0")))
// mod 10: include SMB manipulations to be accessible in determine-basal
this.profile.put("smb_delivery_ratio", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_smb_delivery_ratio, "0.5")))
this.profile.put("smb_delivery_ratio_min", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_smb_delivery_ratio_min, "0.5")))
this.profile.put("smb_delivery_ratio_max", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_smb_delivery_ratio_max, "0.5")))
this.profile.put("smb_delivery_ratio_bg_range", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_smb_delivery_ratio_bg_range, "0")))
this.profile.put("smb_max_range_extension", SafeParse.stringToDouble(sp.getString(R.string.key_openapsama_smb_max_range_extension, "1.0")))
this.profile.put("enableSMB_EvenOn_OddOff", sp.getBoolean(R.string.key_enableSMB_EvenOn_OddOff, false))

if (profileFunction.getUnits() == GlucoseUnit.MMOL) {
this.profile.put("out_units", "mmol/L")
}
Expand All @@ -238,6 +269,24 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader:
mGlucoseStatus.put("short_avgdelta", glucoseStatus.shortAvgDelta)
mGlucoseStatus.put("long_avgdelta", glucoseStatus.longAvgDelta)
mGlucoseStatus.put("date", glucoseStatus.date)

// mod 7: append 2 variables for 5% range
mGlucoseStatus.put("dura_ISF_minutes", glucoseStatus.dura_ISF_minutes)
mGlucoseStatus.put("dura_ISF_average", glucoseStatus.dura_ISF_average)
// mod 8: append variables for linear fit
mGlucoseStatus.put("slope05", glucoseStatus.slope05)
mGlucoseStatus.put("slope15", glucoseStatus.slope15)
mGlucoseStatus.put("slope40", glucoseStatus.slope40)
// mod 14g: append variables for quadratic fit
mGlucoseStatus.put("parabola_fit_correlation", glucoseStatus.r_squ)
mGlucoseStatus.put("parabola_fit_minutes", glucoseStatus.dura_p)
mGlucoseStatus.put("parabola_fit_last_delta", glucoseStatus.delta_pl)
mGlucoseStatus.put("parabola_fit_next_delta", glucoseStatus.delta_pn)
mGlucoseStatus.put("parabola_fit_a0", glucoseStatus.a_0)
mGlucoseStatus.put("parabola_fit_a1", glucoseStatus.a_1)
mGlucoseStatus.put("parabola_fit_a2", glucoseStatus.a_2)
mGlucoseStatus.put("bg_acceleration", glucoseStatus.bg_acceleration)

this.mealData.put("carbs", mealData.carbs)
this.mealData.put("mealCOB", mealData.mealCOB)
this.mealData.put("slopeFromMaxDeviation", mealData.slopeFromMaxDeviation)
Expand All @@ -252,7 +301,7 @@ class DetermineBasalAdapterSMBJS internal constructor(private val scriptReader:
this.microBolusAllowed = microBolusAllowed
smbAlwaysAllowed = advancedFiltering
currentTime = now
saveCgmSource = isSaveCgmSource
saveCgmSource = true // for non_Libre use; was: isSaveCgmSource
}

private fun makeParam(jsonObject: JSONObject?, rhino: Context, scope: Scriptable): Any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BGDatum {
constructor(json: JSONObject, dateUtil: DateUtil) {
this.dateUtil = dateUtil
try {
//if (json.has("_id")) id = json.getLong("_id")
if (json.has("_id")) id = json.getLong("_id")
if (json.has("date")) date = json.getLong("date")
if (json.has("sgv")) value = json.getDouble("sgv")
if (json.has("direction")) direction = TrendArrow.fromString(json.getString("direction"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ class OverviewData @Inject constructor(

val isLow: Boolean
get() = lastBg?.let { lastBg ->
lastBg.valueToUnits(profileFunction.getUnits()) < defaultValueHelper.determineLowLine()
lastBg.valueToUnits(profileFunction.getUnits(), sp) < defaultValueHelper.determineLowLine()
} ?: false

val isHigh: Boolean
get() = lastBg?.let { lastBg ->
lastBg.valueToUnits(profileFunction.getUnits()) > defaultValueHelper.determineHighLine()
lastBg.valueToUnits(profileFunction.getUnits(), sp) > defaultValueHelper.determineHighLine()
} ?: false

@ColorInt
Expand Down Expand Up @@ -249,6 +249,7 @@ class OverviewData @Inject constructor(
var maxBgValue = Double.MIN_VALUE
var bucketedGraphSeries: PointsWithLabelGraphSeries<DataPointWithLabelInterface> = PointsWithLabelGraphSeries()
var bgReadingGraphSeries: PointsWithLabelGraphSeries<DataPointWithLabelInterface> = PointsWithLabelGraphSeries()
var bgReadingRawGraphSeries: PointsWithLabelGraphSeries<DataPointWithLabelInterface> = PointsWithLabelGraphSeries()
var predictionsGraphSeries: PointsWithLabelGraphSeries<DataPointWithLabelInterface> = PointsWithLabelGraphSeries()

val basalScale = Scale()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
val lastBgDescription = overviewData.lastBgDescription
runOnUiThread {
_binding ?: return@runOnUiThread
binding.infoLayout.bg.text = lastBg?.valueToUnitsString(units)
binding.infoLayout.bg.text = lastBg?.valueToUnitsString(units, sp)
?: rh.gs(R.string.notavailable)
binding.infoLayout.bg.setTextColor(lastBgColor)
binding.infoLayout.arrow.setImageResource(trendArrow.directionToIcon())
Expand Down Expand Up @@ -1000,7 +1000,7 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
val menuChartSettings = overviewMenus.setting
if (menuChartSettings.isEmpty()) return
graphData.addInRangeArea(overviewData.fromTime, overviewData.endTime, defaultValueHelper.determineLowLine(), defaultValueHelper.determineHighLine())
graphData.addBgReadings(menuChartSettings[0][OverviewMenus.CharType.PRE.ordinal], context)
graphData.addBgReadings(menuChartSettings[0][OverviewMenus.CharType.PRE.ordinal], menuChartSettings[0][OverviewMenus.CharType.RAW.ordinal], context)
if (buildHelper.isDev()) graphData.addBucketedData()
graphData.addTreatments(context)
graphData.addEps(context, 0.95)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OverviewMenus @Inject constructor(
) {

enum class CharType(@StringRes val nameId: Int, @AttrRes val attrId: Int, @AttrRes val attrTextId: Int, val primary: Boolean, val secondary: Boolean, @StringRes val shortnameId: Int) {
RAW(R.string.overview_show_raw_bg, R.attr.defaultTextColor, R.attr.menuTextColorInverse, primary = true, secondary = false, shortnameId = R.string.activity_shortname),
PRE(R.string.overview_show_predictions, R.attr.predictionColor, R.attr.menuTextColor, primary = true, secondary = false, shortnameId = R.string.prediction_shortname),
TREAT(R.string.overview_show_treatments, R.attr.cobColor, R.attr.menuTextColor, primary = true, secondary = false, shortnameId = R.string.treatments_shortname),
BAS(R.string.overview_show_basals, R.attr.basal, R.attr.menuTextColor, primary = true, secondary = false, shortnameId = R.string.basal_shortname),
Expand Down Expand Up @@ -126,6 +127,7 @@ class OverviewMenus @Inject constructor(
if (g == 0 && !m.primary) return@forEach
if (g > 0 && !m.secondary) return@forEach
var insert = true
if (m == CharType.RAW) insert = sp.getBoolean(info.nightscout.androidaps.core.R.string.key_use_data_smoothing, false)
if (m == CharType.PRE) insert = predictionsAvailable
if (m == CharType.DEVSLOPE) insert = buildHelper.isDev()
if (used.contains(m.ordinal)) insert = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ class GraphData(
addSeries(overviewData.bucketedGraphSeries)
}

fun addBgReadings(addPredictions: Boolean, context: Context?) {
fun addBgReadings(addPredictions: Boolean, addRawBg: Boolean, context: Context?) {
maxY = if (overviewData.bgReadingsArray.isEmpty()) {
if (units == GlucoseUnit.MGDL) 180.0 else 10.0
} else overviewData.maxBgValue
minY = 0.0
if (addRawBg && !overviewData.bgReadingRawGraphSeries.isEmpty) addSeries(overviewData.bgReadingRawGraphSeries)
addSeries(overviewData.bgReadingGraphSeries)
if (addPredictions) addSeries(overviewData.predictionsGraphSeries)
overviewData.bgReadingGraphSeries.setOnDataPointTapListener { _, dataPoint ->
Expand Down Expand Up @@ -93,7 +94,7 @@ class GraphData(
maxY = maxOf(maxY, overviewData.maxTreatmentsValue)
addSeries(overviewData.treatmentsSeries)
overviewData.treatmentsSeries.setOnDataPointTapListener { _, dataPoint ->
if (dataPoint is BolusDataPoint) ToastUtils.showToastInUiThread(context, dataPoint.label)
if (dataPoint is BolusDataPoint) ToastUtils.infoToast(context, dataPoint.label)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.content.Context
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.GlucoseValue
import info.nightscout.androidaps.extensions.rawOrSmoothed
import info.nightscout.androidaps.extensions.useDataSmoothing
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.shared.sharedPreferences.SP

class GlucoseValueDataPoint(
val data: GlucoseValue,
Expand All @@ -17,22 +20,51 @@ class GlucoseValueDataPoint(
private val rh: ResourceHelper
) : DataPointWithLabelInterface {

private var useSmoothed : Boolean = false
private var isAdditional : Boolean = false

constructor(data: GlucoseValue,
defaultValueHelper: DefaultValueHelper,
profileFunction: ProfileFunction,
rh: ResourceHelper,
useSmoothed: Boolean,
isAdditional: Boolean = false) : this(
data,
defaultValueHelper,
profileFunction,
rh) {
this.useSmoothed = useSmoothed
this.isAdditional = isAdditional
}

constructor(data: GlucoseValue,
defaultValueHelper: DefaultValueHelper,
profileFunction: ProfileFunction,
rh: ResourceHelper,
sp: SP) : this(
data,
defaultValueHelper,
profileFunction,
rh) {
useSmoothed = useDataSmoothing(sp)
}
fun valueToUnits(units: GlucoseUnit): Double =
if (units == GlucoseUnit.MGDL) data.value else data.value * Constants.MGDL_TO_MMOLL
if (units == GlucoseUnit.MGDL) data.rawOrSmoothed(useSmoothed) else data.rawOrSmoothed(useSmoothed) * Constants.MGDL_TO_MMOLL

override fun getX(): Double = data.timestamp.toDouble()
override fun getY(): Double = valueToUnits(profileFunction.getUnits())

override fun setY(y: Double) {}
override val label: String = Profile.toCurrentUnitsString(profileFunction, data.value)
override val label: String = Profile.toCurrentUnitsString(profileFunction, data.rawOrSmoothed(useSmoothed))
override val duration = 0L
override val shape get() = if (isPrediction) PointsWithLabelGraphSeries.Shape.PREDICTION else PointsWithLabelGraphSeries.Shape.BG
override val size = 1f
override val size get() = if (isAdditional) 0.5f else 1f
override fun color(context: Context?): Int {
val units = profileFunction.getUnits()
val lowLine = defaultValueHelper.determineLowLine()
val highLine = defaultValueHelper.determineHighLine()
return when {
isAdditional -> rh.gac(context, R.attr.defaultTextColor)
isPrediction -> predictionColor(context)
valueToUnits(units) < lowLine -> rh.gac(context, R.attr.bgLow)
valueToUnits(units) > highLine -> rh.gac(context, R.attr.highColor)
Expand Down
Loading