File size: 977 Bytes
2d82c8b
 
 
 
f92a401
2d82c8b
 
f92a401
 
 
 
 
 
 
 
2d82c8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, Request
from pydantic import BaseModel
from typing import Dict
import time
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # or set this to your frontend's origin
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
# In-memory storage for now
bus_locations: Dict[str, dict] = {}

class Location(BaseModel):
    _type: str
    tid: str
    lat: float
    lon: float
    tst: int

@app.post("/track")
async def track_location(loc: Location):
    # Save by bus_id (tid)
    bus_locations[loc.tid] = {
        "lat": loc.lat,
        "lon": loc.lon,
        "timestamp": loc.tst,
        "last_updated": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(loc.tst))
    }
    print(f"[{loc.tid}] → ({loc.lat}, {loc.lon}) @ {loc.tst}")
    return {"status": "ok"}

@app.get("/locations")
def get_all_locations():
    return bus_locations