Compare commits

...

2 Commits

Author SHA1 Message Date
xyj ebc89ca249 past_seven_days 2024-02-20 10:43:04 +08:00
xyj 154e539d13 update 2024-02-20 09:09:43 +08:00
1 changed files with 45 additions and 12 deletions

57
xumu.py
View File

@ -1,8 +1,7 @@
import re import re
import time
import json import json
from typing import Any from typing import Any
from datetime import datetime, timedelta
import requests import requests
import uvicorn import uvicorn
from fastapi import FastAPI, Request from fastapi import FastAPI, Request
@ -25,6 +24,7 @@ class BaseResponse(BaseModel):
async def video_query(username): async def video_query(username):
return BaseResponse(data=get_video_url(username)) return BaseResponse(data=get_video_url(username))
@app.get("/api/xumu/warning") @app.get("/api/xumu/warning")
async def get_warning(deviceId): async def get_warning(deviceId):
try: try:
@ -38,6 +38,7 @@ async def get_warning(deviceId):
except Exception as e: except Exception as e:
return BaseResponse(code=500, msg=str(e)) return BaseResponse(code=500, msg=str(e))
@app.get("/api/xumu/device/online") @app.get("/api/xumu/device/online")
async def device_online_query(iccid, deviceId): async def device_online_query(iccid, deviceId):
try: try:
@ -172,19 +173,33 @@ async def register(request: Request):
deviceId = payload["d"] deviceId = payload["d"]
iccid = payload["cid"] iccid = payload["cid"]
type = payload["t"] type = payload["t"]
# 该设备是否已经创建 res = []
a_device = await get_device(iccid, deviceId) # 查看设备是否已经创建
a_device = await get_device(None, deviceId)
if a_device.code == 200: if a_device.code == 200:
v = a_device.data["values"] v = a_device.data["values"]
t = a_device.data["timestamps"]
if len(v) != 0: if len(v) != 0:
return BaseResponse(code=302, msg="该设备已经注册过了") timestamp = t[0]
# 创建该设备 theIccid = v[0]
send_json = { # 如果设备上传的注册iccid和数据库查询的iccid一致则说明已经注册过了
"sql": f"insert into root.device(iccid, deviceId, type) values('{iccid}', '{deviceId}', {type})" if theIccid == iccid:
} return BaseResponse(code=302, msg="该设备已经注册过了")
res = [] # 如果不一致,则直接进行更新注册
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json) send_json = {
res.append(r.json()) "sql": f"insert into root.device(timestamp, iccid, deviceId, type) values({timestamp}, '{iccid}', '{deviceId}', {type})"
}
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json())
else:
# 创建该设备
send_json = {
"sql": f"insert into root.device(iccid, deviceId, type) values('{iccid}', '{deviceId}', {type})"
}
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json())
else:
return BaseResponse(code=500, msg="500 Internal error")
# 插入设备状态表 # 插入设备状态表
send_json = { send_json = {
"sql": f"insert into root.farm.clientId(iccid, clientId, is_online, deviceId) values('{iccid}','{clientid}', True, '{deviceId}')" "sql": f"insert into root.farm.clientId(iccid, clientId, is_online, deviceId) values('{iccid}','{clientid}', True, '{deviceId}')"
@ -311,5 +326,23 @@ async def process_data(request: Request):
return BaseResponse(code=500, msg=str(e)) return BaseResponse(code=500, msg=str(e))
# 监测数据最近7天的数据
@app.get("/api/xumu/data/past_seven_days")
async def past_seven_days(deviceId):
try:
current_date = datetime.now().date()
past_7_days_date = current_date - timedelta(days=7)
if deviceId is None or deviceId == "" or len(deviceId) != 4:
return BaseResponse(code=500, msg="参数错误")
sql = f"select * from root.farm.{deviceId} where time >= {past_7_days_date} and time <= {current_date}"
send_json = {
"sql": sql
}
r = requests.post(baseHost + queryUri, headers=headers, json=send_json)
return BaseResponse(data=r.json())
except Exception as e:
return BaseResponse(code=500, msg=str(e))
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8002) uvicorn.run(app, host="0.0.0.0", port=8002)