Send microwave states to lora
This commit is contained in:
@@ -18,6 +18,7 @@ from shared.sensors import RGBLED
|
||||
from shared.logging import log
|
||||
from shared.lora_device import LoraCommands
|
||||
from shared.alerts import Alert, AlertType, AlertManager
|
||||
from shared.microwave_state import MicrowaveState
|
||||
|
||||
# --- READ DEVICE ID ---
|
||||
try:
|
||||
@@ -29,6 +30,7 @@ except Exception:
|
||||
# --- GLOBAL VARIABLES ---
|
||||
cooking_state = None
|
||||
cooking_start_time = None # Track start timestamp
|
||||
microwave_state: str = None
|
||||
data_queue = SafeQueue()
|
||||
lora = None
|
||||
uart_device = None
|
||||
@@ -196,12 +198,11 @@ def cooking_state_on_state_change(state):
|
||||
paused=state.paused
|
||||
)
|
||||
|
||||
microwave_screen.update(cooking_state, defrost_mode)
|
||||
update_screen()
|
||||
|
||||
def cooking_state_on_refresh(state):
|
||||
# Update OLED display
|
||||
if microwave_screen:
|
||||
microwave_screen.update(state, defrost_mode)
|
||||
update_screen()
|
||||
|
||||
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.
|
||||
@@ -215,6 +216,13 @@ def cooking_state_on_pause(state):
|
||||
estimated_remaining_time=state.get_remaining_time()
|
||||
)
|
||||
|
||||
# --- SCREEN UPDATE ---
|
||||
def update_screen():
|
||||
global microwave_screen, cooking_state, defrost_mode, microwave_state
|
||||
|
||||
if microwave_screen:
|
||||
microwave_screen.update(cooking_state, defrost_mode, microwave_state)
|
||||
|
||||
|
||||
# --- ASYNC TASKS ---
|
||||
|
||||
@@ -266,7 +274,7 @@ async def uart_polling_task():
|
||||
|
||||
async def lora_process_task():
|
||||
"""Consumes packets pushed to data_queue by the LoRa hardware thread."""
|
||||
global cooking_state, defrost_mode, microwave_screen
|
||||
global cooking_state, defrost_mode, microwave_screen, microwave_state
|
||||
|
||||
while True:
|
||||
while not data_queue.empty():
|
||||
@@ -293,12 +301,21 @@ async def lora_process_task():
|
||||
elif data["action"] == LoraCommands.TOGGLE_DEFROST:
|
||||
print("[LoRa Process] Toggling defrost mode via orchestrator command.")
|
||||
defrost_mode = data["defrost_state"]
|
||||
microwave_screen.update(cooking_state, defrost_mode)
|
||||
update_screen()
|
||||
elif data["action"] == LoraCommands.NEW_ALERT:
|
||||
alert = Alert(data.get("alert_type"), data.get("message", "Unsafe area"))
|
||||
alert.timestamp = data.get("timestamp", time.time())
|
||||
print(f"[LoRa Process] New alert received via orchestrator: {alert.to_dict()}")
|
||||
alert_manager.add_alert(alert)
|
||||
elif data["action"] == LoraCommands.MICROVAVE_STATE_UPDATE:
|
||||
new_state = data.get("new_microwave_state")
|
||||
if new_state:
|
||||
print(f"[LoRa Process] Microwave state update received: {new_state}")
|
||||
microwave_state = new_state
|
||||
update_screen()
|
||||
else:
|
||||
print("[LoRa Process] Received microwave state update with no state specified.")
|
||||
|
||||
|
||||
await asyncio.sleep_ms(100)
|
||||
|
||||
@@ -358,8 +375,7 @@ async def main():
|
||||
|
||||
print("[Main] All async tasks running concurrently!")
|
||||
|
||||
microwave_screen.update(None, defrost_mode)
|
||||
|
||||
update_screen()
|
||||
# Keep main task alive indefinitely
|
||||
while True:
|
||||
await asyncio.sleep(3600)
|
||||
|
||||
Reference in New Issue
Block a user