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

Dexcom G7 with back smoothing values based on @justmara 's approach for dev #5

Open
wants to merge 2 commits into
base: dev-g7
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 @@ -367,7 +367,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 @@ -798,7 +798,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.value_unavailable_short)
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 @@ -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 @@ -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 @@ -86,7 +86,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 @@ -36,6 +36,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 @@ -53,6 +54,7 @@ class BGSourceFragment : DaggerFragment(), MenuProvider {
@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 @@ -137,7 +139,7 @@ class BGSourceFragment : DaggerFragment(), MenuProvider {
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 @@ -178,7 +180,7 @@ class BGSourceFragment : DaggerFragment(), MenuProvider {
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 @@ -149,8 +149,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 @@ -134,6 +134,7 @@ class IntelligoPlugin @Inject constructor(
timestamp = timestamp,
value = value * Constants.MMOLL_TO_MGDL,
raw = 0.0,
smoothed = null,
noise = null,
trendArrow = GlucoseValue.TrendArrow.NONE,
sourceSensor = GlucoseValue.SourceSensor.INTELLIGO_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
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class QuickWizardEntry @Inject constructor(private val injector: HasAndroidInjec
//BG
var bg = 0.0
if (useBG() == YES) {
bg = lastBG.valueToUnits(profileFunction.getUnits())
bg = lastBG.valueToUnits(profileFunction.getUnits(), sp)
}
// COB
val cob =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.extensions.rawOrSmoothed
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.IobCobCalculator
import info.nightscout.androidaps.interfaces.Profile
Expand All @@ -18,6 +20,7 @@ import info.nightscout.androidaps.receivers.DataWorkerStorage
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.Round
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import java.util.ArrayList
import javax.inject.Inject

Expand All @@ -29,6 +32,7 @@ class PrepareBgDataWorker(
@Inject lateinit var dataWorkerStorage: DataWorkerStorage
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var sp: SP
@Inject lateinit var defaultValueHelper: DefaultValueHelper
@Inject lateinit var repository: AppRepository

Expand All @@ -45,14 +49,15 @@ class PrepareBgDataWorker(

val data = dataWorkerStorage.pickupObject(inputData.getLong(DataWorkerStorage.STORE_KEY, -1)) as PrepareBgData?
?: return Result.failure(workDataOf("Error" to "missing input data"))
val useSmoothed = sp.getBoolean(R.string.key_use_data_smoothing, false)

data.overviewData.maxBgValue = Double.MIN_VALUE
data.overviewData.bgReadingsArray = repository.compatGetBgReadingsDataFromTime(data.overviewData.fromTime, data.overviewData.toTime, false).blockingGet()
val bgListArray: MutableList<DataPointWithLabelInterface> = ArrayList()
for (bg in data.overviewData.bgReadingsArray) {
if (bg.timestamp < data.overviewData.fromTime || bg.timestamp > data.overviewData.toTime) continue
if (bg.value > data.overviewData.maxBgValue) data.overviewData.maxBgValue = bg.value
bgListArray.add(GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh))
if (bg.rawOrSmoothed(useSmoothed) > data.overviewData.maxBgValue) data.overviewData.maxBgValue = bg.rawOrSmoothed(useSmoothed)
bgListArray.add(GlucoseValueDataPoint(bg, defaultValueHelper, profileFunction, rh, sp))
}
bgListArray.sortWith { o1: DataPointWithLabelInterface, o2: DataPointWithLabelInterface -> o1.x.compareTo(o2.x) }
data.overviewData.bgReadingGraphSeries = PointsWithLabelGraphSeries(Array(bgListArray.size) { i -> bgListArray[i] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import info.nightscout.androidaps.plugins.general.overview.graphExtensions.Point
import info.nightscout.androidaps.receivers.DataWorkerStorage
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject

class PrepareBucketedDataWorker(
Expand All @@ -25,6 +26,7 @@ class PrepareBucketedDataWorker(
@Inject lateinit var profileFunction: ProfileFunction
@Inject lateinit var rh: ResourceHelper
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP

init {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
Expand All @@ -48,7 +50,7 @@ class PrepareBucketedDataWorker(
val bucketedListArray: MutableList<DataPointWithLabelInterface> = ArrayList()
for (inMemoryGlucoseValue in bucketedData) {
if (inMemoryGlucoseValue.timestamp < data.overviewData.fromTime || inMemoryGlucoseValue.timestamp > data.overviewData.toTime) continue
bucketedListArray.add(InMemoryGlucoseValueDataPoint(inMemoryGlucoseValue, profileFunction, rh))
bucketedListArray.add(InMemoryGlucoseValueDataPoint(inMemoryGlucoseValue, profileFunction, rh, sp))
}
bucketedListArray.sortWith { o1: DataPointWithLabelInterface, o2: DataPointWithLabelInterface -> o1.x.compareTo(o2.x) }
data.overviewData.bucketedGraphSeries = PointsWithLabelGraphSeries(Array(bucketedListArray.size) { i -> bucketedListArray[i] })
Expand Down
Loading