Compare commits

..

7 Commits

Author SHA1 Message Date
hasslesstech 21f2428a7c [P] Improve marker removal architecture to allow for future extensions
Component testing / Hub testing (push) Successful in 22s
Component testing / Integration smoke testing (push) Has been cancelled
Component testing / Store testing (push) Has been cancelled
2026-03-26 21:11:02 +02:00
Rhinemann 809d3bedf9 [P] Add touch handling (untested). 2026-03-26 18:58:59 +02:00
Rhinemann 694345e705 Correct pothole deletion function 2026-03-26 18:58:59 +02:00
Rhinemann 825ce5cb5c Use lat lon validation 2026-03-26 18:58:59 +02:00
Rhinemann 1fe7e3018f Add lat lon to pothole mapper 2026-03-26 18:58:59 +02:00
Rhinemann 6e84ae35c2 Add type hints 2026-03-26 18:58:59 +02:00
Rhinemann 254e6c8cd2 [P] Add bump remove function 2026-03-26 18:58:59 +02:00
3 changed files with 75 additions and 31 deletions
+18
View File
@@ -37,6 +37,7 @@ class Datasource:
self.user_id = user_id
self.connection_status = None
self._new_points = []
self._active_markers = []
asyncio.ensure_future(self.connect_to_server())
def get_new_points(self):
@@ -60,6 +61,20 @@ class Datasource:
self.connection_status = "Disconnected"
Logger.debug("SERVER DISCONNECT")
def map_lat_lon_to_ProcessedAgentData(self, lat: float, lon: float) -> ProcessedAgentData | None:
distances = tuple((abs(lon - marker.latitude) ** 2 + abs(lat - marker.longitude) ** 2) ** 0.5 for marker in self._active_markers)
if len(distances) == 0:
return None
min_distance = min(distances)
marker = self._active_markers[distances.index(min_distance)]
if min_distance < 0.005:
return marker
else:
return None
def handle_received_data(self, data):
# Update your UI or perform actions with received data here
Logger.debug(f"Received data: {data}")
@@ -70,6 +85,9 @@ class Datasource:
],
key=lambda v: v.timestamp,
)
self._active_markers += [i for i in processed_agent_data_list if i.road_state != 'normal']
new_points = [
(
processed_agent_data.longitude,
+54 -17
View File
@@ -15,18 +15,31 @@ line_layer_colors = [
[1, 0, 1, 1],
]
def get_lat_lon(point: dict[str, float] | list[float] | tuple[float, float]) -> tuple[float, float] | None:
if isinstance(point, dict):
lat = point.get("lat")
lon = point.get("lon")
else:
lat, lon = point
if lat is None or lon is None:
return None
return lat, lon
class MapViewApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.mapview = None
self.mapview: MapView | None = None
self.datasource = Datasource(user_id=1)
self.line_layers = dict()
self.car_markers = dict()
# додати необхідні змінні
self.bump_markers = []
self.pothole_markers = []
self.bump_markers: list[MapMarker] = []
self.pothole_markers: list[MapMarker] = []
def on_start(self):
"""
@@ -91,13 +104,12 @@ class MapViewApp(App):
if user_id == config.TRACK_ID:
self.mapview.center_on(lat, lon)
def set_pothole_marker(self, point):
if isinstance(point, dict):
lat = point.get("lat")
lon = point.get("lon")
else:
lat, lon = point
def map_lat_lon_to_marker(self, lat: float, lon: float) -> MapMarker | None:
flt = filter(lambda marker: lon == marker.lat and lat == marker.lon, self.pothole_markers + self.bump_markers)
return next(flt)
def set_pothole_marker(self, point):
lat, lon = get_lat_lon(point)
if lat is None or lon is None:
return
@@ -111,24 +123,47 @@ class MapViewApp(App):
self.pothole_markers.append(marker)
def set_bump_marker(self, point):
if isinstance(point, dict):
lat = point.get("lat")
lon = point.get("lon")
else:
lat, lon = point
lat, lon = get_lat_lon(point)
if lat is None or lon is None:
return
marker = MapMarker(
lat=lat,
lon=lon,
source="images/bump.png"
source="images/bump.png"
)
self.mapview.add_marker(marker)
self.bump_markers.append(marker)
def delete_pothole_marker(self, point):
lat, lon = get_lat_lon(point)
if lat is None or lon is None:
return
clicked_marker_data = self.datasource.map_lat_lon_to_ProcessedAgentData(lat, lon)
if not clicked_marker_data:
return
clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude)
self.mapview.remove_marker(clicked_marker)
pothole_index = self.pothole_markers.index(clicked_marker)
bump_index = self.bump_markers.index(clicked_marker)
if pothole_marker >= 0:
self.pothole_markers.pop(pothole_index)
elif bump_index >= 0:
self.bump_markers.pop(bump_index)
def on_touch_down(self, widget, touch):
if touch.button == "right":
coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom)
self.delete_pothole_marker(coordinate)
return True
def build(self):
"""
@@ -141,6 +176,8 @@ class MapViewApp(App):
lon=30.5234
)
self.mapview.bind(on_touch_down = self.on_touch_down)
return self.mapview
+3 -14
View File
@@ -48,9 +48,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
try:
# send already available data
r = processed_agent_data.select().order_by(processed_agent_data.c.timestamp)
session = SessionLocal()
stored_data = session.execute(r).fetchall()
session.close()
stored_data = SessionLocal().execute(r).fetchall()
jsonable_data = [{c.name: getattr(i, c.name) for c in processed_agent_data.columns} for i in stored_data]
for i in jsonable_data:
@@ -200,12 +198,8 @@ def update_processed_agent_data(processed_agent_data_id: int, data: ProcessedAge
session.commit()
updated_result = session.execute(query).fetchone()
return ProcessedAgentDataInDB(**updated_result._mapping)
except Exception as err:
session.rollback()
print(f"Database error: {err}")
raise HTTPException(status_code=500, detail="Internal Server Error")
finally:
session.close()
@@ -236,12 +230,7 @@ def delete_processed_agent_data(processed_agent_data_id: int):
session.commit()
return ProcessedAgentDataInDB(**result._mapping)
except Exception as err:
session.rollback()
print(f"Database error: {err}")
raise HTTPException(status_code=500, detail="Internal Server Error")
finally:
session.close()