2023-12-02 10:53:31 +08:00
|
|
|
import json
|
2023-12-04 13:22:54 +08:00
|
|
|
import os
|
2023-12-02 10:53:31 +08:00
|
|
|
import subprocess
|
2023-12-19 10:02:32 +08:00
|
|
|
import threading
|
2023-12-02 10:53:31 +08:00
|
|
|
|
2023-12-04 13:22:54 +08:00
|
|
|
import requests
|
2023-12-18 10:51:06 +08:00
|
|
|
|
|
|
|
from config import mp4_path, post_record_list_url, post_record_url, info_topic
|
|
|
|
|
|
|
|
|
2023-12-18 11:06:04 +08:00
|
|
|
# 统一返回
|
|
|
|
def publish_payload(code, msg):
|
2023-12-18 11:22:31 +08:00
|
|
|
return json.dumps({
|
2023-12-18 11:25:10 +08:00
|
|
|
"code": code,
|
2023-12-18 11:22:31 +08:00
|
|
|
"msg": msg
|
|
|
|
}, ensure_ascii=False)
|
2023-12-18 11:06:04 +08:00
|
|
|
|
|
|
|
|
2023-12-14 14:21:17 +08:00
|
|
|
def exception_handler(func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
print(f"函数{func.__name__}中发生了异常:{e}")
|
|
|
|
|
|
|
|
return wrapper
|
2023-12-18 10:51:06 +08:00
|
|
|
|
|
|
|
|
2023-12-05 09:31:52 +08:00
|
|
|
def push_stream(client):
|
2023-12-11 09:09:07 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/start_push_stream.sh'],
|
|
|
|
shell=True,
|
2023-12-07 16:35:05 +08:00
|
|
|
stdout=subprocess.PIPE,
|
2023-12-07 16:07:53 +08:00
|
|
|
stderr=subprocess.PIPE)
|
2023-12-05 09:31:52 +08:00
|
|
|
out, err = p.communicate()
|
2023-12-11 09:09:07 +08:00
|
|
|
output = out.decode('utf-8').strip()
|
2023-12-18 11:25:10 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(200, output), qos=0)
|
2023-12-02 10:53:31 +08:00
|
|
|
|
|
|
|
|
2023-12-05 09:31:52 +08:00
|
|
|
def close_stream(client):
|
2023-12-11 09:09:07 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/bash/stop_push_stream.sh'],
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2023-12-05 09:31:52 +08:00
|
|
|
out, err = p.communicate()
|
2023-12-11 09:09:07 +08:00
|
|
|
output = out.decode('utf-8').strip()
|
2023-12-18 11:25:10 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
|
2023-12-02 10:53:31 +08:00
|
|
|
|
|
|
|
|
2023-12-05 09:14:18 +08:00
|
|
|
def exec_sh(msg, client):
|
2023-12-07 18:02:17 +08:00
|
|
|
origin_data = json.loads(msg.payload.decode('utf-8'))
|
|
|
|
if 'data' not in origin_data:
|
2023-12-18 11:06:04 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(code=404, msg='data must be supplied'), qos=0)
|
2023-12-07 18:02:17 +08:00
|
|
|
return
|
|
|
|
cmd = origin_data["data"]
|
2023-12-14 09:40:53 +08:00
|
|
|
if cmd in ["supervisorctl stop __mqtt__",
|
|
|
|
"supervisorctl restart __mqtt__",
|
|
|
|
"supervisorctl stop all"]:
|
2023-12-05 09:40:57 +08:00
|
|
|
return
|
2023-12-05 09:45:55 +08:00
|
|
|
if cmd == "supervisorctl reload":
|
2023-12-18 11:06:04 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg='reloading'), qos=0)
|
2023-12-05 09:40:57 +08:00
|
|
|
subprocess.Popen([cmd], shell=True)
|
2023-12-04 10:05:04 +08:00
|
|
|
return
|
2023-12-26 17:48:05 +08:00
|
|
|
subprocess.Popen([cmd], shell=True)
|
|
|
|
# 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)
|
2023-12-02 10:53:31 +08:00
|
|
|
|
|
|
|
|
2023-12-05 09:31:52 +08:00
|
|
|
def get_status(client):
|
2023-12-11 09:09:07 +08:00
|
|
|
p = subprocess.Popen(['supervisorctl status'],
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2023-12-05 09:31:52 +08:00
|
|
|
out, err = p.communicate()
|
2023-12-11 09:09:07 +08:00
|
|
|
output = out.decode('utf-8').strip()
|
2023-12-18 11:25:10 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output), qos=0)
|
2023-12-05 09:31:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def update(client):
|
2023-12-07 16:07:53 +08:00
|
|
|
p = subprocess.Popen(['/bin/bash /home/pi/lot_manager/git_update.sh'],
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
2023-12-05 09:31:52 +08:00
|
|
|
out, err = p.communicate()
|
2023-12-11 09:09:07 +08:00
|
|
|
output = out.decode('utf-8').strip()
|
2023-12-18 11:25:10 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=output),
|
2023-12-18 11:06:04 +08:00
|
|
|
qos=0)
|
2023-12-02 18:32:17 +08:00
|
|
|
|
|
|
|
|
2023-12-14 09:38:19 +08:00
|
|
|
def reload(client):
|
2023-12-18 11:06:04 +08:00
|
|
|
client.publish(info_topic, payload=publish_payload(200, "reloading"), qos=0)
|
2023-12-14 09:54:38 +08:00
|
|
|
subprocess.Popen(['supervisorctl reload'], shell=True)
|
2023-12-14 09:38:19 +08:00
|
|
|
|
|
|
|
|
2023-12-14 10:51:16 +08:00
|
|
|
def get_list_record(client):
|
2023-12-18 11:28:45 +08:00
|
|
|
try:
|
|
|
|
data = {
|
|
|
|
"data": os.listdir(mp4_path)
|
|
|
|
}
|
|
|
|
r = requests.post(post_record_list_url, json=data)
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0)
|
|
|
|
except:
|
|
|
|
pass
|
2023-12-02 20:00:27 +08:00
|
|
|
|
|
|
|
|
2023-12-19 10:02:32 +08:00
|
|
|
def up(msg, client):
|
2023-12-18 11:28:45 +08:00
|
|
|
try:
|
|
|
|
filename = json.loads(msg.payload.decode('utf-8'))["data"]
|
|
|
|
if filename is None or filename == '':
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=404, msg='没有该文件'), qos=0)
|
|
|
|
return
|
|
|
|
|
|
|
|
files = {filename: open(os.path.join(mp4_path, filename), 'rb'), "Content-Type": "application/octet-stream"}
|
|
|
|
r = requests.post(post_record_url, files=files)
|
|
|
|
client.publish(info_topic, payload=publish_payload(code=200, msg=str(r)), qos=0)
|
|
|
|
except:
|
|
|
|
pass
|
2023-12-19 10:02:32 +08:00
|
|
|
|
2023-12-26 17:48:05 +08:00
|
|
|
|
2023-12-19 10:02:32 +08:00
|
|
|
def get_record(msg, client):
|
|
|
|
threading.Thread(target=up, args=(msg, client)).start()
|