This commit is contained in:
xyj 2023-12-11 15:16:16 +08:00
parent 46160694af
commit 62fb681592
4 changed files with 33 additions and 88 deletions

18
MQTT.py
View File

@ -11,9 +11,6 @@ config = configparser.ConfigParser()
config.read('/home/pi/lot_manager/conf/main/config.conf') config.read('/home/pi/lot_manager/conf/main/config.conf')
# config.read('/home/lihai/pythonProjects/lot_manager/conf/main/config.conf')
def valid(msg, client): def valid(msg, client):
origin_data = json.loads(msg.payload.decode('utf-8')) origin_data = json.loads(msg.payload.decode('utf-8'))
if 'msg' not in origin_data: if 'msg' not in origin_data:
@ -34,6 +31,7 @@ class MQTTClient:
self.topic = topic self.topic = topic
self.username = username self.username = username
self.password = password self.password = password
# 千万不要指定client_id 不然死翘翘
self.client = mqtt.Client() self.client = mqtt.Client()
# self.client.username_pw_set(self.username, self.password) # self.client.username_pw_set(self.username, self.password)
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect
@ -83,14 +81,14 @@ class MQTTClient:
if __name__ == '__main__': if __name__ == '__main__':
# broker = config.get("broker", "host") broker = config.get("broker", "host")
# # 这里必须是int类型 # 这里必须是int类型
# port = config.getint("broker", "port") port = config.getint("broker", "port")
# topic = config.get("topic", "name") topic = config.get("topic", "name")
# username = config.get("security", "username") username = config.get("security", "username")
# password = config.get("security", "password") password = config.get("security", "password")
# print(broker, port, topic, username, password) # print(broker, port, topic, username, password)
MQTT = MQTTClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456") MQTT = MQTTClient(broker, 1883, port, username, password)
while True: while True:
try: try:
MQTT.start() MQTT.start()

View File

@ -6,8 +6,8 @@ import paho.mqtt.client as mqtt
from device import device_name from device import device_name
from tool import * from tool import *
# config = configparser.ConfigParser() config = configparser.ConfigParser()
# config.read('/home/pi/lot_manager/conf/main/config.conf') config.read('/home/pi/lot_manager/conf/main/config.conf')
times = 6 times = 6
@ -18,6 +18,7 @@ class DataUploadClient:
self.topic = topic self.topic = topic
self.username = username self.username = username
self.password = password self.password = password
# 千万不要指定client_id 不然死翘翘
self.client = mqtt.Client() self.client = mqtt.Client()
self.client.username_pw_set(self.username, self.password) self.client.username_pw_set(self.username, self.password)
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect
@ -25,30 +26,30 @@ class DataUploadClient:
def on_connect(self, client, userdata, flags, rc): def on_connect(self, client, userdata, flags, rc):
self.client.publish('success', payload='连接成功,执行数据推送和本地存储' + str(time.time()), qos=0) self.client.publish('success', payload='连接成功,执行数据推送和本地存储' + str(time.time()), qos=0)
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True) subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True)
def on_disconnect(self, client, userdata, rc): def on_disconnect(self, client, userdata, rc):
print("失败,执行本地存储") # print("失败,执行本地存储")
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True) subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
def start(self): def start(self):
self.client.connect(self.broker, self.port) self.client.connect(self.broker, self.port)
if __name__ == '__main__': if __name__ == '__main__':
# broker = config.get("broker", "host") broker = config.get("broker", "host")
# # 这里必须是int类型 # 这里必须是int类型
# port = config.getint("broker", "port") port = config.getint("broker", "port")
# topic = config.get("topic", "name") topic = config.get("topic", "name")
# username = config.get("security", "username") username = config.get("security", "username")
# password = config.get("security", "password") password = config.get("security", "password")
MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456") # MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456")
MQTT = DataUploadClient(broker, 1883, port, username, password)
while True: while True:
try: try:
MQTT.start() MQTT.start()
MQTT.client.loop_forever() MQTT.client.loop_forever()
except: except:
# print("重新连接")
time.sleep(10) time.sleep(10)
if times == 0: if times == 0:
MQTT.on_disconnect(None, None, None) MQTT.on_disconnect(None, None, None)

View File

@ -1,55 +0,0 @@
import configparser
import time
import paho.mqtt.client as mqtt
from device import device_name
from tool import *
# config = configparser.ConfigParser()
# config.read('/home/pi/lot_manager/conf/main/config.conf')
times = 6
class DataUploadClient:
def __init__(self, broker, port, topic, username, password):
self.broker = broker
self.port = port
self.topic = topic
self.username = username
self.password = password
self.client = mqtt.Client(client_id=device_name)
self.client.username_pw_set(self.username, self.password)
self.client.on_connect = self.on_connect
self.client.on_disconnect = self.on_disconnect
def on_connect(self, client, userdata, flags, rc):
self.client.publish('success', payload='连接成功,执行数据推送和本地存储' + str(time.time()), qos=0)
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True)
def on_disconnect(self, client, userdata, rc):
print("失败,执行本地存储")
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
def start(self):
self.client.connect(self.broker, self.port)
if __name__ == '__main__':
# broker = config.get("broker", "host")
# # 这里必须是int类型
# port = config.getint("broker", "port")
# topic = config.get("topic", "name")
# username = config.get("security", "username")
# password = config.get("security", "password")
MQTT = DataUploadClient("192.168.1.27", 1883, "lot_mqtt", "demo", "123456")
while True:
try:
MQTT.start()
MQTT.client.loop_forever()
except:
# print("重新连接")
time.sleep(10)
if times == 0:
MQTT.on_disconnect(None, None, None)
times -= 1

View File

@ -236,17 +236,18 @@ def on_connect(client, userdata, flags, rc):
if __name__ == '__main__': if __name__ == '__main__':
# config = configparser.ConfigParser() config = configparser.ConfigParser()
# config.read('/home/pi/lot_manager/conf/main/config.conf') config.read('/home/pi/lot_manager/conf/main/config.conf')
# broker = config.get("broker", "host") broker = config.get("broker", "host")
# # 这里必须是int类型 # 这里必须是int类型
# port = config.getint("broker", "port") port = config.getint("broker", "port")
# username = config.get("security", "username") username = config.get("security", "username")
# password = config.get("security", "password") password = config.get("security", "password")
client = mqtt.Client(client_id=device_name) # 千万不要指定client_id 不然死翘翘!!!
client.username_pw_set("demo", "123456") client = mqtt.Client()
client.username_pw_set(username, password)
# Specify callback function # Specify callback function
client.on_connect = on_connect client.on_connect = on_connect
# Establish a connection # Establish a connection
client.connect("192.168.1.27", 1883) client.connect(broker, port)
client.loop_forever() client.loop_forever()