This commit is contained in:
Amritanshu Agrawal 2023-07-07 06:41:21 +05:30
parent 0203e73cd1
commit d6c9dd61e8
2 changed files with 16 additions and 8 deletions

View File

@ -1,5 +1,3 @@
from typing import Any
from dotenv import load_dotenv
from pydantic import BaseSettings

View File

@ -1,11 +1,15 @@
from typing import Annotated
import csv
import uvicorn
import datetime
from typing import Annotated
import uvicorn
from fastapi import FastAPI, Form
from pydantic import BaseModel
from .core.config import settings
from pydantic import BaseModel
app = FastAPI()
@ -15,16 +19,22 @@ class Reading(BaseModel):
humidity: float
age: int
@app.post("/upload", response_model=Reading)
def add_reading(temp: Annotated[float, Form()], humidity: Annotated[float, Form()], age: Annotated[int, Form()], device: Annotated[str, Form()]):
def add_reading(
temp: Annotated[float, Form()],
humidity: Annotated[float, Form()],
age: Annotated[int, Form()],
device: Annotated[str, Form()],
):
current_datetime = datetime.datetime.now()
deducted_datetime = current_datetime - datetime.timedelta(milliseconds=age)
append_to_csv(settings.FILE_PATH, temp, humidity, deducted_datetime)
return Reading(temperature=temp,humidity=humidity,age=age)
return Reading(temperature=temp, humidity=humidity, age=age)
def append_to_csv(filename, temperature, humidity, datetime_var):
with open(filename, 'a', newline='') as csvfile:
with open(filename, "a", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow([temperature, humidity, datetime_var])