From dff6cb9d41cff0dfad012d387371689d08e59657 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Mon, 10 Aug 2026 17:10:32 +0200 Subject: [PATCH] Bigger timeout to get the cooking plan --- orchestrateur/main.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/orchestrateur/main.py b/orchestrateur/main.py index 26391e7..8cce822 100644 --- a/orchestrateur/main.py +++ b/orchestrateur/main.py @@ -264,7 +264,7 @@ async def handle_new_dish(microwave_id, detected_height): ir_data_events.pop(microwave_id, None) async def request_cloud_cooking_plan(microwave_id, sensors_data): - """Sends all data to the cloud with up to 3 retries (30s interval).""" + """Sends all data to the cloud with up to 3 retries (330s timeout for AI generation).""" global cloud_alert microwave_states[microwave_id] = MicrowaveState.WAITING_FOR_CLOUD URL = "https://smartwave.matthiasg.dev/cooking-params" @@ -276,7 +276,10 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data): print(f"[{microwave_id}] Requesting cooking plan from cloud app...") max_retries = 3 - retry_delay_seconds = 30 + retry_delay_seconds = 10 # Reduced delay since each request already waits up to 5 mins + + # Tuple format: (connect_timeout_seconds, read_timeout_seconds) + HTTP_TIMEOUT = (10, 330) for attempt in range(1, max_retries + 1): if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD: @@ -284,30 +287,43 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data): return try: - print(f"[{microwave_id}] Connection attempt {attempt}/{max_retries}...") - response = await asyncio.to_thread(requests.post, URL, json=sensors_data, timeout=30) - # Abort if state changed (e.g. user removed dish while waiting for wifi) + print(f"[{microwave_id}] Connection attempt {attempt}/{max_retries} (Waiting up to 5.5 min)...") + + response = await asyncio.to_thread( + requests.post, + URL, + json=sensors_data, + timeout=HTTP_TIMEOUT + ) + + # Abort if state changed while waiting for cloud AI response if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD: print(f"[{microwave_id}] Dish removed during API request. Discarding API plan.") return response.raise_for_status() - plan = response.json().get("cook_plan", {}) + + # Robust JSON extraction: handles direct dict or nested {"cook_plan": {...}} + res_json = response.json() + plan = res_json.get("cook_plan", res_json) + c_time = plan.get("cook_time_seconds") c_power = plan.get("effective_power_watts") c_temp = plan.get("target_temp") if c_time is None or c_power is None or c_temp is None: - print(f"[{microwave_id}] ❌ Invalid plan received: {response.json()}") + print(f"[{microwave_id}] ❌ Invalid plan received: {res_json}") microwave_states[microwave_id] = MicrowaveState.DONE return print(f"[{microwave_id}] Cloud Plan Received! Starting microwave: {c_time}s @ {c_power}W") cloud_alert = False # Reset alert flag on success microwave_states[microwave_id] = MicrowaveState.COOKING + if config.DEBUG: c_time = 20 # Set to 20s for debug c_temp = 50 # Set to 50°C for debug + mqtt_client.publish( config.MQTT_TOPIC_COOKING, payloads.mqtt_cooking_config(microwave_id, c_time, c_power, c_temp), @@ -320,7 +336,7 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data): if attempt < max_retries: print(f"[{microwave_id}] Retrying in {retry_delay_seconds} seconds...") - # Interruptible wait loop in case user removes the dish mid-wait + # Interruptible wait loop in case state changes mid-wait for _ in range(retry_delay_seconds): if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD: print(f"[{microwave_id}] State changed during retry wait. Aborting retries.") @@ -328,7 +344,7 @@ async def request_cloud_cooking_plan(microwave_id, sensors_data): await asyncio.sleep(1) # Executed only if all 3 retries failed - print(f"[{microwave_id}] All 3 cloud retries failed. Setting global alert flag.") + print(f"[{microwave_id}] All cloud retries failed. Setting global alert flag.") cloud_alert = True microwave_states[microwave_id] = MicrowaveState.DONE