99 lines
2.5 KiB
PHP
99 lines
2.5 KiB
PHP
<?php
|
|
// get_onu_detail.php
|
|
require __DIR__ . "/../config/encrypt.php";
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$id = isset($_POST['id']) ? $_POST['id'] : null;
|
|
|
|
$datadecrypt = decrypt_url($id);
|
|
$dataurl = json_decode($datadecrypt, true);
|
|
|
|
// Ambil parameter dari request
|
|
$tenant_id = $dataurl[0] ?? null;
|
|
$sn = $dataurl[1] ?? null;
|
|
$mac = $dataurl[2] ?? null;
|
|
|
|
if (!$tenant_id || (!$sn && !$mac)) {
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'message' => 'tenant_id dan salah satu SN/MAC harus diisi'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Fungsi helper untuk request ke API
|
|
*/
|
|
function call_api($tenant_id, $param) {
|
|
$api_url = "http://manjapro.net:20263/api/v1/onu/?tenant_id={$tenant_id}&macsn={$param}";
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $api_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if (curl_errno($ch)) {
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
return ['error' => $error];
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return ['http_code' => $http_code, 'response' => $response];
|
|
}
|
|
|
|
// 1️⃣ Coba pakai SN dulu
|
|
$result = null;
|
|
if ($sn) {
|
|
$result = call_api($tenant_id, $sn);
|
|
if ($result['http_code'] == 200 && !empty($result['response']) && $result['response'] !== '[]') {
|
|
$data = json_decode($result['response'], true);
|
|
|
|
// 🚀 Timpa kolom onu_mac dengan nilai dari $mac
|
|
foreach ($data as &$row) {
|
|
$row['onu_mac'] = $mac; // atau substr($mac, 0, -1) kalau mau custom
|
|
}
|
|
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// 2️⃣ Kalau SN gagal / kosong, coba pakai MAC
|
|
if ($mac) {
|
|
$result = call_api($tenant_id, substr($mac, 0, -1));
|
|
if ($result['http_code'] == 200 && !empty($result['response']) && $result['response'] !== '[]') {
|
|
$data = json_decode($result['response'], true);
|
|
|
|
// 🚀 Timpa kolom onu_mac dengan nilai dari $mac
|
|
foreach ($data as &$row) {
|
|
$row['onu_mac'] = $mac; // atau bisa diubah custom sesuai kebutuhan
|
|
}
|
|
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Jika dua-duanya gagal
|
|
if (isset($result['error'])) {
|
|
echo json_encode([[
|
|
'status' => 'error',
|
|
'message' => $result['error'],
|
|
'onu_mac' => $mac
|
|
]]);
|
|
} else {
|
|
echo json_encode([[
|
|
'status' => 'error',
|
|
'message' => "Data ONU tidak ditemukan dengan SN maupun MAC",
|
|
'onu_mac' => $mac
|
|
]]);
|
|
}
|
|
|
|
|
|
?>
|