diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..06d7405 Binary files /dev/null and b/routes/__init__.py differ diff --git a/routes/auth.py b/routes/auth.py new file mode 100644 index 0000000..42ff96c --- /dev/null +++ b/routes/auth.py @@ -0,0 +1,34 @@ +from flask import Blueprint, jsonify, request +from flask_jwt_extended import create_access_token +import pymysql +from config import Config + +auth_bp = Blueprint('auth', __name__) + +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 + ) + +#POST — Login et génération du token JWT +@auth_bp.route('/login', methods=['POST']) +def login(): + body = request.get_json() + username = body.get('username') + password = body.get('password') + + db = get_db() + cursor = db.cursor() + cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password)) + user = cursor.fetchone() + db.close() + + if user: + token = create_access_token(identity=username) + return jsonify({'token': token}), 200 + else: + return jsonify({'message': 'Identifiants incorrects'}), 401 \ No newline at end of file diff --git a/routes/climate.py b/routes/climate.py new file mode 100644 index 0000000..125b472 --- /dev/null +++ b/routes/climate.py @@ -0,0 +1,41 @@ +from flask import Blueprint, jsonify, request +from flask_jwt_extended import jwt_required +import pymysql +from config import Config + +climate_bp = Blueprint('climate', __name__) + +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 + ) + +#GET — Récupérer toutes les données climatiques +@climate_bp.route('/', methods=['GET']) +@jwt_required() +def get_climate(): + db = get_db() + cursor = db.cursor() + cursor.execute("SELECT * FROM climate_data ORDER BY timestamp DESC LIMIT 50") + data = cursor.fetchall() + db.close() + return jsonify(data), 200 + +#POST — Ajouter une donnée climatique +@climate_bp.route('/', methods=['POST']) +@jwt_required() +def add_climate(): + body = request.get_json() + db = get_db() + cursor = db.cursor() + cursor.execute( + "INSERT INTO climate_data (temperature, humidity, pressure, luminosity) VALUES (%s, %s, %s, %s)", + (body.get('temperature'), body.get('humidity'), body.get('pressure'), body.get('luminosity')) + ) + db.commit() + db.close() + return jsonify({'message': 'Donnée ajoutée avec succès'}), 201 \ No newline at end of file diff --git a/routes/security.py b/routes/security.py new file mode 100644 index 0000000..af622cf --- /dev/null +++ b/routes/security.py @@ -0,0 +1,41 @@ +from flask import Blueprint, jsonify, request +from flask_jwt_extended import jwt_required +import pymysql +from config import Config + +security_bp = Blueprint('security', __name__) + +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 + ) + +#GET — Récupérer tous les événements de sécurité +@security_bp.route('/', methods=['GET']) +@jwt_required() +def get_security(): + db = get_db() + cursor = db.cursor() + cursor.execute("SELECT * FROM security_events ORDER BY timestamp DESC LIMIT 50") + data = cursor.fetchall() + db.close() + return jsonify(data), 200 + +# POST — Ajouter un événement de sécurité +@security_bp.route('/', methods=['POST']) +@jwt_required() +def add_security(): + body = request.get_json() + db = get_db() + cursor = db.cursor() + cursor.execute( + "INSERT INTO security_events (event_type, location) VALUES (%s, %s)", + (body.get('event_type'), body.get('location')) + ) + db.commit() + db.close() + return jsonify({'message': 'Événement ajouté avec succès'}), 201 \ No newline at end of file