diff --git a/config.py b/config.py index d6d42a7..5b39fc4 100644 --- a/config.py +++ b/config.py @@ -28,20 +28,23 @@ headers = { } # RFID send1 = { - "m": ["cid", "r", "v", "t"], - "v": ["abcd", "rfid", 10.62, 0], + "m": ["cid", "r", "v"], + "v": ["abcd", "rfid", 10.62], + "t": 0, "l": 63 } # 空气 send2 = { - "m": ["cid", "at", "ah", "t"], - "v": ["test2", 10.62, 50.22, 1], + "m": ["cid", "at", "ah"], + "v": ["test2", 10.62, 50.22], + "t": 1, "l": 65 } # 普通 send3 = { - "m": ["cid", "v", "t"], - "v": ["test1", 10.62, 2], + "m": ["cid", "v"], + "v": ["test1", 10.62], + "t": 2, "l": 50 } @@ -72,6 +75,12 @@ def air_template(deviceId): ] +def rfid_deviceId(rfid, deviceId): + return [ + f"insert into root.rfid(timestamp, rfid, deviceId) values(0, '{rfid}', '{deviceId}')" + ] + + # 数据库创建字段sql语句 def get_sql(deviceId, type): match type: diff --git a/xumu.py b/xumu.py index 411f290..f649142 100644 --- a/xumu.py +++ b/xumu.py @@ -63,10 +63,25 @@ async def rest_query(request: Request): # RFID查询接口 @app.post("/api/xumu/rfid/query") -async def rest_query(rfid_query: RFIDQuery): - rfid = rfid_query.rfid - - pass +async def rfid_query(rfid_entity: RFIDQuery): + try: + rfid = rfid_entity.rfid + sql = f"select deviceId from root.rfid where rfid='{rfid}'" + send_json = { + "sql": sql + } + r = requests.post(baseHost + queryUri, headers=headers, json=send_json) + if r.status_code != 200: + return BaseResponse(data=r.json(), code=404, msg="404") + r = r.json() + values = r["values"] + if len(values) == 0: + return BaseResponse(msg="No Such RFID", code=500) + deviceId = values[0][0] + send_json = DataQuery(deviceId=deviceId) + return await data_query(send_json) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) # 设备查询接口 @@ -157,12 +172,28 @@ async def process_data(request: Request): m = payload["m"] v = payload["v"] t = payload["t"] - # TODO 判断t是否为0,0 则代表是RFID设备,需要做一个映射 - cid = v[0] + res = [] + # 判断t是否为0,0 则代表是RFID设备,需要做一个映射 + if t == 0: + rfid = v[1] + deviceId = v[0] + sql_list = rfid_deviceId(rfid, deviceId) + sql = sql_list[0] + send_json = { + "sql": sql + } + r = requests.post(baseHost + nonQueryUri, headers=headers, json=send_json) + if r.status_code != 200: + return BaseResponse(code=r.status_code, msg=r.json()) + r = r.json() + if r["code"] != 200: + return BaseResponse(code=500, msg=r) + res.append(r) + deviceId = v[0] m.append("t") v.append(t) send_json = { - "devices": ["root.farm." + cid], + "devices": ["root.farm." + deviceId], "timestamps": [int(time.time() * 1000)], "measurements_list": [m], "data_types_list": [dataTypes[t]], @@ -170,7 +201,9 @@ async def process_data(request: Request): "is_aligned": False } r = requests.post(baseHost + insertUri, headers=headers, json=send_json) - return BaseResponse(data=r.json()) + r = r.json() + res.append(r) + return BaseResponse(data=res) except Exception as e: return BaseResponse(code=500, msg=str(e))