'POST only']); exit; } $reqKey = $_SERVER['HTTP_X_REGISTRATION_KEY'] ?? ''; if ($reqKey !== NETSCAN_KEY) { http_response_code(401); echo json_encode(['error' => 'Unauthorized']); exit; } $body = file_get_contents('php://input'); $payload = json_decode($body, true); $devices = $payload['devices'] ?? []; if (empty($devices)) { echo json_encode(['error' => 'No devices in payload']); exit; } $discoveredIPs = []; $upserted = 0; foreach ($devices as $d) { $ip = trim($d['ip'] ?? ''); $mac = trim($d['mac'] ?? ''); $hostname = trim($d['hostname'] ?? ''); $vendor = trim($d['vendor'] ?? ''); if (!$ip) continue; $discoveredIPs[] = $ip; JarvisDB::execute( 'INSERT INTO network_devices (ip, mac, hostname, status, last_seen) VALUES (?,?,?,"online",NOW()) ON DUPLICATE KEY UPDATE mac = COALESCE(NULLIF(VALUES(mac),""), mac), hostname = COALESCE(NULLIF(VALUES(hostname),""), hostname), status = "online", last_seen = NOW()', [$ip, $mac ?: null, $hostname ?: $vendor ?: null] ); if ($vendor) { JarvisDB::execute( 'UPDATE network_devices SET device_type=? WHERE ip=? AND (device_type IS NULL OR device_type="")', [$vendor, $ip] ); } $upserted++; } // Mark anything NOT in this scan as offline if stale > 10 min if (!empty($discoveredIPs)) { $ph = implode(',', array_fill(0, count($discoveredIPs), '?')); JarvisDB::execute( "UPDATE network_devices SET status='offline' WHERE ip NOT IN ($ph) AND last_seen < DATE_SUB(NOW(), INTERVAL 10 MINUTE)", $discoveredIPs ); } echo json_encode(['ok' => true, 'upserted' => $upserted, 'total_discovered' => count($discoveredIPs)]);