Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 21f2428a7c | |||
| 809d3bedf9 | |||
| 694345e705 | |||
| 825ce5cb5c | |||
| 1fe7e3018f | |||
| 6e84ae35c2 | |||
| 254e6c8cd2 | |||
| d073243c67 | |||
| 0d364ddf61 | |||
| 05c94bda81 | |||
| 26230df612 | |||
| ae10e212cb | |||
| e2e68e8506 | |||
| 1375e6e4be | |||
| 154c5c3a78 |
@@ -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
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -7,5 +7,6 @@ CREATE TABLE processed_agent_data (
|
||||
z FLOAT,
|
||||
latitude FLOAT,
|
||||
longitude FLOAT,
|
||||
timestamp TIMESTAMP
|
||||
);
|
||||
timestamp TIMESTAMP,
|
||||
visible BOOLEAN
|
||||
);
|
||||
|
||||
+22
-2
@@ -8,12 +8,13 @@ from sqlalchemy import (
|
||||
Integer,
|
||||
String,
|
||||
Float,
|
||||
Boolean,
|
||||
DateTime,
|
||||
)
|
||||
from sqlalchemy.sql import select
|
||||
|
||||
from database import metadata, SessionLocal
|
||||
from schemas import ProcessedAgentData, ProcessedAgentDataInDB
|
||||
from schemas import ProcessedAgentData, ProcessedAgentDataInDB, WebSocketData
|
||||
|
||||
# FastAPI app setup
|
||||
app = FastAPI()
|
||||
@@ -30,6 +31,7 @@ processed_agent_data = Table(
|
||||
Column("latitude", Float),
|
||||
Column("longitude", Float),
|
||||
Column("timestamp", DateTime),
|
||||
Column("visible", Boolean),
|
||||
)
|
||||
|
||||
# WebSocket subscriptions
|
||||
@@ -57,7 +59,24 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
||||
|
||||
# receive forever
|
||||
while True:
|
||||
await websocket.receive_text()
|
||||
data = await websocket.receive_text()
|
||||
try:
|
||||
if (data):
|
||||
ws_data = WebSocketData.model_validate(json.loads(data))
|
||||
session = SessionLocal()
|
||||
update_query = (
|
||||
processed_agent_data.update()
|
||||
.where(processed_agent_data.c.id == ws_data.id)
|
||||
.values(visible=False)
|
||||
).returning(processed_agent_data)
|
||||
res = session.execute(update_query).fetchone()
|
||||
if (not res):
|
||||
session.rollback()
|
||||
raise Exception("Error while websocket PUT")
|
||||
session.commit()
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
except WebSocketDisconnect:
|
||||
subscriptions.remove(websocket)
|
||||
|
||||
@@ -81,6 +100,7 @@ def ProcessedAgentData_to_td(data: List[ProcessedAgentData]):
|
||||
"latitude": item.agent_data.gps.latitude,
|
||||
"longitude": item.agent_data.gps.longitude,
|
||||
"timestamp": item.agent_data.timestamp,
|
||||
"visible": True,
|
||||
}
|
||||
for item in data
|
||||
]
|
||||
|
||||
@@ -49,3 +49,7 @@ class AgentData(BaseModel):
|
||||
class ProcessedAgentData(BaseModel):
|
||||
road_state: str
|
||||
agent_data: AgentData
|
||||
|
||||
class WebSocketData(BaseModel):
|
||||
id: int
|
||||
|
||||
|
||||
Reference in New Issue
Block a user