Téléverser les fichiers vers "/"
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
*.venv/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.pyo
|
||||||
|
.env
|
||||||
25
app.py
Normal file
25
app.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from flask import Flask
|
||||||
|
from flask_jwt_extended import JWTManager
|
||||||
|
from flask_cors import CORS
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
from routes.climate import climate_bp
|
||||||
|
from routes.security import security_bp
|
||||||
|
from routes.auth import auth_bp
|
||||||
|
from admin.admin_routes import admin_bp
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_object(Config)
|
||||||
|
|
||||||
|
#Extensions
|
||||||
|
JWTManager(app)
|
||||||
|
CORS(app)
|
||||||
|
|
||||||
|
#Enregistrement des routes
|
||||||
|
app.register_blueprint(climate_bp, url_prefix='/api/climate')
|
||||||
|
app.register_blueprint(security_bp, url_prefix='/api/security')
|
||||||
|
app.register_blueprint(auth_bp, url_prefix='/api/auth')
|
||||||
|
app.register_blueprint(admin_bp, url_prefix='/admin')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||||
17
config.py
Normal file
17
config.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
#Configuration base de données
|
||||||
|
MYSQL_HOST = 'localhost'
|
||||||
|
MYSQL_USER = 'smarthome_user'
|
||||||
|
MYSQL_PASSWORD = 'motdepasse123'
|
||||||
|
MYSQL_DB = 'smarthome'
|
||||||
|
|
||||||
|
#Configuration JWT
|
||||||
|
JWT_SECRET_KEY = 'smarthome_secret_key_2024'
|
||||||
|
|
||||||
|
#Configuration MQTT
|
||||||
|
MQTT_BROKER = 'localhost'
|
||||||
|
MQTT_PORT = 1883
|
||||||
|
MQTT_TOPIC_CLIMATE = 'smarthome/climate'
|
||||||
|
MQTT_TOPIC_SECURITY = 'smarthome/security'
|
||||||
55
mqtt_client.py
Normal file
55
mqtt_client.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import pymysql
|
||||||
|
import json
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
return pymysql.connect(
|
||||||
|
host=Config.MYSQL_HOST,
|
||||||
|
user=Config.MYSQL_USER,
|
||||||
|
password=Config.MYSQL_PASSWORD,
|
||||||
|
database=Config.MYSQL_DB,
|
||||||
|
cursorclass=pymysql.cursors.DictCursor
|
||||||
|
)
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
print(f"Connecté au broker MQTT avec le code : {rc}")
|
||||||
|
client.subscribe(Config.MQTT_TOPIC_CLIMATE)
|
||||||
|
client.subscribe(Config.MQTT_TOPIC_SECURITY)
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
topic = msg.topic
|
||||||
|
payload = json.loads(msg.payload.decode())
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if topic == Config.MQTT_TOPIC_CLIMATE:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO climate_data (temperature, humidity, pressure, luminosity) VALUES (%s, %s, %s, %s)",
|
||||||
|
(payload.get('temperature'), payload.get('humidity'), payload.get('pressure'), payload.get('luminosity'))
|
||||||
|
)
|
||||||
|
print(f"Donnée climatique insérée : {payload}")
|
||||||
|
|
||||||
|
elif topic == Config.MQTT_TOPIC_SECURITY:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO security_events (event_type, location) VALUES (%s, %s)",
|
||||||
|
(payload.get('event_type'), payload.get('location'))
|
||||||
|
)
|
||||||
|
print(f"Événement sécurité inséré : {payload}")
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Erreur lors de l'insertion : {e}")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
def start_mqtt():
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
|
client.connect(Config.MQTT_BROKER, Config.MQTT_PORT, 60)
|
||||||
|
client.loop_forever()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
start_mqtt()
|
||||||
12
requirements.txt
Normal file
12
requirements.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
blinker==1.9.0
|
||||||
|
click==8.4.1
|
||||||
|
Flask==3.1.3
|
||||||
|
flask-cors==6.0.2
|
||||||
|
Flask-JWT-Extended==4.7.4
|
||||||
|
itsdangerous==2.2.0
|
||||||
|
Jinja2==3.1.6
|
||||||
|
MarkupSafe==3.0.3
|
||||||
|
paho-mqtt==2.1.0
|
||||||
|
PyJWT==2.13.0
|
||||||
|
PyMySQL==1.2.0
|
||||||
|
Werkzeug==3.1.8
|
||||||
Reference in New Issue
Block a user