-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
23 lines (18 loc) · 882 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from fastapi import FastAPI, Request, HTTPException
import duckdb
from datetime import datetime
app = FastAPI()
@app.post("/measurements")
async def receive_measurement(request: Request):
try:
with duckdb.connect('devices.duckdb') as db_connection:
data = await request.json()
data['datetime'] = data.get('datetime', datetime.now().isoformat())
db_connection.execute('''
INSERT INTO measurements (datetime, longitude, latitude, device_id, value, unit, height)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', [data['datetime'], float(data['longitude']), float(data['latitude']),
data['device_id'], float(data['value']), data['unit'], float(data['height'])])
return {"status": "success"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))