更新 tool.py

This commit is contained in:
xyj 2023-12-27 11:24:37 +08:00
parent 8d45809094
commit b0e63bc5b5
1 changed files with 122 additions and 122 deletions

244
tool.py
View File

@ -1,122 +1,122 @@
import json import json
import os import os
import subprocess import subprocess
import threading import threading
import requests import requests
from config import mp4_path, post_record_list_url, post_record_url, info_topic from config import mp4_path, post_record_list_url, post_record_url, info_topic
# 统一返回 # 统一返回
def publish_payload(code, msg): def publish_payload(code, msg):
return json.dumps({ return json.dumps({
"code": code, "code": code,
"msg": msg "msg": msg
}, ensure_ascii=False) }, ensure_ascii=False)
def exception_handler(func): def exception_handler(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
return func(*args, **kwargs) return func(*args, **kwargs)
except Exception as e: except Exception as e:
print(f"函数{func.__name__}中发生了异常:{e}") print(f"函数{func.__name__}中发生了异常:{e}")
return wrapper return wrapper
def push_stream(client): def push_stream(client):
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/start_push_stream.sh'], p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/start_push_stream.sh'],
shell=True, shell=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
output = out.decode('utf-8').strip() output = out.decode('utf-8').strip()
client.publish(info_topic, payload=publish_payload(200, output), qos=0) client.publish(info_topic, payload=publish_payload(200, output), qos=0)
def close_stream(client): def close_stream(client):
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/stop_push_stream.sh'], p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/stop_push_stream.sh'],
shell=True, shell=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
output = out.decode('utf-8').strip() output = out.decode('utf-8').strip()
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
def exec_sh(msg, client): def exec_sh(msg, client):
origin_data = json.loads(msg.payload.decode('utf-8')) origin_data = json.loads(msg.payload.decode('utf-8'))
if 'data' not in origin_data: if 'data' not in origin_data:
client.publish(info_topic, payload=publish_payload(code=404, msg='data must be supplied'), qos=0) client.publish(info_topic, payload=publish_payload(code=404, msg='data must be supplied'), qos=0)
return return
cmd = origin_data["data"] cmd = origin_data["data"]
if cmd in ["supervisorctl stop __mqtt__", if cmd in ["supervisorctl stop __mqtt__",
"supervisorctl restart __mqtt__", "supervisorctl restart __mqtt__",
"supervisorctl stop all"]: "supervisorctl stop all"]:
return return
if cmd == "supervisorctl reload": if cmd == "supervisorctl reload":
client.publish(info_topic, payload=publish_payload(code=200, msg='reloading'), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg='reloading'), qos=0)
subprocess.Popen([cmd], shell=True) subprocess.Popen([cmd], shell=True)
return return
subprocess.Popen([cmd], shell=True) # subprocess.Popen([cmd], shell=True)
# p = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# out, err = p.communicate() out, err = p.communicate()
# output = out.decode('utf-8').strip() output = out.decode('utf-8').strip()
# client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
def get_status(client): def get_status(client):
p = subprocess.Popen(['supervisorctl status'], p = subprocess.Popen(['supervisorctl status'],
shell=True, shell=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
output = out.decode('utf-8').strip() output = out.decode('utf-8').strip()
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
def update(client): def update(client):
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/git_update.sh'], p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/git_update.sh'],
shell=True, shell=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
output = out.decode('utf-8').strip() output = out.decode('utf-8').strip()
client.publish(info_topic, payload=publish_payload(code=200, msg=output), client.publish(info_topic, payload=publish_payload(code=200, msg=output),
qos=0) qos=0)
def reload(client): def reload(client):
client.publish(info_topic, payload=publish_payload(200, "reloading"), qos=0) client.publish(info_topic, payload=publish_payload(200, "reloading"), qos=0)
subprocess.Popen(['supervisorctl reload'], shell=True) subprocess.Popen(['supervisorctl reload'], shell=True)
def get_list_record(client): def get_list_record(client):
try: try:
data = { data = {
"data": os.listdir(mp4_path) "data": os.listdir(mp4_path)
} }
r = requests.post(post_record_list_url, json=data) r = requests.post(post_record_list_url, json=data)
client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0)
except: except:
pass pass
def up(msg, client): def up(msg, client):
try: try:
filename = json.loads(msg.payload.decode('utf-8'))["data"] filename = json.loads(msg.payload.decode('utf-8'))["data"]
if filename is None or filename == '': if filename is None or filename == '':
client.publish(info_topic, payload=publish_payload(code=404, msg='没有该文件'), qos=0) client.publish(info_topic, payload=publish_payload(code=404, msg='没有该文件'), qos=0)
return return
files = {filename: open(os.path.join(mp4_path, filename), 'rb'), "Content-Type": "application/octet-stream"} files = {filename: open(os.path.join(mp4_path, filename), 'rb'), "Content-Type": "application/octet-stream"}
r = requests.post(post_record_url, files=files) r = requests.post(post_record_url, files=files)
client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0) client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0)
except: except:
pass pass
def get_record(msg, client): def get_record(msg, client):
threading.Thread(target=up, args=(msg, client)).start() threading.Thread(target=up, args=(msg, client)).start()