From aaa896bd33b04ecdb22acb747b0c18283531242f Mon Sep 17 00:00:00 2001 From: frozenwizard <172203+frozenwizard@users.noreply.github.com> Date: Mon, 8 May 2023 22:46:14 -0500 Subject: [PATCH] Fixing code hiearchy and making sure everything runs --- Makefile | 15 ++----------- src/main.py => main.py | 11 +++++++--- src/config/generic_gundam.json | 1 + src/config/nu_gundam.json | 1 + src/gunpla/BaseGundam.py | 12 +++++------ src/gunpla/GenericGundam.py | 2 +- src/gunpla/nu_gundam.py | 4 ++-- src/pi/DisabledLED.py | 2 +- src/webserver.py | 39 +++++++++++++++++++--------------- src/www/index.html | 3 +-- 10 files changed, 45 insertions(+), 45 deletions(-) rename src/main.py => main.py (52%) diff --git a/Makefile b/Makefile index 40e0922..0e60abe 100644 --- a/Makefile +++ b/Makefile @@ -33,18 +33,8 @@ build-test: ## Builds a test script to sanity check deployments build: ## Builds the server and Gunpla rm -rf target/ mkdir target/ -# mkdir target/config -# mkdir target/www -# cp -r src/phew/ target/phew -# cp src/settings.py target/ -# cp -r src/www/ target/www/ -# cp src/nu_gundam.py target/ -# cp src/LED.py target/ -# cp src/BaseGundam.py target/ - cp -r src/ target/ - #cp src/config/nu_gundam.json target/config/nu_gundam.json - #cp src/webserver.py target/main.py - #mv target/webserver.py target/main.py + cp main.py target/ + cp -r src/ target/src/ .PHONY: deploy deploy: ## Deploys the built artifacts to the pi board @@ -59,7 +49,6 @@ format: ## Format the Python code .PHONY: lint lint: ## Lints the python code pylint src/ - help: ## Show this help. @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//' diff --git a/src/main.py b/main.py similarity index 52% rename from src/main.py rename to main.py index 3ba91af..87dd34c 100644 --- a/src/main.py +++ b/main.py @@ -1,6 +1,11 @@ -from webserver import WebServer -import settings +from src import settings +from src.webserver import WebServer -if __name__ == "__main__": + +def main(): webserver = WebServer(settings.webserver) webserver.main() + + +if __name__ == "__main__": + main() diff --git a/src/config/generic_gundam.json b/src/config/generic_gundam.json index da2a207..43ec352 100644 --- a/src/config/generic_gundam.json +++ b/src/config/generic_gundam.json @@ -1,4 +1,5 @@ { + "name": "gunpla", "leds": [ {"name": "head", "pin": 0, "color": "green"}, {"name": "head_camera", "pin": 1, "color": "green"}, diff --git a/src/config/nu_gundam.json b/src/config/nu_gundam.json index e7b34c2..ceb3193 100644 --- a/src/config/nu_gundam.json +++ b/src/config/nu_gundam.json @@ -1,4 +1,5 @@ { + "name": "RX-93 Nu Gundam", "leds": [ {"name": "head", "pin": 15, "color": "green"}, {"name": "fin_funnel_1", "pin": 1, "color": "white", "disabled": false}, diff --git a/src/gunpla/BaseGundam.py b/src/gunpla/BaseGundam.py index a899c6c..afba778 100644 --- a/src/gunpla/BaseGundam.py +++ b/src/gunpla/BaseGundam.py @@ -1,5 +1,5 @@ - from src.pi.LED import LED +from src.phew.server import logging from src.phew import server import json @@ -38,7 +38,7 @@ def led_on(self, request: Request, led_name: str) -> Response: """ Turns a Single LED on by name """ - src.phew.server.logging.info(f"turning on {led_name}") + logging.info(f"turning on {led_name}") try: led = self._get_led_from_name(led_name) led.on() @@ -50,7 +50,7 @@ def led_off(self, request: Request, led_name: str) -> Response: """ Turns a single LED off by name """ - src.phew.server.logging.info(f"turning off {led_name}") + logging.info(f"turning off {led_name}") try: led = self._get_led_from_name(led_name) led.off() @@ -62,7 +62,7 @@ def all_on(self, request: Request) -> Response: """ Turns all configured LED's on. """ - src.phew.server.logging.info("turning on all leds") + logging.info("turning on all leds") try: leds: str = "" for led_entry in self.config['leds']: @@ -81,7 +81,7 @@ def all_off(self, request: Request) -> Response: """ Turns all configured LED's off """ - src.phew.server.logging.info("turning off all leds") + logging.info("turning off all leds") try: leds: str = "" for led_entry in self.config['leds']: @@ -105,7 +105,7 @@ def _get_led_from_name(self, led_name: str) -> LED: """ entry = self.__get_entry_from_name(led_name) if 'disabled' in entry and entry['disabled']: - src.phew.server.logging.debug(f"{led_name} is disabled") + logging.debug(f"{led_name} is disabled") return DisabledLED(led_name) return LED(entry['pin'], led_name) diff --git a/src/gunpla/GenericGundam.py b/src/gunpla/GenericGundam.py index fed4d6e..2d1cbb1 100644 --- a/src/gunpla/GenericGundam.py +++ b/src/gunpla/GenericGundam.py @@ -10,4 +10,4 @@ def get_config_file(self) -> str: """ :return: Generic Gundam's config file """ - return "config/generic_gundam.json" + return "src/config/generic_gundam.json" diff --git a/src/gunpla/nu_gundam.py b/src/gunpla/nu_gundam.py index a024d7d..e0a506d 100644 --- a/src/gunpla/nu_gundam.py +++ b/src/gunpla/nu_gundam.py @@ -1,5 +1,5 @@ import time -from gunpla.BaseGundam import BaseGundam +from src.gunpla.BaseGundam import BaseGundam from src.phew.server import Response, Request @@ -12,7 +12,7 @@ def get_config_file(self) -> str: """ :return: The Nu Gundam config file """ - return "config/nu_gundam.json" + return "src/config/nu_gundam.json" def activation(self, request: Request) -> Response: """ diff --git a/src/pi/DisabledLED.py b/src/pi/DisabledLED.py index 8e75d07..e4f2e17 100644 --- a/src/pi/DisabledLED.py +++ b/src/pi/DisabledLED.py @@ -1,7 +1,7 @@ from src.pi.LED import LED -class disabled_led(LED): +class DisabledLED(LED): def __init__(self, led_name: str): self.led_name = led_name diff --git a/src/webserver.py b/src/webserver.py index f583619..9ce88b0 100644 --- a/src/webserver.py +++ b/src/webserver.py @@ -1,9 +1,10 @@ +import sys from machine import Pin import time import network -import settings - +from src import settings +from src.phew.server import logging from src.gunpla.GenericGundam import GenericGundam from src.phew import server, connect_to_wifi from src.phew.server import Request, Response @@ -14,21 +15,18 @@ class WebServer: """ Webserver that manages API routes and web pages for the Gunpla """ + def __init__(self, configuration: dict): self.settings: dict = configuration self.gundam: GenericGundam = settings.webserver['model'] self.board_led: Pin = Pin("LED", Pin.OUT) - @server.route("/index", methods=["GET"]) def index(self, request: Request) -> Response: - return await render_template("www/index.html", all_buttons=self.gundam.config['leds']) - - @server.route("/", methods=["GET"]) - def root(self, request: Request) -> Response: - return self.index(request) + return await render_template("src/www/index.html", + title=self.gundam.config['name'], + all_buttons=self.gundam.config['leds']) - @server.route("/canary", methods=["GET"]) - def sanity(self, request: Request) -> Response: + def canary(self, request: Request) -> Response: """ Sanity check to make sure webserver is running. """ @@ -41,7 +39,6 @@ def sanity(self, request: Request) -> Response: self.board_led.off() return Response("chirp", 200) - @server.catchall() def catchall(self, request: Request): return Response("Not found", 404) @@ -58,14 +55,22 @@ def blink(self) -> None: self.board_led.off() def main(self): - network.hostname(self.configuration['hostname']) - src.phew.server.logging.info(f"Set hostname to {network.hostname()}") - src.phew.server.logging.info(f"Connect to {self.configuration['ssid']} with {self.configuration['password']}") - ipaddress: str = connect_to_wifi(self.configuration['ssid'], self.configuration['password']) + network.hostname(self.settings['hostname']) + logging.info(f"Set hostname to {network.hostname()}") + logging.info(f"Connect to {self.settings['ssid']} with {self.settings['password']}") + ipaddress: str = connect_to_wifi(self.settings['ssid'], self.settings['password']) if ipaddress: - src.phew.server.logging.info(f"Server started on {ipaddress}") + logging.info(f"Server started on {ipaddress}") self.blink() else: - src.phew.server.logging.error("Server failed to connect") + logging.error("Server failed to connect") + sys.exit("Cannot start server") + + server.add_route("/", self.index, methods=["GET"]) + server.add_route("/index", self.index, methods=["GET"]) + server.add_route("/canary", self.canary, methods=["GET"]) + server.set_callback(self.catchall) + self.gundam.add_routes(server) + server.run() diff --git a/src/www/index.html b/src/www/index.html index 54843d3..5169ce3 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -1,7 +1,6 @@ - -RX-93 Nu Gundam +{{title}}

Control individual leds