39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { TOES_URL } from '$apps'
|
|
import { Hype } from '@because/hype'
|
|
import { connectToWifi, getWifiStatus, isSetupMode, onSetupModeChange, scanNetworks } from '../wifi'
|
|
|
|
const router = Hype.router()
|
|
|
|
// GET /api/wifi/status - current WiFi state + setup mode flag
|
|
router.get('/status', async c => {
|
|
const status = await getWifiStatus()
|
|
return c.json({ ...status, setupMode: isSetupMode(), url: TOES_URL })
|
|
})
|
|
|
|
// GET /api/wifi/scan - list available networks
|
|
router.get('/scan', async c => {
|
|
const networks = await scanNetworks()
|
|
return c.json(networks)
|
|
})
|
|
|
|
// POST /api/wifi/connect - submit WiFi credentials
|
|
router.post('/connect', async c => {
|
|
const { ssid, password } = await c.req.json<{ ssid: string, password?: string }>()
|
|
|
|
if (!ssid) {
|
|
return c.json({ ok: false, error: 'SSID is required' }, 400)
|
|
}
|
|
|
|
const result = await connectToWifi(ssid, password)
|
|
return c.json(result)
|
|
})
|
|
|
|
// SSE stream for setup mode changes
|
|
router.sse('/stream', (send, c) => {
|
|
send({ setupMode: isSetupMode() })
|
|
const unsub = onSetupModeChange(setupMode => send({ setupMode }))
|
|
return () => unsub()
|
|
})
|
|
|
|
export default router
|