Sending updates to external screen + RGB LCD
This commit is contained in:
@@ -28,6 +28,7 @@ except Exception:
|
||||
|
||||
# --- GLOBAL VARIABLES ---
|
||||
cooking_state = None
|
||||
cooking_start_time = None # Track start timestamp
|
||||
data_queue = SafeQueue()
|
||||
lora = None
|
||||
uart_device = None
|
||||
@@ -46,6 +47,31 @@ PING_PAYLOAD = {
|
||||
"type": deviceTypes.DEVICE_TYPES["MICROWAVE"]
|
||||
}
|
||||
|
||||
|
||||
def send_cooking_update(state=None, start_time=None, estimated_remaining_time=None, paused=None):
|
||||
"""Sends a flexible COOKING_UPDATE payload containing only populated fields."""
|
||||
if not lora:
|
||||
return
|
||||
|
||||
payload = {
|
||||
"id": DEVICE_ID,
|
||||
"action": LoraCommands.COOKING_UPDATE
|
||||
}
|
||||
|
||||
if state is not None:
|
||||
payload["cooking_state"] = state
|
||||
if estimated_remaining_time is not None:
|
||||
payload["estimated_remaining_time"] = estimated_remaining_time
|
||||
if paused is not None:
|
||||
payload["paused"] = paused
|
||||
|
||||
# Only transmit if at least one field beyond id and action was provided
|
||||
if len(payload) > 2:
|
||||
log(f"[LoRa] Sending COOKING_UPDATE payload: {payload}")
|
||||
|
||||
lora.send(payload)
|
||||
|
||||
|
||||
def on_new_alert(alert: Alert):
|
||||
global microwave_screen
|
||||
print(f"[Alert Manager] New alert received: {alert.to_dict()}")
|
||||
@@ -138,9 +164,7 @@ def cooking_state_temperature_provider():
|
||||
# 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
|
||||
|
||||
# Reset current_temp buffer to consume the value
|
||||
last_temp = [temps[0], temps[1]]
|
||||
current_temp = [None, None]
|
||||
return temps
|
||||
|
||||
@@ -152,6 +176,7 @@ def cooking_state_temperature_provider():
|
||||
return (0.0, 0.0)
|
||||
|
||||
def cooking_state_on_state_change(state):
|
||||
global cooking_start_time
|
||||
print(f"[CookingState] State changed to: {state.state}")
|
||||
|
||||
if state.paused or state.state == cookingState.CookingStates.DONE or state.state == cookingState.CookingStates.IDLE:
|
||||
@@ -162,8 +187,14 @@ def cooking_state_on_state_change(state):
|
||||
# Send state updates to WiFi board and Orchestrator
|
||||
if uart_device:
|
||||
uart_device.send_as_command(UARTCommand(UARTCommandType.COOKING_STATE_UPDATE, {"state": state.state}))
|
||||
if lora:
|
||||
lora.send_reliable({"id": DEVICE_ID, "new_cooking_state": state.state})
|
||||
|
||||
# Send LoRa update with start time and remaining time if beginning cooking process
|
||||
send_cooking_update(
|
||||
state=state.state,
|
||||
start_time=cooking_start_time if state.state == cookingState.CookingStates.COOKING else None,
|
||||
estimated_remaining_time=state.get_remaining_time() if hasattr(state, "get_remaining_time") else None,
|
||||
paused=state.paused
|
||||
)
|
||||
|
||||
microwave_screen.update(cooking_state, defrost_mode)
|
||||
|
||||
@@ -176,14 +207,20 @@ def cooking_state_on_pause(state):
|
||||
# If the cooking is unpaused and was in STIRRING_REQUIRED or ALERT state, we set the state back to COOKING.
|
||||
if not state.paused and (state.state == cookingState.CookingStates.STIRRING_REQUIRED or state.state == cookingState.CookingStates.ALERT):
|
||||
state.set_state(cookingState.CookingStates.COOKING)
|
||||
# TODO: send_reliable lora message to orchestrator about pause/resume state
|
||||
|
||||
# Send update on pause/resume toggle
|
||||
send_cooking_update(
|
||||
state=state.state,
|
||||
paused=state.paused,
|
||||
estimated_remaining_time=state.get_remaining_time()
|
||||
)
|
||||
|
||||
|
||||
# --- ASYNC TASKS ---
|
||||
|
||||
async def uart_polling_task():
|
||||
"""Polls UART for incoming messages from the WiFi board."""
|
||||
global cooking_state, temperature_asked
|
||||
global cooking_state, cooking_start_time, temperature_asked
|
||||
|
||||
while True:
|
||||
if uart_device and uart_device.any():
|
||||
@@ -196,6 +233,9 @@ async def uart_polling_task():
|
||||
|
||||
uart_device.send_as_command(UARTCommand(UARTCommandType.TEMPERATURE_REQUEST, {}))
|
||||
|
||||
# Track start time timestamp
|
||||
cooking_start_time = time.time()
|
||||
|
||||
cooking_state = cookingState.CookingState(
|
||||
cook_time=params["cook_time"],
|
||||
power_level=params["power_level"],
|
||||
@@ -271,10 +311,16 @@ async def cooking_loop_task():
|
||||
cooking_state.update_tick()
|
||||
log_counter += 1
|
||||
|
||||
# Print log output every 5 seconds (10 ticks x 500ms)
|
||||
# Print log output and send periodic updates every 5 seconds (10 ticks x 500ms)
|
||||
if log_counter % 10 == 0:
|
||||
print(f"[Cooking Task] State: {cooking_state.state}, Temp: {cooking_state.current_dish_temp}, "
|
||||
f"Paused: {cooking_state.paused}, Remaining: {cooking_state.get_remaining_time():.2f}s")
|
||||
|
||||
# Send periodic time remaining update while actively cooking
|
||||
if not cooking_state.paused and cooking_state.state == cookingState.CookingStates.COOKING:
|
||||
send_cooking_update(
|
||||
estimated_remaining_time=cooking_state.get_remaining_time()
|
||||
)
|
||||
|
||||
await asyncio.sleep_ms(500)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user