diff --git a/.vscode/settings.json b/.vscode/settings.json index d03f082..9fea9f5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,6 @@ "python.analysis.extraPaths": [ ".vscode/Pico-W-Stub/stubs" ], - "picowgo.syncFolder": "", + "picowgo.syncFolder": "firmware", "picowgo.openOnStart": true } \ No newline at end of file diff --git a/firmware/lib/webpage.py b/firmware/lib/webpage.py index 4e55d41..f717874 100644 --- a/firmware/lib/webpage.py +++ b/firmware/lib/webpage.py @@ -19,9 +19,8 @@ def default_route(*args, **kwargs): render = "" for option in options: for key, value in option.items(): - render += f'''
-

''' - return template.render_template(load_webpage("lib/webpages/default.html"), {"render": render}) + render += f'''

''' + return template.render_template(load_webpage("webpages/default.html"), {"render": render}) def config_route(*args, **kwargs): diff --git a/firmware/lib/webrouter.py b/firmware/lib/webrouter.py index b44ee08..66d8bdd 100644 --- a/firmware/lib/webrouter.py +++ b/firmware/lib/webrouter.py @@ -1,11 +1,15 @@ from request import Request import socket +import uos as os class WebRouter: - def __init__(self, ip, port, default): + def __init__(self, ip, port, default, static_location="static"): self.routes = {"default": default} self.socket = self.open_socket(ip, port) + self.static_location = static_location + self.static_files = [] + self.add_static() def route(self, path): def router(function): @@ -16,6 +20,12 @@ def wrapper(*args, **kwargs): return router + def add_static(self): + static_files = os.listdir(self.static_location) + if static_files: + for file in static_files: + self.static_files.append(f"/{file}") + @staticmethod def open_socket(ip, port): address = (ip, port) @@ -24,15 +34,30 @@ def open_socket(ip, port): new_socket.listen(1) return new_socket + @staticmethod + def determine_mimetype(path): + mimetype = "text/html" + if path.endswith('.js'): + mimetype = "text/javascript" + if path.endswith('.css'): + mimetype = "text/css" + + def serve(self): while True: client = self.socket.accept()[0] request = client.recv(1024) request = Request(request.decode("utf-8")) path = request.path + header = 'HTTP/1.1 200 OK\n' + mimetype = self.determine_mimetype(path) if path in self.routes.keys(): webpage = self.routes[path](request=request) + elif path in self.static_files: + with open(f"{self.static_location}/{path}", "rb") as static: + webpage = static.read() else: webpage = self.routes["default"](request=request) + header += f"Content-Type: {mimetype}\n\n" client.send(webpage) client.close() diff --git a/firmware/main.py b/firmware/main.py index 87df45a..e5ef558 100644 --- a/firmware/main.py +++ b/firmware/main.py @@ -85,6 +85,6 @@ def ensure_connection(): print("No matching wifi, falling back to webpage based setup.") ap = pico_connection.access_point_wifi_setup() ip = ap.ifconfig()[0] - app = WebRouter(ip, 80, default_route) + app = WebRouter(ip, 80, default_route, "webpages/static") app.route("/config")(config_route)() app.serve() diff --git a/firmware/lib/webpages/config.html b/firmware/webpages/config.html similarity index 100% rename from firmware/lib/webpages/config.html rename to firmware/webpages/config.html diff --git a/firmware/lib/webpages/default.html b/firmware/webpages/default.html similarity index 68% rename from firmware/lib/webpages/default.html rename to firmware/webpages/default.html index 49eb146..9e333b1 100644 --- a/firmware/lib/webpages/default.html +++ b/firmware/webpages/default.html @@ -2,8 +2,11 @@ BabyScout + + +

BabyScout

{{ render }} diff --git a/firmware/webpages/static/babyscout.css b/firmware/webpages/static/babyscout.css new file mode 100644 index 0000000..e7ad9da --- /dev/null +++ b/firmware/webpages/static/babyscout.css @@ -0,0 +1,22 @@ +body { + font-family: system-ui; + background: #242A2C; + color: #0E7487; + text-align: center; + } + + form { + width: 300px; + background: #2C454A; + border: 15px solid #2C454A; + padding: 50px; + margin: auto; + border-radius: 12px; + } + + input { + padding:10px; + border:0; + box-shadow:0 0 15px 4px rgba(0,0,0,0.06); + border-radius: 8px; + } \ No newline at end of file diff --git a/firmware/webpages/static/babyscout.js b/firmware/webpages/static/babyscout.js new file mode 100644 index 0000000..d213c7b --- /dev/null +++ b/firmware/webpages/static/babyscout.js @@ -0,0 +1 @@ +document.getElementsByTagName("h1")[0].style.fontSize = "3vw"; \ No newline at end of file