false, 'msg' => 'Unauthorized']); exit; } $lat = $_REQUEST['lat'] ?? null; $lng = $_REQUEST['lng'] ?? null; $radius = $_REQUEST['radius'] ?? 5; $limit = $_REQUEST['limit'] ?? 100; if (!$lat || !$lng) { echo json_encode(['status' => false, 'msg' => 'Invalid Coordinates']); exit; } try { $redis = new Client(['scheme'=>'tcp', 'host'=>'127.0.0.1', 'password' => $PASS_REDIS, 'port'=>6379, 'database' => 0] ); $redis->connect(); // 🔥 التعديل هنا: إضافة WITHCOORD لجلب الإحداثيات من الريدز مباشرة $geoResults = $redis->georadius( 'geo:drivers:available', $lng, $lat, $radius, 'km', ['WITHDIST' => true, 'WITHCOORD' => true, 'COUNT' => $limit * 2, 'SORT' => 'ASC'] ); $validDrivers = []; $currentTime = time(); $max_silence = 180; foreach ($geoResults as $res) { // هيكل النتيجة مع WITHCOORD يختلف قليلاً $d_id = $res[0]; // ID $d_dist = $res[1]; // Distance $d_coord= $res[2]; // [0=>lng, 1=>lat] 🔥 الإحداثيات هنا $profile = $redis->hgetall("driver:profile:$d_id"); $lastUpdate = isset($profile['updated_at']) ? (int)$profile['updated_at'] : 0; if (empty($profile) || ($currentTime - $lastUpdate) > $max_silence) { $redis->zrem('geo:drivers:available', $d_id); $redis->zrem('geo:drivers:busy', $d_id); continue; } $validDrivers[] = [ 'id' => $d_id, 'distance' => $d_dist, 'heading' => $profile['heading'] ?? 0, 'speed' => $profile['speed'] ?? 0, 'lat' => $d_coord[1], // Latitude من الريدز (أدق) 'lng' => $d_coord[0] // Longitude من الريدز ]; if (count($validDrivers) >= $limit) break; } echo json_encode(['status' => true, 'data' => $validDrivers]); } catch (Exception $e) { echo json_encode(['status' => false, 'msg' => $e->getMessage()]); } ?>