diff --git a/core/data/src/main/kotlin/app/aaps/core/data/ue/Action.kt b/core/data/src/main/kotlin/app/aaps/core/data/ue/Action.kt index 1996554c8cb..54d2dea3afc 100644 --- a/core/data/src/main/kotlin/app/aaps/core/data/ue/Action.kt +++ b/core/data/src/main/kotlin/app/aaps/core/data/ue/Action.kt @@ -88,6 +88,10 @@ enum class Action(val colorGroup: ColorGroup) { EXIT_AAPS(ColorGroup.Aaps), PLUGIN_ENABLED(ColorGroup.Aaps), PLUGIN_DISABLED(ColorGroup.Aaps), + AUTOISF_ENABLED (ColorGroup.Aaps), + AUTOISF_DISABLED (ColorGroup.Aaps), + IOB_TH_SET (ColorGroup.Aaps), + ACCE_WEIGHT_SET (ColorGroup.Aaps), UNKNOWN(ColorGroup.Aaps) ; diff --git a/core/ui/src/main/res/values/strings.xml b/core/ui/src/main/res/values/strings.xml index 8ca726110d2..98965341c04 100644 --- a/core/ui/src/main/res/values/strings.xml +++ b/core/ui/src/main/res/values/strings.xml @@ -444,6 +444,11 @@ Tune days : Select profile to tune + + AUTOISF ENABLED + AUTOISF DISABLED + IOBTH SET + ACCE WeIGHT SET raise_urgent_alarms_as_android_notification diff --git a/database/impl/src/main/kotlin/app/aaps/database/entities/UserEntry.kt b/database/impl/src/main/kotlin/app/aaps/database/entities/UserEntry.kt index 4b0b603791b..5e14c6b80c1 100644 --- a/database/impl/src/main/kotlin/app/aaps/database/entities/UserEntry.kt +++ b/database/impl/src/main/kotlin/app/aaps/database/entities/UserEntry.kt @@ -113,6 +113,10 @@ data class UserEntry( EXIT_AAPS, PLUGIN_ENABLED, PLUGIN_DISABLED, + AUTOISF_ENABLED, + AUTOISF_DISABLED, + IOB_TH_SET, + ACCE_WEIGHT_SET, UNKNOWN ; } diff --git a/database/persistence/src/main/kotlin/app/aaps/database/persistence/converters/ActionExtension.kt b/database/persistence/src/main/kotlin/app/aaps/database/persistence/converters/ActionExtension.kt index 9fcdcf94691..54bb122da56 100644 --- a/database/persistence/src/main/kotlin/app/aaps/database/persistence/converters/ActionExtension.kt +++ b/database/persistence/src/main/kotlin/app/aaps/database/persistence/converters/ActionExtension.kt @@ -92,6 +92,10 @@ fun UserEntry.Action.fromDb(): Action = UserEntry.Action.EXIT_AAPS -> Action.EXIT_AAPS UserEntry.Action.PLUGIN_ENABLED -> Action.PLUGIN_ENABLED UserEntry.Action.PLUGIN_DISABLED -> Action.PLUGIN_DISABLED + UserEntry.Action.AUTOISF_ENABLED -> Action.AUTOISF_ENABLED + UserEntry.Action.AUTOISF_DISABLED -> Action.AUTOISF_DISABLED + UserEntry.Action.IOB_TH_SET -> Action.IOB_TH_SET + UserEntry.Action.ACCE_WEIGHT_SET -> Action.ACCE_WEIGHT_SET UserEntry.Action.UNKNOWN -> Action.UNKNOWN } @@ -184,6 +188,10 @@ fun Action.toDb(): UserEntry.Action = Action.EXIT_AAPS -> UserEntry.Action.EXIT_AAPS Action.PLUGIN_ENABLED -> UserEntry.Action.PLUGIN_ENABLED Action.PLUGIN_DISABLED -> UserEntry.Action.PLUGIN_DISABLED + Action.AUTOISF_ENABLED -> UserEntry.Action.AUTOISF_ENABLED + Action.AUTOISF_DISABLED -> UserEntry.Action.AUTOISF_DISABLED + Action.IOB_TH_SET -> UserEntry.Action.IOB_TH_SET + Action.ACCE_WEIGHT_SET -> UserEntry.Action.ACCE_WEIGHT_SET Action.UNKNOWN -> UserEntry.Action.UNKNOWN } diff --git a/implementation/src/main/kotlin/app/aaps/implementation/utils/TranslatorImpl.kt b/implementation/src/main/kotlin/app/aaps/implementation/utils/TranslatorImpl.kt index 31a4d2f0ee0..6665da53e01 100644 --- a/implementation/src/main/kotlin/app/aaps/implementation/utils/TranslatorImpl.kt +++ b/implementation/src/main/kotlin/app/aaps/implementation/utils/TranslatorImpl.kt @@ -104,6 +104,10 @@ class TranslatorImpl @Inject internal constructor( Action.PLUGIN_DISABLED -> rh.gs(app.aaps.core.ui.R.string.uel_plugin_disabled) Action.LOOP_CHANGE -> rh.gs(app.aaps.core.ui.R.string.uel_loop_change) Action.LOOP_REMOVED -> rh.gs(app.aaps.core.ui.R.string.uel_loop_removed) + Action.AUTOISF_ENABLED -> rh.gs(app.aaps.core.ui.R.string.uel_autoisf_enabled) + Action.AUTOISF_DISABLED -> rh.gs(app.aaps.core.ui.R.string.uel_autoisf_disabled) + Action.IOB_TH_SET -> rh.gs(app.aaps.core.ui.R.string.uel_iobth_set) + Action.ACCE_WEIGHT_SET -> rh.gs(app.aaps.core.ui.R.string.uel_acce_weight_set) Action.UNKNOWN -> rh.gs(app.aaps.core.ui.R.string.unknown) } diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt index 1e1905a1e29..89f9e18f0c1 100644 --- a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/AutomationPlugin.kt @@ -36,12 +36,16 @@ import app.aaps.core.keys.StringKey import app.aaps.core.validators.preferences.AdaptiveListPreference import app.aaps.plugins.automation.actions.Action import app.aaps.plugins.automation.actions.ActionAlarm +import app.aaps.plugins.automation.actions.ActionAutoisfDisable +import app.aaps.plugins.automation.actions.ActionAutoisfEnable import app.aaps.plugins.automation.actions.ActionCarePortalEvent import app.aaps.plugins.automation.actions.ActionNotification import app.aaps.plugins.automation.actions.ActionProfileSwitch import app.aaps.plugins.automation.actions.ActionProfileSwitchPercent import app.aaps.plugins.automation.actions.ActionRunAutotune import app.aaps.plugins.automation.actions.ActionSendSMS +import app.aaps.plugins.automation.actions.ActionSetAcceWeight +import app.aaps.plugins.automation.actions.ActionSetIobTH import app.aaps.plugins.automation.actions.ActionSettingsExport import app.aaps.plugins.automation.actions.ActionStartTempTarget import app.aaps.plugins.automation.actions.ActionStopProcessing @@ -57,6 +61,7 @@ import app.aaps.plugins.automation.triggers.Trigger import app.aaps.plugins.automation.triggers.TriggerAutosensValue import app.aaps.plugins.automation.triggers.TriggerBTDevice import app.aaps.plugins.automation.triggers.TriggerBg +import app.aaps.plugins.automation.triggers.TriggerBgAcceWeight import app.aaps.plugins.automation.triggers.TriggerBolusAgo import app.aaps.plugins.automation.triggers.TriggerCOB import app.aaps.plugins.automation.triggers.TriggerCannulaAge @@ -65,6 +70,7 @@ import app.aaps.plugins.automation.triggers.TriggerDelta import app.aaps.plugins.automation.triggers.TriggerHeartRate import app.aaps.plugins.automation.triggers.TriggerInsulinAge import app.aaps.plugins.automation.triggers.TriggerIob +import app.aaps.plugins.automation.triggers.TriggerIobTH import app.aaps.plugins.automation.triggers.TriggerLocation import app.aaps.plugins.automation.triggers.TriggerPodChange import app.aaps.plugins.automation.triggers.TriggerProfilePercent @@ -397,9 +403,13 @@ class AutomationPlugin @Inject constructor( ActionProfileSwitch(injector), ActionSendSMS(injector) ) - if (config.isEngineeringMode() && config.isDev()) + if (config.isEngineeringMode() && config.isDev()) { actions.add(ActionRunAutotune(injector)) - + actions.add(ActionAutoisfEnable(injector)) + actions.add(ActionAutoisfDisable(injector)) + actions.add(ActionSetAcceWeight(injector)) + actions.add(ActionSetIobTH(injector)) + } return actions.toList() } @@ -428,6 +438,10 @@ class AutomationPlugin @Inject constructor( TriggerReservoirLevel(injector), TriggerStepsCount(injector) ) + if (config.isEngineeringMode() && config.isDev()) { + triggers.add(TriggerBgAcceWeight(injector)) + triggers.add(TriggerIobTH(injector)) + } val pump = activePlugin.activePump diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/Action.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/Action.kt index b6ef6b15890..70ded7c4f80 100644 --- a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/Action.kt +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/Action.kt @@ -68,6 +68,10 @@ abstract class Action(val injector: HasAndroidInjector) { ActionNotification::class.java.simpleName -> ActionNotification(injector).fromJSON(data.toString()) ActionProfileSwitch::class.java.simpleName -> ActionProfileSwitch(injector).fromJSON(data.toString()) ActionProfileSwitchPercent::class.java.simpleName -> ActionProfileSwitchPercent(injector).fromJSON(data.toString()) + ActionAutoisfEnable::class.java.simpleName -> ActionAutoisfEnable(injector).fromJSON(data.toString()) + ActionAutoisfDisable::class.java.simpleName -> ActionAutoisfDisable(injector).fromJSON(data.toString()) + ActionSetAcceWeight::class.java.simpleName -> ActionSetAcceWeight(injector).fromJSON(data.toString()) + ActionSetIobTH::class.java.simpleName -> ActionSetIobTH(injector).fromJSON(data.toString()) ActionRunAutotune::class.java.simpleName -> ActionRunAutotune(injector).fromJSON(data.toString()) ActionSendSMS::class.java.simpleName -> ActionSendSMS(injector).fromJSON(data.toString()) ActionStartTempTarget::class.java.simpleName -> ActionStartTempTarget(injector).fromJSON(data.toString()) diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfDisable.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfDisable.kt new file mode 100644 index 00000000000..8bb9ee0b54c --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfDisable.kt @@ -0,0 +1,36 @@ +package app.aaps.plugins.automation.actions + +import androidx.annotation.DrawableRes +import dagger.android.HasAndroidInjector +import app.aaps.core.data.ue.Sources +import app.aaps.core.interfaces.logging.UserEntryLogger +import app.aaps.core.interfaces.queue.Callback +import app.aaps.core.interfaces.sharedPreferences.SP +import app.aaps.plugins.automation.R +import javax.inject.Inject + +class ActionAutoisfDisable(injector: HasAndroidInjector) : Action(injector) { + + @Inject lateinit var uel: UserEntryLogger + @Inject lateinit var sp: SP + + override fun friendlyName(): Int = R.string.disableautoisf + override fun shortDescription(): String = rh.gs(R.string.disableautoisf) + @DrawableRes override fun icon(): Int = R.drawable.ic_autoisf_disabled + + override fun doAction(callback: Callback) { + val currentAutoisfStatus: Boolean = sp.getBoolean(R.string.enable_autoISF, false) + if (currentAutoisfStatus) { + uel.log(app.aaps.core.data.ue.Action.AUTOISF_DISABLED, Sources.Automation, title) + sp.putBoolean(R.string.enable_autoISF, false) + callback.result(instantiator.providePumpEnactResult().success(true).comment(R.string.autoisf_disabled)).run() + } else { + callback.result(instantiator.providePumpEnactResult().success(true).comment(R.string.autoisf_alreadydisabled)).run() + } + } + + override fun isValid(): Boolean = true + + override fun hasDialog(): Boolean = false + +} diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetAcceWeight.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetAcceWeight.kt new file mode 100644 index 00000000000..c992de3cc2a --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetAcceWeight.kt @@ -0,0 +1,69 @@ +package app.aaps.plugins.automation.actions + +import android.widget.LinearLayout +import androidx.annotation.DrawableRes +import dagger.android.HasAndroidInjector +import app.aaps.core.data.ue.Sources +import app.aaps.core.interfaces.logging.UserEntryLogger +import app.aaps.core.interfaces.queue.Callback +import app.aaps.core.interfaces.sharedPreferences.SP +import app.aaps.core.utils.JsonHelper +import app.aaps.plugins.automation.elements.InputWeight +import app.aaps.plugins.automation.elements.LabelWithElement +import app.aaps.plugins.automation.elements.LayoutBuilder +import app.aaps.plugins.automation.R +import org.json.JSONObject +import javax.inject.Inject + +class ActionSetAcceWeight(injector: HasAndroidInjector) : Action(injector) { + + @Inject lateinit var uel: UserEntryLogger + @Inject lateinit var sp: SP + + override fun friendlyName(): Int = R.string.autoisf_acce_weight + override fun shortDescription(): String = rh.gs(R.string.automate_set_acce_weight, new_weight.value) + @DrawableRes override fun icon(): Int = R.drawable.ic_acce_weight + + var new_weight = InputWeight( ) + + override fun doAction(callback: Callback) { + val currentAcceWeight:Double = sp.getDouble(R.string.bgAccel_ISF_weight, 0.0) + if (currentAcceWeight != new_weight.value) { + uel.log( + app.aaps.core.data.ue.Action.ACCE_WEIGHT_SET, + Sources.Automation, + title + ": " + rh.gs(R.string.automate_set_acce_weight, new_weight.value) + ) + sp.putDouble(R.string.bgAccel_ISF_weight, new_weight.value) + callback.result(instantiator.providePumpEnactResult().success(true).comment(R.string.weight_new)).run() + } else { + callback.result(instantiator.providePumpEnactResult().success(false).comment(R.string.weight_old)).run() + } + } + + override fun hasDialog(): Boolean { + return true + } + + override fun generateDialog(root: LinearLayout) { + LayoutBuilder() + .add(LabelWithElement(rh, rh.gs(R.string.autoisf_acce_weight), "", new_weight)) + .build(root) + } + + override fun toJSON(): String { + val data = JSONObject() + .put("weight", new_weight.value) + return JSONObject() + .put("type", this.javaClass.name) + .put("data", data) + .toString() + } + + override fun fromJSON(data: String): Action { + val o = JSONObject(data) + new_weight.value = JsonHelper.safeGetDouble(o, "weight") + return this + } + override fun isValid(): Boolean = true +} \ No newline at end of file diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetIobTH.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetIobTH.kt new file mode 100644 index 00000000000..ee4813f5000 --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/actions/ActionSetIobTH.kt @@ -0,0 +1,73 @@ +package app.aaps.plugins.automation.actions + +import android.widget.LinearLayout +import androidx.annotation.DrawableRes +import dagger.android.HasAndroidInjector +import app.aaps.core.data.ue.Sources +import app.aaps.core.data.ue.ValueWithUnit +import app.aaps.core.interfaces.logging.UserEntryLogger +import app.aaps.core.interfaces.queue.Callback +import app.aaps.core.interfaces.sharedPreferences.SP +import app.aaps.core.utils.JsonHelper +import app.aaps.plugins.automation.elements.InputIobTH +import app.aaps.plugins.automation.elements.LabelWithElement +import app.aaps.plugins.automation.elements.LayoutBuilder +import app.aaps.plugins.automation.R +import org.json.JSONObject +import javax.inject.Inject + +class ActionSetIobTH(injector: HasAndroidInjector) : Action(injector) { + + @Inject lateinit var uel: UserEntryLogger + @Inject lateinit var sp: SP + + var new_iobTH = InputIobTH ( ) + // new_weight.value = 1 + + override fun friendlyName(): Int = R.string.autoisf_iobTH_percent + override fun shortDescription(): String = rh.gs(R.string.automate_set_iobTH_percent, new_iobTH.value.toInt()) + @DrawableRes override fun icon(): Int = R.drawable.ic_iobth + + override fun doAction(callback: Callback) { + val currentIobTH:Int = sp.getInt(R.string.iob_threshold_percent, 100) + if (currentIobTH != new_iobTH.value) { + uel.log( + app.aaps.core.data.ue.Action.IOB_TH_SET, + Sources.Automation, + title + ": " + rh.gs(R.string.automate_set_iobTH_percent, new_iobTH.value.toInt()), + ValueWithUnit.Percent(new_iobTH.value.toInt()) + ) + sp.putInt(R.string.iob_threshold_percent, new_iobTH.value) + callback.result(instantiator.providePumpEnactResult().success(true).comment(R.string.weight_new)).run() + } else { + callback.result(instantiator.providePumpEnactResult().success(false).comment(R.string.weight_old)).run() + } + } + + override fun hasDialog(): Boolean { + return true + } + + override fun generateDialog(root: LinearLayout) { + LayoutBuilder() + .add(LabelWithElement(rh, rh.gs(R.string.autoisf_iobTH_percent), "%", new_iobTH)) + .build(root) + } + + override fun toJSON(): String { + val data = JSONObject() + .put("percentage", new_iobTH.value) + return JSONObject() + .put("type", this.javaClass.name) + .put("data", data) + .toString() + } + + override fun fromJSON(data: String): Action { + val o = JSONObject(data) + new_iobTH.value = JsonHelper.safeGetInt(o, "percentage") + return this + } + + override fun isValid(): Boolean = true +} \ No newline at end of file diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/di/AutomationModule.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/di/AutomationModule.kt index 94b30a68e60..176f7b4143e 100644 --- a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/di/AutomationModule.kt +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/di/AutomationModule.kt @@ -6,6 +6,8 @@ import app.aaps.plugins.automation.AutomationFragment import app.aaps.plugins.automation.AutomationPlugin import app.aaps.plugins.automation.actions.Action import app.aaps.plugins.automation.actions.ActionAlarm +import app.aaps.plugins.automation.actions.ActionAutoisfDisable +import app.aaps.plugins.automation.actions.ActionAutoisfEnable import app.aaps.plugins.automation.actions.ActionSettingsExport import app.aaps.plugins.automation.actions.ActionCarePortalEvent import app.aaps.plugins.automation.actions.ActionDummy @@ -18,6 +20,8 @@ import app.aaps.plugins.automation.actions.ActionProfileSwitch import app.aaps.plugins.automation.actions.ActionProfileSwitchPercent import app.aaps.plugins.automation.actions.ActionRunAutotune import app.aaps.plugins.automation.actions.ActionSendSMS +import app.aaps.plugins.automation.actions.ActionSetAcceWeight +import app.aaps.plugins.automation.actions.ActionSetIobTH import app.aaps.plugins.automation.actions.ActionStartTempTarget import app.aaps.plugins.automation.actions.ActionStopProcessing import app.aaps.plugins.automation.actions.ActionStopTempTarget @@ -32,6 +36,7 @@ import app.aaps.plugins.automation.triggers.Trigger import app.aaps.plugins.automation.triggers.TriggerAutosensValue import app.aaps.plugins.automation.triggers.TriggerBTDevice import app.aaps.plugins.automation.triggers.TriggerBg +import app.aaps.plugins.automation.triggers.TriggerBgAcceWeight import app.aaps.plugins.automation.triggers.TriggerBolusAgo import app.aaps.plugins.automation.triggers.TriggerCOB import app.aaps.plugins.automation.triggers.TriggerCannulaAge @@ -41,6 +46,7 @@ import app.aaps.plugins.automation.triggers.TriggerDummy import app.aaps.plugins.automation.triggers.TriggerHeartRate import app.aaps.plugins.automation.triggers.TriggerInsulinAge import app.aaps.plugins.automation.triggers.TriggerIob +import app.aaps.plugins.automation.triggers.TriggerIobTH import app.aaps.plugins.automation.triggers.TriggerLocation import app.aaps.plugins.automation.triggers.TriggerPodChange import app.aaps.plugins.automation.triggers.TriggerProfilePercent @@ -105,6 +111,8 @@ abstract class AutomationModule { @ContributesAndroidInjector abstract fun triggerTimeRangeInjector(): TriggerTimeRange @ContributesAndroidInjector abstract fun triggerWifiSsidInjector(): TriggerWifiSsid @ContributesAndroidInjector abstract fun triggerStepsCountInjector(): TriggerStepsCount + @ContributesAndroidInjector abstract fun triggerBgAcceWeightInjector(): TriggerBgAcceWeight + @ContributesAndroidInjector abstract fun triggerIonTHWeightInjector(): TriggerIobTH @ContributesAndroidInjector abstract fun actionInjector(): Action @ContributesAndroidInjector abstract fun actionStopProcessingInjector(): ActionStopProcessing @@ -119,6 +127,10 @@ abstract class AutomationModule { @ContributesAndroidInjector abstract fun actionProfileSwitchInjector(): ActionProfileSwitch @ContributesAndroidInjector abstract fun actionProfileSwitchPercentInjector(): ActionProfileSwitchPercent @ContributesAndroidInjector abstract fun actionRunAutotuneInjector(): ActionRunAutotune + @ContributesAndroidInjector abstract fun actionAutoisfEnableInjector(): ActionAutoisfEnable + @ContributesAndroidInjector abstract fun actionAutoisfDisableInjector(): ActionAutoisfDisable + @ContributesAndroidInjector abstract fun actionSetAcceWeightInjector(): ActionSetAcceWeight + @ContributesAndroidInjector abstract fun actionSetIobTHInjector(): ActionSetIobTH @ContributesAndroidInjector abstract fun actionSendSMSInjector(): ActionSendSMS @ContributesAndroidInjector abstract fun actionStartTempTargetInjector(): ActionStartTempTarget @ContributesAndroidInjector abstract fun actionStopTempTargetInjector(): ActionStopTempTarget diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputIobTH.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputIobTH.kt new file mode 100644 index 00000000000..9decc7bba5b --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputIobTH.kt @@ -0,0 +1,25 @@ +package app.aaps.plugins.automation.elements + +import android.view.Gravity +import android.widget.LinearLayout +import app.aaps.core.ui.elements.NumberPicker +import java.text.DecimalFormat + +class InputIobTH() : Element { + + // @Inject lateinit var sp: SP + var value = 100 // not working on app start: sp.getDouble(R.string.key_iob...., 100.0) + + constructor(another: Int) : this() { + value = another + } + + override fun addToLayout(root: LinearLayout) { + root.addView( + NumberPicker(root.context, null).also { + it.setParams(value.toDouble(), 10.0, 100.0, 5.0, DecimalFormat("0"), false, root.findViewById(app.aaps.core.ui.R.id.ok)) + it.setOnValueChangedListener { v: Double -> value = v.toInt() } + it.gravity = Gravity.CENTER_HORIZONTAL + }) + } +} \ No newline at end of file diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputWeight.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputWeight.kt new file mode 100644 index 00000000000..164728025cc --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/elements/InputWeight.kt @@ -0,0 +1,25 @@ +package app.aaps.plugins.automation.elements + +import android.view.Gravity +import android.widget.LinearLayout +import app.aaps.core.ui.elements.NumberPicker +import java.text.DecimalFormat + +class InputWeight() : Element { + + // @Inject lateinit var sp: SP + var value = 0.0 // not working on app start: sp.getDouble(R.string.key_bgAccel_ISF_weight, 0.0) + + constructor(another: Double) : this() { + value = another + } + + override fun addToLayout(root: LinearLayout) { + root.addView( + NumberPicker(root.context, null).also { + it.setParams(value, 0.0, 1.0, 0.05, DecimalFormat("0.00"), true, root.findViewById(app.aaps.core.ui.R.id.ok)) + it.setOnValueChangedListener { v: Double -> value = v } + it.gravity = Gravity.CENTER_HORIZONTAL + }) + } +} \ No newline at end of file diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/Trigger.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/Trigger.kt index 533743992b4..699cdf33708 100644 --- a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/Trigger.kt +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/Trigger.kt @@ -90,6 +90,7 @@ abstract class Trigger(val injector: HasAndroidInjector) { return when (type) { TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(data.toString()) TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data.toString()) + TriggerBgAcceWeight::class.java.simpleName -> TriggerBgAcceWeight(injector).fromJSON(data.toString()) TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(data.toString()) TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(data.toString()) TriggerSensorAge::class.java.simpleName -> TriggerSensorAge(injector).fromJSON(data.toString()) @@ -100,6 +101,7 @@ abstract class Trigger(val injector: HasAndroidInjector) { TriggerPumpBatteryAge::class.java.simpleName -> TriggerPumpBatteryAge(injector).fromJSON(data.toString()) TriggerPumpBatteryLevel::class.java.simpleName -> TriggerPumpBatteryLevel(injector).fromJSON(data.toString()) TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString()) + TriggerIobTH::class.java.simpleName -> TriggerIobTH(injector).fromJSON(data.toString()) TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString()) TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data.toString()) TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString()) diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerBgAcceWeight.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerBgAcceWeight.kt new file mode 100644 index 00000000000..5219f325a09 --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerBgAcceWeight.kt @@ -0,0 +1,85 @@ +package app.aaps.plugins.automation.triggers + +import android.widget.LinearLayout +import java.util.Optional +import dagger.android.HasAndroidInjector +import app.aaps.plugins.automation.R +import app.aaps.core.interfaces.logging.LTag +import app.aaps.core.interfaces.sharedPreferences.SP +import app.aaps.plugins.automation.elements.Comparator +import app.aaps.plugins.automation.elements.InputWeight +import app.aaps.plugins.automation.elements.LabelWithElement +import app.aaps.plugins.automation.elements.LayoutBuilder +import app.aaps.plugins.automation.elements.StaticLabel +import org.json.JSONObject +import app.aaps.core.utils.JsonHelper +import javax.inject.Inject + +class TriggerBgAcceWeight(injector: HasAndroidInjector) : Trigger(injector) { + + @Inject lateinit var sp: SP + + var acceWeight = InputWeight() + var comparator = Comparator(rh) + + // from TriggerWifiSsid: @Suppress("unused") + constructor(injector: HasAndroidInjector, acceWeight: Double, compare: Comparator.Compare) : this(injector) { + this.acceWeight = InputWeight(acceWeight) + comparator = Comparator(rh, compare) + } + + constructor(injector: HasAndroidInjector, triggerBgAcceWeight: TriggerBgAcceWeight) : this(injector) { + this.acceWeight = InputWeight(triggerBgAcceWeight.acceWeight.value) + comparator = Comparator(rh, triggerBgAcceWeight.comparator.value) + } + + fun setValue(acceWeight: Double): TriggerBgAcceWeight { + this.acceWeight.value = acceWeight + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerBgAcceWeight { + this.comparator.value = comparator + return this + } + + override fun shouldRun(): Boolean { + val actualWeight = sp.getDouble(R.string.bgAccel_ISF_weight + ,0.0) + if (comparator.value.check(actualWeight, acceWeight.value)) { + aapsLogger.debug(LTag.AUTOMATION, "set bgAccel_ISF_weight ready for execution: " + friendlyDescription()) + return true + } + aapsLogger.debug(LTag.AUTOMATION, "set bgAccel_ISF_weight NOT ready for execution: " + friendlyDescription()) + return false + } + + override fun dataJSON(): JSONObject = + JSONObject() + .put("acce_weight", acceWeight.value) + .put("comparator", comparator.value.toString()) + + override fun fromJSON(data: String): Trigger { + val d = JSONObject(data) + acceWeight.value = JsonHelper.safeGetDouble(d, "acce_weight") + comparator.value = Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!) + return this + } + + override fun friendlyName(): Int = R.string.autoisf_acce_weight + + override fun friendlyDescription(): String = + rh.gs(R.string.acceweightcompared, rh.gs(comparator.value.stringRes), acceWeight.value) + + override fun icon(): Optional = Optional.of(R.drawable.ic_acce_weight) + + override fun duplicate(): Trigger = TriggerBgAcceWeight(injector, this) + + override fun generateDialog(root: LinearLayout) { + LayoutBuilder() + .add(StaticLabel(rh, R.string.autoisf_acce_weight, this)) + .add(comparator) + .add(LabelWithElement(rh, rh.gs(R.string.autoisf_acce_weight) + ": ", "", acceWeight)) + .build(root) + } +} \ No newline at end of file diff --git a/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerIobTH.kt b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerIobTH.kt new file mode 100644 index 00000000000..e55dde62053 --- /dev/null +++ b/plugins/automation/src/main/kotlin/app/aaps/plugins/automation/triggers/TriggerIobTH.kt @@ -0,0 +1,85 @@ +package app.aaps.plugins.automation.triggers + +import android.widget.LinearLayout +import java.util.Optional +import dagger.android.HasAndroidInjector +import app.aaps.plugins.automation.R +import app.aaps.core.interfaces.logging.LTag +import app.aaps.core.interfaces.sharedPreferences.SP +import app.aaps.plugins.automation.elements.Comparator +import app.aaps.plugins.automation.elements.InputIobTH +import app.aaps.plugins.automation.elements.LabelWithElement +import app.aaps.plugins.automation.elements.LayoutBuilder +import app.aaps.plugins.automation.elements.StaticLabel +import app.aaps.core.utils.JsonHelper +import org.json.JSONObject +import javax.inject.Inject + +class TriggerIobTH(injector: HasAndroidInjector) : Trigger(injector) { + + @Inject lateinit var sp: SP + //@Inject lateinit var receiverStatusStore: ReceiverStatusStore + + var IobTHpercent = InputIobTH() + var comparator = Comparator(rh) + + // from TriggerWifiSsid: @Suppress("unused") + constructor(injector: HasAndroidInjector, IobTHpercent: Double, compare: Comparator.Compare) : this(injector) { + this.IobTHpercent = InputIobTH(IobTHpercent.toInt()) + comparator = Comparator(rh, compare) + } + + constructor(injector: HasAndroidInjector, triggerIobTH: TriggerIobTH) : this(injector) { + this.IobTHpercent = InputIobTH(triggerIobTH.IobTHpercent.value) + comparator = Comparator(rh, triggerIobTH.comparator.value) + } + + fun setValue(IobTHpercent: Int): TriggerIobTH { + this.IobTHpercent.value = IobTHpercent + return this + } + + fun comparator(comparator: Comparator.Compare): TriggerIobTH { + this.comparator.value = comparator + return this + } + + override fun shouldRun(): Boolean { + val actualPercent = sp.getInt(R.string.iob_threshold_percent,100) + if (comparator.value.check(actualPercent, IobTHpercent.value)) { + aapsLogger.debug(LTag.AUTOMATION, "set iob_threshold_percent ready for execution: " + friendlyDescription()) + return true + } + aapsLogger.debug(LTag.AUTOMATION, "set iob_threshold_percent NOT ready for execution: " + friendlyDescription()) + return false + } + + override fun dataJSON(): JSONObject = + JSONObject() + .put("iobTH_percent", IobTHpercent.value) + .put("comparator", comparator.value.toString()) + + override fun fromJSON(data: String): Trigger { + val d = JSONObject(data) + IobTHpercent.value = JsonHelper.safeGetInt(d, "iobTH_percent") + comparator.value = Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!) + return this + } + + override fun friendlyName(): Int = R.string.autoisf_iobTH_percent + + override fun friendlyDescription(): String = + rh.gs(R.string.iobTHpercentcompared, rh.gs(comparator.value.stringRes), IobTHpercent.value.toInt()) + + override fun icon(): Optional = Optional.of(R.drawable.ic_iobth) + + override fun duplicate(): Trigger = TriggerIobTH(injector, this) + + override fun generateDialog(root: LinearLayout) { + LayoutBuilder() + .add(StaticLabel(rh, R.string.autoisf_iobTH_percent, this)) + .add(comparator) + .add(LabelWithElement(rh, rh.gs(R.string.autoisf_iobTH_percent) + ": ", "%", IobTHpercent)) + .build(root) + } +} \ No newline at end of file diff --git a/plugins/automation/src/main/res/drawable/ic_acce_weight.xml b/plugins/automation/src/main/res/drawable/ic_acce_weight.xml new file mode 100644 index 00000000000..79a4d6c8503 --- /dev/null +++ b/plugins/automation/src/main/res/drawable/ic_acce_weight.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/plugins/automation/src/main/res/drawable/ic_autoisf_disabled.xml b/plugins/automation/src/main/res/drawable/ic_autoisf_disabled.xml new file mode 100644 index 00000000000..dbec1092526 --- /dev/null +++ b/plugins/automation/src/main/res/drawable/ic_autoisf_disabled.xml @@ -0,0 +1,30 @@ + + + + + + diff --git a/plugins/automation/src/main/res/drawable/ic_autoisf_enabled.xml b/plugins/automation/src/main/res/drawable/ic_autoisf_enabled.xml new file mode 100644 index 00000000000..6e544eb2000 --- /dev/null +++ b/plugins/automation/src/main/res/drawable/ic_autoisf_enabled.xml @@ -0,0 +1,34 @@ + + + + + + diff --git a/plugins/automation/src/main/res/drawable/ic_iobth.xml b/plugins/automation/src/main/res/drawable/ic_iobth.xml new file mode 100644 index 00000000000..6a81e346e6b --- /dev/null +++ b/plugins/automation/src/main/res/drawable/ic_iobth.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/plugins/automation/src/main/res/values/strings.xml b/plugins/automation/src/main/res/values/strings.xml index 62be8deebbe..6912cdf8b8a 100644 --- a/plugins/automation/src/main/res/values/strings.xml +++ b/plugins/automation/src/main/res/values/strings.xml @@ -141,6 +141,25 @@ Error during last Autotune run Another run of Autotune is detected, run cancelled + + AutoISF already enabled + AutoISF has been enabled + AutoISF already disabled + AutoISF has been disabled + openapsama_enable_autoISF + new value + as before + bgAccel_ISF_weight + iob_threshold_percent + Set bgAccel_ISF_weight to %1$.2f + Set iobTH to %1$d%% + BgAccel weight + iobTH percent + acce weight %1$s %2$s + iobTH percent %1$s %2$s + Disable glucose ISF weights(autoISF) + Enable glucose ISF weights(autoISF) + Steps Count diff --git a/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfEnableTest.kt b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfEnableTest.kt new file mode 100644 index 00000000000..0073da60229 --- /dev/null +++ b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/actions/ActionAutoisfEnableTest.kt @@ -0,0 +1,91 @@ +package app.aaps.plugins.automation.actions + +import app.aaps.core.data.plugin.PluginType +import app.aaps.core.interfaces.queue.Callback +import app.aaps.core.keys.BooleanKey +import app.aaps.core.validators.preferences.AdaptiveDoublePreference +import app.aaps.core.validators.preferences.AdaptiveIntPreference +import app.aaps.core.validators.preferences.AdaptiveIntentPreference +import app.aaps.plugins.automation.R +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.Mockito +import org.mockito.Mockito.`when` +import app.aaps.core.validators.preferences.AdaptiveSwitchPreference +import app.aaps.core.validators.preferences.AdaptiveUnitPreference + +// only useful with active AutoISF + +class ActionAutoisfEnableTest : ActionsTestBase() { + + lateinit var sut: ActionAutoisfEnable + + init { + addInjector { + if (it is AdaptiveDoublePreference) { + it.profileUtil = profileUtil + it.preferences = preferences + } + if (it is AdaptiveIntPreference) { + it.profileUtil = profileUtil + it.preferences = preferences + it.config = config + } + if (it is AdaptiveIntentPreference) { + it.preferences = preferences + } + if (it is AdaptiveUnitPreference) { + it.profileUtil = profileUtil + it.preferences = preferences + } + if (it is AdaptiveSwitchPreference) { + it.preferences = preferences + it.config = config + } + } + } + @BeforeEach + fun setup() { + + //testPumpPlugin.pumpDescription.isTempBasalCapable = true + `when`(rh.gs(R.string.enableautoisf)).thenReturn("Enable glucose ISF weights(autoISF)") + `when`(rh.gs(R.string.alreadyenabled)).thenReturn("Already enabled") + + sut = ActionAutoisfEnable(injector) + } + + @Test fun friendlyNameTest() { + assertThat(sut.friendlyName()).isEqualTo(R.string.enableautoisf) + } + + @Test fun shortDescriptionTest() { + assertThat(sut.shortDescription()).isEqualTo("Enable glucose ISF weights(autoISF)") + } + + @Test fun iconTest() { + assertThat(sut.icon()).isEqualTo(R.drawable.ic_autoisf_enabled) + } + + @Test fun doActionTest() { + `when`( preferences.get( BooleanKey.ApsUseAutoIsfWeights )).thenReturn(false) + sut.doAction(object : Callback() { + override fun run() { + assertThat( preferences.get( BooleanKey.ApsUseAutoIsfWeights )).isTrue() + } + }) + assertThat( preferences.get( BooleanKey.ApsUseAutoIsfWeights )).isTrue() + //Mockito.verify(loopPlugin, Mockito.times(1)).setPluginEnabled(PluginType.LOOP, true) + //Mockito.verify(configBuilder, Mockito.times(1)).storeSettings("ActionLoopEnable") + + + // another call should keep it enabled + `when`( preferences.get( BooleanKey.ApsUseAutoIsfWeights )).thenReturn(true) + sut.doAction(object : Callback() { + override fun run() {} + }) + assertThat( preferences.get( BooleanKey.ApsUseAutoIsfWeights )).isTrue() + //Mockito.verify(loopPlugin, Mockito.times(1)).setPluginEnabled(PluginType.LOOP, true) + //Mockito.verify(configBuilder, Mockito.times(1)).storeSettings("ActionLoopEnable") + } +} diff --git a/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputIobTHTest.kt b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputIobTHTest.kt new file mode 100644 index 00000000000..fb6d824d582 --- /dev/null +++ b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputIobTHTest.kt @@ -0,0 +1,16 @@ +package app.aaps.plugins.automation.elements + +import app.aaps.plugins.automation.triggers.TriggerTestBase +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +// only useful with active AutoISF + +class InputIobTHTest : TriggerTestBase() { + + @Test fun setValueTest() { + val i = InputIobTH() + i.value = 50 + assertThat(i.value).isEqualTo(50) + } +} diff --git a/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputWeightTest.kt b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputWeightTest.kt new file mode 100644 index 00000000000..ca126657518 --- /dev/null +++ b/plugins/automation/src/test/kotlin/app/aaps/plugins/automation/elements/InputWeightTest.kt @@ -0,0 +1,16 @@ +package app.aaps.plugins.automation.elements + +import app.aaps.plugins.automation.triggers.TriggerTestBase +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +// only useful with active AutoISF + +class InputWeightTest : TriggerTestBase() { + + @Test fun setValueTest() { + val i = InputWeight() + i.value = 0.5 + assertThat(i.value).isWithin(0.001).of(0.5) + } +}