Correction connexion PDO et liaison dynamique du graphique
This commit is contained in:
@@ -5,27 +5,38 @@ require_once 'vendor/autoload.php';
|
||||
use PhpMqtt\Client\MqttClient;
|
||||
use PhpMqtt\Client\ConnectionSettings;
|
||||
|
||||
function insert_climate($data) {
|
||||
try {
|
||||
$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";
|
||||
} catch (Exception $e) {
|
||||
die("Impossible de démarrer le script : " . $e->getMessage() . "\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";
|
||||
function insert_climate($db, $data) {
|
||||
try {
|
||||
$stmt = $db->prepare("INSERT INTO climate_data (temperature, humidity, pressure, luminosity) VALUES (:temp, :hum, :pres, :lum)");
|
||||
$stmt->execute([
|
||||
':temp' => $data['temperature'],
|
||||
':hum' => $data['humidity'],
|
||||
':pres' => $data['pressure'],
|
||||
':lum' => $data['luminosity']
|
||||
]);
|
||||
echo "Donnée climatique insérée : " . json_encode($data) . "\n";
|
||||
} catch (PDOException $e) {
|
||||
echo "Erreur d'insertion climat : " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
function insert_security($db, $data) {
|
||||
try {
|
||||
$stmt = $db->prepare("INSERT INTO security_events (event_type, location) VALUES (:event, :loc)");
|
||||
$stmt->execute([
|
||||
':event' => $data['event_type'],
|
||||
':loc' => $data['location']
|
||||
]);
|
||||
echo "Événement sécurité inséré : " . json_encode($data) . "\n";
|
||||
} catch (PDOException $e) {
|
||||
echo "Erreur d'insertion sécurité : " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$mqtt = new MqttClient(MQTT_BROKER, MQTT_PORT);
|
||||
@@ -33,14 +44,18 @@ $settings = (new ConnectionSettings())->setKeepAliveInterval(60);
|
||||
$mqtt->connect($settings);
|
||||
echo "Connecté au broker MQTT\n";
|
||||
|
||||
$mqtt->subscribe(MQTT_TOPIC_CLIMATE, function($topic, $message) {
|
||||
$mqtt->subscribe(MQTT_TOPIC_CLIMATE, function($topic, $message) use ($db) {
|
||||
$data = json_decode($message, true);
|
||||
insert_climate($data);
|
||||
if ($data) {
|
||||
insert_climate($db, $data);
|
||||
}
|
||||
}, 0);
|
||||
|
||||
$mqtt->subscribe(MQTT_TOPIC_SECURITY, function($topic, $message) {
|
||||
$mqtt->subscribe(MQTT_TOPIC_SECURITY, function($topic, $message) use ($db) {
|
||||
$data = json_decode($message, true);
|
||||
insert_security($data);
|
||||
if ($data) {
|
||||
insert_security($db, $data);
|
||||
}
|
||||
}, 0);
|
||||
|
||||
$mqtt->loop(true);
|
||||
|
||||
Reference in New Issue
Block a user