Skip to content

Commit

Permalink
Fixing code hiearchy and making sure everything runs
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenwizard committed May 9, 2023
1 parent c072aa6 commit aaa896b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
15 changes: 2 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/##//'
11 changes: 8 additions & 3 deletions src/main.py → main.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions src/config/generic_gundam.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "gunpla",
"leds": [
{"name": "head", "pin": 0, "color": "green"},
{"name": "head_camera", "pin": 1, "color": "green"},
Expand Down
1 change: 1 addition & 0 deletions src/config/nu_gundam.json
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
12 changes: 6 additions & 6 deletions src/gunpla/BaseGundam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from src.pi.LED import LED
from src.phew.server import logging
from src.phew import server
import json

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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']:
Expand All @@ -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']:
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/gunpla/GenericGundam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/gunpla/nu_gundam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from gunpla.BaseGundam import BaseGundam
from src.gunpla.BaseGundam import BaseGundam
from src.phew.server import Response, Request


Expand All @@ -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:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/pi/DisabledLED.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 22 additions & 17 deletions src/webserver.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
"""
Expand All @@ -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)

Expand All @@ -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()
3 changes: 1 addition & 2 deletions src/www/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!DOCTYPE html>
<html>
<!-- todo: make title configurable -->
<title>RX-93 Nu Gundam</title>
<title>{{title}}</title>
<body>

<p>Control individual leds</p>
Expand Down

0 comments on commit aaa896b

Please sign in to comment.