diff --git a/auth.php b/auth.php new file mode 100644 index 0000000..29d243a --- /dev/null +++ b/auth.php @@ -0,0 +1,41 @@ +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']); +} +?> \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cbf9f33 --- /dev/null +++ b/composer.json @@ -0,0 +1,7 @@ +{ + "name": "smarthome/api", + "require": { + "php-mqtt/client": "^2.3", + "firebase/php-jwt": "^7.0" + } +} diff --git a/config.php b/config.php new file mode 100644 index 0000000..2c244a2 --- /dev/null +++ b/config.php @@ -0,0 +1,22 @@ +connect_error) { + die(json_encode(['error' => 'Connexion échouée : ' . $conn->connect_error])); + } + return $conn; +} +?> \ No newline at end of file diff --git a/mqtt_client.php b/mqtt_client.php new file mode 100644 index 0000000..d6a8ca2 --- /dev/null +++ b/mqtt_client.php @@ -0,0 +1,47 @@ +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); +?> \ No newline at end of file