Skip to content

Commit 8e591e5

Browse files
committed
Add an option to trigger only one time the automation A/C
1 parent 06cff81 commit 8e591e5

13 files changed

Lines changed: 105 additions & 4 deletions

File tree

‎app/src/main/java/com/mg4/control/automation/ClimateAutomationDecision.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ object ClimateAutomationDecision {
2020
config.cold.active && temp <= config.cold.threshold.toFloat() -> Outcome.COLD
2121
else -> Outcome.NONE
2222
}
23+
24+
/**
25+
* Faux quand l'option « une seule fois par démarrage de la voiture » retient l'automatisation.
26+
*
27+
* [alreadyTriggered] dit si une règle a déjà été **appliquée** depuis que l'application
28+
* tourne — pas seulement évaluée : un premier contact entre les deux seuils n'applique rien
29+
* et laisse l'essai disponible pour le contact suivant.
30+
*/
31+
fun allowed(config: ClimateAutomationSettings.Config, alreadyTriggered: Boolean): Boolean =
32+
!(config.oncePerStart && alreadyTriggered)
2333
}

‎app/src/main/java/com/mg4/control/automation/ClimateAutomationSettings.kt‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ object ClimateAutomationSettings {
1717
const val PREFS = "mg4_settings"
1818

1919
const val KEY_ENABLED = "ac_auto_enabled"
20+
/** Option « une seule fois par démarrage de la voiture ». Décochée : comportement d'origine. */
21+
const val KEY_ONCE = "ac_auto_once"
2022

2123
// Une règle = un préfixe ; les clés sont dérivées pour éviter douze constantes quasi jumelles.
2224
private const val HOT = "ac_auto_hot_"
@@ -65,7 +67,13 @@ object ClimateAutomationSettings {
6567
data class Config(
6668
val enabled: Boolean,
6769
val hot: Rule,
68-
val cold: Rule
70+
val cold: Rule,
71+
/**
72+
* Vrai pour ne déclencher qu'une fois tant que l'application tourne : après un arrêt
73+
* court, la remise du contact ne renvoie plus les réglages par-dessus ceux que le
74+
* conducteur a pu faire à la main. Voir [ClimateAutomationDecision.allowed].
75+
*/
76+
val oncePerStart: Boolean = false
6977
)
7078

7179
fun read(context: Context): Config {
@@ -85,7 +93,8 @@ object ClimateAutomationSettings {
8593
return Config(
8694
enabled = p.getBoolean(KEY_ENABLED, false),
8795
hot = rule(HOT, DEFAULT_HOT_THRESHOLD, DEFAULT_HOT_TARGET),
88-
cold = rule(COLD, DEFAULT_COLD_THRESHOLD, DEFAULT_COLD_TARGET)
96+
cold = rule(COLD, DEFAULT_COLD_THRESHOLD, DEFAULT_COLD_TARGET),
97+
oncePerStart = p.getBoolean(KEY_ONCE, false)
8998
)
9099
}
91100

‎app/src/main/java/com/mg4/control/service/MG4ControlService.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ class MG4ControlService : Service() {
136136
* IGNITION_RUN arrivent souvent à quelques secondes d'écart : sans ça, on écraserait
137137
* un réglage que l'utilisateur vient de faire à la main entre les deux). */
138138
@Volatile private var climateAutoLastRunMs = 0L
139+
/**
140+
* Vrai dès que l'automatisation A/C a appliqué une règle. Gardé en mémoire seulement, et
141+
* au niveau du processus : il survit à une recréation du service mais retombe au
142+
* redémarrage de l'application — donc au démarrage de la voiture, pas à une simple remise
143+
* du contact. C'est l'option « une seule fois par démarrage » qui le consulte.
144+
*/
145+
@Volatile private var climateAutoTriggered = false
139146
private const val CLIMATE_AUTO_DEBOUNCE_MS = 60_000L
140147

141148
}
@@ -1391,6 +1398,9 @@ class MG4ControlService : Service() {
13911398
*
13921399
* Anti-rebond [CLIMATE_AUTO_DEBOUNCE_MS] : démarrage service et IGNITION_RUN se suivent de
13931400
* près, et réappliquer écraserait un réglage manuel fait entre les deux.
1401+
*
1402+
* Option « une seule fois par démarrage de la voiture » : le même souci, étendu à tout le
1403+
* temps où l'application tourne. Sans elle, chaque remise du contact renvoie les réglages.
13941404
*/
13951405
private fun tryClimateAutomation(origin: String) {
13961406
val ctx = applicationContext
@@ -1422,6 +1432,11 @@ class MG4ControlService : Service() {
14221432
"sa propre climatisation — priorité au profil")
14231433
return
14241434
}
1435+
if (!ClimateAutomationDecision.allowed(cfg, climateAutoTriggered)) {
1436+
AppLogger.i(TAG, "Auto A/C ($origin) : déjà déclenchée depuis le démarrage de " +
1437+
"l'application, option « une seule fois » cochée — rien n'est appliqué")
1438+
return
1439+
}
14251440
val since = System.currentTimeMillis() - climateAutoLastRunMs
14261441
if (climateAutoLastRunMs != 0L && since < CLIMATE_AUTO_DEBOUNCE_MS) {
14271442
AppLogger.i(TAG, "Auto A/C ($origin) : déjà appliquée il y a ${since / 1000}s — skip")
@@ -1439,6 +1454,8 @@ class MG4ControlService : Service() {
14391454
ClimateAutomationDecision.Outcome.NONE -> return@whenKatman1Ready
14401455
}
14411456
climateAutoLastRunMs = System.currentTimeMillis()
1457+
// Levé même si l'option est décochée : la cocher ensuite prend effet tout de suite.
1458+
climateAutoTriggered = true
14421459
// applyClimatePreset enchaîne des bascules (plusieurs secondes) → jamais sur le main thread.
14431460
CoroutineScope(Dispatchers.IO).launch {
14441461
val ok = MG4Hardware.applyClimatePreset(

‎app/src/main/java/com/mg4/control/ui/AutomationFragment.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ class AutomationFragment : Fragment() {
525525
prefs.edit().putBoolean(ClimateAutomationSettings.KEY_ENABLED, checked).apply()
526526
deplier(checked)
527527
}
528+
view.findViewById<CheckBox>(R.id.check_ac_once).apply {
529+
isChecked = prefs.getBoolean(ClimateAutomationSettings.KEY_ONCE, false)
530+
setOnCheckedChangeListener { _, checked ->
531+
prefs.edit().putBoolean(ClimateAutomationSettings.KEY_ONCE, checked).apply()
532+
}
533+
}
528534

529535
bindClimateRule(
530536
view, prefs, hot = true,

‎app/src/main/res/layout/fragment_automation.xml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,30 @@
14471447

14481448
</LinearLayout>
14491449

1450+
<!-- ───────── Une seule fois par démarrage ───────── -->
1451+
<View
1452+
android:layout_width="match_parent"
1453+
android:layout_height="1dp"
1454+
android:background="@color/dash_border"
1455+
android:layout_marginTop="12dp"
1456+
android:layout_marginBottom="10dp" />
1457+
1458+
<CheckBox
1459+
android:id="@+id/check_ac_once"
1460+
android:layout_width="wrap_content"
1461+
android:layout_height="wrap_content"
1462+
android:text="@string/ac_auto_once"
1463+
android:textColor="@color/text_primary"
1464+
android:textSize="16sp" />
1465+
1466+
<TextView
1467+
android:layout_width="match_parent"
1468+
android:layout_height="wrap_content"
1469+
android:layout_marginStart="32dp"
1470+
android:text="@string/ac_auto_once_hint"
1471+
android:textColor="@color/text_secondary"
1472+
android:textSize="13sp" />
1473+
14501474
<TextView
14511475
android:layout_width="match_parent"
14521476
android:layout_height="wrap_content"

‎app/src/main/res/values-de/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@
373373
<string name="ac_auto_def_front">Frontscheibenheizung</string>
374374
<string name="ac_auto_def_rear">Heckscheibenheizung</string>
375375
<string name="ac_auto_note">Wird beim Start des Fahrzeugs angewendet: Klimaanlage und A/C werden eingeschaltet, dann werden die obigen Einstellungen gesendet.</string>
376+
<string name="ac_auto_once">Nur einmal pro Fahrzeugstart</string>
377+
<string name="ac_auto_once_hint">Ohne diese Option werden die Einstellungen bei jedem Einschalten der Zündung erneut gesendet. Mit ihr geschieht das nach einem kurzen Halt nicht mehr: Ihre manuellen Änderungen bleiben erhalten.</string>
376378
<string name="clim_card_title">Klimaanlage</string>
377379
<string name="clim_temperature">Temperatur</string>
378380
<string name="clim_fan">Gebläse</string>

‎app/src/main/res/values-en/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@
373373
<string name="ac_auto_def_front">Front defrost</string>
374374
<string name="ac_auto_def_rear">Rear defrost</string>
375375
<string name="ac_auto_note">Applied when the vehicle starts: climate control and A/C are turned on, then the settings above are sent.</string>
376+
<string name="ac_auto_once">Only once per car start</string>
377+
<string name="ac_auto_once_hint">Without this option, the settings are sent again each time the ignition is switched on. With it, they are not sent again after a short stop, so your manual changes are kept.</string>
376378
<string name="clim_card_title">Climate</string>
377379
<string name="clim_temperature">Temperature</string>
378380
<string name="clim_fan">Fan</string>

‎app/src/main/res/values-es/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@
373373
<string name="ac_auto_def_front">Desempañador delantero</string>
374374
<string name="ac_auto_def_rear">Desempañador trasero</string>
375375
<string name="ac_auto_note">Se aplica al arrancar el vehículo: se encienden la climatización y el A/A, y luego se envían los ajustes anteriores.</string>
376+
<string name="ac_auto_once">Solo una vez por arranque del coche</string>
377+
<string name="ac_auto_once_hint">Sin esta opción, los ajustes se envían de nuevo cada vez que se da el contacto. Con ella, ya no se envían tras una parada corta: tus cambios manuales se conservan.</string>
376378
<string name="clim_card_title">Climatización</string>
377379
<string name="clim_temperature">Temperatura</string>
378380
<string name="clim_fan">Ventilación</string>

‎app/src/main/res/values-it/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@
373373
<string name="ac_auto_def_front">Sbrinamento anteriore</string>
374374
<string name="ac_auto_def_rear">Sbrinamento posteriore</string>
375375
<string name="ac_auto_note">Applicato all\'avvio del veicolo: il climatizzatore e l\'A/C vengono accesi, poi vengono inviate le impostazioni sopra.</string>
376+
<string name="ac_auto_once">Una sola volta per avvio dell’auto</string>
377+
<string name="ac_auto_once_hint">Senza questa opzione, le impostazioni vengono inviate di nuovo a ogni accensione del quadro. Con essa, non vengono più inviate dopo una breve sosta: le tue modifiche manuali vengono mantenute.</string>
376378
<string name="clim_card_title">Climatizzazione</string>
377379
<string name="clim_temperature">Temperatura</string>
378380
<string name="clim_fan">Ventilazione</string>

‎app/src/main/res/values-pt/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@
373373
<string name="ac_auto_def_front">Desembaciamento dianteiro</string>
374374
<string name="ac_auto_def_rear">Desembaciamento traseiro</string>
375375
<string name="ac_auto_note">Aplicado ao arrancar o veículo: a climatização e o A/C são ligados e depois as definições acima são enviadas.</string>
376+
<string name="ac_auto_once">Apenas uma vez por arranque do carro</string>
377+
<string name="ac_auto_once_hint">Sem esta opção, as definições são enviadas de novo sempre que se liga a ignição. Com ela, deixam de ser enviadas após uma paragem curta: as suas alterações manuais são mantidas.</string>
376378
<string name="clim_card_title">Climatização</string>
377379
<string name="clim_temperature">Temperatura</string>
378380
<string name="clim_fan">Ventilação</string>

0 commit comments

Comments
 (0)