2024-01-22 18:01:03 +08:00
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
|
2024-01-24 17:49:33 +08:00
|
|
|
from config import info_topic
|
2024-01-22 18:01:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
# 统一返回
|
|
|
|
def publish_payload(code, msg):
|
|
|
|
return json.dumps({
|
|
|
|
"code": code,
|
|
|
|
"msg": msg
|
|
|
|
}, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
|
|
def exception_handler(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
print(f"函数{func.__name__}中发生了异常:{e}")
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def push_stream(client):
|
2024-01-25 16:30:44 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/agri_xumu/bash/start_push_stream.sh'],
|
2024-01-22 18:01:03 +08:00
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
output = out.decode('utf-8').strip()
|
|
|
|
client.publish(info_topic, payload=publish_payload(200, output), qos=0)
|
|
|
|
|
|
|
|
|
|
|
|
def close_stream(client):
|
2024-01-25 16:30:44 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/agri_xumu/bash/stop_push_stream.sh'],
|
2024-01-22 18:01:03 +08:00
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
output = out.decode('utf-8').strip()
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
|
|
|
|
|
|
|
|
|
|
|
|
def exec_sh(msg, client):
|
|
|
|
origin_data = json.loads(msg.payload.decode('utf-8'))
|
|
|
|
if 'data' not in origin_data:
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=404, msg='data must be supplied'), qos=0)
|
|
|
|
return
|
|
|
|
cmd = origin_data["data"]
|
|
|
|
if cmd in ["supervisorctl stop __mqtt__",
|
|
|
|
"supervisorctl restart __mqtt__",
|
|
|
|
"supervisorctl stop all"]:
|
|
|
|
return
|
|
|
|
if cmd == "supervisorctl reload":
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg='reloading'), qos=0)
|
|
|
|
subprocess.Popen([cmd], shell=True)
|
|
|
|
return
|
|
|
|
p = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
output = out.decode('utf-8').strip()
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
|
|
|
|
|
|
|
|
|
|
|
|
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').strip()
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
|
|
|
|
|
|
|
|
|
|
|
|
def update(client):
|
2024-01-25 16:30:44 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/agri_xumu/git_update.sh'],
|
2024-01-22 18:01:03 +08:00
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
output = out.decode('utf-8').strip()
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output),
|
|
|
|
qos=0)
|
|
|
|
|
|
|
|
|
|
|
|
def reload(client):
|
|
|
|
client.publish(info_topic, payload=publish_payload(200, "reloading"), qos=0)
|
|
|
|
subprocess.Popen(['supervisorctl reload'], shell=True)
|