Files
IoT-Systems/edge/app/usecases/data_processing.py

35 lines
1.1 KiB
Python
Raw Normal View History

2026-03-25 10:23:25 +02:00
import time
2024-02-12 18:21:08 +02:00
from app.entities.agent_data import AgentData
from app.entities.processed_agent_data import ProcessedAgentData
2026-03-25 10:23:25 +02:00
_last_processed_times = {}
2024-02-12 18:21:08 +02:00
def process_agent_data(
agent_data: AgentData,
) -> ProcessedAgentData:
"""
Process agent data and classify the state of the road surface.
Parameters:
agent_data (AgentData): Agent data that containing accelerometer, GPS, and timestamp.
Returns:
processed_data_batch (ProcessedAgentData): Processed data containing the classified state of the road surface and agent data.
"""
2026-03-25 10:23:25 +02:00
user_id = agent_data.user_id
2026-03-25 10:37:01 +02:00
road_state = "normal"
2026-03-25 10:23:25 +02:00
curr_time = time.time()
last_processed_time = _last_processed_times.get(user_id, 0)
2026-03-25 10:37:01 +02:00
if curr_time - last_processed_time > 1.0:
if agent_data.accelerometer.z < -1.0:
road_state = "pothole"
elif agent_data.accelerometer.z > 1.0:
road_state = "bump"
2026-03-25 10:23:25 +02:00
2026-03-25 10:37:01 +02:00
_last_processed_times[user_id] = curr_time
2026-03-25 10:23:25 +02:00
return ProcessedAgentData(
2026-03-25 10:23:25 +02:00
road_state=road_state,
agent_data=agent_data
)