lot_manager/MQTT.py

98 lines
3.2 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 15:23:18 +08:00
from device import device_name
2023-12-09 18:44:14 +08:00
from tool import *
2023-12-11 14:11:12 +08:00
import configparser
config = configparser.ConfigParser()
2023-12-11 14:42:49 +08:00
# config.read('/home/pi/lot_manager/conf/main/config.conf')
config.read('/home/lihai/pythonProjects/lot_manager/conf/main/config.conf')
2023-12-05 17:16:32 +08:00
2023-12-07 19:09:31 +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
if 'device_name' not in origin_data:
client.publish('error', payload='device_name must be supplied', qos=0)
return False
2023-12-09 15:23:18 +08:00
if device_name != origin_data['device_name']:
2023-12-07 19:00:16 +08:00
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
self.client = mqtt.Client(client_id=device_name)
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):
self.client.subscribe(self.topic)
2023-12-11 14:42:49 +08:00
self.client.publish('success', payload='成功订阅lot_mqtt,time=' + str(datetime.datetime.now()), 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)
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):
2023-12-11 14:11:12 +08:00
self.client.username_pw_set(self.username, self.password)
2023-12-09 18:44:14 +08:00
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 14:11:12 +08:00
broker = config.get("broker", "host")
2023-12-11 14:42:49 +08:00
# 这里必须是int类型
port = config.getint("broker", "port")
2023-12-11 14:11:12 +08:00
topic = config.get("topic", "name")
username = config.get("security", "username")
password = config.get("security", "password")
2023-12-11 14:32:29 +08:00
print(broker, port, topic, username, password)
2023-12-11 14:11:12 +08:00
MQTT = MQTTClient(broker, port, topic, username, password)
2023-12-09 18:41:35 +08:00
while True:
try:
2023-12-09 18:44:14 +08:00
MQTT.start()
MQTT.client.loop_forever()
2023-12-11 14:11:12 +08:00
except Exception as e:
2023-12-11 09:09:07 +08:00
time.sleep(30)