Wait, is this peak ?
This commit is contained in:
@@ -19,7 +19,7 @@ except Exception:
|
||||
orchestrator_id = None
|
||||
cooking_state = None
|
||||
mqtt_connected = False
|
||||
unsubscribed_hello = False
|
||||
should_unsubscribe_hello = False
|
||||
|
||||
# --- ASYNC SIGNALS & QUEUES ---
|
||||
# Event to signal when orchestrator requests sensor data (prevents MQTT lock deadlock)
|
||||
@@ -95,25 +95,29 @@ def on_received_cooking_state_update(state, is_error=False, is_terminated=False)
|
||||
|
||||
def on_cooking_state_change(state):
|
||||
"""Callback executed whenever local cooking state transitions."""
|
||||
BLINK_INTERVAL_MS = 500
|
||||
|
||||
if status_led and cookingState:
|
||||
if state == cookingState.CookingStates.IDLE:
|
||||
status_led.set_color(0, 0, 0) # Off
|
||||
elif state == cookingState.CookingStates.PREHEATING:
|
||||
status_led.set_color(255, 165, 0) # Orange
|
||||
status_led.color = status_led.OFF
|
||||
status_led.blink_off()
|
||||
elif state == cookingState.CookingStates.COOKING:
|
||||
status_led.set_color(255, 0, 0) # Red
|
||||
status_led.color = status_led.YELLOW
|
||||
status_led.blink_off()
|
||||
elif state == cookingState.CookingStates.STIRRING_REQUIRED:
|
||||
status_led.color = status_led.ORANGE
|
||||
status_led.blink_on(BLINK_INTERVAL_MS)
|
||||
elif state == cookingState.CookingStates.ALERT:
|
||||
status_led.color = status_led.RED
|
||||
status_led.blink_on(BLINK_INTERVAL_MS)
|
||||
elif state == cookingState.CookingStates.DONE:
|
||||
status_led.set_color(0, 255, 0) # Green
|
||||
elif state in (
|
||||
cookingState.CookingStates.ERROR,
|
||||
cookingState.CookingStates.ABORTED,
|
||||
):
|
||||
status_led.set_color(255, 0, 255) # Magenta/Purple
|
||||
status_led.color = status_led.GREEN
|
||||
status_led.blink_off()
|
||||
|
||||
|
||||
def on_mqtt_message(message):
|
||||
"""Sync callback: Lightweight! Only updates variables or triggers async signals."""
|
||||
global orchestrator_id, cooking_state, unsubscribed_hello
|
||||
global orchestrator_id, cooking_state, should_unsubscribe_hello
|
||||
print("[MQTT] Received message on topic:", message.get("topic"))
|
||||
|
||||
payload_data = None
|
||||
@@ -132,13 +136,7 @@ def on_mqtt_message(message):
|
||||
):
|
||||
orchestrator_id = payload_data.get("id_orchestrator")
|
||||
print("[MQTT] Hello response received from orchestrator:", orchestrator_id)
|
||||
if not unsubscribed_hello:
|
||||
unsubscribed_hello = True
|
||||
try:
|
||||
mqtt_client.unsubscribe(config.MQTT_TOPIC_HELLO)
|
||||
print("[MQTT] Successfully unsubscribed from topic:", config.MQTT_TOPIC_HELLO)
|
||||
except Exception as e:
|
||||
print("[MQTT] Unsubscribe error:", e)
|
||||
should_unsubscribe_hello = True
|
||||
|
||||
# 2. Cooking Parameters / Sensor Request
|
||||
elif (
|
||||
@@ -275,8 +273,7 @@ async def mqtt_poll_task():
|
||||
mqtt_client.poll()
|
||||
now = time.time()
|
||||
if now - last_ping >= 15:
|
||||
if mqtt_client._client:
|
||||
mqtt_client._client.ping()
|
||||
mqtt_client.ping()
|
||||
last_ping = now
|
||||
except OSError as e:
|
||||
print("[MQTT Task] Socket error encountered during poll/ping:", e)
|
||||
@@ -287,9 +284,17 @@ async def mqtt_poll_task():
|
||||
|
||||
|
||||
async def orchestrator_hello_task():
|
||||
global mqtt_connected
|
||||
global mqtt_connected, should_unsubscribe_hello
|
||||
while True:
|
||||
if orchestrator_id is not None:
|
||||
if should_unsubscribe_hello:
|
||||
try:
|
||||
mqtt_client.unsubscribe(config.MQTT_TOPIC_HELLO)
|
||||
should_unsubscribe_hello = False
|
||||
print("[MQTT] Successfully unsubscribed from hello topic.")
|
||||
except Exception as e:
|
||||
print("[MQTT] Unsubscribe error:", e)
|
||||
|
||||
# Hello successfully acknowledged! Stop looping this task.
|
||||
print("[Hello Task] Orchestrator acknowledged. Stopping hello task.")
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user