Files
loustique-home/fastapi/main.py

106 lines
3.1 KiB
Python
Raw Normal View History

2026-04-01 00:23:24 +02:00
import os
import sys
from fastapi import FastAPI
2026-04-01 18:39:00 +02:00
from fastapi.middleware.cors import CORSMiddleware
2026-04-01 00:23:24 +02:00
import RPi.GPIO as GPIO
2026-04-01 18:39:00 +02:00
import uvicorn
2026-04-02 22:19:52 +02:00
import threading
2026-04-01 00:23:24 +02:00
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
composants = os.path.join(BASE_DIR, "composants", "byPanda")
sys.path.insert(0, composants)
from lumieres import SystemeLumieres
from thermostat import SystemeThermostat
#from volets import SystemeVolets
2026-04-01 18:39:00 +02:00
from etatsysteme import EtatSysteme
2026-04-01 00:23:24 +02:00
from septsegments import afficher_temperature
2026-04-02 22:19:52 +02:00
import bouton_servo
import bouton
import LDR
2026-04-01 00:23:24 +02:00
2026-04-01 00:58:00 +02:00
app = FastAPI(title="L'API des loustiques")
2026-04-01 22:23:25 +02:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
2026-04-01 00:23:24 +02:00
controleur_lumieres = SystemeLumieres()
controleur_thermostat = SystemeThermostat()
#controleur_volet = SystemeVolets()
etatSysteme = EtatSysteme()
@app.get("/up_led")
async def allumer_led():
try:
controleur_lumieres.allumerLumieres()
controleur_lumieres.modeManuel = True
etatSysteme.signalerOk()
return {"success": True, "message": "Lumière allumée par le Pi 2"}
except Exception as e:
etatSysteme.signalerProbleme()
return {"success": False, "message": str(e)}
@app.get("/down_led")
async def eteindre_led():
try:
controleur_lumieres.eteindreLumieres()
controleur_lumieres.modeManuel = True
etatSysteme.signalerOk()
return {"success": True, "message": "Lumière éteinte par le Pi 2"}
except Exception as e:
etatSysteme.signalerProbleme()
return {"success": False, "message": str(e)}
2026-04-03 18:35:33 +02:00
2026-04-01 00:23:24 +02:00
@app.get("/temperature")
async def read_temp():
try:
temp = controleur_thermostat.lireTemperature()
if temp is None:
2026-04-03 18:35:33 +02:00
# On renvoie une valeur par défaut ou 0 pour éviter le undefined
return {"success": False, "temperature": "--", "message": "Erreur DHT11"}
2026-04-01 00:23:24 +02:00
2026-04-03 18:35:33 +02:00
afficher_temperature(temp, bouton.temperature_cible)
2026-04-01 00:23:24 +02:00
return {"success": True, "temperature": temp}
except Exception as e:
2026-04-03 18:35:33 +02:00
return {"success": False, "temperature": "Erreur", "message": str(e)}
2026-04-01 18:39:00 +02:00
2026-04-02 22:19:52 +02:00
@app.get("/volet_status")
async def volet_status():
try:
actuel = bouton_servo.etat_porte
return {"success": True, "status": actuel}
except Exception as e:
return {"success": False, "message": str(e)}
@app.get("/luminosite")
def get_luminosite():
return ({"success": True, "status": LDR.lire_etat()})
2026-04-01 00:23:24 +02:00
if __name__ == "__main__":
2026-04-02 22:19:52 +02:00
t1 = threading.Thread(target=bouton_servo.test_boutons, daemon=True)
t2 = threading.Thread(target=bouton.test_boutons, daemon=True)
t1.start()
t2.start()
2026-04-01 18:39:00 +02:00
chemin_cle = os.path.join(BASE_DIR, 'web_secu', 'ssl', 'key.pem')
chemin_cert = os.path.join(BASE_DIR, 'web_secu', 'ssl', 'cert.pem')
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
ssl_keyfile=chemin_cle,
ssl_certfile=chemin_cert
)