Files
home-automation/app/services/location.py
T

37 lines
978 B
Python

from datetime import datetime, timezone
from sqlalchemy import insert
from sqlalchemy.orm import Session
from app.models.location import Location
from app.schemas.location import LocationRecordRequest
def _parse_float_compat(value: str) -> float:
try:
return float(value)
except (TypeError, ValueError):
return 0.0
def _utc_now_rfc3339() -> str:
now = datetime.now(timezone.utc).replace(microsecond=0)
return now.isoformat().replace("+00:00", "Z")
def record_location(session: Session, payload: LocationRecordRequest) -> None:
stmt = (
insert(Location)
.prefix_with("OR IGNORE")
.values(
person=payload.person,
datetime=_utc_now_rfc3339(),
latitude=_parse_float_compat(payload.latitude),
longitude=_parse_float_compat(payload.longitude),
altitude=_parse_float_compat(payload.altitude),
)
)
session.execute(stmt)
session.commit()