diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt index d1f02d3871..6b6cb588e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/DexcomPlugin.kt @@ -8,7 +8,6 @@ import androidx.work.Worker import androidx.work.WorkerParameters import androidx.work.workDataOf import dagger.android.HasAndroidInjector -import info.nightscout.androidaps.interfaces.Config import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.RequestDexcomPermissionActivity import info.nightscout.androidaps.database.AppRepository @@ -18,10 +17,8 @@ import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.database.transactions.CgmSourceTransaction -import info.nightscout.androidaps.interfaces.BgSource -import info.nightscout.androidaps.interfaces.PluginBase -import info.nightscout.androidaps.interfaces.PluginDescription -import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.extensions.fromConstant +import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger @@ -126,11 +123,12 @@ class DexcomPlugin @Inject constructor( meters.getBundle(i.toString())?.let { val timestamp = it.getLong("timestamp") * 1000 val now = dateUtil.now() + val value = it.getInt("meterValue").toDouble() if (timestamp > now - T.months(1).msecs() && timestamp < now) { calibrations.add(CgmSourceTransaction.Calibration( timestamp = it.getLong("timestamp") * 1000, - value = it.getInt("meterValue").toDouble(), - glucoseUnit = TherapyEvent.GlucoseUnit.MGDL + value = value, + glucoseUnit = TherapyEvent.GlucoseUnit.fromConstant(Profile.unit(value)) )) } } diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Profile.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/Profile.kt index a3b1328cb9..3d43cdc60f 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Profile.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Profile.kt @@ -165,16 +165,20 @@ interface Profile { if (units == GlucoseUnit.MGDL) (if (valueInMgdl > 0) "+" else "") + to0Decimal(valueInMgdl) else (if (valueInMmol > 0) "+" else "") + to1Decimal(valueInMmol) + fun isMgdl(anyBg: Double) = anyBg >= 39 + fun isMmol(anyBg: Double) = anyBg < 39 + fun unit(anyBg: Double) = if (isMgdl(anyBg)) GlucoseUnit.MGDL else GlucoseUnit.MMOL + fun toCurrentUnits(profileFunction: ProfileFunction, anyBg: Double): Double = - if (anyBg < 32) fromMmolToUnits(anyBg, profileFunction.getUnits()) + if (isMmol(anyBg)) fromMmolToUnits(anyBg, profileFunction.getUnits()) else fromMgdlToUnits(anyBg, profileFunction.getUnits()) fun toCurrentUnits(units: GlucoseUnit, anyBg: Double): Double = - if (anyBg < 32) fromMmolToUnits(anyBg, units) + if (isMmol(anyBg)) fromMmolToUnits(anyBg, units) else fromMgdlToUnits(anyBg, units) fun toCurrentUnitsString(profileFunction: ProfileFunction, anyBg: Double): String = - if (anyBg < 32) toUnitsString(anyBg * Constants.MMOLL_TO_MGDL, anyBg, profileFunction.getUnits()) + if (isMmol(anyBg)) toUnitsString(anyBg * Constants.MMOLL_TO_MGDL, anyBg, profileFunction.getUnits()) else toUnitsString(anyBg, anyBg * Constants.MGDL_TO_MMOLL, profileFunction.getUnits()) fun toMgdl(value: Double, units: GlucoseUnit): Double =