lot_manager/MQTT.py

51 lines
1.6 KiB
Python
Raw Normal View History

2023-12-02 10:53:31 +08:00
import json
import paho.mqtt.client as mqtt
2023-12-02 19:24:31 +08:00
from tool import push_stream, close_stream, update, exec_sh, get_record
2023-12-02 10:53:31 +08:00
class MQTT:
def on_connect(self, client, userdata, flags, rc):
if rc == 0:
client.subscribe('lot_mqtt')
# 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()
client.publish('success', payload='push_stream success', qos=0)
elif data == "close_stream":
# 关闭推流视频
close_stream()
client.publish('success', payload='close_stream success', qos=0)
elif data == "exec":
# 执行命令
exec_sh(msg)
client.publish('success', payload='exec_sh success', qos=0)
elif data == "update":
# git更新项目和配置文件
update()
client.publish('success', payload='update success', qos=0)
2023-12-02 18:32:17 +08:00
elif data == "record":
2023-12-02 19:24:31 +08:00
get_record(msg, self.client)
2023-12-02 10:53:31 +08:00
else:
client.publish('error', payload='No Such Type', qos=0)
def __init__(self):
self.client = mqtt.Client()
self.client.username_pw_set("demo", "123456")
# Specify callback function
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
# Establish a connection
self.client.connect('ceshi-mqtt.lihaink.cn', 1883)
# Publish a message
self.client.loop_forever(retry_first_connection=True)
if __name__ == '__main__':
mq = MQTT()