diff --git a/common.php b/common.php index 9744535..ee2c985 100644 --- a/common.php +++ b/common.php @@ -494,7 +494,9 @@ function tcp_worker_process_job(array $job): void $status = $uiTimedOut ? 'settled_late' : 'settled'; $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_maybe_store_status($pdo, $job, $trimmed); + if ($uiTimedOut) { + tcp_queue_maybe_store_status($pdo, $job, $trimmed); + } } function tcp_request_wait_for_queue( @@ -701,14 +703,14 @@ function is_valid_status_data(array $data): bool return true; } -function db_insert_status( +function db_insert_status( PDO $pdo, string $cmd, string $rawFull, string $rawTrim, array $data -): void { - $v = (float)($data['battery_voltage'] ?? 0); +): void { + $v = (float)($data['battery_voltage'] ?? 0); // 1V는 저장 금지 if ($v <= 1.0) { @@ -723,11 +725,28 @@ function db_insert_status( (int)$data['remote_start_preparing'] === 1 || (int)$data['remote_start_running'] === 1; - if (!$engineOn && $v >= 14.2) { - return; - } - - $sql = "INSERT INTO car_status ( + if (!$engineOn && $v >= 14.2) { + return; + } + + $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, boundary, engine, driving, battery_voltage, door_fl, door_fr, door_rl, door_rr, door_trunk, @@ -741,10 +760,10 @@ function db_insert_status( :hazard, :raw_full, :raw_trim )"; - $stmt = $pdo->prepare($sql); - $stmt->execute([ - ':ts' => date('Y-m-d H:i:s'), - ':cmd' => $cmd, + $stmt = $pdo->prepare($sql); + $stmt->execute([ + ':ts' => $ts, + ':cmd' => $cmd, ':boundary' => $data['boundary'], ':engine' => $data['engine'],