Prevent duplicate car status rows

This commit is contained in:
seo
2026-06-15 16:35:42 +09:00
parent 470355ebc9
commit 897dfe97b3
+32 -13
View File
@@ -494,7 +494,9 @@ function tcp_worker_process_job(array $job): void
$status = $uiTimedOut ? 'settled_late' : 'settled'; $status = $uiTimedOut ? 'settled_late' : 'settled';
$error = $uiTimedOut ? 'late_response_after_ui_timeout' : ''; $error = $uiTimedOut ? 'late_response_after_ui_timeout' : '';
tcp_queue_record_usage_and_status($pdo, $job, $status, $sentBytes, $receivedBytes, true, $error, $trimmed, $connectMs, $readMs); tcp_queue_record_usage_and_status($pdo, $job, $status, $sentBytes, $receivedBytes, true, $error, $trimmed, $connectMs, $readMs);
tcp_queue_maybe_store_status($pdo, $job, $trimmed); if ($uiTimedOut) {
tcp_queue_maybe_store_status($pdo, $job, $trimmed);
}
} }
function tcp_request_wait_for_queue( function tcp_request_wait_for_queue(
@@ -701,14 +703,14 @@ function is_valid_status_data(array $data): bool
return true; return true;
} }
function db_insert_status( function db_insert_status(
PDO $pdo, PDO $pdo,
string $cmd, string $cmd,
string $rawFull, string $rawFull,
string $rawTrim, string $rawTrim,
array $data array $data
): void { ): void {
$v = (float)($data['battery_voltage'] ?? 0); $v = (float)($data['battery_voltage'] ?? 0);
// 1V는 저장 금지 // 1V는 저장 금지
if ($v <= 1.0) { if ($v <= 1.0) {
@@ -723,11 +725,28 @@ function db_insert_status(
(int)$data['remote_start_preparing'] === 1 || (int)$data['remote_start_preparing'] === 1 ||
(int)$data['remote_start_running'] === 1; (int)$data['remote_start_running'] === 1;
if (!$engineOn && $v >= 14.2) { if (!$engineOn && $v >= 14.2) {
return; return;
} }
$sql = "INSERT INTO car_status ( $ts = date('Y-m-d H:i:s');
$dupStmt = $pdo->prepare(""
. "SELECT seq FROM car_status "
. "WHERE id='car' AND ts = :ts AND cmd <=> :cmd AND raw_trim <=> :raw_trim "
. "ORDER BY seq ASC LIMIT 1"
);
$dupStmt->execute([
':ts' => $ts,
':cmd' => $cmd,
':raw_trim' => $rawTrim,
]);
if ($dupStmt->fetchColumn()) {
return;
}
$sql = "INSERT INTO car_status (
id, ts, cmd, id, ts, cmd,
boundary, engine, driving, battery_voltage, boundary, engine, driving, battery_voltage,
door_fl, door_fr, door_rl, door_rr, door_trunk, door_fl, door_fr, door_rl, door_rr, door_trunk,
@@ -741,10 +760,10 @@ function db_insert_status(
:hazard, :raw_full, :raw_trim :hazard, :raw_full, :raw_trim
)"; )";
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$stmt->execute([ $stmt->execute([
':ts' => date('Y-m-d H:i:s'), ':ts' => $ts,
':cmd' => $cmd, ':cmd' => $cmd,
':boundary' => $data['boundary'], ':boundary' => $data['boundary'],
':engine' => $data['engine'], ':engine' => $data['engine'],