Skip to content

Commit

Permalink
Logic for static files, and finalized a basic setup page for BabyBuddy
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchapp committed Sep 26, 2022
1 parent d2ed3f1 commit 69265d2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"python.analysis.extraPaths": [
".vscode/Pico-W-Stub/stubs"
],
"picowgo.syncFolder": "",
"picowgo.syncFolder": "firmware",
"picowgo.openOnStart": true
}
5 changes: 2 additions & 3 deletions firmware/lib/webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def default_route(*args, **kwargs):
render = ""
for option in options:
for key, value in option.items():
render += f'''<label for="{key}">{value}</label><br>
<input type="text" id="{key}" name="{key}" value=""><br><br>'''
return template.render_template(load_webpage("lib/webpages/default.html"), {"render": render})
render += f''' <input type="text" id="{key}" name="{key}" placeholder="{value}"><br><br>'''
return template.render_template(load_webpage("webpages/default.html"), {"render": render})


def config_route(*args, **kwargs):
Expand Down
27 changes: 26 additions & 1 deletion firmware/lib/webrouter.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
Expand All @@ -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()
2 changes: 1 addition & 1 deletion firmware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<html>
<head>
<title>BabyScout</title>
<link rel="stylesheet" href="babyscout.css">
<script src="babyscout.js"></script>
</head>
<body>
<h1>BabyScout</h1>
<form action="/config">
{{ render }}
<input type="submit" value="Submit">
Expand Down
22 changes: 22 additions & 0 deletions firmware/webpages/static/babyscout.css
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions firmware/webpages/static/babyscout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.getElementsByTagName("h1")[0].style.fontSize = "3vw";

0 comments on commit 69265d2

Please sign in to comment.