-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from MikeSchapp/wifi-configuration
feat(add wifi confiuration) enable computerless setup for BabyScout on the go
- Loading branch information
Showing
17 changed files
with
387 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{'info': 'This file is just used to identify a project folder.'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/home/mschappacher/.config/Code/User/Pico-W-Stub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-python.python", | ||
"visualstudioexptteam.vscodeintellicode", | ||
"ms-python.vscode-pylance" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"python.linting.enabled": true, | ||
"python.analysis.typeshedPaths": [ | ||
".vscode/Pico-W-Stub" | ||
], | ||
"python.languageServer": "Pylance", | ||
"python.analysis.typeCheckingMode": "basic", | ||
"python.analysis.extraPaths": [ | ||
".vscode/Pico-W-Stub/stubs" | ||
], | ||
"picowgo.syncFolder": "firmware", | ||
"picowgo.openOnStart": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Request: | ||
def __init__(self, request): | ||
self.raw = request | ||
self.method = None | ||
self.path = None | ||
self.query_strings = None | ||
self.protocol = None | ||
self.headers = None | ||
self.body = None | ||
self.parse_request() | ||
|
||
def parse_request(self): | ||
print(self.raw) | ||
try: | ||
request, self.body = self.raw.split("\r\n\r\n") | ||
except ValueError: | ||
request = self.raw | ||
split_request = request.split("\r\n") | ||
request_line = split_request.pop(0) | ||
self.method, self.path, self.protocol = request_line.split(" ") | ||
if "?" in self.path: | ||
query_strings = {} | ||
self.path, raw_query = self.path.split("?") | ||
raw_query = raw_query.split("&") | ||
for query in raw_query: | ||
key, value = query.split("=") | ||
query_strings[key] = value | ||
self.query_strings = query_strings | ||
|
||
headers = {} | ||
for header in split_request: | ||
key, value = header.split(": ") | ||
headers[key] = value | ||
self.headers = headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def render_template(loaded_html, variables): | ||
modified_html = loaded_html | ||
for key, value in variables.items(): | ||
modified_html = modified_html.replace("{{ " + key + " }}", value) | ||
return modified_html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import uos as os | ||
import ujson as json | ||
import machine | ||
import template | ||
|
||
|
||
def load_webpage(location): | ||
with open(location, "r") as website: | ||
webpage = website.read() | ||
return webpage | ||
|
||
|
||
def default_route(*args, **kwargs): | ||
options = [ | ||
{"ssid": "SSID:"}, | ||
{"password": "Password:"}, | ||
{"babybuddy": "BabyBuddy URL:"}, | ||
{"babyauth": "BabyBuddy API Key"}] | ||
render = "" | ||
for option in options: | ||
for key, value in option.items(): | ||
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): | ||
request = kwargs.get("request") | ||
secret_json = {} | ||
if request.query_strings: | ||
if "secrets.json" in os.listdir(): | ||
with open("secrets.json", "r+") as secret: | ||
secret_json = json.loads(secret.read()) | ||
with open("secrets.json", "w") as secret: | ||
secret_json["SSIDS_PASSWORD"][ | ||
request.query_strings.get("ssid", "") | ||
] = request.query_strings.get("password", "") | ||
if request.query_strings.get("babybuddy"): | ||
secret_json["BASE_URL"] = request.query_strings.get("babybuddy") | ||
if request.query_strings.get("babyauth"): | ||
secret_json["AUTHORIZATION"]["Authorization"] = request.query_strings.get("babyauth") | ||
secret.write(json.dumps(secret_json)) | ||
machine.reset() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from request import Request | ||
import socket | ||
import uos as os | ||
|
||
|
||
class WebRouter: | ||
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): | ||
def wrapper(*args, **kwargs): | ||
self.routes[path] = function | ||
|
||
return wrapper | ||
|
||
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) | ||
new_socket = socket.socket() | ||
new_socket.bind((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() |
Oops, something went wrong.