57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
import json
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from tool import push_stream, close_stream, update, exec_sh, get_record, get_list_record, get_status
|
|
|
|
|
|
class MQTT:
|
|
def on_connect(self, client, userdata, flags, rc):
|
|
if rc == 0:
|
|
client.subscribe('lot_mqtt')
|
|
def on_connect_fail(self):
|
|
pass
|
|
# 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)
|
|
elif data == "close_stream":
|
|
# 关闭推流视频
|
|
close_stream(client)
|
|
elif data == "exec":
|
|
# 执行命令
|
|
exec_sh(msg, client)
|
|
elif data == "update":
|
|
# git更新项目和配置文件
|
|
update(client)
|
|
elif data == "record_list":
|
|
# 查看录像列表
|
|
get_list_record()
|
|
elif data == "record":
|
|
# 获取录像
|
|
get_record(msg, self.client)
|
|
elif data == "status":
|
|
# 查看运行状态
|
|
get_status(self.client)
|
|
else:
|
|
# 错误类型
|
|
client.publish('success', payload='No Such Type', qos=0)
|
|
|
|
def __init__(self):
|
|
self.client = mqtt.Client(client_id='device1')
|
|
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
|
|
# ceshi-mqtt.lihaink.cn
|
|
self.client.connect('192.168.1.27', 1883)
|
|
# Publish a message
|
|
self.client.loop_forever(retry_first_connection=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
mq = MQTT()
|