From ddc9b9c6adc993d3e183219283d73ebb49f4f6c6 Mon Sep 17 00:00:00 2001 From: Philoul Date: Fri, 24 Sep 2021 18:34:48 +0200 Subject: [PATCH] Add new automation trigger Temp Target Value --- .../automation/di/AutomationModule.kt | 1 + .../general/automation/AutomationPlugin.kt | 1 + .../general/automation/triggers/Trigger.kt | 4 + .../triggers/TriggerTempTargetValue.kt | 98 +++++++++++++++++++ automation/src/main/res/values/strings.xml | 4 + core/src/main/res/values/strings.xml | 1 + 6 files changed, 109 insertions(+) create mode 100644 automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValue.kt diff --git a/automation/src/main/java/info/nightscout/androidaps/automation/di/AutomationModule.kt b/automation/src/main/java/info/nightscout/androidaps/automation/di/AutomationModule.kt index 2fd51b4088..977cc208d5 100644 --- a/automation/src/main/java/info/nightscout/androidaps/automation/di/AutomationModule.kt +++ b/automation/src/main/java/info/nightscout/androidaps/automation/di/AutomationModule.kt @@ -27,6 +27,7 @@ abstract class AutomationModule { @ContributesAndroidInjector abstract fun triggerBTDeviceInjector(): TriggerBTDevice @ContributesAndroidInjector abstract fun triggerRecurringTimeInjector(): TriggerRecurringTime @ContributesAndroidInjector abstract fun triggerTempTargetInjector(): TriggerTempTarget + @ContributesAndroidInjector abstract fun triggerTempTargetValueInjector(): TriggerTempTargetValue @ContributesAndroidInjector abstract fun triggerTime(): TriggerTime @ContributesAndroidInjector abstract fun triggerTimeRangeInjector(): TriggerTimeRange @ContributesAndroidInjector abstract fun triggerWifiSsidInjector(): TriggerWifiSsid diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt index 6fa4f2c439..404df4f4bb 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt @@ -311,6 +311,7 @@ class AutomationPlugin @Inject constructor( TriggerCOB(injector), TriggerProfilePercent(injector), TriggerTempTarget(injector), + TriggerTempTargetValue(injector), TriggerWifiSsid(injector), TriggerLocation(injector), TriggerAutosensValue(injector), diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt index a20bff3c2a..9f58616ed9 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt @@ -131,6 +131,10 @@ abstract class Trigger(val injector: HasAndroidInjector) { TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON( data.toString() ) + TriggerTempTargetValue::class.java.name, + TriggerTempTargetValue::class.java.simpleName -> TriggerTempTargetValue(injector).fromJSON( + data.toString() + ) TriggerTime::class.java.name, TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data.toString()) TriggerTimeRange::class.java.name, diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValue.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValue.kt new file mode 100644 index 0000000000..96d83c0326 --- /dev/null +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTempTargetValue.kt @@ -0,0 +1,98 @@ +package info.nightscout.androidaps.plugins.general.automation.triggers + +import android.widget.LinearLayout +import com.google.common.base.Optional +import dagger.android.HasAndroidInjector +import info.nightscout.androidaps.Constants +import info.nightscout.androidaps.automation.R +import info.nightscout.androidaps.database.ValueWrapper +import info.nightscout.androidaps.interfaces.GlucoseUnit +import info.nightscout.androidaps.interfaces.Profile +import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.plugins.general.automation.elements.Comparator +import info.nightscout.androidaps.plugins.general.automation.elements.InputBg +import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement +import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder +import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel +import info.nightscout.androidaps.utils.JsonHelper +import org.json.JSONObject + +class TriggerTempTargetValue(injector: HasAndroidInjector) : Trigger(injector) { + + var ttValue = InputBg(profileFunction) + var comparator = Comparator(resourceHelper) + + constructor(injector: HasAndroidInjector, value: Double, units: GlucoseUnit, compare: Comparator.Compare) : this(injector) { + ttValue = InputBg(profileFunction, value, units) + comparator = Comparator(resourceHelper, compare) + } + + constructor(injector: HasAndroidInjector, triggerTempTarget: TriggerTempTargetValue) : this(injector) { + ttValue = InputBg(profileFunction, triggerTempTarget.ttValue.value, triggerTempTarget.ttValue.units) + comparator = Comparator(resourceHelper, triggerTempTarget.comparator.value) + } + + fun comparator(comparator: Comparator.Compare): TriggerTempTargetValue { + this.comparator.value = comparator + return this + } + + fun setUnits(units: GlucoseUnit): TriggerTempTargetValue { + ttValue.units = units + return this + } + + fun setValue(value: Double): TriggerTempTargetValue { + ttValue.value = value + return this + } + + override fun shouldRun(): Boolean { + val tt = repository.getTemporaryTargetActiveAt(dateUtil.now()).blockingGet() + if (tt is ValueWrapper.Absent && comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) { + aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) + return true + } + if (tt is ValueWrapper.Existing && comparator.value.check(tt.value.lowTarget, Profile.toMgdl(ttValue.value, ttValue.units))) { + aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription()) + return true + } + aapsLogger.debug(LTag.AUTOMATION, "NOT ready for execution: " + friendlyDescription()) + return false + } + + override fun dataJSON(): JSONObject = + JSONObject() + .put("tt", ttValue.value) + .put("comparator", comparator.value.toString()) + .put("units", ttValue.units.asText) + + override fun fromJSON(data: String): Trigger { + val d = JSONObject(data) + ttValue.setUnits(GlucoseUnit.fromText(JsonHelper.safeGetString(d, "units", Constants.MGDL))) + ttValue.value = JsonHelper.safeGetDouble(d, "tt") + comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!)) + return this + } + + override fun friendlyName(): Int = R.string.careportal_temporarytargetvalue + + override fun friendlyDescription(): String { + return if (comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) + resourceHelper.gs(R.string.notemptarget) + else + resourceHelper.gs(if (ttValue.units == GlucoseUnit.MGDL) R.string.temptargetcomparedmgdl else R.string.temptargetcomparedmmol, resourceHelper.gs(comparator.value.stringRes), ttValue.value, ttValue.units) + } + + override fun icon(): Optional = Optional.of(R.drawable.ic_keyboard_tab) + + override fun duplicate(): Trigger = TriggerTempTargetValue(injector, this) + + override fun generateDialog(root: LinearLayout) { + LayoutBuilder() + .add(StaticLabel(resourceHelper, R.string.careportal_temporarytargetvalue, this)) + .add(comparator) + .add(LabelWithElement(resourceHelper, resourceHelper.gs(R.string.target_u, ttValue.units), "", ttValue)) + .build(root) + } +} \ No newline at end of file diff --git a/automation/src/main/res/values/strings.xml b/automation/src/main/res/values/strings.xml index 2e23b54761..842e53b14d 100644 --- a/automation/src/main/res/values/strings.xml +++ b/automation/src/main/res/values/strings.xml @@ -40,6 +40,9 @@ Glucose is not available Glucose %1$s %2$.0f %3$s Glucose %1$s %2$.1f %3$s + Temp Target does not exist + Temp Target %1$s %2$.0f %3$s + Temp Target %1$s %2$.1f %3$s Profile pct %1$s %2$d IOB %1$s %2$.1f And @@ -88,6 +91,7 @@ Lat: Lon: Glucose [%1$s]: + Target [%1$s]: Last bolus ago Last bolus time %1$s %2$s min ago COB diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 51db838cbe..b2bd4ac0b6 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -272,6 +272,7 @@ OpenAPS Offline Pump Battery Change Temporary target + Temporary target value Temporary target cancel Bolus wizard Finger