107 lines
2.9 KiB
PHP
107 lines
2.9 KiB
PHP
<?php
|
|
// get_onu_detail.php
|
|
require __DIR__ . "/../config/encrypt.php";
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$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',
|
|
'onu_mac' => $mac
|
|
]]);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Fungsi helper untuk request ke API NodeJS
|
|
*/
|
|
function call_api($tenant_id, $param) {
|
|
$api_url = "http://olt.manjapro.net/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];
|
|
}
|
|
|
|
/**
|
|
* Fungsi mapping hasil NodeJS ke format array `onu_*`
|
|
*/
|
|
function map_to_onu_array($data, $mac) {
|
|
return [[
|
|
'onu_sn' => $data['macsn'] ?? null,
|
|
'onu_mac' => '-', //$data['macsn'] ?? $mac,
|
|
'onu_name' => '-', //['user_name'] ?? null,
|
|
'onu_type' => '-', // placeholder, bisa diisi jika ada
|
|
'onu_rxpower' => $data['rx_power'] ?? null,
|
|
'onu_txpower' => '-', // placeholder
|
|
'onu_status' => $data['status'] ?? null,
|
|
'onu_gpon_distance' => '-', // placeholder
|
|
'onu_last_online' => '-', // placeholder
|
|
]];
|
|
}
|
|
|
|
// 1️⃣ Coba pakai SN dulu
|
|
$result = null;
|
|
if ($sn) {
|
|
$result = call_api($tenant_id, $sn);
|
|
if ($result['http_code'] == 200 && !empty($result['response'])) {
|
|
$data = json_decode($result['response'], true);
|
|
if (!isset($data['status']) || $data['status'] !== 'error') {
|
|
echo json_encode(map_to_onu_array($data, $mac));
|
|
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'])) {
|
|
$data = json_decode($result['response'], true);
|
|
if (!isset($data['status']) || $data['status'] !== 'error') {
|
|
echo json_encode(map_to_onu_array($data, $mac));
|
|
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
|
|
]]);
|
|
}
|
|
?>
|