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

Add day automation #3202

Merged
merged 1 commit into from
Jan 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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.TriggerConnector
import app.aaps.plugins.automation.triggers.TriggerDay
import app.aaps.plugins.automation.triggers.TriggerDelta
import app.aaps.plugins.automation.triggers.TriggerHeartRate
import app.aaps.plugins.automation.triggers.TriggerIob
Expand Down Expand Up @@ -386,6 +387,7 @@ class AutomationPlugin @Inject constructor(
TriggerRecurringTime(injector),
TriggerTimeRange(injector),
TriggerBg(injector),
TriggerDay(injector),
TriggerDelta(injector),
TriggerIob(injector),
TriggerCOB(injector),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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.TriggerConnector
import app.aaps.plugins.automation.triggers.TriggerDay
import app.aaps.plugins.automation.triggers.TriggerDelta
import app.aaps.plugins.automation.triggers.TriggerDummy
import app.aaps.plugins.automation.triggers.TriggerHeartRate
Expand Down Expand Up @@ -74,6 +75,7 @@ abstract class AutomationModule {
@ContributesAndroidInjector abstract fun triggerBolusAgoInjector(): TriggerBolusAgo
@ContributesAndroidInjector abstract fun triggerCOBInjector(): TriggerCOB
@ContributesAndroidInjector abstract fun triggerConnectorInjector(): TriggerConnector
@ContributesAndroidInjector abstract fun triggerDayInjector(): TriggerDay
@ContributesAndroidInjector abstract fun triggerDeltaInjector(): TriggerDelta
@ContributesAndroidInjector abstract fun triggerDummyInjector(): TriggerDummy
@ContributesAndroidInjector abstract fun triggerHeartRateInjector(): TriggerHeartRate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ abstract class Trigger(val injector: HasAndroidInjector) {
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())
TriggerDay::class.java.simpleName -> TriggerDay(injector).fromJSON(data.toString())
TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString())
TriggerDummy::class.java.simpleName -> TriggerDummy(injector).fromJSON(data.toString())
TriggerHeartRate::class.java.simpleName -> TriggerHeartRate(injector).fromJSON(data.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package app.aaps.plugins.automation.triggers

import android.widget.LinearLayout
import app.aaps.core.interfaces.logging.LTag
import app.aaps.core.ui.elements.WeekDay
import app.aaps.core.utils.JsonHelper
import app.aaps.plugins.automation.R
import app.aaps.plugins.automation.elements.InputWeekDay
import app.aaps.plugins.automation.elements.LayoutBuilder
import app.aaps.plugins.automation.elements.StaticLabel
import dagger.android.HasAndroidInjector
import org.json.JSONObject
import java.util.Calendar
import java.util.Objects
import java.util.Optional

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

val days = InputWeekDay()

constructor(injector: HasAndroidInjector, triggerDay: TriggerDay) : this(injector) {
if (days.weekdays.size >= 0)
System.arraycopy(triggerDay.days.weekdays, 0, days.weekdays, 0, triggerDay.days.weekdays.size)
}

override fun shouldRun(): Boolean {
val scheduledDayOfWeek = Calendar.getInstance()[Calendar.DAY_OF_WEEK]
if (days.isSet(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(scheduledDayOfWeek)))) {
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 {
val data = JSONObject()
for (i in days.weekdays.indices) {
data.put(WeekDay.DayOfWeek.entries[i].name, days.weekdays[i])
}
return data
}

override fun fromJSON(data: String): Trigger {
val o = JSONObject(data)
for (i in days.weekdays.indices) {
days.weekdays[i] = JsonHelper.safeGetBoolean(o, WeekDay.DayOfWeek.entries[i].name)
}

return this
}

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

override fun friendlyDescription(): String {
val sb = StringBuilder()
sb.append(rh.gs(R.string.every))
sb.append(" ")
var counter = 0
for (i in days.getSelectedDays()) {
if (counter++ > 0) sb.append(",")
sb.append(rh.gs(Objects.requireNonNull(WeekDay.DayOfWeek.fromCalendarInt(i)).shortName))
}
return if (counter == 0) rh.gs(R.string.never) else sb.toString()
}

override fun icon(): Optional<Int> = Optional.of(R.drawable.ic_calendar_days)

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

override fun generateDialog(root: LinearLayout) {
LayoutBuilder()
.add(StaticLabel(rh, R.string.day, this))
.add(days)
.build(root)
}
}
10 changes: 10 additions & 0 deletions plugins/automation/src/main/res/drawable/ic_calendar_days.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="22dp"
android:height="24dp"
android:viewportWidth="22"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="m7.933,2.383c0,-0.551 -0.441,-0.995 -0.99,-0.995 -0.549,0 -0.99,0.444 -0.99,0.995L5.953,4.042L4.303,4.042c-1.456,0 -2.64,1.19 -2.64,2.653L1.663,7.358 1.663,9.348 1.663,19.96c0,1.463 1.184,2.653 2.64,2.653L17.505,22.613c1.456,0 2.64,-1.19 2.64,-2.653L20.145,9.348 20.145,7.358 20.145,6.695c0,-1.463 -1.184,-2.653 -2.64,-2.653L15.854,4.042L15.854,2.383c0,-0.551 -0.441,-0.995 -0.99,-0.995 -0.549,0 -0.99,0.444 -0.99,0.995v1.658h-5.941zM3.643,9.348L6.943,9.348L6.943,11.669L3.643,11.669ZM3.643,13.659h3.3v2.653L3.643,16.312ZM8.924,13.659h3.96v2.653L8.924,16.312ZM14.864,13.659h3.3v2.653h-3.3zM18.165,11.669L14.864,11.669L14.864,9.348h3.3zM18.165,18.302v1.658c0,0.365 -0.297,0.663 -0.66,0.663h-2.64v-2.321zM12.884,18.302v2.321L8.924,20.623v-2.321zM6.943,18.302v2.321h-2.64c-0.363,0 -0.66,-0.298 -0.66,-0.663L3.643,18.302ZM12.884,11.669L8.924,11.669L8.924,9.348h3.96z"
android:strokeWidth="0.0413548"/>
</vector>
1 change: 1 addition & 0 deletions plugins/automation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<string name="triggerHeartRateDesc">HR %1$s %2$.0f</string>
<string name="iob_u">IOB [U]:</string>
<string name="distance_short">Dist [m]:</string>
<string name="day">Day</string>
<string name="recurringTime">Recurring time</string>
<string name="every">Every</string>
<string name="never">Never</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package app.aaps.plugins.automation.triggers

import app.aaps.core.ui.elements.WeekDay
import com.google.common.truth.Truth.assertThat
import org.json.JSONObject
import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert

class TriggerDayTest : TriggerTestBase() {

@Test fun shouldRunTest() {
var t = TriggerDay(injector)
t.days[WeekDay.DayOfWeek.MONDAY] = true
assertThat(t.shouldRun()).isFalse()

// scheduled 1 min before
t = TriggerDay(injector)
t.days[WeekDay.DayOfWeek.SUNDAY] = true
assertThat(t.shouldRun()).isTrue()
}

private var dayJson =
"{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":true,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"type\":\"TriggerRecurringDay\"}"

@Test
fun toJSONTest() {
val t = TriggerDay(injector)
t.days[WeekDay.DayOfWeek.SUNDAY] = true
JSONAssert.assertEquals(dayJson, t.toJSON(), true)
}

@Test
fun fromJSONTest() {
val t = TriggerDay(injector)
val t2 = TriggerDummy(injector).instantiate(JSONObject(t.toJSON())) as TriggerDay
assertThat(t2.days.getSelectedDays()).contains(WeekDay.DayOfWeek.SUNDAY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ open class TriggerTestBase : TestBaseWithProfile() {
if (it is TriggerBg) {
it.profileFunction = profileFunction
}
if (it is TriggerDay) {
it.dateUtil = dateUtil
}
if (it is TriggerTime) {
it.dateUtil = dateUtil
}
Expand Down