rfid_last_data_query

This commit is contained in:
xyj 2024-02-03 18:04:54 +08:00
parent f7e67a3bf9
commit f1730d6176
2 changed files with 68 additions and 19 deletions

View File

@ -1,9 +1,13 @@
# 数据库插入类型
import base64
import time
rfid_type = ["TEXT", "TEXT", "FLOAT", "INT32"]
air_type = ["TEXT", "FLOAT", "FLOAT", "INT32"]
else_type = ["TEXT", "FLOAT", "INT32"]
rfid_measurement = ["iccid", "temperature", "RFID", "type"]
air_measurement = ["iccid", "air_temperature", "air_humidity", "type"]
else_measurement = ["iccid", "value", "type"]
dataTypes = {
0: rfid_type,
1: air_type,
@ -12,6 +16,14 @@ dataTypes = {
4: else_type,
5: else_type,
}
measurements = {
0: rfid_measurement,
1: air_measurement,
2: else_measurement,
3: else_measurement,
4: else_measurement,
5: else_measurement,
}
baseHost = "https://iot.lihaink.cn/iotdb_restapi"
# 注意这里前面不能加/
insertUri = "rest/v2/insertRecords"
@ -51,47 +63,70 @@ send3 = {
}
def RFID_template(deviceId):
def RFID_template(type, 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",
f"CREATE TIMESERIES root.{type}.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.temperature(v) WITH datatype=FLOAT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.RFID(r) WITH datatype=TEXT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN",
]
def common_template(deviceId):
def common_template(type, 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",
f"CREATE TIMESERIES root.{type}.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.value(v) WITH datatype=FLOAT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN",
]
def air_template(deviceId):
def air_template(type, 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",
f"CREATE TIMESERIES root.{type}.{deviceId}.iccid(cid) WITH datatype=TEXT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.air_temperature(at) WITH datatype=FLOAT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.air_humidity(ah) WITH datatype=FLOAT,ENCODING=PLAIN",
f"CREATE TIMESERIES root.{type}.{deviceId}.type(t) WITH datatype=INT32,ENCODING=PLAIN",
]
def warning_sql(deviceId, type):
template = "warning"
match type:
case 0:
return RFID_template(template, deviceId)
case 1:
return air_template(template, deviceId)
case _:
return common_template(template, deviceId)
def rfid_deviceId(rfid, deviceId):
return [
f"insert into root.rfid(rfid, deviceId) values('{rfid}', '{deviceId}')"
]
def insert_to_warning_sql(deviceId, v, t):
return {
"devices": ["root.warning." + deviceId],
"timestamps": [int(time.time() * 1000)],
"measurements_list": [measurements[t]],
"data_types_list": [dataTypes[t]],
"values_list": [v],
"is_aligned": False
}
# 数据库创建字段sql语句
def get_sql(deviceId, type):
def farm_sql(deviceId, type):
template = "farm"
match type:
case 0:
return RFID_template(deviceId)
return RFID_template(template, deviceId)
case 1:
return air_template(deviceId)
return air_template(template, deviceId)
case _:
return common_template(deviceId)
return common_template(template, deviceId)
def get_client_change_status_sql(timestamp, status):

16
xumu.py
View File

@ -180,7 +180,15 @@ async def register(request: Request):
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json())
# 创建相应的数据库和字段
sql_list = get_sql(deviceId, type)
sql_list = farm_sql(deviceId, type)
for sql in sql_list:
send_json = {
"sql": sql
}
r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json())
# 创建相应的数据库和字段
sql_list = warning_sql(deviceId, type)
for sql in sql_list:
send_json = {
"sql": sql
@ -234,6 +242,12 @@ async def message_publish(data):
r = requests.post(baseHost + insertUri, headers=headers, json=send_json)
r = r.json()
res.append(r)
# TODO 如果是报警数据,则发送到报警数据表
send_json = insert_to_warning_sql(deviceId, v, t)
print(send_json)
r = requests.post(baseHost + insertUri, headers=headers, json=send_json)
r = r.json()
res.append(r)
return BaseResponse(data=res)
except Exception as e:
return BaseResponse(code=500, msg=str(e))