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")
async def process_data(request: Request):
# TODO mqtt检测
try:
data = await request.body()
data = data.decode("utf-8")
data = json.loads(data)
event = data["event"]
print(data)
if event == "client.connected":
print("已连接")
print(data["connected_at"])
elif event == "client.disconnected":
print("已断开")
print(data["disconnected_at"])
elif event == "message.publish":
print("消息接收")
print(data["timestamp"])
return 200
# try:
# data = await request.body()
# data = data.decode("utf-8")
# data = json.loads(data)
# payload = data["payload"]
# receive_len = len(payload)
# payload = json.loads(payload)
# send_len = payload["l"]
# if receive_len != send_len:
# return 301
# 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))
payload = data["payload"]
receive_len = len(payload)
payload = json.loads(payload)
send_len = payload["l"]
if receive_len != send_len:
return 301
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__':