mirror of
https://github.com/KevinMidboe/patlite-tower-lights-controller.git
synced 2025-10-28 17:20:29 +00:00
also let server serve www directory for frontend
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
.DS_Store
|
||||
|
||||
frontend/nodes_modules
|
||||
frontend/.env
|
||||
frontend/node_modules/
|
||||
frontend/dist
|
||||
|
||||
server/lights.db
|
||||
|
||||
@@ -3,15 +3,19 @@ import argparse
|
||||
import RPi.GPIO as GPIO
|
||||
from light import Tower
|
||||
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)
|
||||
GPIO_PINS = [{'name': 'green', 'pin': 22},
|
||||
{'name': 'orange', 'pin': 17},
|
||||
{'name': 'red', 'pin': 27}]
|
||||
|
||||
# Create a Flask app
|
||||
app = Flask(__name__)
|
||||
# Path to static files directory
|
||||
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
|
||||
database.ensure_database()
|
||||
@@ -32,39 +36,40 @@ def add_cors_headers(response):
|
||||
def set_state():
|
||||
data = request.data.decode('utf-8')
|
||||
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.orange.on() if state[1] else tower.orange.off()
|
||||
tower.red.on() if state[2] else tower.red.off()
|
||||
|
||||
return jsonify(state)
|
||||
|
||||
|
||||
@app.route('/api/state', methods=['GET'])
|
||||
def get_state():
|
||||
"""Endpoint to get the current state of all lights."""
|
||||
state = database.get_all_states()
|
||||
return jsonify(state)
|
||||
|
||||
|
||||
@app.route('/api/toggle/<color>', methods=['POST'])
|
||||
def toggle_light(color):
|
||||
"""Endpoint to toggle the state of a specified light."""
|
||||
if color not in ['green', 'orange', 'red']:
|
||||
return jsonify({'error': 'Invalid color'}), 404
|
||||
|
||||
# Get the light object based on the color
|
||||
light = getattr(tower, color)
|
||||
|
||||
# Toggle light state (on/off)
|
||||
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})
|
||||
|
||||
|
||||
# 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):
|
||||
"""Run the tower with manual light control."""
|
||||
try:
|
||||
time.sleep(0.5)
|
||||
while True:
|
||||
@@ -72,7 +77,6 @@ def run(tower):
|
||||
tower.green.on()
|
||||
tower.red.off()
|
||||
tower.orange.off()
|
||||
|
||||
time.sleep(10)
|
||||
except KeyboardInterrupt:
|
||||
print("\nInterrupted by user. Cleaning up...")
|
||||
@@ -98,3 +102,4 @@ def main():
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user