107 lines
3.0 KiB
Python
107 lines
3.0 KiB
Python
# 数据库插入类型
|
|
import base64
|
|
|
|
rfid_type = ["TEXT", "TEXT", "FLOAT", "INT32"]
|
|
air_type = ["TEXT", "FLOAT", "FLOAT", "INT32"]
|
|
else_type = ["TEXT", "FLOAT", "INT32"]
|
|
dataTypes = {
|
|
0: rfid_type,
|
|
1: air_type,
|
|
2: else_type,
|
|
3: else_type,
|
|
4: else_type,
|
|
5: else_type,
|
|
}
|
|
baseHost = "https://iot.lihaink.cn/iotdb_restapi"
|
|
# 注意这里前面不能加/
|
|
insertUri = "rest/v2/insertRecords"
|
|
queryUri = "rest/v2/query"
|
|
nonQueryUri = "rest/v2/nonQuery"
|
|
# 鉴权
|
|
username = 'root'
|
|
password = 'root'
|
|
code = (username + ":" + password).encode("utf-8")
|
|
token = base64.encodebytes(code).decode("utf-8").strip()
|
|
headers = {
|
|
'ContentType': 'application/json',
|
|
'Authorization': "Basic " + token
|
|
}
|
|
|
|
# example
|
|
# RFID
|
|
send1 = {
|
|
"m": ["cid", "r", "v"],
|
|
"v": ["abcd", "rfid", 10.62],
|
|
"t": 0,
|
|
"l": 63
|
|
}
|
|
# 空气
|
|
send2 = {
|
|
"m": ["cid", "at", "ah"],
|
|
"v": ["test2", 10.62, 50.22],
|
|
"t": 1,
|
|
"l": 65
|
|
}
|
|
# 普通
|
|
send3 = {
|
|
"m": ["cid", "v"],
|
|
"v": ["test1", 10.62],
|
|
"t": 2,
|
|
"l": 50
|
|
}
|
|
|
|
|
|
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",
|
|
]
|
|
|
|
|
|
def rfid_deviceId(rfid, deviceId):
|
|
return [
|
|
f"insert into root.rfid(rfid, deviceId) values('{rfid}', '{deviceId}')"
|
|
]
|
|
|
|
|
|
# 数据库创建字段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)
|
|
|
|
|
|
def get_client_change_status_sql(timestamp, status):
|
|
if status:
|
|
return [f"insert into root.farm.clientId(timestamp, is_online) values({timestamp}, True)"]
|
|
else:
|
|
return [f"insert into root.farm.clientId(timestamp, is_online) values({timestamp}, False)"]
|
|
|
|
|
|
# 监控视频接口
|
|
def get_video_url(username):
|
|
return f"http://rtsp.lihaink.cn/live/xumu_{username}.live.mp4"
|