2023-12-07 16:35:05 +08:00
|
|
|
import os
|
2023-12-05 16:21:08 +08:00
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import paho.mqtt.client as mqtt
|
2023-12-02 11:45:52 +08:00
|
|
|
|
2023-12-09 15:23:18 +08:00
|
|
|
from device import device_name
|
|
|
|
|
2023-12-02 11:45:52 +08:00
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
global times
|
|
|
|
if rc == 0:
|
2023-12-09 16:38:12 +08:00
|
|
|
print("连接成功,执行数据推送和本地存储")
|
|
|
|
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True)
|
2023-12-05 16:21:08 +08:00
|
|
|
times = 3
|
2023-12-02 10:53:31 +08:00
|
|
|
|
|
|
|
|
2023-12-05 16:21:08 +08:00
|
|
|
def on_connect_fail(client, userdata):
|
2023-12-09 16:38:12 +08:00
|
|
|
print("失败,执行本地存储")
|
|
|
|
# subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
|
2023-12-02 11:37:32 +08:00
|
|
|
|
2023-12-02 11:17:37 +08:00
|
|
|
|
2023-12-05 16:21:08 +08:00
|
|
|
times = 3
|
|
|
|
if __name__ == '__main__':
|
|
|
|
while True:
|
|
|
|
try:
|
2023-12-09 15:23:18 +08:00
|
|
|
client = mqtt.Client(client_id=device_name)
|
2023-12-05 16:21:08 +08:00
|
|
|
client.username_pw_set("demo", "123456")
|
|
|
|
# Specify callback function
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.on_connect_fail = on_connect_fail
|
|
|
|
# Establish a connection
|
2023-12-07 18:28:24 +08:00
|
|
|
client.connect('ceshi-mqtt.lihaink.cn', 1883)
|
2023-12-05 16:21:08 +08:00
|
|
|
# Publish a message
|
|
|
|
client.loop_forever()
|
|
|
|
except Exception as e:
|
2023-12-07 09:20:44 +08:00
|
|
|
print("等待5秒重新连接客户端")
|
2023-12-05 16:21:08 +08:00
|
|
|
time.sleep(5)
|
2023-12-07 18:06:07 +08:00
|
|
|
if times == 0:
|
2023-12-05 16:21:08 +08:00
|
|
|
on_connect_fail(None, None)
|
2023-12-07 18:06:07 +08:00
|
|
|
times -= 1
|