mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
添加 API 服务器代码及相关功能,包括数据上传、获取数据和评分逻辑
This commit is contained in:
commit
2e033886a5
6 files changed
+219
No files matched your search
@@ -0,0 +1,2 @@
|
||||
__pycache__
|
||||
database.csv
|
||||
@@ -0,0 +1,32 @@
|
||||
# API-Server
|
||||
## 运行
|
||||
```bash
|
||||
uvicorn app:app --reload
|
||||
```
|
||||
|
||||
## 水质指标
|
||||
pH值
|
||||
| pH | 评价 |
|
||||
| :---: | :---: |
|
||||
| 6.5~8.5 | 优秀 |
|
||||
| 6~9 | 合格 |
|
||||
| <6 或 >9 | 异常 |
|
||||
|
||||
浊度 (Turbidity)
|
||||
|
||||
| 浊度 (NTU) | 评价 |
|
||||
| :---: | :---: |
|
||||
| <5 | 优秀 |
|
||||
| 5~25 | 一般 |
|
||||
| >25 | 较差 |
|
||||
| >100 | 异常 |
|
||||
|
||||
溶解性总固体 (TDS)
|
||||
|
||||
| TDS (mg/L) | 评价 |
|
||||
| :---: | :---: |
|
||||
| <300 | 优秀 |
|
||||
| 300~500 | 良好 |
|
||||
| 500~1000 | 一般 |
|
||||
| >1000 | 较差 |
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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"}
|
||||
|
||||
return get_data()
|
||||
|
||||
# 获取最新数据
|
||||
@app.get("/data/last")
|
||||
def data_last(token:str):
|
||||
if token!= TOKEN:
|
||||
return {"status": "error", "message": "token error"}
|
||||
|
||||
return get_last_data()
|
||||
|
||||
# 获取最新数据的评分
|
||||
@app.get("/data/score")
|
||||
def data_score(token:str):
|
||||
if token!= TOKEN:
|
||||
return {"status": "error", "message": "token error"}
|
||||
|
||||
data = get_last_data()
|
||||
ph = data["ph"]
|
||||
tds = data["tds"]
|
||||
turbidity = data["turbidity"]
|
||||
|
||||
score = calculate_score(ph, tds, turbidity)
|
||||
return score
|
||||
@@ -0,0 +1,48 @@
|
||||
import csv
|
||||
import os
|
||||
|
||||
###
|
||||
DATA_FILE = "database.csv"
|
||||
###
|
||||
|
||||
# time, ph, tds, turbidity
|
||||
|
||||
# 插入数据
|
||||
def insert_data(time, ph, tds, turbidity):
|
||||
need_header = (
|
||||
not os.path.exists(DATA_FILE)
|
||||
or os.path.getsize(DATA_FILE) == 0
|
||||
)
|
||||
|
||||
with open(DATA_FILE, "a", newline="", encoding="utf-8") as file:
|
||||
writer = csv.writer(file)
|
||||
|
||||
if need_header:
|
||||
writer.writerow(["time", "ph", "tds", "turbidity"])
|
||||
|
||||
writer.writerow([time, ph, tds, turbidity])
|
||||
|
||||
# 获取所有数据
|
||||
def get_data():
|
||||
with open(DATA_FILE, "r", newline="", encoding="utf-8") as file:
|
||||
reader = csv.DictReader(file)
|
||||
|
||||
data = []
|
||||
|
||||
for row in reader:
|
||||
row["ph"] = float(row["ph"])
|
||||
row["tds"] = float(row["tds"])
|
||||
row["turbidity"] = float(row["turbidity"])
|
||||
|
||||
data.append(row)
|
||||
|
||||
return data
|
||||
|
||||
# 获取最新数据
|
||||
def get_last_data():
|
||||
data = get_data()
|
||||
|
||||
if not data:
|
||||
return None
|
||||
|
||||
return data[-1]
|
||||
@@ -0,0 +1,2 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
@@ -0,0 +1,81 @@
|
||||
# score.py
|
||||
|
||||
def score_ph(ph: float):
|
||||
if 6.5 <= ph <= 8.5:
|
||||
return 40, "优秀"
|
||||
|
||||
if 6 <= ph <= 9:
|
||||
return 30, "合格"
|
||||
|
||||
return 0, "异常"
|
||||
|
||||
|
||||
def score_tds(tds: float):
|
||||
if tds < 300:
|
||||
return 30, "优秀"
|
||||
|
||||
if tds < 500:
|
||||
return 25, "良好"
|
||||
|
||||
if tds < 1000:
|
||||
return 15, "一般"
|
||||
|
||||
return 0, "较差"
|
||||
|
||||
|
||||
def score_turbidity(turbidity: float):
|
||||
if turbidity < 5:
|
||||
return 30, "优秀"
|
||||
|
||||
if turbidity < 25:
|
||||
return 20, "一般"
|
||||
|
||||
if turbidity < 100:
|
||||
return 10, "较差"
|
||||
|
||||
return 0, "异常"
|
||||
|
||||
def calculate_score(ph, tds, turbidity):
|
||||
ph_score, ph_level = score_ph(ph)
|
||||
tds_score, tds_level = score_tds(tds)
|
||||
turbidity_score, turbidity_level = score_turbidity(turbidity)
|
||||
|
||||
total = ph_score + tds_score + turbidity_score
|
||||
|
||||
if total >= 90:
|
||||
level = "优秀"
|
||||
elif total >= 75:
|
||||
level = "良好"
|
||||
elif total >= 60:
|
||||
level = "一般"
|
||||
elif total >= 40:
|
||||
level = "较差"
|
||||
else:
|
||||
level = "异常"
|
||||
|
||||
abnormal = (
|
||||
ph_level == "异常"
|
||||
or turbidity_level == "异常"
|
||||
or tds_level == "较差"
|
||||
)
|
||||
|
||||
return {
|
||||
"score": total,
|
||||
"level": level,
|
||||
"abnormal": abnormal,
|
||||
"ph": {
|
||||
"value": ph,
|
||||
"score": ph_score,
|
||||
"level": ph_level
|
||||
},
|
||||
"tds": {
|
||||
"value": tds,
|
||||
"score": tds_score,
|
||||
"level": tds_level
|
||||
},
|
||||
"turbidity": {
|
||||
"value": turbidity,
|
||||
"score": turbidity_score,
|
||||
"level": turbidity_level
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user