""" Seed script to register the ESP32 display device in the database. Run this once to create the device entry that the ESP32 heartbeat authenticates against. """ import asyncio from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from app.models.display import DisplayDevice from app.core.security import get_password_hash engine = create_async_engine("sqlite+aiosqlite:///./icu_local.db", echo=False) AsyncSessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False) # ────────────────────────────────────────────── # Set your device credentials here. # The DEVICE_SECRET below is what the ESP32 # must send as "api_key" in its heartbeat JSON. # ────────────────────────────────────────────── DEVICE_ID = "ESP32_WAVESHARE_1" DEVICE_SECRET = "esp32_secret_key_2024" # <-- Put this in your ESP32 code as api_key DEVICE_NAME = "ICU Bedside Display 1" async def seed_device(): async with AsyncSessionLocal() as session: # Check if device already exists from sqlalchemy.future import select result = await session.execute( select(DisplayDevice).filter(DisplayDevice.device_id == DEVICE_ID) ) existing = result.scalars().first() if existing: print(f"Device '{DEVICE_ID}' already exists (id={existing.id}). Skipping.") return device = DisplayDevice( device_id=DEVICE_ID, device_secret=get_password_hash(DEVICE_SECRET), name=DEVICE_NAME, online_status=False, pending_update=False, ) session.add(device) await session.commit() print(f"✅ Device registered successfully!") print(f" Device ID: {DEVICE_ID}") print(f" API Key: {DEVICE_SECRET}") print(f" Name: {DEVICE_NAME}") print() print(f"Now update your ESP32 code with:") print(f' const char* device_id = "{DEVICE_ID}";') print(f' const char* api_key = "{DEVICE_SECRET}";') asyncio.run(seed_device())