lot_manager/MQTT.py

46 lines
1.1 KiB
Python
Raw Normal View History

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
# 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":
2023-12-01 13:51:47 +08:00
# 启动推流视频
2023-12-01 09:21:23 +08:00
push_stream()
elif data == "close_stream":
2023-12-01 13:51:47 +08:00
# 关闭推流视频
2023-12-01 09:21:23 +08:00
close_stream()
elif data == "exec":
2023-12-01 13:51:47 +08:00
# 执行命令
2023-12-01 15:12:41 +08:00
exec_sh(msg)
2023-12-01 09:21:23 +08:00
elif data == "update":
2023-12-01 13:51:47 +08:00
# git更新项目和配置文件
2023-12-01 09:21:23 +08:00
update()
else:
2023-12-01 15:12:41 +08:00
client.publish('error', payload='No Such Type', qos=0)
2023-12-01 09:21:23 +08:00
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
2023-12-01 13:51:47 +08:00
client.connect('127.0.0.1', 1883)
2023-12-01 09:21:23 +08:00
# Publish a message
client.loop_forever()