Exemple with CalibrationDialog and CarbsDialog.kt

Good Direction ?
This commit is contained in:
Philoul 2021-02-27 15:45:47 +01:00
parent 274cac8ff9
commit 95e32c3406
6 changed files with 96 additions and 37 deletions

View file

@ -9,13 +9,11 @@ import dagger.android.HasAndroidInjector
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
@ -80,7 +78,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(Action.CALIBRATION, d1 = ValueWithUnit(bg, if (units == Constants.MMOL) Units.Mg_Dl else Units.Mmol_L))
uel.log(Action.CALIBRATION, ValueWithUnit(bg, if (units == Constants.MMOL) Units.Mmol_L else Units.Mg_Dl))
xdripCalibrations.sendIntent(bg)
})
}

View file

@ -11,7 +11,7 @@ import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainApp
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.DialogCarbsBinding
import info.nightscout.androidaps.db.CareportalEvent
import info.nightscout.androidaps.db.Source
@ -213,7 +213,7 @@ class CarbsDialog : DialogFragmentWithDate() {
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.carbs), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), {
when {
activitySelected -> {
uel.log(UserEntry.Action.TT_ACTIVITY, d1 = activityTT, i1 = activityTTDuration)
uel.log(Action.TT_ACTIVITY, ValueWithUnit(activityTT, if (units == Constants.MMOL) Units.Mmol_L else Units.Mg_Dl) , ValueWithUnit(activityTTDuration, Units.M))
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(activityTTDuration)
@ -225,7 +225,7 @@ class CarbsDialog : DialogFragmentWithDate() {
}
eatingSoonSelected -> {
uel.log(UserEntry.Action.TT_EATING_SOON, d1 = eatingSoonTT, i1 = eatingSoonTTDuration)
uel.log(Action.TT_EATING_SOON, ValueWithUnit(eatingSoonTT, if (units == Constants.MMOL) Units.Mmol_L else Units.Mg_Dl) , ValueWithUnit(eatingSoonTTDuration, Units.M))
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(eatingSoonTTDuration)
@ -237,7 +237,7 @@ class CarbsDialog : DialogFragmentWithDate() {
}
hypoSelected -> {
uel.log(UserEntry.Action.TT_HYPO, d1 = hypoTT, i1 = hypoTTDuration)
uel.log(Action.TT_HYPO, ValueWithUnit(hypoTT, if (units == Constants.MMOL) Units.Mmol_L else Units.Mg_Dl) , ValueWithUnit(hypoTTDuration, Units.M))
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(hypoTTDuration)
@ -250,10 +250,10 @@ class CarbsDialog : DialogFragmentWithDate() {
}
if (carbsAfterConstraints > 0) {
if (duration == 0) {
uel.log(UserEntry.Action.CARBS, d1 = carbsAfterConstraints.toDouble(), i1 = timeOffset)
uel.log(Action.CARBS, ValueWithUnit(carbsAfterConstraints.toDouble(), Units.G), ValueWithUnit(timeOffset, Units.M))
carbsGenerator.createCarb(carbsAfterConstraints, time, CareportalEvent.CARBCORRECTION, notes)
} else {
uel.log(UserEntry.Action.CARBS, d1 = carbsAfterConstraints.toDouble(), i1 = timeOffset, i2 = duration)
uel.log(Action.CARBS, ValueWithUnit(carbsAfterConstraints.toDouble(), Units.G), ValueWithUnit(timeOffset,Units.M), ValueWithUnit(duration, Units.H))
carbsGenerator.generateCarbs(carbsAfterConstraints, time, duration, notes)
nsUpload.uploadEvent(CareportalEvent.NOTE, DateUtil.now() - 2000, resourceHelper.gs(R.string.generated_ecarbs_note, carbsAfterConstraints, duration, timeOffset))
}

View file

@ -1,7 +1,7 @@
package info.nightscout.androidaps.logging
import info.nightscout.androidaps.Constants
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
@ -20,20 +20,62 @@ class UserEntryLogger @Inject constructor(
private val compositeDisposable = CompositeDisposable()
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)) {
fun log(action: Action, s: String, vararg listvalues: ValueWithUnit) {
val values = mutableListOf<ValueWithUnit>()
for (v in listvalues){
var vConverted = v
// Convertion to always store all values in the same units in database
when(v.unit) {
Units.Mmol_L -> { vConverted = ValueWithUnit(v.dValue * Constants.MMOLL_TO_MGDL, Units.Mg_Dl)}
}
values.add(vConverted)
}
compositeDisposable += repository.runTransaction(UserEntryTransaction(
action = action,
s = s,
d1 = d1,
d2 = d2,
i1 = i1,
i2 = i2
values = values
))
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.io)
.subscribeBy(
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 ""}") }
//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 ""}") }
)
}
fun log(action: Action, vararg listvalues: ValueWithUnit) {
val values = mutableListOf<ValueWithUnit>()
for (v in listvalues){
var vConverted = v
// Convertion to always store all values in the same units in database
when(v.unit) {
Units.Mmol_L -> { vConverted = ValueWithUnit(v.dValue * Constants.MMOLL_TO_MGDL, Units.Mg_Dl)}
}
values.add(vConverted)
}
compositeDisposable += repository.runTransaction(UserEntryTransaction(
action = action,
s = "",
values = values
))
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.io)
.subscribeBy(
//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 ""}") }
)
}
fun log(action: Action) {
compositeDisposable += repository.runTransaction(UserEntryTransaction(
action = action,
s = ""
))
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.io)
.subscribeBy(
onError = { aapsLogger.debug("ERRORED USER ENTRY: $action") },
onComplete = { aapsLogger.debug("USER ENTRY: $action") }
)
}
}

View file

@ -20,8 +20,10 @@ class Converters {
fun fromValueWithUnit(valueWithUnit: UserEntry.ValueWithUnit?): String? {
if (valueWithUnit == null) return null
val jsonObject = JSONObject()
jsonObject.put("sValue", valueWithUnit.sValue)
jsonObject.put("dValue", valueWithUnit.dValue)
jsonObject.put("iValue", valueWithUnit.iValue)
jsonObject.put("lValue", valueWithUnit.lValue)
jsonObject.put("unit", valueWithUnit.unit.name)
return jsonObject.toString()
}
@ -30,7 +32,34 @@ class Converters {
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")) )
return UserEntry.ValueWithUnit(jsonObject.getString("sValue"), jsonObject.getDouble("dValue"), jsonObject.getInt("iValue"), jsonObject.getLong("lValue"), UserEntry.Units.fromString(jsonObject.getString("unit")) )
}
@TypeConverter
fun fromMutableListOfValueWithUnit(values: MutableList<UserEntry.ValueWithUnit>?): String? {
if (values == null) return null
val jsonArray = JSONArray()
values.forEach {
val jsonObject = JSONObject()
jsonObject.put("dValue", it.dValue)
jsonObject.put("iValue", it.iValue)
jsonObject.put("lValue", it.lValue)
jsonObject.put("unit", it.unit.name)
jsonArray.put(jsonObject)
}
return jsonArray.toString()
}
@TypeConverter
fun toMutableListOfValueWithUnit(jsonString: String?): List<Block>? {
if (jsonString == null) return null
val jsonArray = JSONArray(jsonString)
val list = mutableListOf<Block>()
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
list.add(Block(jsonObject.getLong("duration"), jsonObject.getDouble("amount")))
}
return list
}
@TypeConverter

View file

@ -17,10 +17,7 @@ data class UserEntry(
override var utcOffset: Long = TimeZone.getDefault().getOffset(timestamp).toLong(),
var action: Action,
var s: String,
var d1: ValueWithUnit,
var d2: ValueWithUnit,
var i1: ValueWithUnit,
var i2: ValueWithUnit
var values: MutableList<ValueWithUnit>
) : DBEntry, DBEntryWithTime {
enum class Action () {
@SerializedName("BOLUS") BOLUS,
@ -123,16 +120,16 @@ 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)
data class ValueWithUnit (val dValue: Double, val iValue: Int, val lValue: Long, val unit: Units) {
constructor(dvalue:Double, unit:Units) : this(dvalue,0, 0, unit)
constructor(ivalue:Int, unit:Units) : this(0.0, ivalue, 0, unit)
constructor(lvalue:Long, unit:Units) : this(0.0,0, lvalue, unit)
}
enum class Units {
@SerializedName("None") None,
@SerializedName("Mg_Dl") Mg_Dl,
@SerializedName("Mmol_L") Mmol_L,
@SerializedName("Timestamp") Timestamp,
@SerializedName("U") U,
@SerializedName("U_H") U_H,
@SerializedName("G") G,

View file

@ -5,23 +5,16 @@ import info.nightscout.androidaps.database.entities.UserEntry.*
class UserEntryTransaction(
val action: Action,
val s: String = "",
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)
val s: String,
val values: MutableList<ValueWithUnit> = mutableListOf<ValueWithUnit>()
) : Transaction<Unit>() {
override fun run() {
database.userEntryDao.insert(UserEntry(
timestamp = System.currentTimeMillis(),
action = action,
s = s,
d1 = d1,
d2 = d2,
i1 = i1,
i2 = i2
values = values
))
}
}