diff --git a/src/vendor_service.py b/src/vendor_service.py index 18074df..618a6d7 100644 --- a/src/vendor_service.py +++ b/src/vendor_service.py @@ -1225,6 +1225,232 @@ class VendorService: "Invalid WiFi database schema" ) + # ============================================================ + # Recalculate access point vendors + # ============================================================ + + def recalculate_ap_vendors( + self, + progress_callback=None + ): + + def progress( + stage, + message=None + ): + + if progress_callback is not None: + + progress_callback( + stage, + None, + message + ) + + if not self.vendor_database.is_file(): + + raise FileNotFoundError( + f"OUI database not found: {self.vendor_database}" + ) + + if not self.wifi_database.is_file(): + + raise FileNotFoundError( + f"WiFi database not found: {self.wifi_database}" + ) + + oui_conn = None + wifi_conn = None + + try: + + oui_uri = ( + f"{self.vendor_database.resolve().as_uri()}" + "?mode=ro" + ) + + oui_conn = sqlite3.connect( + oui_uri, + uri=True, + timeout=30 + ) + + self._validate_schema( + oui_conn + ) + + wifi_conn = sqlite3.connect( + self.wifi_database, + timeout=30 + ) + + self._validate_wifi_schema( + wifi_conn + ) + + columns = { + row[1] + for row in wifi_conn.execute( + "PRAGMA table_info(access_points)" + ).fetchall() + } + + if "id" not in columns: + + raise ValueError( + "access_points.id column is missing" + ) + + wifi_conn.execute( + "BEGIN IMMEDIATE" + ) + + total = wifi_conn.execute( + """ + SELECT COUNT(*) + FROM access_points + """ + ).fetchone()[0] + + stats = { + "total": total, + "checked": 0, + "updated": 0, + "filled": 0, + "refreshed": 0, + "cleared": 0, + "unknown": 0, + "unchanged": 0 + } + + rows = wifi_conn.execute( + """ + SELECT + id, + bssid, + vendor + FROM access_points + ORDER BY id + """ + ).fetchall() + + placeholders = ( + None, + "", + "-", + "Unknown" + ) + + progress( + "recalculating", + f"Проверка точек доступа: {total}" + ) + + progress_interval = max( + 1, + total // 100 + ) + + for ap_id, bssid, current_vendor in rows: + + stats["checked"] += 1 + + new_vendor = self._lookup_vendor( + oui_conn, + bssid + ) + + is_placeholder = ( + current_vendor in placeholders + ) + + if new_vendor is None: + + stats["unknown"] += 1 + + if is_placeholder: + + stats["unchanged"] += 1 + continue + + desired_vendor = None + + stats["cleared"] += 1 + + else: + + desired_vendor = new_vendor + + if current_vendor == desired_vendor: + + stats["unchanged"] += 1 + continue + + if is_placeholder: + + stats["filled"] += 1 + + else: + + stats["refreshed"] += 1 + + wifi_conn.execute( + """ + UPDATE access_points + SET vendor=? + WHERE id=? + """, + ( + desired_vendor, + ap_id + ) + ) + + stats["updated"] += 1 + + if ( + stats["checked"] % progress_interval == 0 + or stats["checked"] == total + ): + + progress( + "recalculating", + ( + f"Проверено {stats['checked']} " + f"из {total}" + ) + ) + + progress( + "committing", + "Сохранение результатов" + ) + + wifi_conn.commit() + + progress( + "ready", + "Перерасчёт завершён" + ) + + return stats + + except Exception: + + if wifi_conn is not None: + + wifi_conn.rollback() + + raise + + finally: + + if wifi_conn is not None: + wifi_conn.close() + + if oui_conn is not None: + oui_conn.close() + # ============================================================ # Apply # ============================================================