Message loops unified

This commit is contained in:
2026-04-13 21:49:48 +02:00
parent 7d4301d15b
commit 297f0050e3

View File

@@ -470,50 +470,6 @@ def on_message(topic, payload):
warn(f"Invalid set value for gauge {i}: '{payload}'") warn(f"Invalid set value for gauge {i}: '{payload}'")
return return
if topic == T_ZERO:
for i, g in enumerate(gauge_objects):
info(f"Zeroing all gauges")
g.zero()
gauge_last_rezero[i] = utime.ticks_ms()
info("All gauges zeroed")
return
if topic == T_SET:
try:
data = ujson.loads(payload)
if isinstance(data, dict):
for i, val in enumerate(data.values()):
if i < len(gauges):
g = gauges[i]
gauge_targets[i] = max(g["min"], min(g["max"], float(val)))
info(f"Gauge {i} target → {gauge_targets[i]:.1f}")
else:
val = float(payload)
for i in range(len(gauges)):
gauge_targets[i] = max(gauges[i]["min"], min(gauges[i]["max"], val))
info(f"All gauges target → {val:.1f}")
except Exception:
try:
val = float(payload)
for i in range(len(gauges)):
gauge_targets[i] = max(gauges[i]["min"], min(gauges[i]["max"], val))
info(f"All gauges target → {val:.1f}")
except:
warn(f"Invalid set value: '{payload}'")
return
if topic == T_RESET_DISCOVERY:
info("Reset discovery triggered")
try:
import uos
uos.remove(".discovery_ok")
info("Discovery flag file removed")
except:
pass
import machine
machine.reset()
for i, gt in enumerate(gauge_topics):
if topic == gt["led_red"]: if topic == gt["led_red"]:
state = payload.upper() == "ON" state = payload.upper() == "ON"
leds_red[i].value(1 if state else 0) leds_red[i].value(1 if state else 0)
@@ -577,6 +533,48 @@ def on_message(topic, payload):
info(f"Gauge {i} status green → {'ON' if state else 'OFF'}") info(f"Gauge {i} status green → {'ON' if state else 'OFF'}")
return return
if topic == T_ZERO:
for i, g in enumerate(gauge_objects):
g.zero()
gauge_last_rezero[i] = utime.ticks_ms()
info("All gauges zeroed")
return
if topic == T_SET:
try:
data = ujson.loads(payload)
if isinstance(data, dict):
for i, val in enumerate(data.values()):
if i < len(gauges):
g = gauges[i]
gauge_targets[i] = max(g["min"], min(g["max"], float(val)))
info(f"Gauge {i} target → {gauge_targets[i]:.1f}")
else:
val = float(payload)
for i in range(len(gauges)):
gauge_targets[i] = max(gauges[i]["min"], min(gauges[i]["max"], val))
info(f"All gauges target → {val:.1f}")
except Exception:
try:
val = float(payload)
for i in range(len(gauges)):
gauge_targets[i] = max(gauges[i]["min"], min(gauges[i]["max"], val))
info(f"All gauges target → {val:.1f}")
except:
warn(f"Invalid set value: '{payload}'")
return
if topic == T_RESET_DISCOVERY:
info("Reset discovery triggered")
try:
import uos
uos.remove(".discovery_ok")
info("Discovery flag file removed")
except:
pass
import machine
machine.reset()
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# MQTT connect + discovery # MQTT connect + discovery
@@ -622,6 +620,8 @@ def connect_mqtt():
_mqtt_check_interval_ms = 30000 _mqtt_check_interval_ms = 30000
_last_mqtt_check = 0 _last_mqtt_check = 0
client_ref = None
_mqtt_connected = False