-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
232 lines (199 loc) · 6.9 KB
/
Copy pathstorage.py
File metadata and controls
232 lines (199 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
Storage layer for survey data.
A "survey" is a JSON file containing:
- the floor plan filename
- a list of measurement points, each with (x, y) pixel coords and a WifiSample
The file lives at data/survey.json by default. We keep it human-readable so
it can be inspected/edited by hand if needed.
"""
from __future__ import annotations
import json
import os
import time
import uuid
from dataclasses import dataclass, field, asdict
from typing import List, Optional
from scanner import WifiSample
@dataclass
class AccessPoint:
"""A user-placed WiFi access point location on the floor plan."""
id: str
x: int
y: int
name: str # user label, e.g. "TP-Link Living Room"
bssid: str = "" # optional: link to a measured BSSID
def to_dict(self):
return {
"id": self.id,
"x": self.x,
"y": self.y,
"name": self.name,
"bssid": self.bssid,
}
@classmethod
def from_dict(cls, d):
return cls(
id=d["id"],
x=d["x"],
y=d["y"],
name=d["name"],
bssid=d.get("bssid", ""),
)
@dataclass
class Room:
"""A named rectangular region on the floor plan."""
id: str
name: str
x1: int
y1: int
x2: int
y2: int
def to_dict(self):
return {"id": self.id, "name": self.name,
"x1": self.x1, "y1": self.y1, "x2": self.x2, "y2": self.y2}
@classmethod
def from_dict(cls, d):
return cls(id=d["id"], name=d["name"],
x1=d["x1"], y1=d["y1"], x2=d["x2"], y2=d["y2"])
@dataclass
class SurveyPoint:
"""A single measurement: where you clicked + what was measured."""
id: str
x: int # pixel coords on the floor plan
y: int
timestamp: float # unix epoch seconds
sample: WifiSample
all_samples: List[dict] = field(default_factory=list)
bssid_changed: bool = False
note: str = ""
download_mbps: Optional[float] = None
def to_dict(self):
return {
"id": self.id,
"x": self.x,
"y": self.y,
"timestamp": self.timestamp,
"sample": self.sample.to_dict(),
"all_samples": self.all_samples,
"bssid_changed": self.bssid_changed,
"note": self.note,
"download_mbps": self.download_mbps,
}
@classmethod
def from_dict(cls, d):
return cls(
id=d["id"],
x=d["x"],
y=d["y"],
timestamp=d["timestamp"],
sample=WifiSample(**d["sample"]),
all_samples=d.get("all_samples", []),
bssid_changed=d.get("bssid_changed", False),
note=d.get("note", ""),
download_mbps=d.get("download_mbps"),
)
@dataclass
class Survey:
"""A complete survey: a floor plan plus the points measured on it."""
floorplan: Optional[str] = None # filename (relative to data/)
points: List[SurveyPoint] = field(default_factory=list)
access_points: List[AccessPoint] = field(default_factory=list)
rooms: List[Room] = field(default_factory=list)
def add_point(self, x: int, y: int, sample: WifiSample,
all_samples=None, bssid_changed=False,
download_mbps=None) -> SurveyPoint:
point = SurveyPoint(
id=uuid.uuid4().hex[:8],
x=x,
y=y,
timestamp=time.time(),
sample=sample,
all_samples=all_samples or [],
bssid_changed=bssid_changed,
download_mbps=download_mbps,
)
self.points.append(point)
return point
def remove_point(self, point_id: str) -> bool:
before = len(self.points)
self.points = [p for p in self.points if p.id != point_id]
return len(self.points) < before
def clear_points(self):
self.points = []
def add_access_point(self, x: int, y: int, name: str, bssid: str = "") -> AccessPoint:
ap = AccessPoint(
id=uuid.uuid4().hex[:8],
x=x,
y=y,
name=name,
bssid=bssid,
)
self.access_points.append(ap)
return ap
def remove_access_point(self, ap_id: str) -> bool:
before = len(self.access_points)
self.access_points = [a for a in self.access_points if a.id != ap_id]
return len(self.access_points) < before
def add_room(self, name: str, x1: int, y1: int, x2: int, y2: int) -> Room:
room = Room(
id=uuid.uuid4().hex[:8],
name=name,
x1=min(x1, x2), y1=min(y1, y2),
x2=max(x1, x2), y2=max(y1, y2),
)
self.rooms.append(room)
return room
def remove_room(self, room_id: str) -> bool:
before = len(self.rooms)
self.rooms = [r for r in self.rooms if r.id != room_id]
return len(self.rooms) < before
def get_unique_bssids(self) -> List[dict]:
"""Return list of {bssid, ssid, count} for each AP we've seen."""
seen = {}
for p in self.points:
b = p.sample.bssid
if not b:
continue
if b not in seen:
seen[b] = {"bssid": b, "ssid": p.sample.ssid, "count": 0}
seen[b]["count"] += 1
return sorted(seen.values(), key=lambda x: -x["count"])
def to_dict(self):
return {
"floorplan": self.floorplan,
"points": [p.to_dict() for p in self.points],
"access_points": [a.to_dict() for a in self.access_points],
"rooms": [r.to_dict() for r in self.rooms],
}
@classmethod
def from_dict(cls, d):
return cls(
floorplan=d.get("floorplan"),
points=[SurveyPoint.from_dict(p) for p in d.get("points", [])],
access_points=[AccessPoint.from_dict(a) for a in d.get("access_points", [])],
rooms=[Room.from_dict(r) for r in d.get("rooms", [])],
)
class SurveyStorage:
"""Loads and saves a Survey from/to a JSON file on disk."""
def __init__(self, path: str):
self.path = path
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
def load(self) -> Survey:
if not os.path.exists(self.path):
return Survey()
try:
with open(self.path, "r") as f:
data = json.load(f)
return Survey.from_dict(data)
except (json.JSONDecodeError, KeyError) as e:
# Corrupt file - start fresh but don't lose the old data
backup = self.path + ".corrupt"
os.rename(self.path, backup)
print(f"WARNING: survey file was corrupt, backed up to {backup}: {e}")
return Survey()
def save(self, survey: Survey):
# Atomic write: write to .tmp then rename
tmp = self.path + ".tmp"
with open(tmp, "w") as f:
json.dump(survey.to_dict(), f, indent=2)
os.replace(tmp, self.path)