also let server serve www directory for frontend

This commit is contained in:
2025-04-20 01:14:36 +02:00
parent 249e0e23f3
commit 4432049d9a
2 changed files with 22 additions and 16 deletions

3
.gitignore vendored
View File

@@ -1,6 +1,7 @@
.DS_Store .DS_Store
frontend/nodes_modules frontend/.env
frontend/node_modules/
frontend/dist frontend/dist
server/lights.db server/lights.db

View File

@@ -3,15 +3,19 @@ import argparse
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from light import Tower from light import Tower
import database import database
from flask import Flask, jsonify, request from flask import Flask, jsonify, request, send_from_directory
import os
# Pins configuration (order: green, orange, red) # Pins configuration (order: green, orange, red)
GPIO_PINS = [{'name': 'green', 'pin': 22}, GPIO_PINS = [{'name': 'green', 'pin': 22},
{'name': 'orange', 'pin': 17}, {'name': 'orange', 'pin': 17},
{'name': 'red', 'pin': 27}] {'name': 'red', 'pin': 27}]
# Create a Flask app # Path to static files directory
app = Flask(__name__) STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'www')
# Create a Flask app and set static_folder
app = Flask(__name__, static_folder=STATIC_DIR, static_url_path='')
# Initialize the database # Initialize the database
database.ensure_database() database.ensure_database()
@@ -32,39 +36,40 @@ def add_cors_headers(response):
def set_state(): def set_state():
data = request.data.decode('utf-8') data = request.data.decode('utf-8')
state = [int(n.strip()) for n in data.split(',') if n.strip().isdigit()] state = [int(n.strip()) for n in data.split(',') if n.strip().isdigit()]
"""Endpoint to get the current state of all lights."""
tower.green.on() if state[0] else tower.green.off() tower.green.on() if state[0] else tower.green.off()
tower.orange.on() if state[1] else tower.orange.off() tower.orange.on() if state[1] else tower.orange.off()
tower.red.on() if state[2] else tower.red.off() tower.red.on() if state[2] else tower.red.off()
return jsonify(state) return jsonify(state)
@app.route('/api/state', methods=['GET']) @app.route('/api/state', methods=['GET'])
def get_state(): def get_state():
"""Endpoint to get the current state of all lights."""
state = database.get_all_states() state = database.get_all_states()
return jsonify(state) return jsonify(state)
@app.route('/api/toggle/<color>', methods=['POST']) @app.route('/api/toggle/<color>', methods=['POST'])
def toggle_light(color): def toggle_light(color):
"""Endpoint to toggle the state of a specified light."""
if color not in ['green', 'orange', 'red']: if color not in ['green', 'orange', 'red']:
return jsonify({'error': 'Invalid color'}), 404 return jsonify({'error': 'Invalid color'}), 404
# Get the light object based on the color
light = getattr(tower, color) light = getattr(tower, color)
# Toggle light state (on/off)
new_state = GPIO.LOW if light.state == GPIO.HIGH else GPIO.HIGH new_state = GPIO.LOW if light.state == GPIO.HIGH else GPIO.HIGH
light._set(new_state) # Update the light state in the system light._set(new_state)
return jsonify({color: new_state}) return jsonify({color: new_state})
# Serve static files from www/
@app.route('/')
def serve_index():
return send_from_directory(STATIC_DIR, 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory(STATIC_DIR, path)
def run(tower): def run(tower):
"""Run the tower with manual light control."""
try: try:
time.sleep(0.5) time.sleep(0.5)
while True: while True:
@@ -72,7 +77,6 @@ def run(tower):
tower.green.on() tower.green.on()
tower.red.off() tower.red.off()
tower.orange.off() tower.orange.off()
time.sleep(10) time.sleep(10)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nInterrupted by user. Cleaning up...") print("\nInterrupted by user. Cleaning up...")
@@ -98,3 +102,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()