Files
led-controller/src/main.py
Jimmy 5cca60d830 Update main.py with controllers and static route
- Mount model controllers as subroutes
- Add static file serving route
- Integrate controllers into main application
2026-01-11 21:34:16 +13:00

75 lines
1.8 KiB
Python

import asyncio
from settings import Settings
import gc
import machine
from microdot import Microdot, send_file
from microdot.websocket import with_websocket
import aioespnow
import network
from controllers.preset import preset
import controllers.profile as profile
import controllers.group as group
import controllers.sequence as sequence
import controllers.tab as tab
import controllers.palette as palette
async def main():
settings = Settings()
print("Starting")
network.WLAN(network.STA_IF).active(True)
e = aioespnow.AIOESPNow()
e.active(True)
e.add_peer(b"\xbb\xbb\xbb\xbb\xbb\xbb")
app = Microdot()
# Mount model controllers as subroutes
app.mount('/presets', preset.controller)
app.mount('/profiles', profile.controller)
app.mount('/groups', group.controller)
app.mount('/sequences', sequence.controller)
app.mount('/tabs', tab.controller)
app.mount('/palettes', palette.controller)
# Static file route
@app.route("/static/<path:path>")
def static_handler(request, path):
"""Serve static files."""
if '..' in path:
# Directory traversal is not allowed
return 'Not found', 404
return send_file('static/' + path)
@app.route('/ws')
@with_websocket
async def ws(request, ws):
while True:
data = await ws.receive()
if data:
await e.asend(b"\xbb\xbb\xbb\xbb\xbb\xbb", data)
print(data)
else:
break
server = asyncio.create_task(app.start_server(host="0.0.0.0", port=80))
wdt = machine.WDT(timeout=10000)
wdt.feed()
while True:
gc.collect()
for i in range(60):
wdt.feed()
await asyncio.sleep_ms(500)
# cleanup before ending the application
asyncio.run(main())