Compare commits
8 Commits
project/sh
...
92c91c2594
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92c91c2594 | ||
|
|
2e623c3a93 | ||
|
|
edd2360d88 | ||
|
|
8e9dcb50c1 | ||
|
|
e2c3cda458 | ||
|
|
780855c93f | ||
|
|
86d7b1aaf8 | ||
|
|
d627836e47 |
@@ -72,8 +72,8 @@ class Datasource:
|
|||||||
)
|
)
|
||||||
new_points = [
|
new_points = [
|
||||||
(
|
(
|
||||||
processed_agent_data.longitude,
|
|
||||||
processed_agent_data.latitude,
|
processed_agent_data.latitude,
|
||||||
|
processed_agent_data.longitude,
|
||||||
processed_agent_data.road_state,
|
processed_agent_data.road_state,
|
||||||
processed_agent_data.user_id
|
processed_agent_data.user_id
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -87,8 +87,7 @@ class MapViewApp(App):
|
|||||||
self.car_markers[user_id].lat = lat
|
self.car_markers[user_id].lat = lat
|
||||||
self.car_markers[user_id].lon = lon
|
self.car_markers[user_id].lon = lon
|
||||||
|
|
||||||
if user_id == 1:
|
self.mapview.center_on(lat, lon)
|
||||||
self.mapview.center_on(lat, lon)
|
|
||||||
|
|
||||||
def set_pothole_marker(self, point):
|
def set_pothole_marker(self, point):
|
||||||
if isinstance(point, dict):
|
if isinstance(point, dict):
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ def connect_mqtt(broker, port):
|
|||||||
print("Failed to connect {broker}:{port}, return code %d\n", rc)
|
print("Failed to connect {broker}:{port}, return code %d\n", rc)
|
||||||
exit(rc) # Stop execution
|
exit(rc) # Stop execution
|
||||||
|
|
||||||
logging.info(f"Acting as USER_ID = {config.USER_ID}")
|
|
||||||
|
|
||||||
client = mqtt_client.Client()
|
client = mqtt_client.Client()
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
client.connect(broker, port)
|
client.connect(broker, port)
|
||||||
@@ -31,14 +29,13 @@ def publish(client, topic, datasource):
|
|||||||
data = datasource.read()
|
data = datasource.read()
|
||||||
msg = AggregatedDataSchema().dumps(data)
|
msg = AggregatedDataSchema().dumps(data)
|
||||||
result = client.publish(topic, msg)
|
result = client.publish(topic, msg)
|
||||||
logging.debug(f"Published to {topic}: {msg[:50]}...")
|
logging.info(f"Published to {topic}: {msg[:50]}...")
|
||||||
status = result[0]
|
status = result[0]
|
||||||
if status != 0:
|
if status != 0:
|
||||||
logging.error(f"Failed to send message to topic {topic}")
|
print(f"Failed to send message to topic {topic}")
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
logging.basicConfig(level = logging.INFO)
|
|
||||||
# Prepare mqtt client
|
# Prepare mqtt client
|
||||||
client = connect_mqtt(config.MQTT_BROKER_HOST, config.MQTT_BROKER_PORT)
|
client = connect_mqtt(config.MQTT_BROKER_HOST, config.MQTT_BROKER_PORT)
|
||||||
# Prepare datasource
|
# Prepare datasource
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ CREATE TABLE processed_agent_data (
|
|||||||
z FLOAT,
|
z FLOAT,
|
||||||
latitude FLOAT,
|
latitude FLOAT,
|
||||||
longitude FLOAT,
|
longitude FLOAT,
|
||||||
timestamp TIMESTAMP
|
timestamp TIMESTAMP,
|
||||||
);
|
visible BOOLEAN
|
||||||
|
);
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ from sqlalchemy import (
|
|||||||
Integer,
|
Integer,
|
||||||
String,
|
String,
|
||||||
Float,
|
Float,
|
||||||
|
Boolean,
|
||||||
DateTime,
|
DateTime,
|
||||||
)
|
)
|
||||||
from sqlalchemy.sql import select
|
from sqlalchemy.sql import select
|
||||||
|
|
||||||
from database import metadata, SessionLocal
|
from database import metadata, SessionLocal
|
||||||
from schemas import ProcessedAgentData, ProcessedAgentDataInDB
|
from schemas import ProcessedAgentData, ProcessedAgentDataInDB, WebSocketData
|
||||||
|
|
||||||
# FastAPI app setup
|
# FastAPI app setup
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -30,6 +31,7 @@ processed_agent_data = Table(
|
|||||||
Column("latitude", Float),
|
Column("latitude", Float),
|
||||||
Column("longitude", Float),
|
Column("longitude", Float),
|
||||||
Column("timestamp", DateTime),
|
Column("timestamp", DateTime),
|
||||||
|
Column("visible", Boolean),
|
||||||
)
|
)
|
||||||
|
|
||||||
# WebSocket subscriptions
|
# WebSocket subscriptions
|
||||||
@@ -45,7 +47,7 @@ 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()
|
||||||
stored_data = SessionLocal().execute(r).fetchall()
|
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]
|
jsonable_data = [{c.name: getattr(i, c.name) for c in processed_agent_data.columns} for i in stored_data]
|
||||||
@@ -57,7 +59,24 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
|
|||||||
|
|
||||||
# receive forever
|
# receive forever
|
||||||
while True:
|
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:
|
except WebSocketDisconnect:
|
||||||
subscriptions.remove(websocket)
|
subscriptions.remove(websocket)
|
||||||
|
|
||||||
@@ -81,6 +100,7 @@ def ProcessedAgentData_to_td(data: List[ProcessedAgentData]):
|
|||||||
"latitude": item.agent_data.gps.latitude,
|
"latitude": item.agent_data.gps.latitude,
|
||||||
"longitude": item.agent_data.gps.longitude,
|
"longitude": item.agent_data.gps.longitude,
|
||||||
"timestamp": item.agent_data.timestamp,
|
"timestamp": item.agent_data.timestamp,
|
||||||
|
"visible": True,
|
||||||
}
|
}
|
||||||
for item in data
|
for item in data
|
||||||
]
|
]
|
||||||
@@ -96,7 +116,7 @@ async def create_processed_agent_data(data: List[ProcessedAgentData], user_id: i
|
|||||||
created_records = [dict(row._mapping) for row in result.fetchall()]
|
created_records = [dict(row._mapping) for row in result.fetchall()]
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
for record in sorted(created_records, key = lambda x: x['timestamp']):
|
for record in created_records:
|
||||||
await send_data_to_subscribers(jsonable_encoder(record))
|
await send_data_to_subscribers(jsonable_encoder(record))
|
||||||
return created_records
|
return created_records
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
|||||||
@@ -49,3 +49,7 @@ class AgentData(BaseModel):
|
|||||||
class ProcessedAgentData(BaseModel):
|
class ProcessedAgentData(BaseModel):
|
||||||
road_state: str
|
road_state: str
|
||||||
agent_data: AgentData
|
agent_data: AgentData
|
||||||
|
|
||||||
|
class WebSocketData(BaseModel):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
|
||||||
|
|
||||||
print("Checking for dead containers...")
|
print("Checking for dead containers...")
|
||||||
|
|
||||||
@@ -15,9 +14,6 @@ for i in statuses:
|
|||||||
if not i[status_index:].startswith("Up "):
|
if not i[status_index:].startswith("Up "):
|
||||||
service_name = i[name_index:]
|
service_name = i[name_index:]
|
||||||
print(f"Crash detected in {service_name}")
|
print(f"Crash detected in {service_name}")
|
||||||
print(f"docker logs for the container:\n")
|
|
||||||
os.system(f"docker logs {i.split(' ')[0]}")
|
|
||||||
print()
|
|
||||||
exit_code = 1
|
exit_code = 1
|
||||||
|
|
||||||
sys.exit(exit_code)
|
sys.exit(exit_code)
|
||||||
|
|||||||
Reference in New Issue
Block a user