mirror of
https://github.com/Rhinemann/IoT-Systems.git
synced 2026-03-14 20:50:39 +02:00
33 lines
793 B
Python
33 lines
793 B
Python
|
|
from datetime import datetime
|
||
|
|
from pydantic import BaseModel, field_validator
|
||
|
|
|
||
|
|
|
||
|
|
class AccelerometerData(BaseModel):
|
||
|
|
x: float
|
||
|
|
y: float
|
||
|
|
z: float
|
||
|
|
|
||
|
|
|
||
|
|
class GpsData(BaseModel):
|
||
|
|
latitude: float
|
||
|
|
longitude: float
|
||
|
|
|
||
|
|
|
||
|
|
class AgentData(BaseModel):
|
||
|
|
accelerometer: AccelerometerData
|
||
|
|
gps: GpsData
|
||
|
|
timestamp: datetime
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
@field_validator("timestamp", mode="before")
|
||
|
|
def parse_timestamp(cls, value):
|
||
|
|
# Convert the timestamp to a datetime object
|
||
|
|
if isinstance(value, datetime):
|
||
|
|
return value
|
||
|
|
try:
|
||
|
|
return datetime.fromisoformat(value)
|
||
|
|
except (TypeError, ValueError):
|
||
|
|
raise ValueError(
|
||
|
|
"Invalid timestamp format. Expected ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ)."
|
||
|
|
)
|