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

28
tool.py
View File

@ -7,12 +7,18 @@ import requests
mp4_path = '/home/pi/mp4'
def push_stream():
subprocess.Popen(['/bin/bash start_push.sh'], shell=True)
def push_stream(client):
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():
subprocess.Popen(['/bin/bash stop_push.sh'], shell=True)
def close_stream(client):
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):
@ -25,8 +31,18 @@ def exec_sh(msg, client):
client.publish('success', payload=json.dumps(output, ensure_ascii=False), qos=0)
def update():
subprocess.Popen(['/bin/bash update.sh'], shell=True)
def get_status(client):
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():