mirror of
https://github.com/Rhinemann/IoT-Systems.git
synced 2026-03-14 20:50:39 +02:00
Compare commits
24 Commits
fba2fdd241
...
6b5831ff1b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b5831ff1b | ||
|
|
54505db70e | ||
|
|
6f4b3b0ea6 | ||
|
|
948a936a1f | ||
|
|
87facff668 | ||
|
|
294ed5958e | ||
|
|
cbdf81c028 | ||
|
|
a98223cbb0 | ||
|
|
0bd3eaa91d | ||
|
|
9bdb98c19b | ||
|
|
c3b71daaeb | ||
|
|
31c760f4a8 | ||
|
|
24aeb1a19f | ||
|
|
4a81434cb6 | ||
|
|
a52da042ef | ||
|
|
11c590cf25 | ||
|
|
550d29c48c | ||
|
|
8a1327b10a | ||
|
|
db1b7cc6fc | ||
|
|
a899ef6a6e | ||
|
|
95176ea467 | ||
|
|
081a2d4240 | ||
|
|
92c20ef612 | ||
|
|
c31363aa57 |
1423
MapView/data.csv
1423
MapView/data.csv
File diff suppressed because one or more lines are too long
7
MapView/domain/accelerometer.py
Normal file
7
MapView/domain/accelerometer.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Accelerometer:
|
||||||
|
x: int
|
||||||
|
y: int
|
||||||
|
z: int
|
||||||
50
MapView/fileReader.py
Normal file
50
MapView/fileReader.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from csv import reader
|
||||||
|
import config
|
||||||
|
from domain.accelerometer import Accelerometer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class FileReader:
|
||||||
|
def __init__(
|
||||||
|
self, data_filename: str,
|
||||||
|
) -> None:
|
||||||
|
self.file_path = data_filename
|
||||||
|
pass
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
return self.getNextValue()
|
||||||
|
|
||||||
|
def startReading(self, *args, **kwargs):
|
||||||
|
self.file = open(self.file_path, newline='')
|
||||||
|
self.file_reader = reader(self.file, skipinitialspace=True)
|
||||||
|
file_header = next(self.file_reader)
|
||||||
|
|
||||||
|
self.x_idx = file_header.index('X')
|
||||||
|
self.y_idx = file_header.index('Y')
|
||||||
|
self.z_idx = file_header.index('Z')
|
||||||
|
|
||||||
|
def getNextValue(self):
|
||||||
|
while True:
|
||||||
|
row = next(self.file_reader, None)
|
||||||
|
if row is None:
|
||||||
|
self._rewind_file()
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
x = int(row[self.x_idx])
|
||||||
|
y = int(row[self.y_idx])
|
||||||
|
z = int(row[self.z_idx])
|
||||||
|
return Accelerometer(x=x, y=y, z=z)
|
||||||
|
except Exception as e:
|
||||||
|
continue
|
||||||
|
|
||||||
|
def _rewind_file(self):
|
||||||
|
self.file.seek(0)
|
||||||
|
self.file_reader = reader(self.file)
|
||||||
|
next(self.file_reader)
|
||||||
|
|
||||||
|
def stopReading(self, *args, **kwargs):
|
||||||
|
if self.file:
|
||||||
|
self.file.close()
|
||||||
|
self.file_reader = None
|
||||||
|
|
||||||
|
|
||||||
@ -8,9 +8,17 @@ from datasource import Datasource
|
|||||||
|
|
||||||
class MapViewApp(App):
|
class MapViewApp(App):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__()
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
self.mapview = None
|
||||||
|
self.datasource = Datasource(user_id=1)
|
||||||
|
self.line_layer = None
|
||||||
|
self.car_marker = None
|
||||||
|
|
||||||
# додати необхідні змінні
|
# додати необхідні змінні
|
||||||
self.bump_markers = []
|
self.bump_markers = []
|
||||||
|
self.pothole_markers = []
|
||||||
|
|
||||||
def on_start(self):
|
def on_start(self):
|
||||||
"""
|
"""
|
||||||
Встановлює необхідні маркери, викликає функцію для оновлення мапи
|
Встановлює необхідні маркери, викликає функцію для оновлення мапи
|
||||||
@ -21,19 +29,73 @@ class MapViewApp(App):
|
|||||||
"""
|
"""
|
||||||
Викликається регулярно для оновлення мапи
|
Викликається регулярно для оновлення мапи
|
||||||
"""
|
"""
|
||||||
|
new_points = self.datasource.get_new_points()
|
||||||
|
|
||||||
|
if not new_points:
|
||||||
|
return
|
||||||
|
|
||||||
|
for point in new_points:
|
||||||
|
|
||||||
|
lat, lon, road_state = point
|
||||||
|
|
||||||
|
# Оновлює лінію маршрута
|
||||||
|
self.line_layer.add_point((lat, lon))
|
||||||
|
|
||||||
|
# Оновлює маркер маниши
|
||||||
|
self.update_car_marker((lat, lon))
|
||||||
|
|
||||||
|
# Перевіряємо стан дороги
|
||||||
|
self.check_road_quality(point)
|
||||||
|
|
||||||
|
def check_road_quality(self, point):
|
||||||
|
"""
|
||||||
|
Аналізує дані акселерометра для подальшого визначення
|
||||||
|
та відображення ям та лежачих поліцейських
|
||||||
|
"""
|
||||||
|
if len(point) < 3:
|
||||||
|
return
|
||||||
|
|
||||||
|
lat, lon, road_state = point
|
||||||
|
|
||||||
|
if road_state == "pothole":
|
||||||
|
self.set_pothole_marker((lat, lon))
|
||||||
|
elif road_state == "bump":
|
||||||
|
self.set_bump_marker((lat, lon))
|
||||||
|
|
||||||
def update_car_marker(self, point):
|
def update_car_marker(self, point):
|
||||||
"""
|
"""
|
||||||
Оновлює відображення маркера машини на мапі
|
Оновлює відображення маркера машини на мапі
|
||||||
:param point: GPS координати
|
:param point: GPS координати
|
||||||
"""
|
"""
|
||||||
|
lat, lon = point[0], point[1]
|
||||||
|
|
||||||
|
if not hasattr(self, 'car_marker'):
|
||||||
|
self.car_marker = MapMarker(lat=lat, lon=lon, source='./images/car')
|
||||||
|
self.mapview.add_marker(self.car_marker)
|
||||||
|
else:
|
||||||
|
self.car_marker.lat = lat
|
||||||
|
self.car_marker.lon = lon
|
||||||
|
|
||||||
|
self.mapview.center_on(lat, lon)
|
||||||
|
|
||||||
def set_pothole_marker(self, point):
|
def set_pothole_marker(self, point):
|
||||||
"""
|
if isinstance(point, dict):
|
||||||
Встановлює маркер для ями
|
lat = point.get("lat")
|
||||||
:param point: GPS координати
|
lon = point.get("lon")
|
||||||
"""
|
else:
|
||||||
|
lat, lon = point
|
||||||
|
|
||||||
|
if lat is None or lon is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
marker = MapMarker(
|
||||||
|
lat=lat,
|
||||||
|
lon=lon,
|
||||||
|
source="images/pothole.png"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.mapview.add_marker(marker)
|
||||||
|
self.pothole_markers.append(marker)
|
||||||
|
|
||||||
def set_bump_marker(self, point):
|
def set_bump_marker(self, point):
|
||||||
if isinstance(point, dict):
|
if isinstance(point, dict):
|
||||||
@ -60,7 +122,15 @@ class MapViewApp(App):
|
|||||||
Ініціалізує мапу MapView(zoom, lat, lon)
|
Ініціалізує мапу MapView(zoom, lat, lon)
|
||||||
:return: мапу
|
:return: мапу
|
||||||
"""
|
"""
|
||||||
self.mapview = MapView()
|
self.mapview = MapView(
|
||||||
|
zoom=15,
|
||||||
|
lat=50.4501,
|
||||||
|
lon=30.5234
|
||||||
|
)
|
||||||
|
|
||||||
|
self.line_layer = LineMapLayer()
|
||||||
|
self.mapview.add_layer(self.line_layer)
|
||||||
|
|
||||||
return self.mapview
|
return self.mapview
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -14,11 +14,30 @@ class StoreApiAdapter(StoreGateway):
|
|||||||
self.api_base_url = api_base_url
|
self.api_base_url = api_base_url
|
||||||
|
|
||||||
def save_data(self, processed_agent_data_batch: List[ProcessedAgentData]):
|
def save_data(self, processed_agent_data_batch: List[ProcessedAgentData]):
|
||||||
"""
|
if not processed_agent_data_batch:
|
||||||
Save the processed road data to the Store API.
|
return False
|
||||||
Parameters:
|
|
||||||
processed_agent_data_batch (dict): Processed road data to be saved.
|
# Extract user_id from the first element
|
||||||
Returns:
|
user_id = processed_agent_data_batch[0].agent_data.user_id
|
||||||
bool: True if the data is successfully saved, False otherwise.
|
|
||||||
"""
|
payload = {
|
||||||
# Implement it
|
"data": [item.model_dump(mode='json') for item in processed_agent_data_batch],
|
||||||
|
"user_id": user_id
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Perform a POST request to the Store API with a 10-second timeout
|
||||||
|
response = requests.post(
|
||||||
|
f"{self.api_base_url}/processed_agent_data/",
|
||||||
|
json=payload,
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
if response.status_code == 200:
|
||||||
|
logging.info(f"Batch of {len(processed_agent_data_batch)} items sent to Store.")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logging.error(f"Store API error: {response.status_code} - {response.text}")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to send data to Store: {e}")
|
||||||
|
return False
|
||||||
|
|||||||
@ -93,7 +93,7 @@ services:
|
|||||||
MQTT_BROKER_HOST: "mqtt"
|
MQTT_BROKER_HOST: "mqtt"
|
||||||
MQTT_BROKER_PORT: 1883
|
MQTT_BROKER_PORT: 1883
|
||||||
MQTT_TOPIC: "processed_data_topic"
|
MQTT_TOPIC: "processed_data_topic"
|
||||||
BATCH_SIZE: 1
|
BATCH_SIZE: 20
|
||||||
ports:
|
ports:
|
||||||
- "9000:8000"
|
- "9000:8000"
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
16
hub/main.py
16
hub/main.py
@ -70,18 +70,20 @@ def on_message(client, userdata, msg):
|
|||||||
processed_agent_data = ProcessedAgentData.model_validate_json(
|
processed_agent_data = ProcessedAgentData.model_validate_json(
|
||||||
payload, strict=True
|
payload, strict=True
|
||||||
)
|
)
|
||||||
|
|
||||||
redis_client.lpush(
|
redis_client.lpush(
|
||||||
"processed_agent_data", processed_agent_data.model_dump_json()
|
"processed_agent_data", processed_agent_data.model_dump_json()
|
||||||
)
|
)
|
||||||
processed_agent_data_batch: List[ProcessedAgentData] = []
|
|
||||||
if redis_client.llen("processed_agent_data") >= BATCH_SIZE:
|
if redis_client.llen("processed_agent_data") >= BATCH_SIZE:
|
||||||
|
processed_agent_data_batch: List[ProcessedAgentData] = []
|
||||||
for _ in range(BATCH_SIZE):
|
for _ in range(BATCH_SIZE):
|
||||||
processed_agent_data = ProcessedAgentData.model_validate_json(
|
raw_data = redis_client.lpop("processed_agent_data")
|
||||||
redis_client.lpop("processed_agent_data")
|
if raw_data:
|
||||||
)
|
data_item = ProcessedAgentData.model_validate_json(raw_data)
|
||||||
processed_agent_data_batch.append(processed_agent_data)
|
processed_agent_data_batch.append(data_item)
|
||||||
store_adapter.save_data(processed_agent_data_batch=processed_agent_data_batch)
|
|
||||||
|
store_adapter.save_data(processed_agent_data_batch=processed_agent_data_batch)
|
||||||
|
|
||||||
return {"status": "ok"}
|
return {"status": "ok"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"Error processing MQTT message: {e}")
|
logging.info(f"Error processing MQTT message: {e}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user