2023-12-01 09:21:23 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
|
|
from tool import push_stream, close_stream, update, exec_sh
|
|
|
|
|
|
|
|
|
2023-12-01 18:00:11 +08:00
|
|
|
class MQTT:
|
|
|
|
def on_connect(self, client, userdata, flags, rc):
|
|
|
|
if rc == 0:
|
|
|
|
client.subscribe('lot_stream')
|
2023-12-02 09:28:42 +08:00
|
|
|
|
2023-12-01 18:00:11 +08:00
|
|
|
# Message receiving callback
|
|
|
|
def on_message(self, client, userdata, msg):
|
|
|
|
data = json.loads(msg.payload.decode('utf-8'))["msg"]
|
|
|
|
if data == "push_stream":
|
|
|
|
# 启动推流视频
|
|
|
|
push_stream()
|
2023-12-02 09:28:42 +08:00
|
|
|
client.publish('success', payload='push_stream success', qos=0)
|
2023-12-01 18:00:11 +08:00
|
|
|
elif data == "close_stream":
|
|
|
|
# 关闭推流视频
|
|
|
|
close_stream()
|
2023-12-02 09:28:42 +08:00
|
|
|
client.publish('success', payload='close_stream success', qos=0)
|
2023-12-01 18:00:11 +08:00
|
|
|
elif data == "exec":
|
|
|
|
# 执行命令
|
|
|
|
exec_sh(msg)
|
2023-12-02 09:28:42 +08:00
|
|
|
client.publish('success', payload='exec_sh success', qos=0)
|
2023-12-01 18:00:11 +08:00
|
|
|
elif data == "update":
|
|
|
|
# git更新项目和配置文件
|
|
|
|
update()
|
2023-12-02 09:28:42 +08:00
|
|
|
client.publish('success', payload='update success', qos=0)
|
2023-12-01 18:00:11 +08:00
|
|
|
else:
|
|
|
|
client.publish('error', payload='No Such Type', qos=0)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.client = mqtt.Client()
|
|
|
|
self.client.username_pw_set("ceshi", "123456")
|
|
|
|
# Specify callback function
|
|
|
|
self.client.on_connect = self.on_connect
|
|
|
|
self.client.on_message = self.on_message
|
|
|
|
# Establish a connection
|
2023-12-01 20:21:48 +08:00
|
|
|
self.client.connect('60.204.152.17', 1883)
|
2023-12-01 18:00:11 +08:00
|
|
|
# Publish a message
|
|
|
|
self.client.loop_forever(retry_first_connection=True)
|
2023-12-01 09:21:23 +08:00
|
|
|
|
2023-12-02 09:28:42 +08:00
|
|
|
|
2023-12-01 09:21:23 +08:00
|
|
|
if __name__ == '__main__':
|
2023-12-01 18:00:11 +08:00
|
|
|
mq = MQTT()
|