This commit is contained in:
xyj 2024-01-25 09:42:51 +08:00
parent bf33dc04e1
commit 823fbfa21b
2 changed files with 4 additions and 7 deletions

View File

@ -16,6 +16,7 @@ baseHost = "https://iot.lihaink.cn/iotdb_restapi"
# 注意这里前面不能加/ # 注意这里前面不能加/
insertUri = "rest/v2/insertRecords" insertUri = "rest/v2/insertRecords"
queryUri = "rest/v2/query" queryUri = "rest/v2/query"
nonQueryUri = "rest/v2/nonQuery"
# 鉴权 # 鉴权
username = 'root' username = 'root'
password = 'root' password = 'root'

10
xumu.py
View File

@ -58,7 +58,7 @@ async def rest_query(request: Request):
@app.post("/api/xumu/device/query") @app.post("/api/xumu/device/query")
async def get_device(device: Device): async def get_device(device: Device):
try: try:
sql = "SELECT * FROM root.device where 1=1" sql = "SELECT * FROM root.device where time>=0"
# 检查iccid是否有值如果有添加到SQL语句中 # 检查iccid是否有值如果有添加到SQL语句中
if device.iccid: if device.iccid:
sql += f" and iccid = '{device.iccid}'" sql += f" and iccid = '{device.iccid}'"
@ -89,19 +89,16 @@ async def register(request: Request):
payload = json.loads(payload) payload = json.loads(payload)
send_len = payload["l"] send_len = payload["l"]
if receive_len != send_len: if receive_len != send_len:
print(receive_len)
return BaseResponse(code=301, msg=f"data valid error, receive_len:{receive_len}") return BaseResponse(code=301, msg=f"data valid error, receive_len:{receive_len}")
deviceId = payload["d"] deviceId = payload["d"]
iccid = payload["cid"] iccid = payload["cid"]
type = payload["t"] type = payload["t"]
# TODO 检测该设备是否已经创建 # 该设备是否已经创建
device = Device(iccid=iccid, deviceId=deviceId, type=type) device = Device(iccid=iccid, deviceId=deviceId, type=type)
a_device = await get_device(device) a_device = await get_device(device)
print(a_device)
if a_device.code == 200: if a_device.code == 200:
v = a_device.data["values"] v = a_device.data["values"]
if len(v) != 0: if len(v) != 0:
print("该设备已经注册过了")
return BaseResponse(code=302, msg="该设备已经注册过了") return BaseResponse(code=302, msg="该设备已经注册过了")
# 创建该设备 # 创建该设备
send_json = { send_json = {
@ -121,11 +118,10 @@ async def register(request: Request):
send_json = { send_json = {
"sql": sql "sql": sql
} }
r = requests.post(baseHost + insertUri, headers=headers, json=send_json) r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json)
res.append(r.json()) res.append(r.json())
return BaseResponse(data=res) return BaseResponse(data=res)
except Exception as e: except Exception as e:
print(e)
return BaseResponse(code=500, msg=str(e)) return BaseResponse(code=500, msg=str(e))