Skip to content

Commit

Permalink
Smoothing migrated to master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
khskekec committed Nov 8, 2022
1 parent 1de236a commit ce81e8c
Show file tree
Hide file tree
Showing 46 changed files with 3,923 additions and 69 deletions.
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 @@ -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
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ 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.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,
private val defaultValueHelper: DefaultValueHelper,
private val profileFunction: ProfileFunction,
private val rh: ResourceHelper
private val rh: ResourceHelper,
private val sp: SP
) : DataPointWithLabelInterface {

fun valueToUnits(units: GlucoseUnit): Double =
if (units == GlucoseUnit.MGDL) data.value else data.value * Constants.MGDL_TO_MMOLL
if (units == GlucoseUnit.MGDL) data.rawOrSmoothed(sp) else data.rawOrSmoothed(sp) * 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(sp))
override val duration = 0L
override val shape get() = if (isPrediction) PointsWithLabelGraphSeries.Shape.PREDICTION else PointsWithLabelGraphSeries.Shape.BG
override val size = 1f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import info.nightscout.androidaps.data.InMemoryGlucoseValue
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP

class InMemoryGlucoseValueDataPoint(
val data: InMemoryGlucoseValue,
private val profileFunction: ProfileFunction,
private val rh: ResourceHelper
private val rh: ResourceHelper,
private val sp: SP
) : DataPointWithLabelInterface {

fun valueToUnits(units: GlucoseUnit): Double =
if (units == GlucoseUnit.MGDL) data.value else data.value * Constants.MGDL_TO_MMOLL
if (units == GlucoseUnit.MGDL) data.rawOrSmoothed(sp)
else data.rawOrSmoothed(sp) * Constants.MGDL_TO_MMOLL

override fun getX(): Double = data.timestamp.toDouble()
override fun getY(): Double = valueToUnits(profileFunction.getUnits())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import javax.inject.Inject
Expand All @@ -34,6 +35,7 @@ class PersistentNotificationPlugin @Inject constructor(
injector: HasAndroidInjector,
aapsLogger: AAPSLogger,
rh: ResourceHelper,
private val sp: SP,
private val aapsSchedulers: AapsSchedulers,
private val profileFunction: ProfileFunction,
private val fabricPrivacy: FabricPrivacy,
Expand Down Expand Up @@ -112,7 +114,7 @@ class PersistentNotificationPlugin @Inject constructor(
val lastBG = iobCobCalculator.ads.lastBg()
val glucoseStatus = glucoseStatusProvider.glucoseStatusData
if (lastBG != null) {
line1aa = lastBG.valueToUnitsString(units)
line1aa = lastBG.valueToUnitsString(units, sp)
line1 = line1aa
if (glucoseStatus != null) {
line1 += (" Δ" + Profile.toSignedUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ class SmsCommunicatorPlugin @Inject constructor(
var reply = ""
val units = profileFunction.getUnits()
if (actualBG != null) {
reply = rh.gs(R.string.sms_actualbg) + " " + actualBG.valueToUnitsString(units) + ", "
reply = rh.gs(R.string.sms_actualbg) + " " + actualBG.valueToUnitsString(units, sp) + ", "
} else if (lastBG != null) {
val agoMilliseconds = dateUtil.now() - lastBG.timestamp
val agoMin = (agoMilliseconds / 60.0 / 1000.0).toInt()
reply = rh.gs(R.string.sms_lastbg) + " " + lastBG.valueToUnitsString(units) + " " + rh.gs(R.string.sms_minago, agoMin) + ", "
reply = rh.gs(R.string.sms_lastbg) + " " + lastBG.valueToUnitsString(units, sp) + " " + rh.gs(R.string.sms_minago, agoMin) + ", "
}
val glucoseStatus = glucoseStatusProvider.glucoseStatusData
if (glucoseStatus != null) reply += rh.gs(R.string.sms_delta) + " " + Profile.toUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) + " " + units + ", "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class DataHandlerMobile @Inject constructor(
tempTarget = tempTarget,
carbs = carbsAfterConstraints,
cob = cobInfo.displayCob!!,
bg = bgReading.valueToUnits(profileFunction.getUnits()),
bg = bgReading.valueToUnits(profileFunction.getUnits(), sp),
correction = 0.0,
percentageCorrection = percentage,
useBg = sp.getBoolean(R.string.key_wearwizard_bg, true),
Expand Down Expand Up @@ -830,7 +830,7 @@ class DataHandlerMobile @Inject constructor(
val finalLastRun = loop.lastRun
if (sp.getBoolean("wear_predictions", true) && finalLastRun?.request?.hasPredictions == true && finalLastRun.constraintsProcessed != null) {
val predArray = finalLastRun.constraintsProcessed!!.predictions
.stream().map { bg: GlucoseValue -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh) }
.stream().map { bg: GlucoseValue -> GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh, sp) }
.collect(Collectors.toList())
if (predArray.isNotEmpty())
for (bg in predArray) if (bg.data.value > 39)
Expand Down Expand Up @@ -917,7 +917,7 @@ class DataHandlerMobile @Inject constructor(

return EventData.SingleBg(
timeStamp = glucoseValue.timestamp,
sgvString = glucoseValue.valueToUnitsString(units),
sgvString = glucoseValue.valueToUnitsString(units, sp),
glucoseUnits = units.asText,
slopeArrow = trendCalculator.getTrendArrow(glucoseValue).symbol,
delta = glucoseStatus?.let { deltaString(it.delta, it.delta * Constants.MGDL_TO_MMOLL, units) } ?: "--",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class IobCobCalculatorPlugin @Inject constructor(
private var iobTable = LongSparseArray<IobTotal>() // oldest at index 0
private var basalDataTable = LongSparseArray<BasalData>() // oldest at index 0

override var ads: AutosensDataStore = AutosensDataStore()
override var ads: AutosensDataStore = AutosensDataStore(sp)

private val dataLock = Any()
private var thread: Thread? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ class IobCobOref1Worker(
//console.error(bgTime , bucketed_data[i].glucose);
var avgDelta: Double
var delta: Double
val bg: Double = bucketedData[i].value
if (bg < 39 || bucketedData[i + 3].value < 39) {
val bg: Double = bucketedData[i].rawOrSmoothed(sp)
if (bg < 39 || bucketedData[i + 3].rawOrSmoothed(sp) < 39) {
aapsLogger.error("! value < 39")
continue
}
autosensData.bg = bg
delta = bg - bucketedData[i + 1].value
avgDelta = (bg - bucketedData[i + 3].value) / 3
delta = bg - bucketedData[i + 1].rawOrSmoothed(sp)
avgDelta = (bg - bucketedData[i + 3].rawOrSmoothed(sp)) / 3
val iob = data.iobCobCalculator.calculateFromTreatmentsAndTemps(bgTime, profile)
val bgi = -iob.activity * sens * 5
val deviation = delta - bgi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ class IobCobOrefWorker @Inject internal constructor(
//console.error(bgTime , bucketed_data[i].glucose);
var avgDelta: Double
var delta: Double
val bg: Double = bucketedData[i].value
if (bg < 39 || bucketedData[i + 3].value < 39) {
val bg: Double = bucketedData[i].rawOrSmoothed(sp)
if (bg < 39 || bucketedData[i + 3].rawOrSmoothed(sp) < 39) {
aapsLogger.error("! value < 39")
continue
}
autosensData.bg = bg
delta = bg - bucketedData[i + 1].value
avgDelta = (bg - bucketedData[i + 3].value) / 3
delta = bg - bucketedData[i + 1].rawOrSmoothed(sp)
avgDelta = (bg - bucketedData[i + 3].rawOrSmoothed(sp)) / 3
val iob = data.iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile)
val bgi = -iob.activity * sens * 5
val deviation = delta - bgi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class AidexPlugin @Inject constructor(
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = timestamp,
value = bgValueTarget,
smoothed = null,
raw = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(bundle.getString(Intents.AIDEX_BG_SLOPE_NAME)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import java.util.concurrent.TimeUnit
Expand All @@ -51,6 +52,7 @@ class BGSourceFragment : DaggerFragment() {
@Inject lateinit var uel: UserEntryLogger
@Inject lateinit var activePlugin: ActivePlugin
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP

private val disposable = CompositeDisposable()
private val millsToThePast = T.hours(36).msecs()
Expand Down Expand Up @@ -139,7 +141,7 @@ class BGSourceFragment : DaggerFragment() {
holder.binding.date.visibility = newDay.toVisibility()
holder.binding.date.text = if (newDay) dateUtil.dateStringRelative(glucoseValue.timestamp, rh) else ""
holder.binding.time.text = dateUtil.timeString(glucoseValue.timestamp)
holder.binding.value.text = glucoseValue.valueToUnitsString(profileFunction.getUnits())
holder.binding.value.text = glucoseValue.valueToUnitsString(profileFunction.getUnits(), sp)
holder.binding.direction.setImageResource(glucoseValue.trendArrow.directionToIcon())
if (position > 0) {
val previous = glucoseValues[position - 1]
Expand Down Expand Up @@ -180,7 +182,7 @@ class BGSourceFragment : DaggerFragment() {
private fun getConfirmationText(selectedItems: SparseArray<GlucoseValue>): String {
if (selectedItems.size() == 1) {
val glucoseValue = selectedItems.valueAt(0)
return dateUtil.dateAndTimeString(glucoseValue.timestamp) + "\n" + glucoseValue.valueToUnitsString(profileFunction.getUnits())
return dateUtil.dateAndTimeString(glucoseValue.timestamp) + "\n" + glucoseValue.valueToUnitsString(profileFunction.getUnits(), sp)
}
return rh.gs(R.string.confirm_remove_multiple_items, selectedItems.size())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ class DexcomPlugin @Inject constructor(
glucoseValues += CgmSourceTransaction.TransactionGlucoseValue(
timestamp = timestamp,
value = glucoseValueBundle.getInt("glucoseValue").toDouble(),
noise = null,
raw = null,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(glucoseValueBundle.getString("trendArrow")!!),
sourceSensor = sourceSensor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class EversensePlugin @Inject constructor(
timestamp = glucoseTimestamps[i],
value = glucoseLevels[i].toDouble(),
raw = glucoseLevels[i].toDouble(),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.EVERSENSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class GlimpPlugin @Inject constructor(
timestamp = inputData.getLong("myTimestamp", 0),
value = inputData.getDouble("mySGV", 0.0),
raw = inputData.getDouble("mySGV", 0.0),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(inputData.getString("myTrend")),
sourceSensor = GlucoseValue.SourceSensor.GLIMP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class GlunovoPlugin @Inject constructor(
timestamp = timestamp,
value = value * Constants.MMOLL_TO_MGDL,
raw = 0.0,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class MM640gPlugin @Inject constructor(
timestamp = jsonObject.getLong("date"),
value = jsonObject.getDouble("sgv"),
raw = jsonObject.getDouble("sgv"),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(jsonObject.getString("direction")),
sourceSensor = GlucoseValue.SourceSensor.MM_600_SERIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ class NSClientSourcePlugin @Inject constructor(
return CgmSourceTransaction.TransactionGlucoseValue(
timestamp = sgv.mills ?: return null,
value = sgv.mgdl?.toDouble() ?: return null,
noise = null,
raw = sgv.filtered?.toDouble() ?: sgv.mgdl?.toDouble(),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(sgv.direction),
nightscoutId = sgv.id,
sourceSensor = GlucoseValue.SourceSensor.fromString(sgv.device)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class PoctechPlugin @Inject constructor(
timestamp = json.getLong("date"),
value = if (safeGetString(json, "units", Constants.MGDL) == "mmol/L") json.getDouble("current") * Constants.MMOLL_TO_MGDL
else json.getDouble("current"),
smoothed = null,
raw = json.getDouble("raw"),
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(json.getString("direction")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class RandomBgPlugin @Inject constructor(
value = bgMgdl,
raw = 0.0,
noise = null,
smoothed = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.RANDOM
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class TomatoPlugin @Inject constructor(
value = inputData.getDouble("com.fanqies.tomatofn.Extras.BgEstimate", 0.0),
raw = 0.0,
noise = null,
smoothed = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.LIBRE_1_TOMATO
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class XdripPlugin @Inject constructor(
timestamp = bundle.getLong(Intents.EXTRA_TIMESTAMP, 0),
value = bundle.getDouble(Intents.EXTRA_BG_ESTIMATE, 0.0),
raw = bundle.getDouble(Intents.EXTRA_RAW, 0.0),
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.fromString(bundle.getString(Intents.EXTRA_BG_SLOPE_NAME)),
sourceSensor = GlucoseValue.SourceSensor.fromString(bundle.getString(Intents.XDRIP_DATA_SOURCE_DESCRIPTION)
Expand Down
Loading

0 comments on commit ce81e8c

Please sign in to comment.