diff --git a/src/main.py b/src/main.py index 1064bc1..77805ad 100644 --- a/src/main.py +++ b/src/main.py @@ -1,14 +1,65 @@ import asyncio from settings import Settings -from web import web 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") - w = web(settings) - server = asyncio.create_task(w.start_server(host="0.0.0.0", port=80)) + + 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/") + 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() @@ -19,5 +70,5 @@ async def main(): wdt.feed() await asyncio.sleep_ms(500) # cleanup before ending the application - await server + asyncio.run(main())