getConnection();
// ================= CONFIG =================
$login_url = "http://10.255.254.21/login";
$data_url = "http://10.255.254.21/ontinfo_table";
$username = "root";
$password = "@lokal234";
$cookie = __DIR__ . "/cookie.txt";
// ================= LOGIN =================
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
"username" => $username,
"password" => $password
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
// ================= AMBIL DATA =================
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $data_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
if ($response === false) {
die("CURL ERROR: " . curl_error($ch));
}
curl_close($ch);
file_put_contents("debug_onu.json", $response);
$data = json_decode($response, true);
if (!isset($data['data']) || empty($data['data'])) {
die("DATA KOSONG");
}
// ================= PROCESS =================
foreach ($data['data'] as $onu) {
$sn = $onu['ont_sn'] ?? '';
$rx = $onu['receive_power'] ?? 0;
$state = $onu['state'] ?? 0;
$cause = strtolower($onu['last_d_cause'] ?? '');
if (empty($sn)) continue;
// ================= STATUS =================
if (strpos($cause, 'laser out') !== false || strpos($cause, 'fiber') !== false) {
$status = "LASER OUT";
} elseif (strpos($cause, 'power down') !== false || strpos($cause, 'dying-gasp') !== false) {
$status = "POWER FAIL";
} else {
if ($state == 0 && $rx <= -30) {
$status = "LASER OUT";
} elseif ($rx <= -30) {
$status = "POWER FAIL";
} else {
$status = "ONLINE";
}
}
// ================= GET LAST STATUS =================
$cek = $conn->prepare("SELECT status FROM onu_status WHERE sn = ?");
$cek->execute([$sn]);
$last = $cek->fetch(PDO::FETCH_ASSOC);
$lastStatus = $last['status'] ?? null;
// ================= UPSERT REALTIME =================
$conn->query("
INSERT INTO onu_status (sn, status, rx_power, last_update)
VALUES ('$sn', '$status', '$rx', NOW())
ON DUPLICATE KEY UPDATE
last_status = status,
status = '$status',
rx_power = '$rx',
last_update = NOW()
");
// ================= LOG HISTORI (HANYA ANOMALI) =================
$anomali = in_array($status, ['LASER OUT', 'POWER FAIL']);
$trigger = ($lastStatus !== $status);
if ($anomali && $trigger) {
$stmt = $conn->prepare("
INSERT INTO status_onu_histori (sn, status, rx_power, created_at)
VALUES (?, ?, ?, NOW())
");
$stmt->execute([$sn, $status, $rx]);
echo "LOG ANOMALI: $sn | $status | RX $rx
";
}
echo "SN: $sn | STATUS: $status | RX: $rx
";
}
echo "DONE ✅";
?>