From 2e59dd3112484e13f035fabbf6948defc6f6a45b Mon Sep 17 00:00:00 2001 From: Philoul Date: Sat, 27 Mar 2021 00:58:01 +0100 Subject: [PATCH] Add UserEntry for Wear --- .../general/wear/ActionStringHandler.kt | 22 ++++++++++++++++--- .../utils/extensions/UserEntryExt.kt | 1 + .../src/main/res/drawable/ic_watch.xml | 0 .../androidaps/database/entities/UserEntry.kt | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) rename {app => core}/src/main/res/drawable/ic_watch.xml (100%) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.kt index 8216e093fc..3aa3c8af8e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/ActionStringHandler.kt @@ -17,6 +17,7 @@ import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.database.ValueWrapper import info.nightscout.androidaps.database.entities.TemporaryTarget import info.nightscout.androidaps.database.entities.TherapyEvent +import info.nightscout.androidaps.database.entities.UserEntry.* import info.nightscout.androidaps.database.interfaces.end import info.nightscout.androidaps.database.transactions.CancelCurrentTemporaryTargetIfAnyTransaction import info.nightscout.androidaps.database.transactions.InsertTemporaryTargetAndCancelCurrentTransaction @@ -24,6 +25,7 @@ import info.nightscout.androidaps.db.Source import info.nightscout.androidaps.db.TDD import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker @@ -82,6 +84,7 @@ class ActionStringHandler @Inject constructor( private val config: Config, private val databaseHelper: DatabaseHelperInterface, private val repository: AppRepository, + private val uel: UserEntryLogger, private val nsUpload: NSUpload ) { @@ -105,7 +108,7 @@ class ActionStringHandler @Inject constructor( } @Synchronized - private fun handleInitiate(actionString: String) { + fun handleInitiate(actionString: String) { if (!sp.getBoolean(R.string.key_wear_control, false)) return lastBolusWizard = null var rTitle = "CONFIRM" //TODO: i18n @@ -546,8 +549,10 @@ class ActionStringHandler @Inject constructor( if (carbs > 0) { if (duration == 0) { carbsGenerator.createCarb(carbs, time, TherapyEvent.Type.CARBS_CORRECTION.text, "watch") + uel.log(Action.CARBS, ValueWithUnit(Sources.Wear), ValueWithUnit(time, Units.Timestamp), ValueWithUnit(carbs, Units.G)) } else { carbsGenerator.generateCarbs(carbs, time, duration, "watch eCarbs") + uel.log(Action.EXTENDED_CARBS, ValueWithUnit(Sources.Wear), ValueWithUnit(time, Units.Timestamp), ValueWithUnit(carbs, Units.G), ValueWithUnit(duration, Units.H)) } } } @@ -575,11 +580,12 @@ class ActionStringHandler @Inject constructor( return } //send profile to pump + uel.log(Action.PROFILE_SWITCH, ValueWithUnit(Sources.Wear), ValueWithUnit(percentage, Units.Percent), ValueWithUnit(timeshift, Units.H, timeshift != 0)) activePlugin.activeTreatments.doProfileSwitch(0, percentage, timeshift) } private fun generateTempTarget(duration: Int, low: Double, high: Double) { - if (duration != 0) + if (duration != 0) { disposable += repository.runTransactionForResult(InsertTemporaryTargetAndCancelCurrentTransaction( timestamp = System.currentTimeMillis(), duration = TimeUnit.MINUTES.toMillis(duration.toLong()), @@ -592,13 +598,16 @@ class ActionStringHandler @Inject constructor( }, { aapsLogger.error("Error while saving temporary target", it) }) - else + uel.log(Action.TT, ValueWithUnit(Sources.Wear), ValueWithUnit(TemporaryTarget.Reason.WEAR.text, Units.TherapyEvent), ValueWithUnit(low, profileFunction.getUnits()), ValueWithUnit(high, profileFunction.getUnits(), low!=high), ValueWithUnit(duration, Units.M)) + } else { disposable += repository.runTransactionForResult(CancelCurrentTemporaryTargetIfAnyTransaction(System.currentTimeMillis())) .subscribe({ result -> result.updated.forEach { nsUpload.updateTempTarget(it) } }, { aapsLogger.error("Error while saving temporary target", it) }) + uel.log(Action.CANCEL_TT, ValueWithUnit(Sources.Wear), ValueWithUnit(TemporaryTarget.Reason.WEAR.text, Units.TherapyEvent)) + } } private fun doFillBolus(amount: Double) { @@ -606,6 +615,7 @@ class ActionStringHandler @Inject constructor( detailedBolusInfo.insulin = amount detailedBolusInfo.isValid = false detailedBolusInfo.source = Source.USER + uel.log(Action.PRIME_BOLUS, ValueWithUnit(Sources.Wear), ValueWithUnit(amount, Units.U, amount != 0.0)) commandQueue.bolus(detailedBolusInfo, object : Callback() { override fun run() { if (!result.success) { @@ -624,6 +634,12 @@ class ActionStringHandler @Inject constructor( detailedBolusInfo.source = Source.USER val storesCarbs = activePlugin.activePump.pumpDescription.storesCarbInfo if (detailedBolusInfo.insulin > 0 || storesCarbs) { + val action = when { + amount.equals(0.0) -> Action.CARBS + carbs.equals(0) -> Action.BOLUS + else -> Action.TREATMENT + } + uel.log(action, ValueWithUnit(Sources.Wear), ValueWithUnit(amount, Units.U, amount != 0.0), ValueWithUnit(carbs, Units.G, carbs != 0)) commandQueue.bolus(detailedBolusInfo, object : Callback() { override fun run() { if (!result.success) { diff --git a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt index e5c9e1e21e..c1048f1973 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/extensions/UserEntryExt.kt @@ -42,6 +42,7 @@ fun Sources.iconId(): Int { Sources.Automation -> R.drawable.ic_automation Sources.Loop -> R.drawable.ic_loop_closed_white Sources.NSClient -> R.drawable.ic_nightscout_syncs + Sources.Wear -> R.drawable.ic_watch else -> -1 } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_watch.xml b/core/src/main/res/drawable/ic_watch.xml similarity index 100% rename from app/src/main/res/drawable/ic_watch.xml rename to core/src/main/res/drawable/ic_watch.xml diff --git a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt index ad91b61507..ed86b48343 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/entities/UserEntry.kt @@ -102,7 +102,7 @@ data class UserEntry( constructor(lvalue: Long, unit: Units, condition:Boolean = true) : this(0.0,0, lvalue, "", unit, condition) constructor(svalue: String, unit:Units) : this(0.0,0, 0, svalue, unit, svalue != "") constructor(source: Sources) : this(0.0,0, 0, source.text, Units.Source, true) - constructor(dvalue: Double, unit:String) : this(dvalue,0, 0, "", Units.fromText(unit)) + constructor(dvalue: Double, unit:String, condition:Boolean = true) : this(dvalue,0, 0, "", Units.fromText(unit), condition) constructor(rStringRef: Int, nbParam: Long) : this(0.0, rStringRef, nbParam, "", Units.R_String, !rStringRef.equals(0)) // additionnal constructors for formated strings with additional values as parameters (define number of parameters as long fun value() : Any {