From 650643c9abd1627447d65101865d375379d427a6 Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Wed, 24 Jan 2024 15:32:58 +0800 Subject: [PATCH] first --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 51 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/xumu.iml | 8 + __pycache__/config.cpython-311.pyc | Bin 0 -> 987 bytes config.py | 27 ++++ main.py | 146 ++++++++++++++++++ 9 files changed, 256 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/xumu.iml create mode 100644 __pycache__/config.cpython-311.pyc create mode 100644 config.py create mode 100644 main.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..af68e56 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,51 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0a29fb --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d18be63 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/xumu.iml b/.idea/xumu.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/xumu.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/__pycache__/config.cpython-311.pyc b/__pycache__/config.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5d7d8a32390d7bcf53e21caf8a05f9db95b74835 GIT binary patch literal 987 zcmZ8ezi-n(6h7OreXipsRaFU9suX@il?Uo7R8>?Vq=G;U2r38yd8u6AwQ-x+=6nH? z$s=st=+GfzC`Db^`45KCKtaM`2-L^LJinp2O#mYU<=Gj%!(+3tLuz*D@jV4kK6+44eTSm}U zkY=lB%7*OIFhqKb<-;1sc6$5xp1%fVj|2ct*f$!sE&>d8vlET=fBkJ^;<;-7&(fa0 z>YeZ-MouOI*g0gNX`JJ=q1>Tp=aKLNnE3&Zcxp;Xrt#@}nou)+?rzDw1Ja zBjC=InF?V~3I@E7LCUymz#|7TgVwDorD3#YSzbVwYF^dx{JkaD=Mrsg5*$${^jdSA zSZ_;~=SP^(=hzJhidx2G`%MFrW|AR6Kre;0mfWD*x67@(6NWXinOp!w! zo+3&hKk_(;XrhgZd`Y|nriG=f!3y`CI!hEfQS^@0pD5UO1B8=Iiuy=xH7U!cAl4#t z$q^-9n8-BP!+xSxv4b#)zJd$(cHpvXRf9UV(r2?anr_u0^$im2VwXl%*knEVV`;r_ z)Ekx?_}kvjQrNtNck2Oa)bKrWgC*jBC;Gu)C<%hl2S%LEeztg;u2}2o#nW`fuxHHf zzdFq>#9Cj?_tadE<9H1X)ewh(L97k)AX|!IKYNoOpJz;;VW(4o`TaW|rw^`m;PrFW zJX6i%#g1C)s--y7gYzGhE}TDD>A*r47S7?~8C*P8J8-!Rm(Stq8C>naau=3kxj$R% X%`LR$&RlUQOFFxmL9Fsg3ap(!X!#8q literal 0 HcmV?d00001 diff --git a/config.py b/config.py new file mode 100644 index 0000000..a099506 --- /dev/null +++ b/config.py @@ -0,0 +1,27 @@ +# 数据库插入类型 +import base64 + +rfid_type = ["TEXT", "TEXT", "FLOAT"] +air_type = ["TEXT", "FLOAT", "FLOAT"] +else_type = ["TEXT", "FLOAT"] +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" +# 鉴权 +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 +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..5b26278 --- /dev/null +++ b/main.py @@ -0,0 +1,146 @@ +import time +import json +from typing import Any + +import requests +import uvicorn +from fastapi import FastAPI, Request +from pydantic import BaseModel +from config import * + +# fastapi +app = FastAPI() + + +# 统一返回数据 +class BaseResponse(BaseModel): + code: int = 200 + msg: str = "success" + data: Any = None + + +# 设备注册实体 +class Device(BaseModel): + iccid: str = "" + deviceId: str = "" + type: int = None + + +# 数据查询参数实体 +class DataQuery(BaseModel): + deviceId: str + limit: int = 1 + + +# 数据查询接口 +@app.post("/api/xumu/data/query") +async def data_query(params: DataQuery): + try: + deviceId = params.deviceId + limit = params.limit + sql = f"select * from root.farm.{deviceId} order by time desc limit {limit}" + send_json = { + "sql": sql + } + r = requests.post(baseHost + queryUri, headers=headers, json=send_json) + return BaseResponse(data=r.json()) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +# 原生查询接口 +@app.post("/api/xumu/rest/v2/query") +async def rest_query(request: Request): + data = await request.json() + r = requests.post(baseHost + queryUri, headers=headers, json=data) + return BaseResponse(data=r.json()) + + +# 设备查询接口 +@app.post("/api/xumu/device/query") +async def get_device(device: Device): + try: + sql = "SELECT * FROM root.device where 1=1" + # 检查iccid是否有值,如果有,添加到SQL语句中 + if device.iccid: + sql += f" and iccid = '{device.iccid}'" + # 检查deviceId是否有值,如果有,添加到SQL语句中 + if device.deviceId: + sql += f" and deviceId = '{device.deviceId}'" + # 检查type是否有值,如果有,添加到SQL语句中 + if device.type is not None: + sql += f" and type = {device.type}" + send_json = { + "sql": sql + } + r = requests.post(baseHost + queryUri, headers=headers, json=send_json) + return BaseResponse(data=r.json()) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +# 设备注册接口 +@app.post("/api/xumu/device/register") +async def register(request: Request): + try: + data = await request.body() + data = data.decode("utf-8") + data = json.loads(data) + payload = data["payload"] + receive_len = len(payload) + payload = json.loads(payload) + send_len = payload["l"] + if receive_len != send_len: + return BaseResponse(code=301, msg="data valid error") + deviceId = payload["d"] + iccid = payload["cid"] + type = payload["t"] + send_json = { + "devices": ["root.device"], + "timestamps": [int(time.time() * 1000)], + "measurements_list": [["iccid", "deviceId", "type"]], + "data_types_list": [["TEXT", "TEXT", "INT32"]], + "values_list": [[iccid, deviceId, type]], + "is_aligned": False + } + r = requests.post(baseHost + insertUri, headers=headers, json=send_json) + return BaseResponse(data=r.json()) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +# 数据上传接口 +@app.post("/api/xumu/data/collect") +async def process_data(request: Request): + # TODO mqtt检测 + + try: + data = await request.body() + data = data.decode("utf-8") + data = json.loads(data) + payload = data["payload"] + receive_len = len(payload) + payload = json.loads(payload) + send_len = payload["l"] + if receive_len != send_len: + return 301 + m = payload["m"] + v = payload["v"] + t = payload["t"] + cid = v[0] + send_json = { + "devices": ["root.farm." + cid], + "timestamps": [int(time.time() * 1000)], + "measurements_list": [m], + "data_types_list": [dataTypes[t]], + "values_list": [v], + "is_aligned": False + } + r = requests.post(baseHost + insertUri, headers=headers, json=send_json) + return BaseResponse(data=r.json()) + except Exception as e: + return BaseResponse(code=500, msg=str(e)) + + +if __name__ == '__main__': + uvicorn.run(app, host="0.0.0.0", port=8002)