This commit is contained in:
xyj 2023-12-01 09:21:23 +08:00
commit 839d42549e
38 changed files with 486 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

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

4
.idea/encodings.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8" />
</project>

View File

@ -0,0 +1,26 @@
<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="9">
<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" />
</list>
</value>
</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>

12
.idea/lot_manager.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="chatchat" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>

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="Python 3.8" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="chatchat" 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/data_manager.iml" filepath="$PROJECT_DIR$/.idea/data_manager.iml" />
</modules>
</component>
</project>

45
MQTT.py Normal file
View File

@ -0,0 +1,45 @@
import json
import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh
# Connection success callback
def on_connect(client, userdata, flags, rc):
print(rc)
if rc == 0:
client.subscribe('lot_data')
else:
client.publish('conn_error', payload=rc, qos=0)
# Message receiving callback
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream":
# TODO 启动推流视频
push_stream()
elif data == "close_stream":
# TODO 关闭推流视频
close_stream()
elif data == "exec":
# TODO 执行命令 更新配置
exec_sh()
elif data == "update":
# TODO git更新项目
update()
else:
client.publish('conn_error', payload='No Such Type', qos=0)
if __name__ == '__main__':
client = mqtt.Client()
client.username_pw_set("ceshi", "123456")
# Specify callback function
client.on_connect = on_connect
client.on_message = on_message
# Establish a connection
client.connect('ceshi-mqtt.lihaink.cn', 1883)
# Publish a message
client.loop_forever()

Binary file not shown.

Binary file not shown.

99
api.py Normal file
View File

@ -0,0 +1,99 @@
import os
import subprocess
import uvicorn
from db.models.base import BaseResponse
from db.models.log_data_model import LOT_DATA
from db.repository import add_kb_to_db, get_kb_detail, get_kb_detail_by_time, delete_kb_detail_by_time
from fastapi import FastAPI
app = FastAPI()
@app.post("/add/lot_data/{device_name}")
async def add(data: LOT_DATA, device_name: str):
try:
add_kb_to_db(data, device_name)
return BaseResponse()
except Exception as e:
return BaseResponse(code=500, msg=e)
@app.get("/get/all/lot_data")
async def get_data():
try:
data = get_kb_detail()
return BaseResponse(data=data)
except Exception as e:
return BaseResponse(code=404, msg=e)
@app.get("/get/data/by/time")
async def get_data(start_time, end_time):
try:
data = get_kb_detail_by_time(start_time, end_time)
return BaseResponse(data=data)
except Exception as e:
return BaseResponse(code=404, msg=e)
@app.get("/delete/data/by/time")
async def delete_data(start_time, end_time):
try:
data = delete_kb_detail_by_time(start_time, end_time)
return BaseResponse(data=data)
except Exception as e:
return BaseResponse(code=404, msg=e)
@app.get("/create/db")
async def create():
from db.base import Base, engine
try:
Base.metadata.create_all(bind=engine)
return BaseResponse()
except Exception as e:
return BaseResponse(code=404, msg=e)
@app.get("/start")
def device_test():
## 不管有没有都进行一个杀死
# os.popen()
# stop_cmd = r'kill -9 `lsof -t -i:554`'
# os.system(stop_cmd)
start_cmd = r'nohup ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.226:554/mpeg4 -c:v copy -c:a aac -preset ultrafast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test > app.out 2>&1 & echo $! > app.pid'
p = subprocess.Popen(start_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return_code = p.returncode
if return_code == 0:
return BaseResponse(code=return_code, msg=out,
data="http://192.168.1.27/live/test.live.mp4?secret=gqig2yFKkDpIMic1uWZY1L5MsIo0eflm")
else:
return BaseResponse(code=return_code, msg=err)
@app.get("/stop")
async def device_test2():
## 不管有没有都进行一个杀死
cmd = 'lsof -t -i:554'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 获取执行结果
out, err = p.communicate()
# 获取返回值
return_code = p.returncode
if out == b'':
print("失败")
else:
pid = out
cmd = f'kill -9 {pid}'
print(pid)
os.system(cmd)
if return_code == 0:
return BaseResponse(code=return_code, msg=out)
return BaseResponse(code=return_code, msg=err)
if __name__ == '__main__':
uvicorn.run(app, host="127.0.0.1", port=8000)

10
conf/mqtt.conf Normal file
View File

@ -0,0 +1,10 @@
[program:mqtt]
directory=/home/lihai/PycharmProjects/pythonProject/lot_manager
command=/home/lihai/anaconda3/envs/chatchat/bin/python MQTT.py
user=lihai
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=TERM
stopasgroup=True
priority=1

9
conf/push_stream.conf Normal file
View File

@ -0,0 +1,9 @@
[program:push_stream]
directory=/home/lihai/PycharmProjects/pythonProject/lot_manager
command=/usr/bin/ffmpeg -rtsp_transport tcp -re -i rtsp://admin:123456@192.168.0.123:554/mpeg4 -c:v copy -c:a aac -preset ultrafast -r 20 -flvflags no_duration_filesize -f rtsp -rtsp_transport tcp rstp://47.108.186.87:554/live/test9
user=lihai
autostart=true
autorestart=true
redirect_stderr=true
stopsignal=TERM
stopasgroup=True

0
db/__init__.py Executable file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

19
db/base.py Executable file
View File

@ -0,0 +1,19 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy.orm import sessionmaker
import json
KB_ROOT_PATH = "./"
DB_ROOT_PATH = os.path.join(KB_ROOT_PATH, "lot_data.db")
SQLALCHEMY_DATABASE_URI = f"sqlite:///{DB_ROOT_PATH}"
engine = create_engine(
SQLALCHEMY_DATABASE_URI,
json_serializer=lambda obj: json.dumps(obj, ensure_ascii=False),
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base: DeclarativeMeta = declarative_base()

0
db/models/__init__.py Executable file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

25
db/models/base.py Executable file
View File

@ -0,0 +1,25 @@
from datetime import datetime
from typing import Any, List
import pydantic
from pydantic import BaseModel
from sqlalchemy import Column, DateTime, String, Integer
from db.models.log_data_model import LOT_DATA_MODEL, LOT_DATA
class BaseResponse(BaseModel):
code: int = 200
msg: str = "success"
data: Any = None
class BaseModel:
"""
基础模型
"""
id = Column(Integer, primary_key=True, index=True, comment="主键ID")
create_time = Column(DateTime, default=datetime.utcnow, comment="创建时间")
update_time = Column(DateTime, default=None, onupdate=datetime.utcnow, comment="更新时间")
create_by = Column(String, default=None, comment="创建者")
update_by = Column(String, default=None, comment="更新者")

49
db/models/log_data_model.py Executable file
View File

@ -0,0 +1,49 @@
from pydantic import BaseModel, Field
from sqlalchemy import *
from db.base import Base
class LOT_DATA(BaseModel):
wind_speed: float = Field(None, description='风速:(0到30)m/s ')
wind_direction: float = Field(None, description='风向:0360°')
ambient_temperature: float = Field(None, description='环境温度:℃')
ambient_humidity: float = Field(None, description='环境湿度:%RH')
carbon_dioxide: float = Field(None, description='二氧化碳0~5000ppm')
ambient_air_pressure: float = Field(None, description='环境气压0~120KPa')
rainfall: float = Field(None, description='雨量mm')
ambient_lighting: float = Field(None, description='环境光照0-65535Lux0-20万Lux')
soil_temperature: float = Field(None, description='土壤温度:-40~80℃')
soil_moisture: float = Field(None, description='土壤湿度0-100%RH')
soil_conductivity: float = Field(None, description='土壤电导率0-20000μS/cm')
soil_PH: float = Field(None, description='土壤PH:3~9PH')
soil_potassium_phosphate_nitrogen: float = Field(None, description='土壤磷酸钾氮的标准值在140-225mg/kg')
soil_potassium_phosphate_phosphorus: float = Field(None, description='土壤磷酸钾磷的标准值在57-100mg/kg,')
soil_potassium_phosphate_potassium: float = Field(None, description='土壤磷酸钾钾的标准值在106-150mg/kg')
class LOT_DATA_MODEL(Base):
"""
物联网数据模型
"""
__tablename__ = 'LOT_DATA_MODEL'
id = Column(Integer, primary_key=True, autoincrement=True, comment='ID')
device_name = Column(String(50), comment='设备编号', default="0")
create_time = Column(Integer, comment="创建时间")
wind_speed = Column(Float, comment='风速:(0到30)m/s ')
wind_direction = Column(Float, comment='风向:0360°')
ambient_temperature = Column(Float, comment='环境温度:℃')
ambient_humidity = Column(Float, comment='环境湿度:%RH')
carbon_dioxide = Column(Float, comment='二氧化碳0~5000ppm')
ambient_air_pressure = Column(Float, comment='环境气压0~120KPa')
rainfall = Column(Float, comment='雨量mm')
ambient_lighting = Column(Float, comment='环境光照0-65535Lux0-20万Lux')
soil_temperature = Column(Float, comment='土壤温度:-40~80℃')
soil_moisture = Column(Float, comment='土壤湿度0-100%RH')
soil_conductivity = Column(Float, comment='土壤电导率0-20000μS/cm')
soil_PH = Column(Float, comment='土壤PH:3~9PH')
soil_potassium_phosphate_nitrogen = Column(Float, comment='土壤磷酸钾氮的标准值在140-225mg/kg')
soil_potassium_phosphate_phosphorus = Column(Float, comment='土壤磷酸钾磷的标准值在57-100mg/kg,')
soil_potassium_phosphate_potassium = Column(Float, comment='土壤磷酸钾钾的标准值在106-150mg/kg')
def __repr__(self):
return f"LOT_DATA_MODEL(id={self.id}, wind_speed={self.wind_speed}, wind_direction={self.wind_direction}, ambient_temperature={self.ambient_temperature}, ambient_humidity={self.ambient_humidity}, carbon_dioxide={self.carbon_dioxide}, ambient_air_pressure={self.ambient_air_pressure}, rainfall={self.rainfall}, ambient_lighting={self.ambient_lighting}, soil_temperature={self.soil_temperature}, soil_moisture={self.soil_moisture}, soil_conductivity={self.soil_conductivity}, Soil_PH={self.soil_PH}, soil_potassium_phosphate_nitrogen={self.soil_potassium_phosphate_nitrogen}, soil_potassium_phosphate_phosphorus={self.soil_potassium_phosphate_phosphorus}, soil_potassium_phosphate_potassium={self.soil_potassium_phosphate_potassium})"

1
db/repository/__init__.py Executable file
View File

@ -0,0 +1 @@
from .lot_data_repository import *

Binary file not shown.

View File

@ -0,0 +1,51 @@
import time
from db.models.log_data_model import LOT_DATA_MODEL, LOT_DATA
from db.session import with_session
@with_session
def add_kb_to_db(session, data: LOT_DATA, device_name: str):
# 创建知识库实例
kb = LOT_DATA_MODEL(**data.__dict__, device_name=device_name, create_time=int(time.time()))
session.add(kb)
@with_session
def delete_kb_from_db(session, id):
kb = session.query(LOT_DATA_MODEL).filter_by(id=id).first()
if kb:
session.delete(kb)
return True
# 直接查询所有数据
@with_session
def get_kb_detail(session):
all_data = session.query(LOT_DATA_MODEL).all()
data = [LOT_DATA(**d.__dict__) for d in all_data]
return data
# 根据时间查询数据
@with_session
def get_kb_detail_by_time(session, start_time, end_time):
all_data = session.query(LOT_DATA_MODEL).filter(
start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).all()
data = [LOT_DATA(**d.__dict__) for d in all_data]
return data
# 删除数据
@with_session
def delete_kb_detail_by_time(session, start_time, end_time):
d = session.query(LOT_DATA_MODEL).filter(start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).delete()
return d
# 查询数据
@with_session
def get_data_by_field(session, field, value):
query = session.query(LOT_DATA_MODEL).filter(getattr(LOT_DATA_MODEL, field) == value)
result = query.all()
return result

46
db/session.py Executable file
View File

@ -0,0 +1,46 @@
from functools import wraps
from contextlib import contextmanager
from db.base import SessionLocal
from sqlalchemy.orm import Session
@contextmanager
def session_scope() -> Session:
"""上下文管理器用于自动获取 Session, 避免错误"""
session = SessionLocal()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def with_session(f):
@wraps(f)
def wrapper(*args, **kwargs):
with session_scope() as session:
try:
result = f(session, *args, **kwargs)
session.commit()
return result
except:
session.rollback()
raise
return wrapper
def get_db() -> SessionLocal:
db = SessionLocal()
try:
yield db
finally:
db.close()
def get_db0() -> SessionLocal:
db = SessionLocal()
return db

1
exec_sh.sh Normal file
View File

@ -0,0 +1 @@
#!/bin/bash

BIN
lot_data.db Normal file

Binary file not shown.

7
reload_conf.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
#复制配置文件
cp -r conf/*.conf /etc/supervisor/conf.d/
# 重启所有配置
supervisorctl reload
# 推流不启动
supervisorctl stop push_stream

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
pydantic~=1.10.13
sqlalchemy~=2.0.19
uvicorn~=0.23.2
fastapi~=0.95.1
paho-mqtt~=1.6.1
requests~=2.31.0

2
start_push.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
supervisorctl start push_stream

2
stop_push.sh Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
supervisorctl stop push_stream

26
test.py Normal file
View File

@ -0,0 +1,26 @@
import datetime
import time
import requests
while (True):
print(time.time())
time.sleep(10.0)
# data = {
# "wind_speed": 26,
# "wind_direction": 6,
# "ambient_temperature": 66,
# "ambient_humidity": 78,
# "carbon_dioxide": 94,
# "ambient_air_pressure": 48,
# "rainfall": 3,
# "ambient_lighting": 59,
# "soil_temperature": 71,
# "soil_moisture": 19,
# "soil_conductivity": 62,
# "soil_PH": 49,
# "soil_potassium_phosphate_nitrogen": 5,
# "soil_potassium_phosphate_phosphorus": 5,
# "soil_potassium_phosphate_potassium": 86
# }
# requests.post("http://192.168.1.27:8000/add/lot_data", json=data)

18
tool.py Normal file
View File

@ -0,0 +1,18 @@
import subprocess
def push_stream():
print("启动推流")
subprocess.Popen(['start_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def close_stream():
subprocess.Popen(['stop_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def exec_sh():
subprocess.Popen(['exec_sh.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def update():
subprocess.Popen(['update.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

4
update.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
# TODO git更新
git pull