Reset observed WiFi session when client disappears
This commit is contained in:
+40
-15
@@ -1464,18 +1464,48 @@ function wifi_connected_time_missing(mixed $value): bool
|
||||
return $text === '' || $text === 'N/A' || $text === '-';
|
||||
}
|
||||
|
||||
function wifi_valid_mac(string $mac): bool
|
||||
{
|
||||
return preg_match('/^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$/', strtolower($mac)) === 1;
|
||||
}
|
||||
|
||||
function prune_observed_wifi_sessions(PDO $pdo, array $visibleMacs): void
|
||||
{
|
||||
$visibleMacs = array_values(array_unique(array_filter(
|
||||
array_map(static fn($mac) => strtolower((string)$mac), $visibleMacs),
|
||||
static fn($mac) => wifi_valid_mac($mac)
|
||||
)));
|
||||
|
||||
if ($visibleMacs === []) {
|
||||
$pdo->exec("DELETE FROM wifi_observed_sessions WHERE band = '5G'");
|
||||
return;
|
||||
}
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($visibleMacs), '?'));
|
||||
$stmt = $pdo->prepare("
|
||||
DELETE FROM wifi_observed_sessions
|
||||
WHERE band = '5G'
|
||||
AND mac NOT IN ($placeholders)
|
||||
");
|
||||
$stmt->execute($visibleMacs);
|
||||
}
|
||||
|
||||
function observed_wifi_duration_seconds(array $clients): array
|
||||
{
|
||||
$targets = [];
|
||||
$visible5gMacs = [];
|
||||
|
||||
foreach ($clients as $client) {
|
||||
$band = (string)($client['band'] ?? '');
|
||||
$mac = strtolower((string)($client['mac'] ?? ''));
|
||||
|
||||
if ($band !== '5G'
|
||||
|| !wifi_connected_time_missing($client['connected_time'] ?? null)
|
||||
|| !preg_match('/^[0-9a-f]{2}(?::[0-9a-f]{2}){5}$/', $mac)
|
||||
) {
|
||||
if ($band !== '5G' || !wifi_valid_mac($mac)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$visible5gMacs[] = $mac;
|
||||
|
||||
if (!wifi_connected_time_missing($client['connected_time'] ?? null)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1488,18 +1518,15 @@ function observed_wifi_duration_seconds(array $clients): array
|
||||
];
|
||||
}
|
||||
|
||||
if ($targets === []) {
|
||||
try {
|
||||
db()->exec("DELETE FROM wifi_observed_sessions WHERE last_seen_at < DATE_SUB(NOW(3), INTERVAL 2 MINUTE)");
|
||||
} catch (Throwable) {
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo = db();
|
||||
$pdo->beginTransaction();
|
||||
prune_observed_wifi_sessions($pdo, $visible5gMacs);
|
||||
|
||||
if ($targets === []) {
|
||||
$pdo->commit();
|
||||
return [];
|
||||
}
|
||||
|
||||
$upsert = $pdo->prepare("
|
||||
INSERT INTO wifi_observed_sessions
|
||||
@@ -1524,8 +1551,6 @@ function observed_wifi_duration_seconds(array $clients): array
|
||||
]);
|
||||
}
|
||||
|
||||
$pdo->exec("DELETE FROM wifi_observed_sessions WHERE last_seen_at < DATE_SUB(NOW(3), INTERVAL 2 MINUTE)");
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($targets), '?'));
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT
|
||||
|
||||
Reference in New Issue
Block a user