81 lines
2.6 KiB
PHP
Executable File
81 lines
2.6 KiB
PHP
Executable File
<?php
|
|
// api_get_nearby.php (Updated)
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
use Predis\Client;
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$INTERNAL_KEY = trim(file_get_contents('/home/location/.internal_socket_key'));
|
|
$PASS_REDIS = trim(file_get_contents('/home/location/.reds_pass_key'));
|
|
|
|
$headers = getallheaders();
|
|
$receivedKey = $headers['x-internal-key'] ?? $_SERVER['HTTP_X_INTERNAL_KEY'] ?? '';
|
|
|
|
if ($receivedKey !== $INTERNAL_KEY) {
|
|
http_response_code(403);
|
|
echo json_encode(['status' => 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()]);
|
|
}
|
|
?>
|