From 62fb24d7a4f7587bb3e1bebb373213328d89593f Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Wed, 24 Jan 2024 17:09:03 +0800 Subject: [PATCH] update --- config.py | 39 ++++++++++++++++++++++++++++++++++++++- main.py | 14 ++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/config.py b/config.py index eb4b07a..c044429 100644 --- a/config.py +++ b/config.py @@ -42,4 +42,41 @@ send3 = { "m": ["cid", "v", "t"], "v": ["test1", 10.62, 2], "l": 50 -} \ No newline at end of file +} + + +def RFID_template(deviceId): + return [ + f"CREATE TIMESERIES root.farm.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.temperature(v) WITH datatype=FLOAT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.RFID(r) WITH datatype=TEXT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN", + ] + + +def common_template(deviceId): + return [ + f"CREATE TIMESERIES root.farm.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.value(v) WITH datatype=FLOAT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN", + ] + + +def air_template(deviceId): + return [ + f"CREATE TIMESERIES root.farm.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.air_temperature(at) WITH datatype=FLOAT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.air_humidity(ah) WITH datatype=FLOAT,ENCODING=PLAIN", + f"CREATE TIMESERIES root.farm.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN", + ] + + +# 数据库创建字段sql语句 +def get_sql(deviceId, type): + match type: + case 0: + return RFID_template(deviceId) + case 1: + return air_template(deviceId) + case _: + return common_template(deviceId) diff --git a/main.py b/main.py index e782f86..9919926 100644 --- a/main.py +++ b/main.py @@ -91,7 +91,7 @@ async def register(request: Request): payload = json.loads(payload) send_len = payload["l"] if receive_len != send_len: - return BaseResponse(code=301, msg="data valid error") + return BaseResponse(code=301, msg=f"data valid error, receive_len:{receive_len}") deviceId = payload["d"] iccid = payload["cid"] type = payload["t"] @@ -103,8 +103,18 @@ async def register(request: Request): "values_list": [[iccid, deviceId, type]], "is_aligned": False } + res = [] r = requests.post(baseHost + insertUri, headers=headers, json=send_json) - return BaseResponse(data=r.json()) + res.append(r.json()) + # 创建相应的数据库和字段 + sql_list = get_sql(deviceId, type) + for sql in sql_list: + send_json = { + "sql": sql + } + r = requests.post(baseHost + insertUri, headers=headers, json=send_json) + res.append(r.json()) + return BaseResponse(data=json.dumps(res)) except Exception as e: return BaseResponse(code=500, msg=str(e))