From 741518c749ca23b121dffbb6ee5efa07d9bbb5f1 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Wed, 24 Jan 2024 16:15:56 +0800 Subject: [PATCH] update --- main.py | 68 ++++++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index b7f8bfe..45e0735 100644 --- a/main.py +++ b/main.py @@ -112,48 +112,32 @@ async def register(request: Request): # 数据上传接口 @app.post("/api/xumu/data/collect") async def process_data(request: Request): - # TODO mqtt检测 - 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)) + 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)) if __name__ == '__main__':