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 time
import json
from typing import Any
from datetime import datetime, timedelta
import requests
import uvicorn
from fastapi import FastAPI, Request
@ -25,6 +24,7 @@ class BaseResponse(BaseModel):
async def video_query(username):
return BaseResponse(data=get_video_url(username))
@app.get("/api/xumu/warning")
async def get_warning(deviceId):
try:
@ -38,6 +38,7 @@ async def get_warning(deviceId):
except Exception as e:
return BaseResponse(code=500, msg=str(e))
@app.get("/api/xumu/device/online")
async def device_online_query(iccid, deviceId):
try:
@ -172,19 +173,33 @@ async def register(request: Request):
deviceId = payload["d"]
iccid = payload["cid"]
type = payload["t"]
# 该设备是否已经创建
a_device = await get_device(iccid, deviceId)
res = []
# 查看设备是否已经创建
a_device = await get_device(None, deviceId)
if a_device.code == 200:
v = a_device.data["values"]
t = a_device.data["timestamps"]
if len(v) != 0:
return BaseResponse(code=302, msg="该设备已经注册过了")
# 创建该设备
send_json = {
"sql": f"insert into root.device(iccid, deviceId, type) values('{iccid}', '{deviceId}', {type})"
}
res = []
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json())
timestamp = t[0]
theIccid = v[0]
# 如果设备上传的注册iccid和数据库查询的iccid一致则说明已经注册过了
if theIccid == iccid:
return BaseResponse(code=302, msg="该设备已经注册过了")
# 如果不一致,则直接进行更新注册
send_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 = {
"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))
# 监测数据最近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__':
uvicorn.run(app, host="0.0.0.0", port=8002)