From be32fda9664f6cf60bf106d55474717754cf2acc Mon Sep 17 00:00:00 2001 From: xyj <10908227994@qq.com> Date: Mon, 11 Dec 2023 15:09:21 +0800 Subject: [PATCH] update --- data_upload.py | 6 +++--- data_upload2.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 data_upload2.py diff --git a/data_upload.py b/data_upload.py index 4e73b63..cca1990 100644 --- a/data_upload.py +++ b/data_upload.py @@ -25,11 +25,11 @@ class DataUploadClient: 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) + # 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) + 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) diff --git a/data_upload2.py b/data_upload2.py new file mode 100644 index 0000000..cca1990 --- /dev/null +++ b/data_upload2.py @@ -0,0 +1,55 @@ +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