Better cooking_state_temperature_provider

This commit is contained in:
2026-08-07 16:50:14 +02:00
parent 053964e56c
commit 988cb7958c
+49 -35
View File
@@ -36,6 +36,7 @@ defrost_mode = False
current_temp = [None, None] current_temp = [None, None]
last_temp = [None, None] last_temp = [None, None]
temperature_asked = False temperature_asked = False
last_temp_request_time = 0
PING_PAYLOAD = { PING_PAYLOAD = {
@@ -88,7 +89,7 @@ def lora_hardware_thread():
# 1. Send periodic heartbeat # 1. Send periodic heartbeat
if now - last_heartbeat_time >= config.LORA_HEARTBEAT_INTERVAL: if now - last_heartbeat_time >= config.LORA_HEARTBEAT_INTERVAL:
last_heartbeat_time = now last_heartbeat_time = now
print("\n[LoRa Thread] Sending Heartbeat...") log("\n[LoRa Thread] Sending Heartbeat...")
if lora: if lora:
lora.send(PING_PAYLOAD) lora.send(PING_PAYLOAD)
@@ -104,36 +105,44 @@ def lora_hardware_thread():
# --- COOKING STATE CALLBACKS --- # --- COOKING STATE CALLBACKS ---
def cooking_state_temperature_provider(): def cooking_state_temperature_provider():
global current_temp, last_temp, temperature_asked, uart_device global current_temp, last_temp, temperature_asked, last_temp_request_time, uart_device
def temp_is_none(temp): def temp_is_invalid(temp):
return temp is None or temp[0] is None or temp[1] is None return (
temp is None
# Return the current temperature if available, or not isinstance(temp, (list, tuple))
# otherwise return the last recorded temperature or len(temp) < 2
# and request a new reading from the WiFi board. or temp[0] is None
if temp_is_none(current_temp): or temp[1] is None
if temp_is_none(last_temp): )
temps = (0.0, 0.0)
print("[CookingState] No temperature data available. Returning default (0.0, 0.0).") # 1. Check for UART request timeout (reset lock if 3 seconds pass without a response)
else: now = time.time()
temps = [last_temp[0], last_temp[1]] if temperature_asked and (now - last_temp_request_time > 3):
print(f"[CookingState] Returning last known temperature: {temps}") print("[CookingState] Temperature request timed out. Retrying UART request...")
temperature_asked = False
# 2. Trigger new UART request if idle
if not temperature_asked and uart_device:
temperature_asked = True
last_temp_request_time = now
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
# 3. Handle fresh incoming reading
if not temp_is_invalid(current_temp):
temps = [float(current_temp[0]), float(current_temp[1])]
last_temp = [temps[0], temps[1]] # Keep a safe reference copy
# Asks the esp-wifi the temperature over UART # Reset current_temp buffer to consume the value
if not temperature_asked: current_temp = [None, None]
temperature_asked = True return temps
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
print("[CookingState] Requested new temperature reading from WiFi board.") # 4. Fallback: Use last valid reading
else: if not temp_is_invalid(last_temp):
print(f"[CookingState] Returning current temperature: {current_temp}") return [float(last_temp[0]), float(last_temp[1])]
temps = [current_temp[0], current_temp[1]]
last_temp[0] = current_temp[0] # 5. Default fallback if no data has ever arrived
last_temp[1] = current_temp[1] return (0.0, 0.0)
current_temp[0] = None
current_temp[1] = None
return temps
def cooking_state_on_state_change(state): def cooking_state_on_state_change(state):
print(f"[CookingState] State changed to: {state.state}") print(f"[CookingState] State changed to: {state.state}")
@@ -173,7 +182,7 @@ async def uart_polling_task():
if uart_device and uart_device.any(): if uart_device and uart_device.any():
command = uart_device.read_as_command() command = uart_device.read_as_command()
if command: if command:
print(f"[UART Task] Received command from WiFi Board: {command.command_type}") log(f"[UART Task] Received command from WiFi Board: {command.command_type}")
if command.command_type == UARTCommandType.COOKING_PARAMS: if command.command_type == UARTCommandType.COOKING_PARAMS:
params = command.payload params = command.payload
print(f"[UART Task] Cooking parameters received: {params}") print(f"[UART Task] Cooking parameters received: {params}")
@@ -193,10 +202,15 @@ async def uart_polling_task():
await asyncio.sleep_ms(200) await asyncio.sleep_ms(200)
cooking_state_on_state_change(cooking_state) cooking_state_on_state_change(cooking_state)
elif command.command_type == UARTCommandType.TEMPERATURE_RESPONSE: elif command.command_type == UARTCommandType.TEMPERATURE_RESPONSE:
payload = ujson.loads(command.payload) try:
current_temp[0] = payload["dish_temp"] # Check if payload is already a dict or needs JSON decoding
current_temp[1] = payload["ambient_temp"] payload = ujson.loads(command.payload) if isinstance(command.payload, str) else command.payload
temperature_asked = False current_temp[0] = payload.get("dish_temp", 0.0)
current_temp[1] = payload.get("ambient_temp", 0.0)
except Exception as e:
print(f"[UART Task] Error parsing temperature payload: {e}")
finally:
temperature_asked = False
else: else:
print(f"[UART Task] Unknown command type received: {command.command_type}") print(f"[UART Task] Unknown command type received: {command.command_type}")