Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation triggers for age and pump related things #3267

Merged
merged 8 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/objects/src/main/res/drawable/ic_cp_age_battery.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M20.234,10.422c-0.362,0 -0.656,0.294 -0.656,0.656v0.297h-0.469V9.031c0,-0.87 -0.708,-1.578 -1.578,-1.578H4.188c-0.87,0 -1.578,0.708 -1.578,1.578v5.938c0,0.87 0.708,1.578 1.578,1.578h13.344c0.72,0 1.323,-0.488 1.511,-1.149c0.01,0.001 0.018,0.006 0.028,0.006c0.332,0 0.602,-0.297 0.602,-0.664c0,-0.351 -0.25,-0.632 -0.563,-0.655v-1.46h0.469v0.297c0,0.362 0.294,0.656 0.656,0.656s0.656,-0.294 0.656,-0.656v-1.844C20.891,10.716 20.597,10.422 20.234,10.422zM18.109,14.969c0,0.318 -0.26,0.578 -0.578,0.578H4.188c-0.319,0 -0.578,-0.26 -0.578,-0.578V9.031c0,-0.319 0.259,-0.578 0.578,-0.578h13.344c0.318,0 0.578,0.259 0.578,0.578V14.969z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
<path
android:pathData="M9.703,10.273l-3.937,2.922l3.453,-1.218l1.781,1.765l4.438,-3.484l-4.297,1.687z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
</vector>
11 changes: 11 additions & 0 deletions core/objects/src/main/res/drawable/ic_cp_age_cannula.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M20.951,10.458h-1.99c-0.054,-0.733 -0.659,-1.313 -1.406,-1.313h-2.354c-0.747,0 -1.352,0.58 -1.406,1.313h-1.99c-0.388,0 -0.696,0.27 -0.771,0.622c-1.32,-0.021 -2.226,-0.555 -2.936,-0.999c-0.7,-0.437 -1.304,-0.814 -2.005,-0.434c-1.202,0.654 -0.843,2.024 -0.554,3.126c0.192,0.731 0.39,1.489 0.098,1.836c-0.603,0.718 -2.212,0.276 -2.781,0.045c-0.143,-0.057 -0.31,0.011 -0.369,0.156c-0.059,0.146 0.011,0.311 0.156,0.369c0.061,0.024 0.971,0.388 1.893,0.388c0.574,0 1.153,-0.142 1.534,-0.593c0.482,-0.571 0.256,-1.433 0.017,-2.345c-0.323,-1.234 -0.479,-2.074 0.276,-2.485c0.392,-0.213 0.762,-0.003 1.434,0.417c0.755,0.471 1.766,1.123 3.359,1.096C11.301,11.859 11.531,12 11.806,12h4.406v5.771l0.333,-0.854V12h4.406c0.443,0 0.802,-0.345 0.802,-0.771S21.394,10.458 20.951,10.458z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
</vector>
11 changes: 11 additions & 0 deletions core/objects/src/main/res/drawable/ic_cp_age_insulin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M19.455,9.988l-0.947,-1.652c-0.144,-0.252 -0.482,-0.329 -0.753,-0.174c-0.171,0.098 -0.266,0.267 -0.283,0.44l-1.149,-0.585c-0.093,-0.042 -0.202,-0.037 -0.292,0.015l-9.796,5.614l-0.156,-0.271c-0.187,-0.326 -0.61,-0.434 -0.945,-0.242c-0.336,0.192 -0.456,0.612 -0.269,0.938l2.747,4.79c0.187,0.326 0.61,0.434 0.945,0.242c0.336,-0.192 0.456,-0.612 0.269,-0.938L8.67,17.896l9.796,-5.614c0.09,-0.052 0.15,-0.143 0.16,-0.244l0.076,-1.289c0.158,0.072 0.351,0.076 0.522,-0.022C19.496,10.57 19.599,10.239 19.455,9.988zM17.407,12.176l-1.127,-1.769c-0.055,-0.086 -0.172,-0.11 -0.261,-0.054c-0.086,0.055 -0.115,0.165 -0.066,0.251c0.001,0.002 0.003,0.005 0.004,0.007l1.118,1.755l-0.752,0.431l-1.127,-1.769c-0.055,-0.086 -0.172,-0.111 -0.261,-0.054c-0.086,0.054 -0.115,0.165 -0.066,0.251c0.001,0.002 0.003,0.005 0.004,0.007l1.118,1.755l-0.752,0.431l-1.127,-1.769c-0.055,-0.086 -0.172,-0.111 -0.261,-0.054c-0.087,0.055 -0.115,0.165 -0.066,0.251c0.001,0.002 0.003,0.005 0.004,0.007l1.118,1.755l-0.655,0.375l-1.552,-2.482c-0.053,-0.084 -0.165,-0.111 -0.254,-0.06L6.82,14.666l-0.277,-0.483l9.652,-5.532l1.685,0.87l0.236,0.412l-0.101,1.894L17.407,12.176z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
</vector>
21 changes: 21 additions & 0 deletions core/objects/src/main/res/drawable/ic_cp_age_sensor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M13.568,13.328c0.673,0 1.219,-0.546 1.219,-1.219s-0.546,-1.219 -1.219,-1.219c-0.493,0 -0.913,0.296 -1.105,0.718h-1.603L9.464,8.126c-0.078,-0.195 -0.246,-0.309 -0.48,-0.314c-0.21,0.006 -0.394,0.144 -0.459,0.343l-2.16,6.53l-1.131,-2.751c-0.077,-0.188 -0.26,-0.31 -0.463,-0.31h-2c-0.276,0 -0.5,0.224 -0.5,0.5s0.224,0.5 0.5,0.5h1.665l1.518,3.69c0.078,0.188 0.261,0.31 0.463,0.31c0.006,0 0.012,0 0.018,0c0.209,-0.008 0.392,-0.145 0.458,-0.343L9.045,9.77l1.012,2.525c0.076,0.19 0.26,0.314 0.464,0.314h1.941C12.654,13.031 13.074,13.328 13.568,13.328z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
<path
android:pathData="M13.567,15.055c-0.276,0 -0.5,0.224 -0.5,0.5s0.224,0.5 0.5,0.5c2.176,0 3.945,-1.77 3.945,-3.945s-1.77,-3.946 -3.945,-3.946c-0.276,0 -0.5,0.224 -0.5,0.5s0.224,0.5 0.5,0.5c1.624,0 2.945,1.321 2.945,2.945S15.191,15.055 13.567,15.055z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
<path
android:pathData="M13.567,5.14c-0.276,0 -0.5,0.224 -0.5,0.5s0.224,0.5 0.5,0.5c3.291,0 5.969,2.677 5.969,5.968s-2.678,5.969 -5.969,5.969c-0.276,0 -0.5,0.224 -0.5,0.5s0.224,0.5 0.5,0.5c3.843,0 6.969,-3.126 6.969,-6.969S17.41,5.14 13.567,5.14z"
android:strokeAlpha="0.5"
android:fillColor="?attr/colorControlNormal"
android:fillAlpha="0.5"/>
</vector>
1 change: 1 addition & 0 deletions plugins/automation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
testImplementation(project(":shared:impl"))
testImplementation(project(":implementation"))
testImplementation(project(":plugins:main"))
testImplementation(project(":pump:virtual"))

api(Libs.AndroidX.constraintLayout)
api(Libs.Google.Android.PlayServices.location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.preference.PreferenceManager
import androidx.preference.PreferenceScreen
import app.aaps.core.data.model.GlucoseUnit
import app.aaps.core.data.plugin.PluginType
import app.aaps.core.data.pump.defs.PumpType
import app.aaps.core.data.time.T
import app.aaps.core.interfaces.aps.Loop
import app.aaps.core.interfaces.automation.Automation
Expand Down Expand Up @@ -56,14 +57,20 @@ import app.aaps.plugins.automation.triggers.TriggerBTDevice
import app.aaps.plugins.automation.triggers.TriggerBg
import app.aaps.plugins.automation.triggers.TriggerBolusAgo
import app.aaps.plugins.automation.triggers.TriggerCOB
import app.aaps.plugins.automation.triggers.TriggerCannulaAge
import app.aaps.plugins.automation.triggers.TriggerConnector
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.TriggerLocation
import app.aaps.plugins.automation.triggers.TriggerProfilePercent
import app.aaps.plugins.automation.triggers.TriggerPumpBatteryAge
import app.aaps.plugins.automation.triggers.TriggerPumpBatteryLevel
import app.aaps.plugins.automation.triggers.TriggerPumpLastConnection
import app.aaps.plugins.automation.triggers.TriggerRecurringTime
import app.aaps.plugins.automation.triggers.TriggerReservoirLevel
import app.aaps.plugins.automation.triggers.TriggerSensorAge
import app.aaps.plugins.automation.triggers.TriggerTempTarget
import app.aaps.plugins.automation.triggers.TriggerTempTargetValue
import app.aaps.plugins.automation.triggers.TriggerTime
Expand Down Expand Up @@ -384,7 +391,7 @@ class AutomationPlugin @Inject constructor(
}

fun getTriggerDummyObjects(): List<Trigger> {
return listOf(
val triggers = mutableListOf(
TriggerConnector(injector),
TriggerTime(injector),
TriggerRecurringTime(injector),
Expand All @@ -403,7 +410,24 @@ class AutomationPlugin @Inject constructor(
TriggerPumpLastConnection(injector),
TriggerBTDevice(injector),
TriggerHeartRate(injector),
TriggerSensorAge(injector),
TriggerCannulaAge(injector),
TriggerReservoirLevel(injector)
)

val pump = activePlugin.activePump
if (!pump.pumpDescription.isPatchPump) {
triggers.add(TriggerInsulinAge(injector))
}
if (pump.pumpDescription.isBatteryReplaceable || pump.isBatteryChangeLoggingEnabled()) {
triggers.add(TriggerPumpBatteryAge(injector))
}
val erosBatteryLinkAvailable = pump.model() == PumpType.OMNIPOD_EROS && pump.isUseRileyLinkBatteryLevel()
if (pump.model().supportBatteryLevel || erosBatteryLinkAvailable) {
triggers.add(TriggerPumpBatteryLevel(injector))
}

return triggers.toList()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ import app.aaps.plugins.automation.triggers.TriggerBTDevice
import app.aaps.plugins.automation.triggers.TriggerBg
import app.aaps.plugins.automation.triggers.TriggerBolusAgo
import app.aaps.plugins.automation.triggers.TriggerCOB
import app.aaps.plugins.automation.triggers.TriggerCannulaAge
import app.aaps.plugins.automation.triggers.TriggerConnector
import app.aaps.plugins.automation.triggers.TriggerDelta
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.TriggerLocation
import app.aaps.plugins.automation.triggers.TriggerProfilePercent
import app.aaps.plugins.automation.triggers.TriggerPumpBatteryAge
import app.aaps.plugins.automation.triggers.TriggerPumpBatteryLevel
import app.aaps.plugins.automation.triggers.TriggerPumpLastConnection
import app.aaps.plugins.automation.triggers.TriggerRecurringTime
import app.aaps.plugins.automation.triggers.TriggerReservoirLevel
import app.aaps.plugins.automation.triggers.TriggerSensorAge
import app.aaps.plugins.automation.triggers.TriggerTempTarget
import app.aaps.plugins.automation.triggers.TriggerTempTargetValue
import app.aaps.plugins.automation.triggers.TriggerTime
Expand Down Expand Up @@ -72,6 +78,12 @@ abstract class AutomationModule {
@ContributesAndroidInjector abstract fun triggerAutosensValueInjector(): TriggerAutosensValue
@ContributesAndroidInjector abstract fun triggerBgInjector(): TriggerBg
@ContributesAndroidInjector abstract fun triggerBolusAgoInjector(): TriggerBolusAgo
@ContributesAndroidInjector abstract fun triggerSensorAgeInjector(): TriggerSensorAge
@ContributesAndroidInjector abstract fun triggerCanulaAgeInjector(): TriggerCannulaAge
@ContributesAndroidInjector abstract fun triggerInsulinAgeInjector(): TriggerInsulinAge
@ContributesAndroidInjector abstract fun triggerReservoirLevelInjector(): TriggerReservoirLevel
@ContributesAndroidInjector abstract fun triggerPumpBatteryAgeInjector(): TriggerPumpBatteryAge
@ContributesAndroidInjector abstract fun triggerPumpBatteryLevelInjector(): TriggerPumpBatteryLevel
@ContributesAndroidInjector abstract fun triggerCOBInjector(): TriggerCOB
@ContributesAndroidInjector abstract fun triggerConnectorInjector(): TriggerConnector
@ContributesAndroidInjector abstract fun triggerDeltaInjector(): TriggerDelta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ abstract class Trigger(val injector: HasAndroidInjector) {
TriggerBg::class.java.simpleName -> TriggerBg(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())
TriggerCannulaAge::class.java.simpleName -> TriggerCannulaAge(injector).fromJSON(data.toString())
TriggerInsulinAge::class.java.simpleName -> TriggerInsulinAge(injector).fromJSON(data.toString())
TriggerReservoirLevel::class.java.simpleName -> TriggerReservoirLevel(injector).fromJSON(data.toString())
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())
TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString())
TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package app.aaps.plugins.automation.triggers

import android.widget.LinearLayout
import app.aaps.core.data.model.TE
import app.aaps.core.interfaces.logging.LTag
import app.aaps.core.utils.JsonHelper
import app.aaps.core.utils.JsonHelper.safeGetDouble
import app.aaps.plugins.automation.R
import app.aaps.plugins.automation.elements.Comparator
import app.aaps.plugins.automation.elements.InputDouble
import app.aaps.plugins.automation.elements.LabelWithElement
import app.aaps.plugins.automation.elements.LayoutBuilder
import app.aaps.plugins.automation.elements.StaticLabel
import dagger.android.HasAndroidInjector
import org.json.JSONObject
import java.text.DecimalFormat
import java.util.Optional

class TriggerCannulaAge(injector: HasAndroidInjector) : Trigger(injector) {

var cannulaAgeHours: InputDouble = InputDouble(0.0, 0.0, 336.0, 0.1, DecimalFormat("0.1"))
var comparator: Comparator = Comparator(rh)

private constructor(injector: HasAndroidInjector, triggerCannulaAge: TriggerCannulaAge) : this(injector) {
cannulaAgeHours = InputDouble(triggerCannulaAge.cannulaAgeHours)
comparator = Comparator(rh, triggerCannulaAge.comparator.value)
}

fun setValue(value: Double): TriggerCannulaAge {
cannulaAgeHours.value = value
return this
}

fun comparator(comparator: Comparator.Compare): TriggerCannulaAge {
this.comparator.value = comparator
return this
}

override fun shouldRun(): Boolean {
val therapyEvent = persistenceLayer.getLastTherapyRecordUpToNow(TE.Type.CANNULA_CHANGE)
val currentAgeHours = therapyEvent?.timestamp?.let { timestamp ->
(dateUtil.now() - timestamp) / (60 * 60 * 1000.0)
}?.toDouble() ?: 0.0
if (therapyEvent == null && comparator.value == Comparator.Compare.IS_NOT_AVAILABLE) {
aapsLogger.debug(LTag.AUTOMATION, "Ready for execution: " + friendlyDescription())
return true
}
if (therapyEvent == null) {
aapsLogger.debug(LTag.AUTOMATION, "NOT ready for execution: " + friendlyDescription())
return false
}
if (comparator.value.check(currentAgeHours, cannulaAgeHours.value)) {
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("cannulaAgeHours", cannulaAgeHours.value)
.put("comparator", comparator.value.toString())

override fun fromJSON(data: String): Trigger {
val d = JSONObject(data)
cannulaAgeHours.setValue(safeGetDouble(d, "cannulaAgeHours"))
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")!!))
return this
}

override fun friendlyName(): Int = R.string.triggerCannulaAgeLabel

override fun friendlyDescription(): String =
rh.gs(R.string.triggerCannulaAgeDesc, rh.gs(comparator.value.stringRes), cannulaAgeHours.value)

override fun icon(): Optional<Int> {
val isPatchPump = activePlugin.activePump.pumpDescription.isPatchPump
return if (isPatchPump) {
Optional.of(app.aaps.core.objects.R.drawable.ic_patch_pump_outline)
} else {
Optional.of(app.aaps.core.objects.R.drawable.ic_cp_age_cannula)
}
}

override fun duplicate(): Trigger = TriggerCannulaAge(injector, this)

override fun generateDialog(root: LinearLayout) {
LayoutBuilder()
.add(StaticLabel(rh, R.string.triggerCannulaAgeLabel, this))
.add(comparator)
.add(LabelWithElement(rh, rh.gs(R.string.triggerCannulaAgeLabel) + ": ", rh.gs(app.aaps.core.interfaces.R.string.unit_hour), cannulaAgeHours))
.build(root)
}
}
Loading