Compare commits
4 Commits
dev
...
f5486f0469
| Author | SHA1 | Date | |
|---|---|---|---|
| f5486f0469 | |||
| 51fca8436a | |||
| c5f6fbf7af | |||
|
|
7b02884b10 |
@@ -4,6 +4,8 @@ from datetime import datetime
|
|||||||
import websockets
|
import websockets
|
||||||
from kivy import Logger
|
from kivy import Logger
|
||||||
from pydantic import BaseModel, field_validator
|
from pydantic import BaseModel, field_validator
|
||||||
|
from websockets.asyncio.connection import Connection
|
||||||
|
|
||||||
from config import STORE_HOST, STORE_PORT
|
from config import STORE_HOST, STORE_PORT
|
||||||
|
|
||||||
|
|
||||||
@@ -33,6 +35,7 @@ class ProcessedAgentData(BaseModel):
|
|||||||
|
|
||||||
class Datasource:
|
class Datasource:
|
||||||
def __init__(self, user_id: int):
|
def __init__(self, user_id: int):
|
||||||
|
self.websocket: Connection | None = None
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.user_id = user_id
|
self.user_id = user_id
|
||||||
self.connection_status = None
|
self.connection_status = None
|
||||||
@@ -52,6 +55,7 @@ class Datasource:
|
|||||||
Logger.debug("CONNECT TO SERVER")
|
Logger.debug("CONNECT TO SERVER")
|
||||||
async with websockets.connect(uri) as websocket:
|
async with websockets.connect(uri) as websocket:
|
||||||
self.connection_status = "Connected"
|
self.connection_status = "Connected"
|
||||||
|
self.websocket = websocket
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = await websocket.recv()
|
data = await websocket.recv()
|
||||||
@@ -61,8 +65,14 @@ 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:
|
async def update_db_records(self):
|
||||||
distances = tuple((abs(lon - marker.latitude) ** 2 + abs(lat - marker.longitude) ** 2) ** 0.5 for marker in self._active_markers)
|
if self.websocket:
|
||||||
|
data = json.dumps({"id": self.user_id})
|
||||||
|
await self.websocket.send(data, True)
|
||||||
|
|
||||||
|
def map_lat_lon_to_processed_agent_data(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:
|
if len(distances) == 0:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ class MapViewApp(App):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return next(flt)
|
return next(flt)
|
||||||
except StopIteration as e:
|
except StopIteration:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def set_pothole_marker(self, point):
|
def set_pothole_marker(self, point):
|
||||||
@@ -145,14 +145,14 @@ class MapViewApp(App):
|
|||||||
if lat is None or lon is None:
|
if lat is None or lon is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
clicked_marker_data = self.datasource.map_lat_lon_to_ProcessedAgentData(lat, lon)
|
clicked_marker_data = self.datasource.map_lat_lon_to_processed_agent_data(lat, lon)
|
||||||
|
|
||||||
if not clicked_marker_data:
|
if not clicked_marker_data:
|
||||||
return
|
return
|
||||||
|
|
||||||
clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude)
|
clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude)
|
||||||
|
|
||||||
if clicked_marker == None:
|
if clicked_marker is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.mapview.remove_marker(clicked_marker)
|
self.mapview.remove_marker(clicked_marker)
|
||||||
@@ -162,13 +162,14 @@ class MapViewApp(App):
|
|||||||
elif clicked_marker in self.bump_markers:
|
elif clicked_marker in self.bump_markers:
|
||||||
self.bump_markers.pop(self.bump_markers.index(clicked_marker))
|
self.bump_markers.pop(self.bump_markers.index(clicked_marker))
|
||||||
|
|
||||||
|
self.datasource.update_db_records()
|
||||||
|
|
||||||
def on_touch_down(self, widget, touch):
|
def on_touch_down(self, widget, touch):
|
||||||
if touch.button == "right":
|
if touch.button == "right":
|
||||||
coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom)
|
coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom)
|
||||||
self.delete_pothole_marker(coordinate)
|
self.delete_pothole_marker(coordinate)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def build(self):
|
def build(self):
|
||||||
"""
|
"""
|
||||||
Ініціалізує мапу MapView(zoom, lat, lon)
|
Ініціалізує мапу MapView(zoom, lat, lon)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def test_create_processed_agent_data():
|
|||||||
"user_id": 123,
|
"user_id": 123,
|
||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
"road_state": "normal",
|
"road_state": "good",
|
||||||
"agent_data": {
|
"agent_data": {
|
||||||
"user_id": 999,
|
"user_id": 999,
|
||||||
"accelerometer": {
|
"accelerometer": {
|
||||||
|
|||||||
Reference in New Issue
Block a user