Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eca98c4469 | |||
| c553384ce7 | |||
| 1bf5687505 | |||
| 0d9dcef994 |
@@ -37,7 +37,6 @@ class Datasource:
|
|||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
self.connection_status = None
|
self.connection_status = None
|
||||||
self._new_points = []
|
self._new_points = []
|
||||||
self._active_markers = []
|
|
||||||
asyncio.ensure_future(self.connect_to_server())
|
asyncio.ensure_future(self.connect_to_server())
|
||||||
|
|
||||||
def get_new_points(self):
|
def get_new_points(self):
|
||||||
@@ -61,20 +60,6 @@ class Datasource:
|
|||||||
self.connection_status = "Disconnected"
|
self.connection_status = "Disconnected"
|
||||||
Logger.debug("SERVER DISCONNECT")
|
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):
|
def handle_received_data(self, data):
|
||||||
# Update your UI or perform actions with received data here
|
# Update your UI or perform actions with received data here
|
||||||
Logger.debug(f"Received data: {data}")
|
Logger.debug(f"Received data: {data}")
|
||||||
@@ -85,9 +70,6 @@ class Datasource:
|
|||||||
],
|
],
|
||||||
key=lambda v: v.timestamp,
|
key=lambda v: v.timestamp,
|
||||||
)
|
)
|
||||||
|
|
||||||
self._active_markers += [i for i in processed_agent_data_list if i.road_state != 'normal']
|
|
||||||
|
|
||||||
new_points = [
|
new_points = [
|
||||||
(
|
(
|
||||||
processed_agent_data.longitude,
|
processed_agent_data.longitude,
|
||||||
|
|||||||
+15
-52
@@ -15,31 +15,18 @@ line_layer_colors = [
|
|||||||
[1, 0, 1, 1],
|
[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):
|
class MapViewApp(App):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.mapview: MapView | None = None
|
self.mapview = None
|
||||||
self.datasource = Datasource(user_id=1)
|
self.datasource = Datasource(user_id=1)
|
||||||
self.line_layers = dict()
|
self.line_layers = dict()
|
||||||
self.car_markers = dict()
|
self.car_markers = dict()
|
||||||
|
|
||||||
# додати необхідні змінні
|
# додати необхідні змінні
|
||||||
self.bump_markers: list[MapMarker] = []
|
self.bump_markers = []
|
||||||
self.pothole_markers: list[MapMarker] = []
|
self.pothole_markers = []
|
||||||
|
|
||||||
def on_start(self):
|
def on_start(self):
|
||||||
"""
|
"""
|
||||||
@@ -104,12 +91,13 @@ class MapViewApp(App):
|
|||||||
if user_id == config.TRACK_ID:
|
if user_id == config.TRACK_ID:
|
||||||
self.mapview.center_on(lat, lon)
|
self.mapview.center_on(lat, lon)
|
||||||
|
|
||||||
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):
|
def set_pothole_marker(self, point):
|
||||||
lat, lon = get_lat_lon(point)
|
if isinstance(point, dict):
|
||||||
|
lat = point.get("lat")
|
||||||
|
lon = point.get("lon")
|
||||||
|
else:
|
||||||
|
lat, lon = point
|
||||||
|
|
||||||
if lat is None or lon is None:
|
if lat is None or lon is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -123,7 +111,12 @@ class MapViewApp(App):
|
|||||||
self.pothole_markers.append(marker)
|
self.pothole_markers.append(marker)
|
||||||
|
|
||||||
def set_bump_marker(self, point):
|
def set_bump_marker(self, point):
|
||||||
lat, lon = get_lat_lon(point)
|
if isinstance(point, dict):
|
||||||
|
lat = point.get("lat")
|
||||||
|
lon = point.get("lon")
|
||||||
|
else:
|
||||||
|
lat, lon = point
|
||||||
|
|
||||||
if lat is None or lon is None:
|
if lat is None or lon is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -136,34 +129,6 @@ class MapViewApp(App):
|
|||||||
self.mapview.add_marker(marker)
|
self.mapview.add_marker(marker)
|
||||||
self.bump_markers.append(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):
|
def build(self):
|
||||||
"""
|
"""
|
||||||
@@ -176,8 +141,6 @@ class MapViewApp(App):
|
|||||||
lon=30.5234
|
lon=30.5234
|
||||||
)
|
)
|
||||||
|
|
||||||
self.mapview.bind(on_touch_down = self.on_touch_down)
|
|
||||||
|
|
||||||
return self.mapview
|
return self.mapview
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+13
-2
@@ -48,7 +48,9 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
|||||||
try:
|
try:
|
||||||
# send already available data
|
# send already available data
|
||||||
r = processed_agent_data.select().order_by(processed_agent_data.c.timestamp)
|
r = processed_agent_data.select().order_by(processed_agent_data.c.timestamp)
|
||||||
stored_data = SessionLocal().execute(r).fetchall()
|
session = SessionLocal()
|
||||||
|
stored_data = session.execute(r).fetchall()
|
||||||
|
session.close()
|
||||||
|
|
||||||
jsonable_data = [{c.name: getattr(i, c.name) for c in processed_agent_data.columns} for i in stored_data]
|
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:
|
for i in jsonable_data:
|
||||||
@@ -198,9 +200,13 @@ def update_processed_agent_data(processed_agent_data_id: int, data: ProcessedAge
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
updated_result = session.execute(query).fetchone()
|
updated_result = session.execute(query).fetchone()
|
||||||
|
|
||||||
return ProcessedAgentDataInDB(**updated_result._mapping)
|
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:
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
@@ -231,6 +237,11 @@ def delete_processed_agent_data(processed_agent_data_id: int):
|
|||||||
|
|
||||||
return ProcessedAgentDataInDB(**result._mapping)
|
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:
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user