commit a9a4709a074312b899d6c115e1eafa651a57e00b Author: Kalzu Rekku Date: Tue Feb 25 17:51:06 2025 +0200 First test app thing. diff --git a/frontend.js b/frontend.js new file mode 100644 index 0000000..4035fba --- /dev/null +++ b/frontend.js @@ -0,0 +1,20 @@ +function updateLights(state) { + document.getElementById("red").classList.toggle("red", state === "red"); + document.getElementById("green").classList.toggle("green", state === "green"); +} + +function fetchStatus() { + fetch("/status") + .then(response => response.json()) + .then(data => updateLights(data.state)) + .catch(console.error); +} + +function toggleLight() { + fetch("/toggle", { method: "POST" }) + .then(response => response.json()) + .then(data => updateLights(data.state)) + .catch(console.error); +} + +setInterval(fetchStatus, 1000); diff --git a/main.py b/main.py new file mode 100644 index 0000000..bec45cb --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +from flask import Flask, render_template, jsonify +import threading +import time + +app = Flask(__name__) +current_state = "red" +lock = threading.Lock() + +def reset_to_red(): + global current_state + time.sleep(10) + with lock: + current_state = "red" + +@app.route("/") +def index(): + return render_template("site_template.j2", state=current_state) + +@app.route("/status") +def status(): + return jsonify({"state": current_state}) + +@app.route("/toggle", methods=["POST"]) +def toggle(): + global current_state + with lock: + if current_state != "green": + current_state = "green" + threading.Thread(target=reset_to_red, daemon=True).start() + return jsonify({"state": current_state}) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000) diff --git a/site_template.j2 b/site_template.j2 new file mode 100644 index 0000000..6f38abf --- /dev/null +++ b/site_template.j2 @@ -0,0 +1,27 @@ + + + + + + Traffic Light + + + + +

Traffic Light

+
+
+
+
+
+
+ + +