Update: 2026-07-22 18:11:54
This commit is contained in:
@@ -1,129 +0,0 @@
|
|||||||
<?php
|
|
||||||
// add_batch.php
|
|
||||||
include "../../connect.php";
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 1. استقبال البيانات
|
|
||||||
$driver_id = filterRequest("driver_id");
|
|
||||||
$json_batch = $_POST['batch_data'];
|
|
||||||
|
|
||||||
if (empty($driver_id) || empty($json_batch)) {
|
|
||||||
printFailure("No data received");
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$points = json_decode($json_batch, true);
|
|
||||||
if (!is_array($points) || count($points) === 0) {
|
|
||||||
printFailure("Invalid JSON");
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// الجزء الأول: حساب مدة العمل الإضافية (PHP Calculation)
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
// أ. ترتيب النقاط زمنياً للتأكد (احتياطاً)
|
|
||||||
usort($points, function($a, $b) {
|
|
||||||
return strtotime($a['ts']) - strtotime($b['ts']);
|
|
||||||
});
|
|
||||||
|
|
||||||
$batch_added_seconds = 0;
|
|
||||||
$first_point_time = strtotime($points[0]['ts']);
|
|
||||||
|
|
||||||
// ب. جلب آخر وقت مسجل لهذا السائق من قاعدة البيانات
|
|
||||||
// (لحساب الزمن الضائع بين الباتش السابق وأول نقطة في هذا الباتش)
|
|
||||||
$stmtLast = $con->prepare("SELECT created_at FROM car_tracks WHERE driver_id = ? ORDER BY created_at DESC LIMIT 1");
|
|
||||||
$stmtLast->execute([$driver_id]);
|
|
||||||
$lastRow = $stmtLast->fetch(PDO::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if ($lastRow) {
|
|
||||||
$last_db_time = strtotime($lastRow['created_at']);
|
|
||||||
$diff_from_db = $first_point_time - $last_db_time;
|
|
||||||
|
|
||||||
// إذا كان الفرق منطقياً (أقل من 5 دقائق) وموجباً، نحسبه
|
|
||||||
if ($diff_from_db > 0 && $diff_from_db < 300) {
|
|
||||||
$batch_added_seconds += $diff_from_db;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ج. حساب الفروقات داخل الباتش نفسه
|
|
||||||
$prev_time = $first_point_time;
|
|
||||||
|
|
||||||
// نحدد تاريخ هذا الباتش (لنعرف أي يوم نحدث في الجدول اليومي)
|
|
||||||
$batch_date = date('Y-m-d', $first_point_time);
|
|
||||||
|
|
||||||
foreach ($points as $key => $point) {
|
|
||||||
if ($key === 0) continue; // تخطي النقطة الأولى لأننا قارناها مع الداتابيز
|
|
||||||
|
|
||||||
$current_time = strtotime($point['ts']);
|
|
||||||
$diff = $current_time - $prev_time;
|
|
||||||
|
|
||||||
// تجاهل القفزات الكبيرة (أكثر من 5 دقائق)
|
|
||||||
if ($diff > 0 && $diff < 300) {
|
|
||||||
$batch_added_seconds += $diff;
|
|
||||||
}
|
|
||||||
$prev_time = $current_time;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// الجزء الثاني: إدخال التراكات (Bulk Insert) - كودك الأصلي
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
$values = [];
|
|
||||||
$placeholders = [];
|
|
||||||
|
|
||||||
foreach ($points as $point) {
|
|
||||||
$lat = $point['lat'] ?? 0;
|
|
||||||
$lng = $point['lng'] ?? 0;
|
|
||||||
$spd = $point['spd'] ?? 0;
|
|
||||||
$head = $point['head'] ?? 0;
|
|
||||||
$dist = $point['dst'] ?? 0;
|
|
||||||
$stat = $point['st'] ?? 'off';
|
|
||||||
$time = $point['ts'] ?? date('Y-m-d H:i:s');
|
|
||||||
|
|
||||||
$placeholders[] = "(?, ?, ?, ?, ?, ?, ?, ?)";
|
|
||||||
array_push($values, $driver_id, $lat, $lng, $head, $spd, $dist, $stat, $time);
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = "INSERT INTO `car_tracks`
|
|
||||||
(`driver_id`, `latitude`, `longitude`, `heading`, `speed`, `distance`, `status`, `created_at`)
|
|
||||||
VALUES " . implode(', ', $placeholders);
|
|
||||||
|
|
||||||
$stmt = $con->prepare($sql);
|
|
||||||
$ok = $stmt->execute($values);
|
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// الجزء الثالث: تحديث جدول الملخص اليومي (The Smart Update)
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
|
|
||||||
if ($ok && $batch_added_seconds > 0) {
|
|
||||||
// نستخدم ON DUPLICATE KEY UPDATE:
|
|
||||||
// إذا كان السائق موجوداً لهذا اليوم، أضف الثواني للرصيد الموجود
|
|
||||||
// إذا لم يكن موجوداً، أنشئ سجلاً جديداً
|
|
||||||
|
|
||||||
$sqlSummary = "INSERT INTO `driver_daily_summary` (`driver_id`, `date`, `total_seconds`)
|
|
||||||
VALUES (?, ?, ?)
|
|
||||||
ON DUPLICATE KEY UPDATE `total_seconds` = `total_seconds` + VALUES(`total_seconds`)";
|
|
||||||
|
|
||||||
$stmtSum = $con->prepare($sqlSummary);
|
|
||||||
$stmtSum->execute([$driver_id, $batch_date, $batch_added_seconds]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($ok) {
|
|
||||||
echo json_encode(array(
|
|
||||||
"status" => "success",
|
|
||||||
"count" => count($points),
|
|
||||||
"added_seconds" => $batch_added_seconds // للتتبع فقط
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
printFailure("Failed to insert batch");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
error_log("Batch insert error: " . $e->getMessage());
|
|
||||||
printFailure("Database error");
|
|
||||||
} catch (Throwable $e) {
|
|
||||||
printFailure("Server error");
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -44,9 +44,12 @@ $INTERNAL_KEY = trim(file_get_contents('/home/location/.internal_socket_key'));
|
|||||||
// });
|
// });
|
||||||
function authenticateJWT()
|
function authenticateJWT()
|
||||||
{
|
{
|
||||||
$secretKey = trim(file_get_contents('/home/location/.secret_key')); // Access secret key (ensure it's set in .env)
|
$secretKeyFile = file_exists(__DIR__ . '/../../loction-keys/.secret_key')
|
||||||
|
? __DIR__ . '/../../loction-keys/.secret_key'
|
||||||
|
: (file_exists('/keys/.secret_key') ? '/keys/.secret_key' : '/home/location/.secret_key');
|
||||||
|
$secretKey = trim((string) @file_get_contents($secretKeyFile)); // Access secret key (ensure it's set in .env)
|
||||||
if (!$secretKey) {
|
if (!$secretKey) {
|
||||||
error_log("SECRET_KEY not set in environment variables.");
|
error_log("SECRET_KEY not set in environment variables. Path checked: $secretKeyFile");
|
||||||
http_response_code(500); // Internal Server Error
|
http_response_code(500); // Internal Server Error
|
||||||
echo json_encode(['error' => 'Internal server configuration error.']);
|
echo json_encode(['error' => 'Internal server configuration error.']);
|
||||||
exit;
|
exit;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class AppLink {
|
|||||||
case 'Egypt':
|
case 'Egypt':
|
||||||
return 'https://location-egypt.siromove.com/siro/ride/location';
|
return 'https://location-egypt.siromove.com/siro/ride/location';
|
||||||
case 'Jordan':
|
case 'Jordan':
|
||||||
return 'https://jordan-siro.intaleqapp.com/backend/ride/location';
|
return 'https://jordan-siro.intaleqapp.com/loction_server/siro/ride/location';
|
||||||
default:
|
default:
|
||||||
return 'https://location-jordan.siromove.com/siro/ride/location';
|
return 'https://location-jordan.siromove.com/siro/ride/location';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user