specify glucoseUnit, DB++

This commit is contained in:
Milos Kozak 2021-03-09 22:11:30 +01:00
parent 2ef91e6814
commit 8ec346cd35
16 changed files with 2841 additions and 27 deletions

View file

@ -26,6 +26,7 @@ import info.nightscout.androidaps.utils.HtmlHelper
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.Translator
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.extensions.fromConstant
import info.nightscout.androidaps.utils.resources.ResourceHelper
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
@ -184,7 +185,7 @@ class CareDialog : DialogFragmentWithDate() {
EventType.QUESTION -> TherapyEvent.Type.QUESTION
EventType.ANNOUNCEMENT -> TherapyEvent.Type.ANNOUNCEMENT
},
units = profileFunction.getUnits()
glucoseUnit = TherapyEvent.GlucoseUnit.fromConstant(profileFunction.getUnits())
)
val actions: LinkedList<String> = LinkedList()

View file

@ -143,7 +143,8 @@ class FillDialog : DialogFragmentWithDate() {
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
timestamp = eventTime,
type = TherapyEvent.Type.CANNULA_CHANGE,
note = notes
note = notes,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {
@ -156,7 +157,8 @@ class FillDialog : DialogFragmentWithDate() {
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
timestamp = eventTime + 1000,
type = TherapyEvent.Type.INSULIN_CHANGE,
note = notes
note = notes,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {

View file

@ -658,7 +658,8 @@ open class LoopPlugin @Inject constructor(
timestamp = dateUtil._now(),
type = TherapyEvent.Type.APS_OFFLINE,
duration = T.mins(durationInMinutes.toLong()).msecs(),
enteredBy = "openaps://" + "AndroidAPS"
enteredBy = "openaps://" + "AndroidAPS",
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {

View file

@ -29,6 +29,7 @@ import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin
import info.nightscout.androidaps.utils.extensions.toConstant
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.sharedPreferences.SP
@ -205,9 +206,8 @@ class OpenHumansUploader @Inject constructor(
put("nsId", therapyEvent.interfaceIDs.nightscoutId)
put("eventType", therapyEvent.type.text)
put("glucose", therapyEvent.glucose)
put("units", therapyEvent.units)
put("units", therapyEvent.glucoseUnit.toConstant())
put("glucoseType", therapyEvent.glucoseType?.text)
put("units", therapyEvent.units)
put("duration", therapyEvent.duration)
put("isDeletion", deleted)
}

View file

@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.general.tidepool.elements
import com.google.gson.annotations.Expose
import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.utils.extensions.toConstant
import java.util.*
class BloodGlucoseElement(therapyEvent: TherapyEvent)
@ -20,8 +21,8 @@ class BloodGlucoseElement(therapyEvent: TherapyEvent)
init {
type = "cbg"
subType = "manual" // TODO
value = if (therapyEvent.glucose != null && therapyEvent.units != null)
Profile.toMgdl(therapyEvent.glucose!!, therapyEvent.units).toInt()
value = if (therapyEvent.glucose != null)
Profile.toMgdl(therapyEvent.glucose!!, therapyEvent.glucoseUnit.toConstant()).toInt()
else 0
}

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.RequestDexcomPermissionActivity
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.entities.GlucoseValue
import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.database.transactions.CgmSourceTransaction
import info.nightscout.androidaps.interfaces.BgSourceInterface
import info.nightscout.androidaps.interfaces.PluginBase
@ -122,8 +123,11 @@ class DexcomPlugin @Inject constructor(
val timestamp = it.getLong("timestamp") * 1000
val now = DateUtil.now()
if (timestamp > now - T.months(1).msecs() && timestamp < now) {
calibrations.add(CgmSourceTransaction.Calibration(it.getLong("timestamp") * 1000,
it.getInt("meterValue").toDouble()))
calibrations.add(CgmSourceTransaction.Calibration(
timestamp = it.getLong("timestamp") * 1000,
value = it.getInt("meterValue").toDouble(),
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
))
}
}
}
@ -133,7 +137,7 @@ class DexcomPlugin @Inject constructor(
} else {
null
}
dexcomPlugin.disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, sensorStartTime)).subscribe({ result ->
dexcomPlugin.disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, sensorStartTime)).subscribe({ result ->
result.inserted.forEach {
broadcastToXDrip(it)
if (sp.getBoolean(R.string.key_dexcomg5_nsupload, false)) {

View file

@ -141,7 +141,7 @@ class EversensePlugin @Inject constructor(
type = TherapyEvent.Type.FINGER_STICK_BG_VALUE,
glucose = calibrationGlucoseLevels[i].toDouble(),
glucoseType = TherapyEvent.MeterType.FINGER,
units = Constants.MGDL,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL,
enteredBy = "AndroidAPS-Eversense"
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }

View file

@ -13,7 +13,7 @@ import java.util.Date;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.core.R;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.IobTotal;
@ -471,11 +471,14 @@ public class NSUpload {
data.put("eventType", event.getType().getText());
data.put("created_at", event.getTimestamp());
data.put("enteredBy", event.getEnteredBy());
if (event.getUnits() != null) data.put("units", event.getUnits());
if (event.getGlucoseUnit() == TherapyEvent.GlucoseUnit.MGDL)
data.put("units", Constants.MGDL);
else data.put("units", Constants.MMOL);
if (event.getDuration() != 0) data.put("duration", T.msecs(event.getDuration()).mins());
if (event.getNote() != null) data.put("notes", event.getNote());
if (event.getGlucose() != null) data.put("glucose", event.getGlucose());
if (event.getGlucoseType() != null) data.put("glucoseType", event.getGlucoseType().getText());
if (event.getGlucoseType() != null)
data.put("glucoseType", event.getGlucoseType().getText());
} catch (JSONException e) {
aapsLogger.error("Unhandled exception", e);
}

View file

@ -31,11 +31,20 @@ fun TherapyEvent.getHoursFromStart(): Double {
return (System.currentTimeMillis() - timestamp) / (60 * 60 * 1000.0)
}
fun TherapyEvent.GlucoseUnit.toConstant(): String =
if (this == TherapyEvent.GlucoseUnit.MGDL) Constants.MGDL
else Constants.MMOL
fun TherapyEvent.GlucoseUnit.Companion.fromConstant(units: String): TherapyEvent.GlucoseUnit =
if (units == Constants.MGDL) TherapyEvent.GlucoseUnit.MGDL
else TherapyEvent.GlucoseUnit.MMOL
fun therapyEventFromNsMbg(mbg: NSMbg) =
TherapyEvent(
type = TherapyEvent.Type.NS_MBG,
timestamp = mbg.date,
glucose = mbg.mbg
glucose = mbg.mbg,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
).also {
it.interfaceIDs.nightscoutId = mbg.id()
}
@ -52,7 +61,7 @@ fun therapyEventFromNsIdForInvalidating(nsId: String): TherapyEvent =
)!!
fun therapyEventFromJson(jsonObject: JSONObject): TherapyEvent? {
val units = JsonHelper.safeGetString(jsonObject, "units", Constants.MGDL)
val glucoseUnit = if (JsonHelper.safeGetString(jsonObject, "units", Constants.MGDL) == Constants.MGDL) 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")
@ -66,7 +75,7 @@ fun therapyEventFromJson(jsonObject: JSONObject): TherapyEvent? {
val te = TherapyEvent(
timestamp = timestamp,
duration = TimeUnit.MINUTES.toMillis(duration),
units = units,
glucoseUnit = glucoseUnit,
type = type,
glucose = glucose,
glucoseType = glucoseType,

View file

@ -183,7 +183,8 @@ open class DanaRS_Packet_APS_History_Events(
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
timestamp = datetime,
type = TherapyEvent.Type.INSULIN_CHANGE,
note = resourceHelper.gs(R.string.danarspump)
note = resourceHelper.gs(R.string.danarspump),
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {
@ -219,7 +220,8 @@ open class DanaRS_Packet_APS_History_Events(
disposable += repository.runTransactionForResult(InsertTherapyEventIfNewTransaction(
timestamp = datetime,
type = TherapyEvent.Type.CANNULA_CHANGE,
note = resourceHelper.gs(R.string.danarspump)
note = resourceHelper.gs(R.string.danarspump),
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
)).subscribe({ result ->
result.inserted.forEach { nsUpload.uploadEvent(it) }
}, {

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ import androidx.room.TypeConverters
import info.nightscout.androidaps.database.daos.*
import info.nightscout.androidaps.database.entities.*
const val DATABASE_VERSION = 4
const val DATABASE_VERSION = 5
@Database(version = DATABASE_VERSION,
entities = [APSResult::class, Bolus::class, BolusCalculatorResult::class, Carbs::class,

View file

@ -58,6 +58,12 @@ class Converters {
@TypeConverter
fun toGlucoseUnit(glucoseUnit: String?) = glucoseUnit?.let { ProfileSwitch.GlucoseUnit.valueOf(it) }
@TypeConverter
fun fromTherapyGlucoseUnit(glucoseUnit: TherapyEvent.GlucoseUnit?) = glucoseUnit?.name
@TypeConverter
fun toTherapyGlucoseUnit(glucoseUnit: String?) = glucoseUnit?.let { TherapyEvent.GlucoseUnit.valueOf(it) }
@TypeConverter
fun fromPumpType(pumpType: InterfaceIDs.PumpType?) = pumpType?.name

View file

@ -35,9 +35,15 @@ data class TherapyEvent(
var enteredBy: String? = null,
var glucose: Double? = null,
var glucoseType: MeterType? = null,
var units: String? = null
var glucoseUnit: GlucoseUnit,
) : TraceableDBEntry, DBEntryWithTimeAndDuration {
enum class GlucoseUnit {
MGDL,
MMOL;
companion object { }
}
enum class MeterType(val text: String) {
@SerializedName("Finger")
FINGER("Finger"),

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.database.transactions
import info.nightscout.androidaps.database.entities.GlucoseValue
import info.nightscout.androidaps.database.entities.ProfileSwitch
import info.nightscout.androidaps.database.entities.TherapyEvent
/**
@ -56,7 +57,8 @@ class CgmSourceTransaction(
database.therapyEventDao.insertNewEntry(TherapyEvent(
timestamp = it.timestamp,
type = TherapyEvent.Type.FINGER_STICK_BG_VALUE,
glucose = it.value
glucose = it.value,
glucoseUnit = it.glucoseUnit
))
}
}
@ -64,7 +66,8 @@ class CgmSourceTransaction(
if (database.therapyEventDao.findByTimestamp(TherapyEvent.Type.SENSOR_CHANGE, it) == null) {
database.therapyEventDao.insertNewEntry(TherapyEvent(
timestamp = it,
type = TherapyEvent.Type.SENSOR_CHANGE
type = TherapyEvent.Type.SENSOR_CHANGE,
glucoseUnit = TherapyEvent.GlucoseUnit.MGDL
))
}
}
@ -83,7 +86,8 @@ class CgmSourceTransaction(
data class Calibration(
val timestamp: Long,
val value: Double
val value: Double,
val glucoseUnit: TherapyEvent.GlucoseUnit
)
class TransactionResult {

View file

@ -6,8 +6,8 @@ class InsertTherapyEventIfNewTransaction(
val therapyEvent: TherapyEvent
) : Transaction<InsertTherapyEventIfNewTransaction.TransactionResult>() {
constructor(timestamp: Long, type: TherapyEvent.Type, duration: Long = 0, note: String? = null, enteredBy: String? = null, glucose: Double? = null, glucoseType: TherapyEvent.MeterType? = null, units: String? = null) :
this(TherapyEvent(timestamp = timestamp, type = type, duration = duration, note = note, enteredBy = enteredBy, glucose = glucose, glucoseType = glucoseType, units = units))
constructor(timestamp: Long, type: TherapyEvent.Type, duration: Long = 0, note: String? = null, enteredBy: String? = null, glucose: Double? = null, glucoseType: TherapyEvent.MeterType? = null, glucoseUnit: TherapyEvent.GlucoseUnit) :
this(TherapyEvent(timestamp = timestamp, type = type, duration = duration, note = note, enteredBy = enteredBy, glucose = glucose, glucoseType = glucoseType, glucoseUnit = glucoseUnit))
override fun run(): TransactionResult {
val result = TransactionResult()