lot_manager/MQTT.py

46 lines
1.1 KiB
Python

import json
import paho.mqtt.client as mqtt
from tool import push_stream, close_stream, update, exec_sh
# Connection success callback
def on_connect(client, userdata, flags, rc):
print(rc)
if rc == 0:
client.subscribe('lot_data')
else:
client.publish('conn_error', payload=rc, qos=0)
# Message receiving callback
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode('utf-8'))["msg"]
if data == "push_stream":
# 启动推流视频
push_stream()
elif data == "close_stream":
# 关闭推流视频
close_stream()
elif data == "exec":
# 执行命令
exec_sh()
elif data == "update":
# git更新项目和配置文件
update()
else:
client.publish('conn_error', payload='No Such Type', qos=0)
if __name__ == '__main__':
client = mqtt.Client()
client.username_pw_set("ceshi", "123456")
# Specify callback function
client.on_connect = on_connect
client.on_message = on_message
# Establish a connection
client.connect('127.0.0.1', 1883)
# Publish a message
client.loop_forever()