lot_manager/MQTT.py

55 lines
1.7 KiB
Python
Raw Normal View History

2023-12-02 10:53:31 +08:00
import json
import paho.mqtt.client as mqtt
2023-12-05 09:31:52 +08:00
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
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":
# 启动推流视频
2023-12-05 09:31:52 +08:00
push_stream(client)
2023-12-02 10:53:31 +08:00
elif data == "close_stream":
# 关闭推流视频
2023-12-05 09:31:52 +08:00
close_stream(client)
2023-12-02 10:53:31 +08:00
elif data == "exec":
# 执行命令
2023-12-05 09:14:18 +08:00
exec_sh(msg, client)
2023-12-02 10:53:31 +08:00
elif data == "update":
# git更新项目和配置文件
2023-12-05 09:31:52 +08:00
update(client)
2023-12-04 13:46:37 +08:00
elif data == "record_list":
2023-12-05 09:31:52 +08:00
# 查看录像列表
2023-12-04 13:46:37 +08:00
get_list_record()
2023-12-02 18:32:17 +08:00
elif data == "record":
2023-12-05 09:31:52 +08:00
# 获取录像
2023-12-02 19:24:31 +08:00
get_record(msg, self.client)
2023-12-05 09:31:52 +08:00
elif data == "status":
# 查看运行状态
get_status(client)
2023-12-02 10:53:31 +08:00
else:
2023-12-05 09:31:52 +08:00
# 错误类型
client.publish('success', payload='No Such Type', qos=0)
2023-12-02 10:53:31 +08:00
def __init__(self):
2023-12-05 09:31:52 +08:00
self.client = mqtt.Client(client_id='device1')
2023-12-02 10:53:31 +08:00
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()