Téléverser les fichiers vers "/"
This commit is contained in:
41
auth.php
Normal file
41
auth.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'config.php';
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use Firebase\JWT\JWT;
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
header('Access-Control-Allow-Origin: *');
|
||||||
|
header('Access-Control-Allow-Methods: POST');
|
||||||
|
header('Access-Control-Allow-Headers: Content-Type');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$body = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$username = $body['username'] ?? '';
|
||||||
|
$password = $body['password'] ?? '';
|
||||||
|
|
||||||
|
$db = get_db();
|
||||||
|
$stmt = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
|
||||||
|
$stmt->bind_param("ss", $username, $password);
|
||||||
|
$stmt->execute();
|
||||||
|
$result = $stmt->get_result();
|
||||||
|
$user = $result->fetch_assoc();
|
||||||
|
$db->close();
|
||||||
|
|
||||||
|
if ($user) {
|
||||||
|
$payload = [
|
||||||
|
'sub' => $username,
|
||||||
|
'iat' => time(),
|
||||||
|
'exp' => time() + 900
|
||||||
|
];
|
||||||
|
$token = JWT::encode($payload, JWT_SECRET, 'HS256');
|
||||||
|
echo json_encode(['token' => $token]);
|
||||||
|
} else {
|
||||||
|
http_response_code(401);
|
||||||
|
echo json_encode(['message' => 'Identifiants incorrects']);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
http_response_code(405);
|
||||||
|
echo json_encode(['message' => 'Méthode non autorisée']);
|
||||||
|
}
|
||||||
|
?>
|
||||||
7
composer.json
Normal file
7
composer.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"name": "smarthome/api",
|
||||||
|
"require": {
|
||||||
|
"php-mqtt/client": "^2.3",
|
||||||
|
"firebase/php-jwt": "^7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
config.php
Normal file
22
config.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
define('DB_HOST', 'localhost');
|
||||||
|
define('DB_USER', 'smarthome_user');
|
||||||
|
define('DB_PASSWORD', 'motdepasse123');
|
||||||
|
define('DB_NAME', 'smarthome');
|
||||||
|
|
||||||
|
define('JWT_SECRET', 'smarthome_secret_key_2024_smart_home_project_very_long_key');
|
||||||
|
|
||||||
|
define('MQTT_BROKER', 'localhost');
|
||||||
|
define('MQTT_PORT', 1883);
|
||||||
|
define('MQTT_TOPIC_CLIMATE', 'smarthome/climate');
|
||||||
|
define('MQTT_TOPIC_SECURITY', 'smarthome/security');
|
||||||
|
|
||||||
|
function get_db() {
|
||||||
|
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
die(json_encode(['error' => 'Connexion échouée : ' . $conn->connect_error]));
|
||||||
|
}
|
||||||
|
return $conn;
|
||||||
|
}
|
||||||
|
?>
|
||||||
47
mqtt_client.php
Normal file
47
mqtt_client.php
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'config.php';
|
||||||
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use PhpMqtt\Client\MqttClient;
|
||||||
|
use PhpMqtt\Client\ConnectionSettings;
|
||||||
|
|
||||||
|
function insert_climate($data) {
|
||||||
|
$db = get_db();
|
||||||
|
$stmt = $db->prepare("INSERT INTO climate_data (temperature, humidity, pressure, luminosity) VALUES (?, ?, ?, ?)");
|
||||||
|
$stmt->bind_param("dddd",
|
||||||
|
$data['temperature'],
|
||||||
|
$data['humidity'],
|
||||||
|
$data['pressure'],
|
||||||
|
$data['luminosity']
|
||||||
|
);
|
||||||
|
$stmt->execute();
|
||||||
|
$db->close();
|
||||||
|
echo "Donnée climatique insérée : " . json_encode($data) . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function insert_security($data) {
|
||||||
|
$db = get_db();
|
||||||
|
$stmt = $db->prepare("INSERT INTO security_events (event_type, location) VALUES (?, ?)");
|
||||||
|
$stmt->bind_param("ss", $data['event_type'], $data['location']);
|
||||||
|
$stmt->execute();
|
||||||
|
$db->close();
|
||||||
|
echo "Événement sécurité inséré : " . json_encode($data) . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$mqtt = new MqttClient(MQTT_BROKER, MQTT_PORT);
|
||||||
|
$settings = (new ConnectionSettings())->setKeepAliveInterval(60);
|
||||||
|
$mqtt->connect($settings);
|
||||||
|
echo "Connecté au broker MQTT\n";
|
||||||
|
|
||||||
|
$mqtt->subscribe(MQTT_TOPIC_CLIMATE, function($topic, $message) {
|
||||||
|
$data = json_decode($message, true);
|
||||||
|
insert_climate($data);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
$mqtt->subscribe(MQTT_TOPIC_SECURITY, function($topic, $message) {
|
||||||
|
$data = json_decode($message, true);
|
||||||
|
insert_security($data);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
$mqtt->loop(true);
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user