This commit is contained in:
xyj 2023-12-29 09:56:44 +08:00
parent c00f2c2c62
commit 86a4fa3265
3 changed files with 24 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import subprocess
import time
import paho.mqtt.client as mqtt
@ -34,6 +34,8 @@ class MQTTClient:
self.client.subscribe(self.topic)
client.publish(info_topic, payload=publish_payload(code=200, msg='成功订阅' + self.topic),
qos=0)
# 关闭摄像机电
subprocess.Popen(['sudo /usr/bin/python close_led.py'], shell=True)
def on_message(self, client, userdata, msg):
if not valid(msg, client):
@ -65,6 +67,10 @@ class MQTTClient:
elif data == "status":
# 查看运行状态
get_status(client)
elif data == "open_led":
open_led()
elif data == "close_led":
close_led()
else:
# 错误类型
client.publish(info_topic, payload=publish_payload(code=404, msg='No Such Msg Type'), qos=0)

View File

@ -8,8 +8,6 @@ import paho.mqtt.client as mqtt
app = FastAPI()
class MQTTClient:
def __init__(self, broker, port, topic, username, password):
self.broker = broker
@ -22,12 +20,16 @@ class MQTTClient:
self.client.username_pw_set(self.username, self.password)
def push(self):
self.client.publish(self.topic, payload=json.dumps({"msg": "open_led"}, ensure_ascii=False),
qos=0)
self.client.publish(self.topic, payload=json.dumps({"msg": "push_stream"}, ensure_ascii=False),
qos=0)
def close(self):
self.client.publish(self.topic, payload=json.dumps({"msg": "close_stream"}, ensure_ascii=False),
qos=0)
self.client.publish(self.topic, payload=json.dumps({"msg": "close_led"}, ensure_ascii=False),
qos=0)
def start(self):
self.client.connect(self.broker, self.port)

18
tool.py
View File

@ -60,11 +60,11 @@ def exec_sh(msg, client):
client.publish(info_topic, payload=publish_payload(code=200, msg='reloading'), qos=0)
subprocess.Popen([cmd], shell=True)
return
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)
# 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)
def get_status(client):
@ -120,3 +120,11 @@ def up(msg, client):
def get_record(msg, client):
threading.Thread(target=up, args=(msg, client)).start()
def open_led():
subprocess.Popen(['sudo /usr/bin/python open_led.py'], shell=True)
def close_led():
subprocess.Popen(['sudo /usr/bin/python close_led.py'], shell=True)