diff --git a/MQTT.py b/MQTT.py index 271ada5..46ec712 100644 --- a/MQTT.py +++ b/MQTT.py @@ -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) diff --git a/test.py b/test.py index 4868eb2..c82d622 100644 --- a/test.py +++ b/test.py @@ -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) diff --git a/tool.py b/tool.py index 702a6ba..0cfcccc 100755 --- a/tool.py +++ b/tool.py @@ -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)