mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
76 lines
1.5 KiB
Python
76 lines
1.5 KiB
Python
from fastapi import FastAPI
|
|
from datetime import datetime
|
|
from data import *
|
|
from score import *
|
|
|
|
###
|
|
TOKEN = "mE7yG0kI"
|
|
###
|
|
|
|
app = FastAPI()
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return "OK"
|
|
|
|
# ESP8266 上传数据
|
|
@app.get("/data/upload")
|
|
def data_upload(token:str, ph:float, tds:float, turbidity:float):
|
|
if token!= TOKEN:
|
|
return {"status": "error", "message": "token error"}
|
|
|
|
insert_data(datetime.now(), ph, tds, turbidity)
|
|
|
|
return {"status": "ok", "message": "data_received"}
|
|
|
|
# 获取全部数据
|
|
@app.get("/data/all")
|
|
def data_all(token:str):
|
|
if token!= TOKEN:
|
|
return {"status": "error", "message": "token error"}
|
|
|
|
data = get_data()
|
|
|
|
if data is None:
|
|
return []
|
|
|
|
return data
|
|
|
|
# 获取最新数据
|
|
@app.get("/data/last")
|
|
def data_last(token:str):
|
|
if token!= TOKEN:
|
|
return {"status": "error", "message": "token error"}
|
|
|
|
data = get_last_data()
|
|
|
|
if data is None:
|
|
return {
|
|
"time": None,
|
|
"ph": None,
|
|
"tds": None,
|
|
"turbidity": None
|
|
}
|
|
|
|
return data
|
|
|
|
# 获取最新数据的评分
|
|
@app.get("/data/score")
|
|
def data_score(token:str):
|
|
if token!= TOKEN:
|
|
return {"status": "error", "message": "token error"}
|
|
|
|
data = get_last_data()
|
|
|
|
if data is None:
|
|
return {
|
|
"code": 1,
|
|
"message": "暂无数据"
|
|
}
|
|
|
|
ph = data["ph"]
|
|
tds = data["tds"]
|
|
turbidity = data["turbidity"]
|
|
|
|
score = calculate_score(ph, tds, turbidity)
|
|
return score |