First draft with bug

This commit is contained in:
Philoul 2021-02-25 12:28:36 +01:00
parent 51add9a4fd
commit 274cac8ff9
8 changed files with 100 additions and 13 deletions

View file

@ -10,10 +10,12 @@ import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.*
import info.nightscout.androidaps.databinding.DialogCalibrationBinding
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.utils.extensions.fromStringKey
import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.XdripCalibrations
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
@ -78,7 +80,7 @@ class CalibrationDialog : DialogFragmentWithDate() {
if (bg > 0) {
activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
uel.log(UserEntry.Action.CALIBRATION, d1 = bg)
uel.log(Action.CALIBRATION, d1 = ValueWithUnit(bg, if (units == Constants.MMOL) Units.Mg_Dl else Units.Mmol_L))
xdripCalibrations.sendIntent(bg)
})
}

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.logging
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.*
import info.nightscout.androidaps.database.transactions.UserEntryTransaction
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.CompositeDisposable
@ -19,7 +20,7 @@ class UserEntryLogger @Inject constructor(
private val compositeDisposable = CompositeDisposable()
fun log(action: UserEntry.Action, s: String = "", d1: Double = 0.0, d2: Double = 0.0, i1: Int = 0, i2: Int = 0) {
fun log(action: UserEntry.Action, s: String = "", d1: ValueWithUnit = ValueWithUnit(0.0,Units.None), d2: ValueWithUnit = ValueWithUnit(0.0,Units.None), i1: ValueWithUnit = ValueWithUnit(0,Units.None), i2: ValueWithUnit = ValueWithUnit(0,Units.None)) {
compositeDisposable += repository.runTransaction(UserEntryTransaction(
action = action,
s = s,
@ -31,8 +32,8 @@ class UserEntryLogger @Inject constructor(
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.io)
.subscribeBy(
onError = { aapsLogger.debug("ERRORED USER ENTRY: $action $s ${if (d1 == 0.0) d1 else ""} ${if (d2 == 0.0) d2 else ""} ${if (i1 == 0) i1 else ""} ${if (i2 == 0) i2 else ""}") },
onComplete = { aapsLogger.debug("USER ENTRY: $action $s ${if (d1 == 0.0) d1 else ""} ${if (d2 == 0.0) d2 else ""} ${if (i1 == 0) i1 else ""} ${if (i2 == 0) i2 else ""}") }
onError = { aapsLogger.debug("ERRORED USER ENTRY: $action $s ${if (d1.dValue != 0.0) d1 else ""} ${if (d2.dValue != 0.0) d2 else ""} ${if (i1.iValue != 0) i1 else ""} ${if (i2.iValue != 0) i2 else ""}") },
onComplete = { aapsLogger.debug("USER ENTRY: $action $s ${if (d1.dValue != 0.0) d1 else ""} ${if (d2.dValue != 0.0) d2 else ""} ${if (i1.iValue != 0) i1 else ""} ${if (i2.iValue != 0) i2 else ""}") }
)
}
}

View file

@ -0,0 +1,36 @@
package info.nightscout.androidaps.utils.extensions
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.utils.resources.ResourceHelper
fun UserEntry.Units.stringId(): Int {
return when {
this == UserEntry.Units.Mg_Dl -> R.string.mgdl
this == UserEntry.Units.Mmol_L -> R.string.mmol
this == UserEntry.Units.U -> R.string.insulin_unit_shortname
this == UserEntry.Units.U_H -> R.string.profile_ins_units_per_hour
this == UserEntry.Units.G -> R.string.shortgram
this == UserEntry.Units.M -> R.string.shortminute
this == UserEntry.Units.H -> R.string.shorthour
this == UserEntry.Units.Percent -> R.string.percent
else -> 0
}
}
fun UserEntry.Units.stringkey(): String {
return when {
this == UserEntry.Units.Mg_Dl -> Constants.MGDL
this == UserEntry.Units.Mmol_L -> Constants.MMOL
this == UserEntry.Units.U -> UserEntry.Units.U.name
this == UserEntry.Units.U_H -> UserEntry.Units.U_H.name
this == UserEntry.Units.G -> UserEntry.Units.G.name
this == UserEntry.Units.M -> UserEntry.Units.M.name
this == UserEntry.Units.H -> UserEntry.Units.H.name
this == UserEntry.Units.Percent -> "%"
else -> ""
}
}
fun UserEntry.Units.Companion(source: String?) = UserEntry.Units.values().firstOrNull { it.stringkey() == source } ?: UserEntry.Units.None

View file

@ -92,6 +92,8 @@
<string name="eventtype">Event type</string>
<string name="mgdl">mg/dl</string>
<string name="mmol">mmol/l</string>
<string name="shortgram">g</string>
<string name="percent">%%</string>
<string name="advancedsettings_title">Advanced Settings</string>
<string name="bluetooth">Bluetooth</string>
<string name="btwatchdog_title">BT Watchdog</string>

View file

@ -16,6 +16,23 @@ class Converters {
@TypeConverter
fun toAction(action: String?) = action?.let { UserEntry.Action.fromString(it) }
@TypeConverter
fun fromValueWithUnit(valueWithUnit: UserEntry.ValueWithUnit?): String? {
if (valueWithUnit == null) return null
val jsonObject = JSONObject()
jsonObject.put("dValue", valueWithUnit.dValue)
jsonObject.put("iValue", valueWithUnit.iValue)
jsonObject.put("unit", valueWithUnit.unit.name)
return jsonObject.toString()
}
@TypeConverter
fun toValueWithUnit(jsonString: String?): UserEntry.ValueWithUnit? {
if (jsonString == null) return null
val jsonObject = JSONObject(jsonString)
return UserEntry.ValueWithUnit(jsonObject.getDouble("dValue"), jsonObject.getInt("iValue"),UserEntry.Units.fromString(jsonObject.getString("unit")) )
}
@TypeConverter
fun fromBolusType(bolusType: Bolus.Type?) = bolusType?.name

View file

@ -33,4 +33,10 @@ open class DatabaseModule {
database.execSQL("CREATE TABLE IF NOT EXISTS userEntry (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `timestamp` INTEGER NOT NULL, `utcOffset` INTEGER NOT NULL, `action` TEXT NOT NULL, `s` TEXT NOT NULL, `d1` REAL NOT NULL, `d2` REAL NOT NULL, `i1` INTEGER NOT NULL, `i2` INTEGER NOT NULL)")
}
}
private val migration3to4 = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS userEntry (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `timestamp` INTEGER NOT NULL, `utcOffset` INTEGER NOT NULL, `action` TEXT NOT NULL, `s` TEXT NOT NULL, `d1` TEXT NOT NULL, `d2` TEXT NOT NULL, `i1` TEXT NOT NULL, `i2` TEXT NOT NULL)")
}
}
}

View file

@ -17,10 +17,10 @@ data class UserEntry(
override var utcOffset: Long = TimeZone.getDefault().getOffset(timestamp).toLong(),
var action: Action,
var s: String,
var d1: Double,
var d2: Double,
var i1: Int,
var i2: Int
var d1: ValueWithUnit,
var d2: ValueWithUnit,
var i1: ValueWithUnit,
var i2: ValueWithUnit
) : DBEntry, DBEntryWithTime {
enum class Action () {
@SerializedName("BOLUS") BOLUS,
@ -123,4 +123,26 @@ data class UserEntry(
fun fromString(source: String?) = UserEntry.Action.values().firstOrNull { it.name == source } ?: UserEntry.Action.UNKNOWN
}
}
data class ValueWithUnit (val dValue: Double, val iValue: Int, val unit: Units) {
constructor(value:Int, unit:Units) : this(0.0, value, unit)
constructor(value:Double, unit:Units) : this(value,0, unit)
}
enum class Units {
@SerializedName("None") None,
@SerializedName("Mg_Dl") Mg_Dl,
@SerializedName("Mmol_L") Mmol_L,
@SerializedName("U") U,
@SerializedName("U_H") U_H,
@SerializedName("G") G,
@SerializedName("M") M,
@SerializedName("H") H,
@SerializedName("Percent") Percent
;
companion object {
fun fromString(unit: String?) = UserEntry.Units.values().firstOrNull { it.name == unit } ?: UserEntry.Units.None
}
}
}

View file

@ -1,14 +1,15 @@
package info.nightscout.androidaps.database.transactions
import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.UserEntry.*
class UserEntryTransaction(
val action: UserEntry.Action,
val action: Action,
val s: String = "",
val d1: Double = 0.0,
val d2: Double = 0.0,
val i1: Int = 0,
val i2: Int = 0,
val d1: ValueWithUnit = ValueWithUnit(0.0, Units.None),
val d2: ValueWithUnit = ValueWithUnit(0.0, Units.None),
val i1: ValueWithUnit = ValueWithUnit(0, Units.None),
val i2: ValueWithUnit = ValueWithUnit(0, Units.None)
) : Transaction<Unit>() {
override fun run() {