Update: 2026-06-29 23:09:43
This commit is contained in:
83
loction_server/find_drivers_redis.php
Executable file
83
loction_server/find_drivers_redis.php
Executable file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
// find_drivers_redis.php
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
use Predis\Client;
|
||||
|
||||
header('Content-Type: application/json');
|
||||
$PASS_REDIS = trim(file_get_contents('/home/location/.reds_pass_key'));
|
||||
|
||||
|
||||
// 1. استقبال البيانات (من تطبيق الراكب أو السيرفر الرئيسي)
|
||||
// نفترض أنها تأتي POST أو GET
|
||||
$lat = $_REQUEST['lat'] ?? null;
|
||||
$lng = $_REQUEST['lng'] ?? null;
|
||||
$radius = $_REQUEST['radius'] ?? 5; // القطر بالكيلومتر
|
||||
$limit = $_REQUEST['limit'] ?? 50; // عدد السائقين
|
||||
|
||||
if (!$lat || !$lng) {
|
||||
echo json_encode(['status' => false, 'message' => 'Missing coordinates']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 2. الاتصال بالريدز
|
||||
try {
|
||||
$redis = new Client([
|
||||
'scheme' => 'tcp',
|
||||
'host' => '127.0.0.1','password' => $PASS_REDIS,
|
||||
'port' => 6379,
|
||||
]);
|
||||
$redis->connect();
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['status' => false, 'message' => 'Redis Error']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 3. البحث الجغرافي (السحر هنا) 🎩
|
||||
// يبحث عن السائقين المتاحين (available) فقط
|
||||
// التعديل: تفعيل WITHDIST و SORT كـ مفاتيح
|
||||
$driversGeo = $redis->georadius(
|
||||
'geo:drivers:available',
|
||||
$lng,
|
||||
$lat,
|
||||
$radius,
|
||||
'km',
|
||||
['WITHDIST' => true,'WITHCOORD' => true, 'COUNT' => $limit, 'SORT' => 'ASC']
|
||||
);
|
||||
|
||||
// النتيجة تكون مصفوفة: [ [driver_id, distance], ... ]
|
||||
|
||||
$resultDrivers = [];
|
||||
|
||||
// 4. جلب التفاصيل
|
||||
if (!empty($driversGeo)) {
|
||||
foreach ($driversGeo as $item) {
|
||||
$driverId = $item[0];
|
||||
$distance = $item[1]; // المسافة بالكيلومتر
|
||||
$d_coord= $item[2]; // [0=>lng, 1=>lat] 🔥 الإحداثيات هنا
|
||||
|
||||
// جلب البيانات الوصفية من الرام (Profile)
|
||||
// هذه البيانات خزنها السوكيت قبل قليل
|
||||
$profile = $redis->hgetall("driver:profile:$driverId");
|
||||
|
||||
// دمج البيانات
|
||||
$resultDrivers[] = [
|
||||
'driver_id' => $driverId,
|
||||
'distance_km' => $distance,
|
||||
'heading' => $profile['heading'] ?? 0,
|
||||
'speed' => $profile['speed'] ?? 0,
|
||||
'status' => $profile['status'] ?? 'off',
|
||||
'lat' => $d_coord[1], // Latitude من الريدز (أدق)
|
||||
'lng' => $d_coord[0] // Longitude
|
||||
// يمكنك هنا إضافة استعلام MySQL بسيط لجلب اسم السائق وصورته إذا لم تكن مخزنة في الريدز
|
||||
// لكن للسرعة، سنكتفي بهذا الآن
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 5. إرجاع النتيجة
|
||||
echo json_encode([
|
||||
'status' => true,
|
||||
'count' => count($resultDrivers),
|
||||
'drivers' => $resultDrivers
|
||||
]);
|
||||
?>
|
||||
Reference in New Issue
Block a user