-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanejo_Estados.go
More file actions
72 lines (61 loc) · 2.68 KB
/
Copy pathmanejo_Estados.go
File metadata and controls
72 lines (61 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import "time"
// ── Estados del sistema ───────────────────────────────────────────────────
type SistemaEstado uint8
const (
EstadoActivo SistemaEstado = iota
EstadoSleep // pantalla OFF, sensores corriendo
EstadoAlerta // pantalla ON con mensaje urgente
)
// ── Configuración de umbrales de alerta ───────────────────────────────────
const (
AlertaNivelBajo = 30 // % — enciende pantalla si baja de aquí
AlertaBateriaBaja = 20 // % — enciende pantalla si batería baja de aquí
AlertaTempAlta = 50 // °C — temperatura de agua peligrosa
TimeoutSleep = 5 * time.Minute // inactividad → sleep
)
var (
estadoSistema = EstadoActivo
ultimaActividad = time.Now()
wakeupChan = make(chan struct{}, 1)
)
// ── Tipo de alerta para saber qué mensaje mostrar ─────────────────────────
type TipoAlerta uint8
const (
AlertaNinguna TipoAlerta = iota
AlertaNivelBajoT // NivelAgua < 30%
AlertaBateriaT // BatPct < 20%
AlertaTempAltaT // TempAgua > 50°C
AlertaSensorErr // TempAgua == -99
AlertaTanqueLleno // NivelAgua >= UmbralApagado
)
var alertaActual = AlertaNinguna
// ── Evalúa si hay que salir de SLEEP por una condición crítica ────────────
// Llamar desde el loop principal (Core 0) en cada iteración.
func checkAlertas(datos SensorData) (TipoAlerta, bool) {
// Prioridad: sensor roto > temp alta > batería > nivel bajo > tanque lleno
switch {
case datos.TempAgua == -99 || datos.TempExterior == -99:
return AlertaSensorErr, true
case datos.TempAgua > AlertaTempAlta:
return AlertaTempAltaT, true
case datos.BatPct < AlertaBateriaBaja:
return AlertaBateriaT, true
case datos.NivelAgua < AlertaNivelBajo:
return AlertaNivelBajoT, true
case datos.NivelAgua >= UmbralApagado:
return AlertaTanqueLleno, true
}
return AlertaNinguna, false
}
// ── Registra actividad (llamar al detectar pulsación de botón) ────────────
func registrarActividad() {
ultimaActividad = time.Now()
if estadoSistema == EstadoSleep {
estadoSistema = EstadoActivo
}
}
// ── Verifica si expiró el timeout de inactividad ──────────────────────────
func verificarTimeout() bool {
return time.Since(ultimaActividad) > TimeoutSleep
}