This commit is contained in:
xyj 2023-12-05 09:31:52 +08:00
parent 5366686433
commit 516b9bdfed
2 changed files with 34 additions and 16 deletions

22
MQTT.py
View File

@ -2,7 +2,7 @@ import json
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
class MQTT: class MQTT:
@ -15,29 +15,31 @@ class MQTT:
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)
client.publish('success', payload='push_stream success', qos=0)
elif data == "close_stream": elif data == "close_stream":
# 关闭推流视频 # 关闭推流视频
close_stream() close_stream(client)
client.publish('success', payload='close_stream success', qos=0)
elif data == "exec": elif data == "exec":
# 执行命令 # 执行命令
exec_sh(msg, client) exec_sh(msg, client)
client.publish('success', payload='exec_sh success', qos=0)
elif data == "update": elif data == "update":
# git更新项目和配置文件 # git更新项目和配置文件
update() update(client)
client.publish('success', payload='update success', qos=0)
elif data == "record_list": elif data == "record_list":
# 查看录像列表
get_list_record() get_list_record()
elif data == "record": elif data == "record":
# 获取录像
get_record(msg, self.client) get_record(msg, self.client)
elif data == "status":
# 查看运行状态
get_status(client)
else: else:
client.publish('error', payload='No Such Type', qos=0) # 错误类型
client.publish('success', payload='No Such Type', qos=0)
def __init__(self): def __init__(self):
self.client = mqtt.Client(client_id='') self.client = mqtt.Client(client_id='device1')
self.client.username_pw_set("demo", "123456") self.client.username_pw_set("demo", "123456")
# Specify callback function # Specify callback function
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect

28
tool.py
View File

@ -7,12 +7,18 @@ import requests
mp4_path = '/home/pi/mp4' mp4_path = '/home/pi/mp4'
def push_stream(): def push_stream(client):
subprocess.Popen(['/bin/bash start_push.sh'], shell=True) p = subprocess.Popen(['/bin/bash start_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
output = out.decode('utf-8')
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def close_stream(): def close_stream(client):
subprocess.Popen(['/bin/bash stop_push.sh'], shell=True) p = subprocess.Popen(['/bin/bash stop_push.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
output = out.decode('utf-8')
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def exec_sh(msg, client): def exec_sh(msg, client):
@ -25,8 +31,18 @@ def exec_sh(msg, client):
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0) client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def update(): def get_status(client):
subprocess.Popen(['/bin/bash update.sh'], shell=True) p = subprocess.Popen(['supervisorctl status'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
output = out.decode('utf-8')
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def update(client):
p = subprocess.Popen(['/bin/bash update.sh'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
output = out.decode('utf-8')
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def get_list_record(): def get_list_record():