add access point on map

This commit is contained in:
timur 2026-08-31 21:31:28 +05:00
parent c3e54cdb50
commit b6b4be3018
16 changed files with 233 additions and 7 deletions

View File

@ -25,7 +25,7 @@ WIFI_SPOOL_POSTGRES_CONNECT_TIMEOUT=10
# Web heatmap service. Listen on all network interfaces for remote access.
WIFI_SPOOL_WEB_HOST=0.0.0.0
WIFI_SPOOL_WEB_PORT=8080
WIFI_SPOOL_WEB_DEFAULT_DATE_RANGE_MINUTES=60
WIFI_SPOOL_WEB_DEFAULT_DATE_RANGE_MINUTES=1440
# Initial map bounds shown in the web interface
WIFI_SPOOL_WEB_MAP_MIN_X=-150
WIFI_SPOOL_WEB_MAP_MAX_X=-70

View File

@ -52,6 +52,9 @@ python3 wifi_spool.py --stdin < messages.txt
## Структура БД
- `bssids`: уникальные BSSID (`id`, `bssid`);
- `access_point_coordinates`: координаты точки доступа (`bssid_id`, точные
`location_x`/`location_y`); для точки без известных координат оба значения
равны `NULL`;
- `agents`: уникальные агенты (`id`, `agent_name`);
- `scan_results`: ссылка `bssid_id`, целые `location_x`/`location_y`, целый
`signal_level`, ссылка `agent_id` и дата сбора `collected_at` с часовым поясом.
@ -62,6 +65,12 @@ python3 wifi_spool.py --stdin < messages.txt
строку измерения в `scan_results`. Имена агентов также не дублируются: в
`scan_results` хранится только числовой идентификатор агента.
При первом запуске после установки сервис загружает
`access_point_coordinates.csv` в таблицу `access_point_coordinates`. BSSID из
файла также добавляются в справочник `bssids`. Повторный запуск безопасен:
существующие координаты обновляются, дубликаты не создаются. Формат файла —
`bssid;x;y`; пустые `x` и `y` сохраняются как `NULL`.
Каждое исходное MQTT-сообщение с `message_kind = scan_results` дополнительно
сохраняется без изменений в отдельный файл вида
`scan_results-20260830T154059.123456Z-<id>.txt`. По умолчанию файлы находятся в

View File

@ -0,0 +1,27 @@
94:64:24:6b:c2:b0;-72.93;-27
94:64:24:5b:40:70;-125.45;29.17
48:2f:6b:7b:33:d0;-58.45;10.09
94:64:24:6c:28:a0;-58.45;94.62
94:64:24:6c:28:b0;-58.45;94.62
94:64:24:67:3c:d0;-58.45;44.2
94:64:24:67:1b:b0;-34.24;73.14
94:64:24:67:1d:50;-72.93;59.36
94:64:24:5c:f8:90;-62.79;73.14
f0:61:c0:86:10:30;-58.45;94.62
94:64:24:67:86:30;-39.31;11.65
94:64:24:67:1b:70;-49.88;32.56
f0:61:c0:86:f8:b0;-62.79;45.58
94:64:24:67:5a:30;-39.31;45.58
48:2f:6b:7a:63:b0;-125.45;73.14
48:2f:6b:7a:eb:70;-105.63;73.14
48:2f:6b:79:0d:51;-99.19;100.17
48:2f:6b:7a:2a:f1;-42.7;100.17
48:2f:6b:78:b0:f0;-83.67;73.14
f0:61:c0:87:20:b0;-115.36;100.17
f0:61:c0:86:01:90;-72.93;92.34
94:64:24:6c:28:b1;-58.45;94.62
f0:61:c0:85:95:10;-58.45;29.17
f0:61:c0:86:7d:30;-58.45;29.17
94:64:24:67:60:30;;
48:2f:6b:79:0d:50;-99.19;100.17
48:2f:6b:7a:2a:f0;-42.7;100.17
1 94:64:24:6b:c2:b0 -72.93 -27
2 94:64:24:5b:40:70 -125.45 29.17
3 48:2f:6b:7b:33:d0 -58.45 10.09
4 94:64:24:6c:28:a0 -58.45 94.62
5 94:64:24:6c:28:b0 -58.45 94.62
6 94:64:24:67:3c:d0 -58.45 44.2
7 94:64:24:67:1b:b0 -34.24 73.14
8 94:64:24:67:1d:50 -72.93 59.36
9 94:64:24:5c:f8:90 -62.79 73.14
10 f0:61:c0:86:10:30 -58.45 94.62
11 94:64:24:67:86:30 -39.31 11.65
12 94:64:24:67:1b:70 -49.88 32.56
13 f0:61:c0:86:f8:b0 -62.79 45.58
14 94:64:24:67:5a:30 -39.31 45.58
15 48:2f:6b:7a:63:b0 -125.45 73.14
16 48:2f:6b:7a:eb:70 -105.63 73.14
17 48:2f:6b:79:0d:51 -99.19 100.17
18 48:2f:6b:7a:2a:f1 -42.7 100.17
19 48:2f:6b:78:b0:f0 -83.67 73.14
20 f0:61:c0:87:20:b0 -115.36 100.17
21 f0:61:c0:86:01:90 -72.93 92.34
22 94:64:24:6c:28:b1 -58.45 94.62
23 f0:61:c0:85:95:10 -58.45 29.17
24 f0:61:c0:86:7d:30 -58.45 29.17
25 94:64:24:67:60:30
26 48:2f:6b:79:0d:50 -99.19 100.17
27 48:2f:6b:7a:2a:f0 -42.7 100.17

View File

@ -49,6 +49,7 @@ install -o root -g root -m 0644 "$SOURCE_DIR/wifi_spool.py" "$APP_DIR/wifi_spool
install -o root -g root -m 0644 "$SOURCE_DIR/settings.py" "$APP_DIR/settings.py"
install -o root -g root -m 0644 "$SOURCE_DIR/web_app.py" "$APP_DIR/web_app.py"
install -o root -g root -m 0644 "$SOURCE_DIR/gunicorn.conf.py" "$APP_DIR/gunicorn.conf.py"
install -o root -g root -m 0644 "$SOURCE_DIR/access_point_coordinates.csv" "$APP_DIR/access_point_coordinates.csv"
install -o root -g root -m 0644 "$SOURCE_DIR/templates/dashboard.html" "$APP_DIR/templates/dashboard.html"
install -o root -g root -m 0644 "$SOURCE_DIR/static/dashboard.css" "$APP_DIR/static/dashboard.css"
install -o root -g root -m 0644 "$SOURCE_DIR/static/dashboard.js" "$APP_DIR/static/dashboard.js"

1
doc/.~lock.BSSid.xlsx# Normal file
View File

@ -0,0 +1 @@
,timur,timur-MS-7E26,31.08.2026 20:09,file:///home/timur/.config/libreoffice/4;

View File

@ -0,0 +1 @@
,timur,timur-MS-7E26,31.08.2026 20:28,file:///home/timur/.config/libreoffice/4;

BIN
doc/BSSid.xlsx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -36,6 +36,7 @@ canvas { display: block; width: 100%; min-height: 420px; }
.map-tooltip { position: absolute; z-index: 3; pointer-events: none; padding: 7px 9px; border: 1px solid #53779c; border-radius: 6px; background: #07101ded; color: #edf5ff; font-size: 12px; white-space: nowrap; box-shadow: 0 5px 16px #0008; }
.legend { position: absolute; right: 16px; bottom: 14px; display: flex; overflow: hidden; font-size: 11px; background: #07101dcc; padding: 7px; border-radius: 6px; }
.legend span { padding: 5px 9px; color: #101722; font-weight: 700; }.legend .red { background:#ec402f; }.legend .yellow { background:#f3d93a; }.legend .green { background:#43d35c; }
.legend .access-point { color: #d8f3ff; background: #0d3854; }
.summary { display: flex; gap: 20px; color: #8fa3ba; font-size: 13px; }
.status { padding: 7px 11px; border-radius: 20px; background: #28384b; font-size: 12px; }.status.ok { background: #174b36; }.status.bad { background: #702d32; }
.error { color: #ff9a9f; margin: 0; font-size: 12px; }

View File

@ -1,5 +1,5 @@
const state = {
points: [], bounds: null, view: 'maximum', hitCells: [],
points: [], accessPoints: [], bounds: null, view: 'maximum', hitCells: [], hitAccessPoints: [],
bssids: [], agents: [],
filters: { bssids: { loaded: false, error: '' }, agents: { loaded: false, error: '' } }
};
@ -83,7 +83,7 @@ async function loadHeatmap() {
const response = await fetch('/api/heatmap?' + params);
const payload = await response.json();
if (!response.ok) throw new Error(payload.error || 'Ошибка загрузки данных');
state.points = payload.points; state.bounds = payload.bounds;
state.points = payload.points; state.accessPoints = payload.access_points || []; state.bounds = payload.bounds;
$('selected-period').textContent = `${dateFrom.toLocaleString()} — ${dateTo.toLocaleString()}`;
setStatus('Подключено', true); showError(''); draw();
} catch (error) { setStatus('Нет связи', false); showError(error.message); }
@ -103,9 +103,10 @@ function switchView(view) {
function draw() {
resizeCanvas(); ctx.clearRect(0, 0, canvas.width, canvas.height);
state.hitCells = [];
const points = state.points; $('empty').hidden = Boolean(points.length);
if (!points.length || !state.bounds) { $('points-count').textContent = '0 точек'; $('samples-count').textContent = '0 измерений'; return; }
state.hitCells = []; state.hitAccessPoints = [];
const points = state.points, accessPoints = state.accessPoints;
$('empty').hidden = Boolean(points.length || accessPoints.length);
if ((!points.length && !accessPoints.length) || !state.bounds) { $('points-count').textContent = '0 точек'; $('samples-count').textContent = '0 измерений'; return; }
const {min_x: minX, max_x: maxX, min_y: minY, max_y: maxY} = state.bounds;
const padding = 55, width = canvas.width - padding * 2, height = canvas.height - padding * 2;
const rangeX = maxX - minX + 1, rangeY = maxY - minY + 1;
@ -119,6 +120,15 @@ function draw() {
state.hitCells.push({ x, y, size: Math.max(1, cellSize), point });
if (cellSize >= 5) { ctx.strokeStyle = '#07101d66'; ctx.strokeRect(x, y, cellSize, cellSize); }
});
accessPoints.forEach(point => {
const x = originX + (point.x - minX) * cellSize;
const y = originY + (maxY + 1 - point.y) * cellSize;
const radius = 5;
ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = '#31b9ff'; ctx.fill();
ctx.lineWidth = 2; ctx.strokeStyle = '#d8f3ff'; ctx.stroke();
state.hitAccessPoints.push({ x, y, radius: radius + 3, point });
});
$('points-count').textContent = `${points.length} точек`;
$('samples-count').textContent = `${points.reduce((sum, point) => sum + point.samples, 0)} измерений`;
}
@ -139,14 +149,25 @@ function showMapTooltip(event) {
const rect = canvas.getBoundingClientRect();
const canvasX = (event.clientX - rect.left) * canvas.width / rect.width;
const canvasY = (event.clientY - rect.top) * canvas.height / rect.height;
const accessPoint = state.hitAccessPoints.find(item =>
Math.hypot(canvasX - item.x, canvasY - item.y) <= item.radius
);
const cell = state.hitCells.find(item =>
canvasX >= item.x && canvasX <= item.x + item.size &&
canvasY >= item.y && canvasY <= item.y + item.size
);
const tooltip = $('map-tooltip');
if (accessPoint) {
tooltip.textContent = `Точка доступа ${accessPoint.point.bssid}, X: ${accessPoint.point.x}, Y: ${accessPoint.point.y}`;
positionMapTooltip(tooltip, event);
return;
}
if (!cell) { tooltip.hidden = true; return; }
const signal = Number(cell.point.signal);
tooltip.textContent = `X: ${cell.point.x}, Y: ${cell.point.y}, сигнал: ${signal.toFixed(state.view === 'maximum' ? 0 : 1)} dBm`;
positionMapTooltip(tooltip, event);
}
function positionMapTooltip(tooltip, event) {
tooltip.hidden = false;
const parentRect = canvas.parentElement.getBoundingClientRect();
const desiredX = event.clientX - parentRect.left + 12;

View File

@ -53,7 +53,7 @@
<canvas id="heatmap"></canvas>
<div id="empty" class="empty">За выбранный период данных нет</div>
<div id="map-tooltip" class="map-tooltip" hidden></div>
<div class="legend" aria-label="Шкала сигнала"><span class="red">&lt; −79</span><span class="yellow">−79…&lt; −70</span><span class="green">≥ −70 dBm</span></div>
<div class="legend" aria-label="Условные обозначения"><span class="red">&lt; −79</span><span class="yellow">−79…&lt; −70</span><span class="green">≥ −70 dBm</span><span class="access-point">● точка доступа</span></div>
</div>
<div class="summary">
<span id="selected-period">Период не выбран</span>

View File

@ -1,4 +1,5 @@
import unittest
from unittest.mock import patch
from settings import Settings
from web_app import create_app, database_text, parse_bssid_ids, parse_positive_ids
@ -67,6 +68,49 @@ class WebAppTest(unittest.TestCase):
def test_parses_agent_filters(self):
self.assertEqual(parse_positive_ids(["1,2", "2"], "agent", 100), [1, 2])
def test_heatmap_returns_filtered_access_point_coordinates(self):
class Result:
def __init__(self, rows):
self.rows = rows
def fetchall(self):
return self.rows
class Connection:
def __init__(self):
self.calls = []
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def execute(self, query, parameters):
self.calls.append((query, parameters))
if "access_point_coordinates" in query:
return Result([(7, "aa:bb:cc:dd:ee:ff", -72.93, 29.17)])
return Result([])
connection = Connection()
with patch("web_app.connect", return_value=connection):
response = self.client.get(
"/api/heatmap",
query_string={
"from": "2026-08-29T11:00:00+05:00",
"to": "2026-08-29T12:00:00+05:00",
"bssid": "7",
},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get_json()["access_points"],
[{"id": 7, "bssid": "aa:bb:cc:dd:ee:ff", "x": -72.93, "y": 29.17}],
)
coordinate_call = connection.calls[1]
self.assertIn("coordinates.bssid_id = ANY(%s)", coordinate_call[0])
self.assertEqual(coordinate_call[1][-1], [7])
if __name__ == "__main__":
unittest.main()

View File

@ -1,6 +1,7 @@
import json
import tempfile
import unittest
from decimal import Decimal
from pathlib import Path
from settings import SETTINGS, Settings
@ -8,6 +9,7 @@ from wifi_spool import (
SCHEMA,
RawScanArchive,
is_scan_results_message,
load_access_point_coordinates,
mosquitto_command,
normalize_bssid,
parse_message,
@ -70,6 +72,26 @@ class WifiSpoolTest(unittest.TestCase):
self.assertIn("agent_id BIGINT NOT NULL", SCHEMA)
self.assertIn("FOREIGN KEY (agent_id) REFERENCES agents(id)", SCHEMA)
def test_schema_contains_access_point_coordinates(self):
self.assertIn("CREATE TABLE IF NOT EXISTS access_point_coordinates", SCHEMA)
self.assertIn("location_x NUMERIC", SCHEMA)
self.assertIn("FOREIGN KEY (bssid_id) REFERENCES bssids(id)", SCHEMA)
def test_loads_access_point_coordinates_with_empty_pair(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "coordinates.csv"
path.write_text(
"AA:BB:CC:DD:EE:FF;-72.93;-27\n"
"11:22:33:44:55:66;;\n",
encoding="utf-8",
)
coordinates = load_access_point_coordinates(path)
self.assertEqual(coordinates[0].bssid, "aa:bb:cc:dd:ee:ff")
self.assertEqual(coordinates[0].location_x, Decimal("-72.93"))
self.assertEqual(coordinates[0].location_y, Decimal("-27"))
self.assertIsNone(coordinates[1].location_x)
self.assertIsNone(coordinates[1].location_y)
def test_schema_merges_existing_bssids_with_on_suffix(self):
self.assertIn("SET bssid_id = duplicates.canonical_id", SCHEMA)
self.assertIn("regexp_replace(bssid, '\\(on\\)?$'", SCHEMA)

View File

@ -148,6 +148,24 @@ def create_app(settings: Settings = SETTINGS) -> Flask:
"""
with connect(settings) as connection:
rows = connection.execute(query, parameters).fetchall()
access_point_rows: list[tuple[Any, ...]] = []
if aggregation in ("max", "avg"):
coordinate_parameters: list[Any] = [min_x, max_x, min_y, max_y]
coordinate_filter = ""
if bssid_ids:
coordinate_filter = "AND coordinates.bssid_id = ANY(%s)"
coordinate_parameters.append(bssid_ids)
access_point_rows = connection.execute(
f"""SELECT b.id, b.bssid,
coordinates.location_x, coordinates.location_y
FROM access_point_coordinates AS coordinates
JOIN bssids AS b ON b.id = coordinates.bssid_id
WHERE coordinates.location_x BETWEEN %s AND %s
AND coordinates.location_y BETWEEN %s AND %s
{coordinate_filter}
ORDER BY b.bssid""",
coordinate_parameters,
).fetchall()
points: list[dict[str, Any]] = []
bounds = {"min_x": min_x, "max_x": max_x, "min_y": min_y, "max_y": max_y}
@ -155,6 +173,15 @@ def create_app(settings: Settings = SETTINGS) -> Flask:
points.append(
{"x": x, "y": y, "signal": float(signal), "samples": samples}
)
access_points = [
{
"id": row[0],
"bssid": database_text(row[1]),
"x": float(row[2]),
"y": float(row[3]),
}
for row in access_point_rows
]
return jsonify(
date_from=date_from.isoformat(),
date_to=date_to.isoformat(),
@ -163,6 +190,7 @@ def create_app(settings: Settings = SETTINGS) -> Flask:
aggregation=aggregation,
bounds=bounds,
points=points,
access_points=access_points,
)
@app.errorhandler(psycopg.Error)

View File

@ -10,6 +10,7 @@ import subprocess
from collections.abc import Iterable, Iterator
from dataclasses import dataclass
from datetime import UTC, datetime
from decimal import Decimal, InvalidOperation
from pathlib import Path
from typing import Any
from uuid import uuid4
@ -17,6 +18,7 @@ from uuid import uuid4
from settings import SETTINGS, Settings
LOGGER = logging.getLogger("wifi_spool")
ACCESS_POINT_COORDINATES_FILE = Path(__file__).with_name("access_point_coordinates.csv")
SCHEMA_STATEMENTS = (
"""
@ -26,6 +28,15 @@ CREATE TABLE IF NOT EXISTS bssids (
)
""",
"""
CREATE TABLE IF NOT EXISTS access_point_coordinates (
bssid_id BIGINT PRIMARY KEY,
location_x NUMERIC,
location_y NUMERIC,
FOREIGN KEY (bssid_id) REFERENCES bssids(id),
CHECK ((location_x IS NULL) = (location_y IS NULL))
)
""",
"""
CREATE TABLE IF NOT EXISTS agents (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
agent_name TEXT NOT NULL UNIQUE
@ -137,6 +148,13 @@ class ScanResult:
agent_id: str
@dataclass(frozen=True)
class AccessPointCoordinate:
bssid: str
location_x: Decimal | None
location_y: Decimal | None
_BSSID_WITH_OPTIONAL_ON_SUFFIX = re.compile(
r"^((?:[0-9a-f]{2}:){5}[0-9a-f]{2})(?:\(on\)?)?$", re.IGNORECASE
)
@ -149,6 +167,39 @@ def normalize_bssid(value: Any) -> str:
return match.group(1).lower() if match else text
def load_access_point_coordinates(path: Path) -> list[AccessPointCoordinate]:
"""Read semicolon-separated BSSID coordinates shipped with the service."""
coordinates: list[AccessPointCoordinate] = []
seen: set[str] = set()
with path.open(encoding="utf-8") as source:
for line_number, raw_line in enumerate(source, 1):
line = raw_line.strip()
if not line:
continue
fields = [field.strip() for field in line.split(";")]
if len(fields) != 3:
raise ValueError(f"{path}:{line_number}: expected 3 fields")
bssid = normalize_bssid(fields[0])
if _BSSID_WITH_OPTIONAL_ON_SUFFIX.fullmatch(bssid) is None:
raise ValueError(f"{path}:{line_number}: invalid BSSID")
if bssid in seen:
raise ValueError(f"{path}:{line_number}: duplicate BSSID {bssid}")
if bool(fields[1]) != bool(fields[2]):
raise ValueError(
f"{path}:{line_number}: both coordinates must be set or empty"
)
try:
location_x = Decimal(fields[1]) if fields[1] else None
location_y = Decimal(fields[2]) if fields[2] else None
except InvalidOperation as error:
raise ValueError(
f"{path}:{line_number}: invalid coordinate"
) from error
coordinates.append(AccessPointCoordinate(bssid, location_x, location_y))
seen.add(bssid)
return coordinates
def _integer_location(value: Any) -> int:
"""Convert a numeric coordinate to the nearest integer."""
if isinstance(value, bool):
@ -246,6 +297,26 @@ class Repository:
with self.connection.transaction():
for statement in SCHEMA_STATEMENTS:
self.connection.execute(statement)
self._import_access_point_coordinates(ACCESS_POINT_COORDINATES_FILE)
def _import_access_point_coordinates(self, path: Path) -> None:
for coordinate in load_access_point_coordinates(path):
bssid_row = self.connection.execute(
"""INSERT INTO bssids (bssid) VALUES (%s)
ON CONFLICT (bssid) DO UPDATE SET bssid = EXCLUDED.bssid
RETURNING id""",
(coordinate.bssid,),
).fetchone()
if bssid_row is None: # pragma: no cover - database invariant
raise RuntimeError("failed to resolve coordinate bssid id")
self.connection.execute(
"""INSERT INTO access_point_coordinates
(bssid_id, location_x, location_y) VALUES (%s, %s, %s)
ON CONFLICT (bssid_id) DO UPDATE SET
location_x = EXCLUDED.location_x,
location_y = EXCLUDED.location_y""",
(bssid_row[0], coordinate.location_x, coordinate.location_y),
)
def close(self) -> None:
self.connection.close()