데이터 사용량 계산을 통신사 과금 단위에 맞춤
This commit is contained in:
+36
-3
@@ -36,7 +36,8 @@ define('ALLOWED_IPS', array_values(array_filter(
|
||||
)));
|
||||
|
||||
const ALLOWED_CMD = ['se', 'ef', 'en', 'hn', 'hf', 'dl', 'du', 'tu'];
|
||||
const CONTROL_CMD = ['ef', 'en', 'hn', 'hf', 'dl', 'du', 'tu'];
|
||||
const CONTROL_CMD = ['ef', 'en', 'hn', 'hf', 'dl', 'du', 'tu'];
|
||||
const CARRIER_BILLING_UNIT_BYTES = 512;
|
||||
|
||||
const RAW_FULL_REGEX = '/(?<modem>\d{11})\/R:(?<r>[a-z])\/E:(?<E0>[io]{5}\d{3}[io])\/D:(?<D0>[oi]{7})\/L:(?<L0>o{5})\/F:(?<F0>[ots][oi]\d{4}[oi]{4})\/S:(?<S0>[^\/]+)/';
|
||||
|
||||
@@ -113,12 +114,14 @@ function db_insert_data_usage(
|
||||
?string $requestId = null
|
||||
): void {
|
||||
try {
|
||||
db_ensure_data_usage_schema($pdo);
|
||||
$carrierEstimatedBytes = carrier_billing_bytes($sentBytes + $receivedBytes);
|
||||
$stmt = $pdo->prepare("INSERT INTO car_data_usage (
|
||||
id, ts, source, cmd, sent_bytes, received_bytes, total_bytes,
|
||||
tcp_ok, tcp_error, request_id
|
||||
carrier_estimated_bytes, tcp_ok, tcp_error, request_id
|
||||
) VALUES (
|
||||
'car', :ts, :source, :cmd, :sent_bytes, :received_bytes, :total_bytes,
|
||||
:tcp_ok, :tcp_error, :request_id
|
||||
:carrier_estimated_bytes, :tcp_ok, :tcp_error, :request_id
|
||||
)");
|
||||
|
||||
$stmt->execute([
|
||||
@@ -128,6 +131,7 @@ function db_insert_data_usage(
|
||||
':sent_bytes' => max(0, $sentBytes),
|
||||
':received_bytes' => max(0, $receivedBytes),
|
||||
':total_bytes' => max(0, $sentBytes + $receivedBytes),
|
||||
':carrier_estimated_bytes' => $carrierEstimatedBytes,
|
||||
':tcp_ok' => $tcpOk ? 1 : 0,
|
||||
':tcp_error' => $tcpError !== '' ? substr($tcpError, 0, 100) : null,
|
||||
':request_id' => $requestId,
|
||||
@@ -137,6 +141,35 @@ function db_insert_data_usage(
|
||||
}
|
||||
}
|
||||
|
||||
function carrier_billing_bytes(int $payloadBytes): int
|
||||
{
|
||||
$payloadBytes = max(0, $payloadBytes);
|
||||
if ($payloadBytes <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int)(ceil($payloadBytes / CARRIER_BILLING_UNIT_BYTES) * CARRIER_BILLING_UNIT_BYTES);
|
||||
}
|
||||
|
||||
function db_ensure_data_usage_schema(PDO $pdo): void
|
||||
{
|
||||
static $done = false;
|
||||
if ($done) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = $pdo->query("SHOW COLUMNS FROM car_data_usage LIKE 'carrier_estimated_bytes'");
|
||||
if (!$stmt || !$stmt->fetch()) {
|
||||
$pdo->exec("ALTER TABLE car_data_usage ADD COLUMN carrier_estimated_bytes INT UNSIGNED NOT NULL DEFAULT 0 AFTER total_bytes");
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// 스키마 보강 실패가 차량 제어/조회 자체를 막으면 안 된다.
|
||||
}
|
||||
|
||||
$done = true;
|
||||
}
|
||||
|
||||
function record_tcp_usage(
|
||||
string $source,
|
||||
string $cmd,
|
||||
|
||||
Reference in New Issue
Block a user