This commit is contained in:
xyj 2023-12-02 09:28:42 +08:00
parent 83e70e8508
commit f80f71e3a2
4 changed files with 26 additions and 10 deletions

View File

@ -9,21 +9,26 @@ class MQTT:
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
if rc == 0: if rc == 0:
client.subscribe('lot_stream') client.subscribe('lot_stream')
# Message receiving callback # Message receiving callback
def on_message(self, client, userdata, msg): def on_message(self, client, userdata, msg):
data = json.loads(msg.payload.decode('utf-8'))["msg"] data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream": if data == "push_stream":
# 启动推流视频 # 启动推流视频
push_stream() push_stream()
client.publish('success', payload='push_stream success', qos=0)
elif data == "close_stream": elif data == "close_stream":
# 关闭推流视频 # 关闭推流视频
close_stream() close_stream()
client.publish('success', payload='close_stream success', qos=0)
elif data == "exec": elif data == "exec":
# 执行命令 # 执行命令
exec_sh(msg) exec_sh(msg)
client.publish('success', payload='exec_sh success', qos=0)
elif data == "update": elif data == "update":
# git更新项目和配置文件 # git更新项目和配置文件
update() update()
client.publish('success', payload='update success', qos=0)
else: else:
client.publish('error', payload='No Such Type', qos=0) client.publish('error', payload='No Such Type', qos=0)
@ -38,5 +43,6 @@ class MQTT:
# Publish a message # Publish a message
self.client.loop_forever(retry_first_connection=True) self.client.loop_forever(retry_first_connection=True)
if __name__ == '__main__': if __name__ == '__main__':
mq = MQTT() mq = MQTT()

10
api.py
View File

@ -3,7 +3,7 @@ import subprocess
import uvicorn import uvicorn
from db.models.base import BaseResponse from db.models.base import BaseResponse
from db.models.log_data_model import LOT_DATA from db.models.log_data_model import LOT_DATA, LOT_DATA_MODEL
from db.repository import add_kb_to_db, get_kb_detail, get_kb_detail_by_time, delete_kb_detail_by_time 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 from fastapi import FastAPI
@ -17,6 +17,14 @@ def add(data: LOT_DATA):
return BaseResponse(code=500, msg=e) return BaseResponse(code=500, msg=e)
def delete(start_time, end_time):
try:
delete_kb_detail_by_time(start_time, end_time)
return BaseResponse()
except Exception as e:
return BaseResponse(code=500, msg=e)
def get_data(): def get_data():
try: try:
data = get_kb_detail() data = get_kb_detail()

View File

@ -202,13 +202,14 @@ def run_with_client(client):
'soil_potassium_phosphate_potassium': jia_data, 'soil_potassium_phosphate_potassium': jia_data,
'rainfall': rainfall_data, 'rainfall': rainfall_data,
'wind_speed': speedwind_data, 'wind_speed': speedwind_data,
'wind_direction': winddirection_data} 'wind_direction': winddirection_data,
'create_time': int(time.time())
t = LOT_DATA(**data, create_time=int(time.time())) }
t = LOT_DATA(**data)
# TODO 判断数据是否正常 # TODO 判断数据是否正常
add(t)
# 发送给服务器 # 发送给服务器
client.publish('lot_data', payload=data, qos=0) client.publish('lot_data', payload=json.dumps(data, ensure_ascii=False), qos=0)
r = add(t)
def run_no_client(): def run_no_client():
@ -375,13 +376,12 @@ def run_no_client():
'wind_direction': winddirection_data} 'wind_direction': winddirection_data}
data = LOT_DATA(**data, create_time=int(time.time())) data = LOT_DATA(**data, create_time=int(time.time()))
# TODO 判断数据是否正常 # TODO 判断数据是否正常
add(data) r = add(data)
def t1(): def t1():
import time import time
for i in range(100): while (1):
time.sleep(2) time.sleep(2)
data = { data = {
"wind_speed": 1, "wind_speed": 1,
@ -411,6 +411,8 @@ def t2():
time = 3 time = 3
from threading import Thread from threading import Thread
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
global time global time
if rc == 0: if rc == 0:

View File

@ -11,7 +11,7 @@ def close_stream():
def exec_sh(msg): def exec_sh(msg):
cmd = json.loads(msg.payload.decode('utf-8'))["cmd"] cmd = json.loads(msg.payload.decode('utf-8'))["data"]
subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)