lot_manager/MQTT.py

94 lines
3.0 KiB
Python
Raw Normal View History

2023-12-11 14:42:49 +08:00
import datetime
2023-12-05 17:16:32 +08:00
import time
2023-12-09 18:44:14 +08:00
2023-12-02 10:53:31 +08:00
import paho.mqtt.client as mqtt
2023-12-09 18:44:14 +08:00
from tool import *
2023-12-11 14:11:12 +08:00
2023-12-11 16:46:03 +08:00
from config import broker, port, subscribe_topic, publish_topic, username, password
2023-12-11 14:56:51 +08:00
2023-12-07 19:00:16 +08:00
def valid(msg, client):
origin_data = json.loads(msg.payload.decode('utf-8'))
if 'msg' not in origin_data:
client.publish('error', payload='msg must be supplied', qos=0)
return False
return True
2023-12-07 15:27:38 +08:00
2023-12-09 18:44:14 +08:00
class MQTTClient:
2023-12-11 14:11:12 +08:00
def __init__(self, broker, port, topic, username, password):
2023-12-09 18:44:14 +08:00
self.broker = broker
self.port = port
self.topic = topic
2023-12-11 14:11:12 +08:00
self.username = username
self.password = password
2023-12-11 15:16:16 +08:00
# 千万不要指定client_id 不然死翘翘
2023-12-11 15:11:22 +08:00
self.client = mqtt.Client()
2023-12-11 16:46:03 +08:00
self.client.username_pw_set(self.username, self.password)
2023-12-09 18:44:14 +08:00
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
def on_connect(self, client, userdata, flags, rc):
2023-12-11 15:19:35 +08:00
if rc == 0:
2023-12-11 15:21:25 +08:00
self.client.subscribe(self.topic)
2023-12-11 16:57:21 +08:00
client.publish('success', payload='成功订阅' + subscribe_topic + ',time=' + str(datetime.datetime.now()),
2023-12-11 16:46:03 +08:00
qos=0)
2023-12-09 18:44:14 +08:00
def on_message(self, client, userdata, msg):
if not valid(msg, client):
client.publish('error', payload='验证失败', qos=0)
return
client.publish('success', payload='验证通过', qos=0)
try:
origin_data = json.loads(msg.payload.decode('utf-8'))
data = origin_data["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)
2023-12-14 09:49:53 +08:00
elif data == "reload":
2023-12-14 09:54:38 +08:00
# 重启配置
2023-12-14 09:38:19 +08:00
reload(client)
2023-12-09 18:44:14 +08:00
elif data == "record_list":
# 查看录像列表
get_list_record()
elif data == "record":
# 获取录像
get_record(msg, client)
elif data == "status":
# 查看运行状态
get_status(client)
else:
# 错误类型
client.publish('error', payload='No Such Type', qos=0)
except Exception as e:
pass
def start(self):
self.client.connect(self.broker, self.port)
2023-12-05 17:16:32 +08:00
2023-12-09 18:41:35 +08:00
if __name__ == '__main__':
2023-12-13 12:54:25 +08:00
# print(broker, port, subscribe_topic, publish_topic, username, password)
2023-12-11 16:46:03 +08:00
# MQTT客户端
MQTT = MQTTClient(broker, port, subscribe_topic, username, password)
# 循环连接
2023-12-09 18:41:35 +08:00
while True:
try:
2023-12-09 18:44:14 +08:00
MQTT.start()
2023-12-11 16:46:03 +08:00
# 阻塞监听
2023-12-09 18:44:14 +08:00
MQTT.client.loop_forever()
2023-12-11 14:11:12 +08:00
except Exception as e:
2023-12-13 12:54:25 +08:00
# print(e)
2023-12-11 16:46:03 +08:00
# 异常等待时间在进行连接
2023-12-11 09:09:07 +08:00
time.sleep(30)