58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// For the dashboard, we'll fetch the latest reading from any node as a general overview.
|
|
$stmt = $pdo->query("
|
|
SELECT
|
|
`temperature`,
|
|
`humidity`,
|
|
`co2`,
|
|
`gas_level`,
|
|
`pressure`,
|
|
`light_level`
|
|
FROM `sensor_readings`
|
|
ORDER BY `timestamp` DESC
|
|
LIMIT 1
|
|
");
|
|
|
|
$latest_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// If the database is empty, provide default zero values to prevent frontend errors.
|
|
if (!$latest_data) {
|
|
$latest_data = [
|
|
'temperature' => 0,
|
|
'humidity' => 0,
|
|
'co2' => 0,
|
|
'gas_level' => 0,
|
|
'pressure' => 1000, // A more realistic default for pressure
|
|
'light_level' => 0,
|
|
];
|
|
}
|
|
|
|
// These thresholds will be used by the frontend to determine the gauge color.
|
|
$thresholds = [
|
|
'temperature' => ['max' => 50, 'warning' => 28, 'critical' => 35],
|
|
'humidity' => ['max' => 100, 'warning' => 60, 'critical' => 80],
|
|
'co2' => ['max' => 5000, 'warning' => 1000, 'critical' => 2000],
|
|
'gas_level' => ['max' => 1000, 'warning' => 300, 'critical' => 500],
|
|
'pressure' => ['max' => 1100, 'warning' => 1030, 'critical' => 1050],
|
|
'light_level' => ['max' => 1000, 'warning' => 500, 'critical' => 750],
|
|
];
|
|
|
|
$response = [
|
|
'data' => $latest_data,
|
|
'thresholds' => $thresholds
|
|
];
|
|
|
|
echo json_encode($response);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
// In a production environment, you might want to log this error instead of echoing it.
|
|
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|