WEB_PROJECT_TA/api/histori_redaman.php

94 lines
2.1 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../config/database.php';
$db = new Database();
$conn = $db->getConnection();
$item_id = intval($_GET['id'] ?? 0);
if (!$item_id) {
echo json_encode([
'success' => false,
'message' => 'ID tidak valid'
]);
exit;
}
/*
|--------------------------------------------------------------------------
| Ambil SN dari ftth_items
|--------------------------------------------------------------------------
*/
$stmt = $conn->prepare("
SELECT sn_onu, name
FROM ftth_items
WHERE id = ?
");
$stmt->execute([$item_id]);
$item = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$item || empty($item['sn_onu'])) {
echo json_encode([
'success' => false,
'message' => 'SN tidak ditemukan'
]);
exit;
}
$sn = trim($item['sn_onu']);
/*
|--------------------------------------------------------------------------
| Ambil histori redaman & voltage
|--------------------------------------------------------------------------
*/
$stmt = $conn->prepare("
SELECT
rx_power,
voltage,
updated_at
FROM onu_daily_history
WHERE sn = ?
ORDER BY updated_at DESC
LIMIT 30
");
$stmt->execute([$sn]);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$data) {
echo json_encode([
'success' => false,
'message' => 'Histori kosong'
]);
exit;
}
/*
|--------------------------------------------------------------------------
| Format biar rapi (opsional)
|--------------------------------------------------------------------------
*/
foreach ($data as &$row) {
// format RX
if ($row['rx_power'] !== null) {
$row['rx_power'] = number_format((float)$row['rx_power'], 2) . ' dBm';
}
// format voltage
if ($row['voltage'] !== null) {
$row['voltage'] = number_format((float)$row['voltage'], 2) . ' V';
}
// tanggal lebih enak dibaca
$row['updated_at'] = date('d-m-Y H:i:s', strtotime($row['updated_at']));
}
echo json_encode([
'success' => true,
'sn' => $sn,
'data' => $data
]);