optimize mgdl/mmol constants

This commit is contained in:
Milos Kozak 2023-09-19 16:30:40 +02:00
parent e7355de811
commit fd82ef371b
20 changed files with 30 additions and 32 deletions

View file

@ -52,7 +52,7 @@ class RealPumpTest {
configBuilderPlugin.performPluginSwitch(randomBgPlugin,true, PluginType.BGSOURCE)
// Profile
configBuilderPlugin.performPluginSwitch(localProfilePlugin, true, PluginType.PROFILE)
val profile = Profile(JSONObject(validProfile), Constants.MGDL)
val profile = Profile(JSONObject(validProfile), GlucoseUnit.MGDL.asText)
Assert.assertTrue(profile.isValid("Test"))
localProfilePlugin.profiles.clear()
localProfilePlugin.numOfProfiles = 0

View file

@ -154,7 +154,7 @@ class SetupWizardActivityTest {
onView(withId(R.id.finish_button)).waitAndPerform(click())
// Verify settings
Assert.assertEquals(Constants.MMOL, ProfileFunctions.getSystemUnits())
Assert.assertEquals(GlucoseUnit.MMOL.asText, ProfileFunctions.getSystemUnits())
Assert.assertEquals(17.0, HardLimits.maxBolus(), 0.0001) // Adult
Assert.assertTrue(RandomBgPlugin.isEnabled(PluginType.BGSOURCE))
Assert.assertTrue(LocalProfilePlugin.isEnabled(PluginType.PROFILE))

View file

@ -5,8 +5,6 @@ package info.nightscout.interfaces
*/
object Constants {
const val MGDL = "mg/dl" // This is Nightscout's representation
const val MMOL = "mmol"
const val MMOLL_TO_MGDL = 18.0 // 18.0182;
const val MGDL_TO_MMOLL = 1 / MMOLL_TO_MGDL
const val defaultDIA = 5.0

View file

@ -4,6 +4,7 @@ import info.nightscout.database.entities.TemporaryTarget
import info.nightscout.database.entities.TherapyEvent
import info.nightscout.database.entities.ValueWithUnit
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
sealed class ValueWithUnitMapper { //I use a sealed class because of StringResource that contains a listOf as second parameter
@ -79,12 +80,9 @@ sealed class ValueWithUnitMapper { //I use a sealed class because of St
companion object {
const val MGDL = Constants.MGDL
const val MMOL = Constants.MMOL
fun fromGlucoseUnit(value: Double, string: String): ValueWithUnitMapper? = when (string) {
MGDL, "mgdl" -> Mgdl(value)
MMOL, "mmol/l" -> Mmoll(value)
GlucoseUnit.MGDL.asText, "mgdl" -> Mgdl(value)
GlucoseUnit.MMOL.asText, "mmol/l" -> Mmoll(value)
else -> null
}
}

View file

@ -8,7 +8,6 @@ import info.nightscout.database.entities.ProfileSwitch
import info.nightscout.database.impl.AppRepository
import info.nightscout.database.impl.transactions.InsertOrUpdateProfileSwitch
import info.nightscout.interfaces.Config
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.nsclient.ProcessedDeviceStatusData
import info.nightscout.interfaces.plugin.ActivePlugin
@ -143,7 +142,7 @@ class ProfileFunctionImpl @Inject constructor(
}
override fun getUnits(): GlucoseUnit =
if (sp.getString(info.nightscout.core.utils.R.string.key_units, Constants.MGDL) == Constants.MGDL) GlucoseUnit.MGDL
if (sp.getString(info.nightscout.core.utils.R.string.key_units, GlucoseUnit.MGDL.asText) == GlucoseUnit.MGDL.asText) GlucoseUnit.MGDL
else GlucoseUnit.MMOL
override fun buildProfileSwitch(profileStore: ProfileStore, profileName: String, durationInMinutes: Int, percentage: Int, timeShiftInHours: Int, timestamp: Long): ProfileSwitch? {

View file

@ -101,7 +101,7 @@ class ActionStartTempTarget(injector: HasAndroidInjector) : Action(injector) {
override fun fromJSON(data: String): Action {
val o = JSONObject(data)
value.units = GlucoseUnit.fromText(JsonHelper.safeGetString(o, "units", Constants.MGDL))
value.units = GlucoseUnit.fromText(JsonHelper.safeGetString(o, "units", GlucoseUnit.MGDL.asText))
value.value = safeGetDouble(o, "value")
duration.setMinutes(JsonHelper.safeGetInt(o, "durationInMinutes"))
return this

View file

@ -10,7 +10,6 @@ import info.nightscout.automation.elements.LabelWithElement
import info.nightscout.automation.elements.LayoutBuilder
import info.nightscout.automation.elements.StaticLabel
import info.nightscout.core.utils.JsonHelper
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.rx.logging.LTag
import org.json.JSONObject
@ -71,7 +70,7 @@ class TriggerBg(injector: HasAndroidInjector) : Trigger(injector) {
override fun fromJSON(data: String): Trigger {
val d = JSONObject(data)
bg.setUnits(GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", Constants.MGDL)))
bg.setUnits(GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", GlucoseUnit.MGDL.asText)))
bg.value = JsonHelper.safeGetDouble(d, "bg")
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!))
return this

View file

@ -94,7 +94,7 @@ class TriggerDelta(injector: HasAndroidInjector) : Trigger(injector) {
override fun fromJSON(data: String): Trigger {
val d = JSONObject(data)
units = GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", Constants.MGDL))
units = GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", GlucoseUnit.MGDL.asText))
val type = DeltaType.valueOf(JsonHelper.safeGetString(d, "deltaType", ""))
val value = JsonHelper.safeGetDouble(d, "value")
delta =

View file

@ -68,7 +68,7 @@ class TriggerTempTargetValue(injector: HasAndroidInjector) : Trigger(injector) {
override fun fromJSON(data: String): Trigger {
val d = JSONObject(data)
ttValue.setUnits(GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", Constants.MGDL)))
ttValue.setUnits(GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", GlucoseUnit.MGDL.asText)))
ttValue.value = JsonHelper.safeGetDouble(d, "tt")
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!))
return this

View file

@ -884,7 +884,7 @@ class DataHandlerMobile @Inject constructor(
predictions.add(
EventData.SingleBg(
timeStamp = bg.data.timestamp,
glucoseUnits = Constants.MGDL,
glucoseUnits = GlucoseUnit.MGDL.asText,
sgv = bg.data.value,
high = 0.0,
low = 0.0,

View file

@ -131,7 +131,7 @@ class ProfileFragment : DaggerFragment() {
val pumpDescription = activePlugin.activePump.pumpDescription
if (profilePlugin.numOfProfiles == 0) profilePlugin.addNewProfile()
val currentProfile = profilePlugin.currentProfile() ?: return
val units = if (currentProfile.mgdl) Constants.MGDL else Constants.MMOL
val units = if (currentProfile.mgdl) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText
binding.name.removeTextChangedListener(textWatch)
binding.name.setText(currentProfile.name)
@ -173,7 +173,7 @@ class ProfileFragment : DaggerFragment() {
DecimalFormat("0.00"),
save
)
if (units == Constants.MGDL) {
if (units == GlucoseUnit.MGDL.asText) {
val isfRange = doubleArrayOf(HardLimits.MIN_ISF, HardLimits.MAX_ISF)
TimeListEdit(
requireContext(),

View file

@ -163,7 +163,7 @@ class ProfilePlugin @Inject constructor(
profile.put("basal", basal)
profile.put("target_low", targetLow)
profile.put("target_high", targetHigh)
profile.put("units", if (mgdl) Constants.MGDL else Constants.MMOL)
profile.put("units", if (mgdl) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText)
profile.put("timezone", TimeZone.getDefault().id)
}
val defaultUnits = JsonHelper.safeGetStringAllowNull(profile, "units", null)
@ -400,7 +400,7 @@ class ProfilePlugin @Inject constructor(
profile.put("basal", basal)
profile.put("target_low", targetLow)
profile.put("target_high", targetHigh)
profile.put("units", if (mgdl) Constants.MGDL else Constants.MMOL)
profile.put("units", if (mgdl) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText)
profile.put("timezone", TimeZone.getDefault().id)
store.put(name, profile)
}

View file

@ -11,6 +11,7 @@ import info.nightscout.database.impl.AppRepository
import info.nightscout.database.impl.transactions.CgmSourceTransaction
import info.nightscout.database.transactions.TransactionGlucoseValue
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.plugin.PluginBase
import info.nightscout.interfaces.plugin.PluginDescription
import info.nightscout.interfaces.plugin.PluginType
@ -63,7 +64,7 @@ class PoctechPlugin @Inject constructor(
val json = jsonArray.getJSONObject(i)
glucoseValues += TransactionGlucoseValue(
timestamp = json.getLong("date"),
value = if (safeGetString(json, "units", Constants.MGDL) == "mmol/L") json.getDouble("current") * Constants.MMOLL_TO_MGDL
value = if (safeGetString(json, "units", GlucoseUnit.MGDL.asText) == "mmol/L") json.getDouble("current") * Constants.MMOLL_TO_MGDL
else json.getDouble("current"),
raw = json.getDouble("raw"),
noise = null,

View file

@ -56,6 +56,7 @@ import info.nightscout.database.impl.transactions.UpdateNsIdTherapyEventTransact
import info.nightscout.database.transactions.TransactionGlucoseValue
import info.nightscout.interfaces.Config
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.logging.UserEntryLogger
import info.nightscout.interfaces.notifications.Notification
import info.nightscout.interfaces.nsclient.StoreDataForDb
@ -314,8 +315,8 @@ class StoreDataForDbImpl @Inject constructor(
note = "",
values = listOf(
ValueWithUnit.TherapyEventTTReason(tt.reason),
ValueWithUnit.fromGlucoseUnit(tt.lowTarget, Constants.MGDL),
ValueWithUnit.fromGlucoseUnit(tt.highTarget, Constants.MGDL).takeIf { tt.lowTarget != tt.highTarget },
ValueWithUnit.fromGlucoseUnit(tt.lowTarget, GlucoseUnit.MGDL.asText),
ValueWithUnit.fromGlucoseUnit(tt.highTarget, GlucoseUnit.MGDL.asText).takeIf { tt.lowTarget != tt.highTarget },
ValueWithUnit.Minute(TimeUnit.MILLISECONDS.toMinutes(tt.duration).toInt())
)
)

View file

@ -10,7 +10,7 @@ import info.nightscout.shared.utils.T
import org.json.JSONObject
fun TemporaryTarget.Companion.fromJson(jsonObject: JSONObject, profileUtil: ProfileUtil): TemporaryTarget? {
val units = GlucoseUnit.fromText(JsonHelper.safeGetString(jsonObject, "units", Constants.MGDL))
val units = GlucoseUnit.fromText(JsonHelper.safeGetString(jsonObject, "units", GlucoseUnit.MGDL.asText))
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val duration = JsonHelper.safeGetLongAllowNull(jsonObject, "duration", null) ?: return null
val durationInMilliseconds = JsonHelper.safeGetLongAllowNull(jsonObject, "durationInMilliseconds")

View file

@ -22,7 +22,7 @@ fun therapyEventFromNsMbg(mbg: NSMbg) =
)
fun TherapyEvent.Companion.fromJson(jsonObject: JSONObject): TherapyEvent? {
val glucoseUnit = if (JsonHelper.safeGetString(jsonObject, "units", Constants.MGDL) == Constants.MGDL) TherapyEvent.GlucoseUnit.MGDL else TherapyEvent.GlucoseUnit.MMOL
val glucoseUnit = if (JsonHelper.safeGetString(jsonObject, "units", GlucoseUnit.MGDL.asText) == GlucoseUnit.MGDL.asText) TherapyEvent.GlucoseUnit.MGDL else TherapyEvent.GlucoseUnit.MMOL
val timestamp = JsonHelper.safeGetLongAllowNull(jsonObject, "mills", null) ?: return null
val type = TherapyEvent.Type.fromString(JsonHelper.safeGetString(jsonObject, "eventType", TherapyEvent.Type.NONE.text))
val duration = JsonHelper.safeGetLong(jsonObject, "duration")
@ -57,7 +57,7 @@ fun TherapyEvent.toJson(isAdd: Boolean, dateUtil: DateUtil): JSONObject =
.put("isValid", isValid)
.put("created_at", dateUtil.toISOString(timestamp))
.put("enteredBy", enteredBy)
.put("units", if (glucoseUnit == TherapyEvent.GlucoseUnit.MGDL) Constants.MGDL else Constants.MMOL)
.put("units", if (glucoseUnit == TherapyEvent.GlucoseUnit.MGDL) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText)
.also {
if (duration != 0L) it.put("duration", T.msecs(duration).mins())
if (duration != 0L) it.put("durationInMilliseconds", duration)

View file

@ -1,6 +1,7 @@
package info.nightscout.pump.dana
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.profile.Instantiator
import info.nightscout.interfaces.profile.Profile
import info.nightscout.interfaces.profile.ProfileStore
@ -268,7 +269,7 @@ class DanaPump @Inject constructor(
var bolusCalculationOption = 0
var missedBolusConfig = 0
fun getUnits(): String {
return if (units == UNITS_MGDL) Constants.MGDL else Constants.MMOL
return if (units == UNITS_MGDL) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText
}
var bolusStartErrorCode: Int = 0 // last start bolus errorCode
@ -368,7 +369,7 @@ class DanaPump @Inject constructor(
.put("value", currentTarget)
)
)
profile.put("units", if (units == UNITS_MGDL) Constants.MGDL else Constants.MMOL)
profile.put("units", if (units == UNITS_MGDL) GlucoseUnit.MGDL.asText else GlucoseUnit.MMOL.asText)
store.put(PROFILE_PREFIX + (activeProfile + 1), profile)
} catch (e: JSONException) {
aapsLogger.error("Unhandled exception", e)

View file

@ -5,6 +5,7 @@ import android.os.Bundle
import info.nightscout.core.ui.activities.TranslatedDaggerAppCompatActivity
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.interfaces.Constants
import info.nightscout.interfaces.GlucoseUnit
import info.nightscout.interfaces.plugin.ActivePlugin
import info.nightscout.interfaces.pump.defs.PumpType
import info.nightscout.interfaces.queue.Callback
@ -127,7 +128,7 @@ class DanaUserOptionsActivity : TranslatedDaggerAppCompatActivity() {
binding.beep.isChecked = danaPump.beepAndAlarm > 4
binding.screenTimeout.value = danaPump.lcdOnTimeSec.toDouble()
binding.backlight.value = danaPump.backlightOnTimeSec.toDouble()
binding.units.isChecked = danaPump.getUnits() == Constants.MMOL
binding.units.isChecked = danaPump.getUnits() == GlucoseUnit.MMOL.asText
binding.shutdown.value = danaPump.shutdownHour.toDouble()
binding.lowReservoir.value = danaPump.lowReservoirRate.toDouble()
}

View file

@ -32,7 +32,7 @@ abstract class PumpStatus(var pumpType: PumpType) {
// TDD
var dailyTotalUnits: Double? = null
var maxDailyTotalUnits: String? = null
var units: String? = null // Constants.MGDL or Constants.MMOL
var units: String? = null // GlucoseUnit.MGDL.asText or GlucoseUnit.MMOL.asText
var pumpRunningState = PumpRunningState.Running
var basalsByHour: DoubleArray? = null
var tempBasalStart: Long? = null

View file

@ -364,7 +364,7 @@ class WizardDialog : DaggerDialogFragment() {
}
private fun valueToUnitsToString(value: Double, units: String): String =
if (units == Constants.MGDL) decimalFormatter.to0Decimal(value)
if (units == GlucoseUnit.MGDL.asText) decimalFormatter.to0Decimal(value)
else decimalFormatter.to1Decimal(value * Constants.MGDL_TO_MMOLL)
private fun initDialog() {