lot_manager/MQTT.py

101 lines
3.3 KiB
Python
Raw Normal View History

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-18 10:51:06 +08:00
from config import broker, port, subscribe_topic, username, password, info_topic
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:
2023-12-18 11:06:04 +08:00
client.publish(info_topic, payload=publish_payload(code=404, msg="msg must be supplied"), qos=0)
2023-12-14 11:29:40 +08:00
return False
2023-12-07 19:00:16 +08:00
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 不然死翘翘
2024-01-13 11:38:45 +08:00
self.client = mqtt.Client(client_id="测试")
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-18 11:06:04 +08:00
client.publish(info_topic, payload=publish_payload(code=200, msg='成功订阅' + self.topic),
2023-12-11 16:46:03 +08:00
qos=0)
2023-12-29 09:56:44 +08:00
# 关闭摄像机电
subprocess.Popen(['sudo /usr/bin/python close_led.py'], shell=True)
2024-01-05 16:47:27 +08:00
# 关闭LED灯
subprocess.Popen(['sudo /usr/bin/python close_blue_led.py'], shell=True)
2023-12-09 18:44:14 +08:00
def on_message(self, client, userdata, msg):
if not valid(msg, client):
return
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":
# 查看录像列表
2023-12-14 10:51:16 +08:00
get_list_record(client)
2023-12-09 18:44:14 +08:00
elif data == "record":
# 获取录像
get_record(msg, client)
elif data == "status":
# 查看运行状态
get_status(client)
2023-12-29 09:56:44 +08:00
elif data == "open_led":
open_led()
elif data == "close_led":
close_led()
2024-01-05 16:47:27 +08:00
elif data == "open_blue_led":
open_blue_led()
elif data == "close_blue_led":
close_blue_led()
2023-12-09 18:44:14 +08:00
else:
# 错误类型
2023-12-18 11:06:04 +08:00
client.publish(info_topic, payload=publish_payload(code=404, msg='No Such Msg Type'), qos=0)
2023-12-09 18:44:14 +08:00
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-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-11 16:46:03 +08:00
# 异常等待时间在进行连接
2023-12-11 09:09:07 +08:00
time.sleep(30)