Bigger timeout to get the cooking plan
This commit is contained in:
+25
-9
@@ -264,7 +264,7 @@ async def handle_new_dish(microwave_id, detected_height):
|
|||||||
ir_data_events.pop(microwave_id, None)
|
ir_data_events.pop(microwave_id, None)
|
||||||
|
|
||||||
async def request_cloud_cooking_plan(microwave_id, sensors_data):
|
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
|
global cloud_alert
|
||||||
microwave_states[microwave_id] = MicrowaveState.WAITING_FOR_CLOUD
|
microwave_states[microwave_id] = MicrowaveState.WAITING_FOR_CLOUD
|
||||||
URL = "https://smartwave.matthiasg.dev/cooking-params"
|
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...")
|
print(f"[{microwave_id}] Requesting cooking plan from cloud app...")
|
||||||
|
|
||||||
max_retries = 3
|
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):
|
for attempt in range(1, max_retries + 1):
|
||||||
if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD:
|
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
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print(f"[{microwave_id}] Connection attempt {attempt}/{max_retries}...")
|
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=30)
|
|
||||||
# Abort if state changed (e.g. user removed dish while waiting for wifi)
|
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:
|
if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD:
|
||||||
print(f"[{microwave_id}] Dish removed during API request. Discarding API plan.")
|
print(f"[{microwave_id}] Dish removed during API request. Discarding API plan.")
|
||||||
return
|
return
|
||||||
|
|
||||||
response.raise_for_status()
|
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_time = plan.get("cook_time_seconds")
|
||||||
c_power = plan.get("effective_power_watts")
|
c_power = plan.get("effective_power_watts")
|
||||||
c_temp = plan.get("target_temp")
|
c_temp = plan.get("target_temp")
|
||||||
|
|
||||||
if c_time is None or c_power is None or c_temp is None:
|
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
|
microwave_states[microwave_id] = MicrowaveState.DONE
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"[{microwave_id}] Cloud Plan Received! Starting microwave: {c_time}s @ {c_power}W")
|
print(f"[{microwave_id}] Cloud Plan Received! Starting microwave: {c_time}s @ {c_power}W")
|
||||||
cloud_alert = False # Reset alert flag on success
|
cloud_alert = False # Reset alert flag on success
|
||||||
microwave_states[microwave_id] = MicrowaveState.COOKING
|
microwave_states[microwave_id] = MicrowaveState.COOKING
|
||||||
|
|
||||||
if config.DEBUG:
|
if config.DEBUG:
|
||||||
c_time = 20 # Set to 20s for debug
|
c_time = 20 # Set to 20s for debug
|
||||||
c_temp = 50 # Set to 50°C for debug
|
c_temp = 50 # Set to 50°C for debug
|
||||||
|
|
||||||
mqtt_client.publish(
|
mqtt_client.publish(
|
||||||
config.MQTT_TOPIC_COOKING,
|
config.MQTT_TOPIC_COOKING,
|
||||||
payloads.mqtt_cooking_config(microwave_id, c_time, c_power, c_temp),
|
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:
|
if attempt < max_retries:
|
||||||
print(f"[{microwave_id}] Retrying in {retry_delay_seconds} seconds...")
|
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):
|
for _ in range(retry_delay_seconds):
|
||||||
if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD:
|
if microwave_states.get(microwave_id) != MicrowaveState.WAITING_FOR_CLOUD:
|
||||||
print(f"[{microwave_id}] State changed during retry wait. Aborting retries.")
|
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)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
# Executed only if all 3 retries failed
|
# 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
|
cloud_alert = True
|
||||||
microwave_states[microwave_id] = MicrowaveState.DONE
|
microwave_states[microwave_id] = MicrowaveState.DONE
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user