This commit is contained in:
xyj 2024-01-24 16:15:56 +08:00
parent 8f5391bfdf
commit 741518c749
1 changed files with 26 additions and 42 deletions

62
main.py
View File

@ -112,48 +112,32 @@ async def register(request: Request):
# 数据上传接口 # 数据上传接口
@app.post("/api/xumu/data/collect") @app.post("/api/xumu/data/collect")
async def process_data(request: Request): async def process_data(request: Request):
# TODO mqtt检测 try:
data = await request.body() data = await request.body()
data = data.decode("utf-8") data = data.decode("utf-8")
data = json.loads(data) data = json.loads(data)
event = data["event"] payload = data["payload"]
print(data) receive_len = len(payload)
if event == "client.connected": payload = json.loads(payload)
print("已连接") send_len = payload["l"]
print(data["connected_at"]) if receive_len != send_len:
elif event == "client.disconnected": return 301
print("已断开") m = payload["m"]
print(data["disconnected_at"]) v = payload["v"]
elif event == "message.publish": t = payload["t"]
print("消息接收") cid = v[0]
print(data["timestamp"]) send_json = {
return 200 "devices": ["root.farm." + cid],
# try: "timestamps": [int(time.time() * 1000)],
# data = await request.body() "measurements_list": [m],
# data = data.decode("utf-8") "data_types_list": [dataTypes[t]],
# data = json.loads(data) "values_list": [v],
# payload = data["payload"] "is_aligned": False
# receive_len = len(payload) }
# payload = json.loads(payload) r = requests.post(baseHost + insertUri, headers=headers, json=send_json)
# send_len = payload["l"] return BaseResponse(data=r.json())
# if receive_len != send_len: except Exception as e:
# return 301 return BaseResponse(code=500, msg=str(e))
# m = payload["m"]
# v = payload["v"]
# t = payload["t"]
# cid = v[0]
# send_json = {
# "devices": ["root.farm." + cid],
# "timestamps": [int(time.time() * 1000)],
# "measurements_list": [m],
# "data_types_list": [dataTypes[t]],
# "values_list": [v],
# "is_aligned": False
# }
# r = requests.post(baseHost + insertUri, 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__':