'No vehicle licenses available for this user', 'user' => $user )); exit; } // Build license list untuk WHERE IN clause $lics = ''; foreach ($_SESSION['license'] as $r) { $lics .= "'" . $r[1] . "',"; // $r[1] adalah license plate } $lics = substr($lics, 0, -1); // Remove trailing comma // Debug: Log license list // error_log("Maps Kendaraan API - License list: " . $lics); // error_log("Maps Kendaraan API - License count: " . count($_SESSION['license'])); // Get GMT setting dari session (default 7 jika tidak ada) $gmt = isset($_SESSION['GMT']) ? $_SESSION['GMT'] : 7; // Get userGroupID dari session untuk query groups $userGroupID = isset($_SESSION['userGroupID']) ? $_SESSION['userGroupID'] : ''; // Debug: Log GMT setting dan userGroupID // error_log("Maps Kendaraan API - GMT setting: " . $gmt); // error_log("Maps Kendaraan API - UserGroupID: " . $userGroupID); // Build SQL query untuk mendapatkan data kendaraan terkini dengan groups $sql = "SELECT dbo.tblatestlocation.license AS License, CAST(dbo.tblatestlocation.TID AS VARCHAR(20)) AS TID, dbo.tblatestlocation.driver, dbo.tblatestlocation.driverHP, dbo.tblatestlocation.latitude AS Latitude, dbo.tblatestlocation.longitude AS Longitude, dbo.tblatestlocation.speed AS Speed, dbo.tblatestlocation.course AS Course, dbo.tblatestlocation.Alarm, dbo.tblatestlocation.Panic, dbo.tblatestlocation.[Ignition On] AS IgnitionOn, dbo.tblatestlocation.oilLevel, dbo.tblatestlocation.[Input Power] AS InputPower, CONVERT(VARCHAR(19), DATEADD(HOUR, {$gmt}, [Time]), 120) AS [Time], CONVERT(VARCHAR(19), DATEADD(HOUR, {$gmt}, ReceiveTime), 120) AS ReceiveTime, CONVERT(VARCHAR(19), DATEADD(HOUR, {$gmt}, LastRcvTime), 120) AS LastRcvTime, ISNULL(CAST(dtKendaraan.Tahun AS VARCHAR), '') AS tahun, ISNULL(dtKendaraan.Jenis_Kendaraan, '') AS jenis, ISNULL(dtKendaraan.Produk, '') AS muatan, ISNULL(dtProduk.warna, '255,255,255,0.7') AS warna, ISNULL(tbLicenseTran.tid, '') AS groups FROM dbo.tblatestlocation LEFT JOIN dtKendaraan ON tblatestlocation.license = dtKendaraan.License LEFT JOIN dtProduk ON dtProduk.produk = dtKendaraan.Produk LEFT JOIN tbLicenseTran ON tblatestlocation.license = tbLicenseTran.license AND tbLicenseTran.customerID BETWEEN (SELECT MinNo FROM tbUserGroup WHERE userGroupID = '{$userGroupID}') AND (SELECT MaxNo FROM tbUserGroup WHERE userGroupID = '{$userGroupID}') WHERE tblatestlocation.License IN ({$lics}) ORDER BY LastRcvTime DESC"; // Debug: Log final SQL query // error_log("Maps Kendaraan API - SQL Query: " . $sql); // Execute query $pdo = Database::getConnection(); $stmt = $pdo->prepare($sql); $stmt->execute(); $rows = $stmt->fetchAll(); // Debug: Log query result // error_log("Maps Kendaraan API - Query result count: " . count($rows)); // Process data untuk format response $datas = array(); if (count($rows) > 0) { foreach ($rows as $row) { // Convert course ke arah mata angin $arah = SessionHelper::fArah($row['Course']); $arah = ($arah == '-') ? '' : $arah; // Determine status mesin berdasarkan IgnitionOn value // Logic: jika IgnitionOn < 260000 maka mesin mati $mesin = ($row['IgnitionOn'] < 260000) ? 'Mati' : 'Hidup'; // Debug: Log individual vehicle processing // error_log("Processing vehicle: " . $row['License'] . " - Course: " . $row['Course'] . " - Arah: " . $arah . " - Mesin: " . $mesin); // Build response data structure $data = array(); $data["TID"] = $row['TID']; $data["License"] = $row['License']; $data["Address"] = ''; // TODO: Implement reverse geocoding jika diperlukan $data["Latitude"] = floatval($row['Latitude']); $data["Longitude"] = floatval($row['Longitude']); $data["Waktu"] = $row['ReceiveTime']; $data["StatusMesin"] = $mesin; $data["Kecepatan"] = floatval($row['Speed']); $data["ArahInt"] = intval($row['Course']); $data["Arah"] = $arah; $data["Aki"] = $row['InputPower']; $data["groups"] = $row['groups']; // Updated from Group to groups $data["WaktuGPS"] = $row['Time']; $data["tahun"] = $row['tahun']; $data["jenis"] = $row['jenis']; $data["muatan"] = $row['muatan']; $data["warna"] = $row['warna']; // Additional debug fields (uncomment jika perlu debug) // $data["debug_ignition"] = $row['IgnitionOn']; // $data["debug_alarm"] = $row['Alarm']; // $data["debug_panic"] = $row['Panic']; // $data["debug_oil"] = $row['oilLevel']; array_push($datas, $data); } } // Debug: Log final response // error_log("Maps Kendaraan API - Final response count: " . count($datas)); // error_log("Maps Kendaraan API - Sample data: " . json_encode(array_slice($datas, 0, 2))); // Set response headers header('Content-Type: application/json'); // Return JSON response echo json_encode($datas); } catch (Exception $e) { // Log error untuk debugging error_log("Maps Kendaraan API Error: " . $e->getMessage()); error_log("Maps Kendaraan API Error Trace: " . $e->getTraceAsString()); http_response_code(500); echo json_encode(array( 'error' => 'Failed to retrieve vehicle data: ' . $e->getMessage() // Debug version dengan stack trace: // 'error' => 'Failed to retrieve vehicle data: ' . $e->getMessage(), // 'trace' => $e->getTraceAsString(), // 'user' => $user, // 'session_data' => array( // 'username' => isset($_SESSION['username']) ? $_SESSION['username'] : 'not set', // 'license_count' => count(isset($_SESSION['license']) ? $_SESSION['license'] : array()) // ) )); }