33 lines
931 B
PHP
33 lines
931 B
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
$sql = "
|
|
SELECT
|
|
sr.reading_time,
|
|
sr.reading_value,
|
|
w.name AS warehouse_name,
|
|
s.name AS slot_name,
|
|
n.name AS node_name
|
|
FROM sensor_readings sr
|
|
JOIN nodes n ON sr.node_id = n.id
|
|
JOIN slots s ON n.slot_id = s.id
|
|
JOIN warehouses w ON s.warehouse_id = w.id
|
|
WHERE sr.sensor_type = 'CO2'
|
|
ORDER BY sr.reading_time DESC
|
|
LIMIT 100; // Limit to the latest 100 readings for performance
|
|
";
|
|
|
|
$stmt = $pdo->query($sql);
|
|
$readings = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo json_encode(['success' => true, 'data' => $readings]);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
|
}
|