This commit is contained in:
xyj 2024-01-24 15:32:58 +08:00
commit 650643c9ab
9 changed files with 256 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,51 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="19">
<item index="0" class="java.lang.String" itemvalue="transformers" />
<item index="1" class="java.lang.String" itemvalue="fastapi" />
<item index="2" class="java.lang.String" itemvalue="typing_extensions" />
<item index="3" class="java.lang.String" itemvalue="tokenizers" />
<item index="4" class="java.lang.String" itemvalue="sentencepiece" />
<item index="5" class="java.lang.String" itemvalue="streamlit" />
<item index="6" class="java.lang.String" itemvalue="torch" />
<item index="7" class="java.lang.String" itemvalue="huggingface_hub" />
<item index="8" class="java.lang.String" itemvalue="starlette" />
<item index="9" class="java.lang.String" itemvalue="pydantic" />
<item index="10" class="java.lang.String" itemvalue="pyserial" />
<item index="11" class="java.lang.String" itemvalue="uvicorn" />
<item index="12" class="java.lang.String" itemvalue="requests" />
<item index="13" class="java.lang.String" itemvalue="sqlalchemy" />
<item index="14" class="java.lang.String" itemvalue="board" />
<item index="15" class="java.lang.String" itemvalue="websockets" />
<item index="16" class="java.lang.String" itemvalue="dashscope" />
<item index="17" class="java.lang.String" itemvalue="qianfan" />
<item index="18" class="java.lang.String" itemvalue="Pillow" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
<option value="N806" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="requests.models.Response.__getitem__" />
</list>
</option>
</inspection_tool>
<inspection_tool class="ReassignedToPlainText" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ShellCheck" enabled="true" level="ERROR" enabled_by_default="true">
<shellcheck_settings value="SC2239" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="ai" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="ai" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/xumu.iml" filepath="$PROJECT_DIR$/.idea/xumu.iml" />
</modules>
</component>
</project>

8
.idea/xumu.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

27
config.py Normal file
View File

@ -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
}

146
main.py Normal file
View File

@ -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)