40 lines
1.2 KiB
Python
Executable File
40 lines
1.2 KiB
Python
Executable File
import os
|
|
import subprocess
|
|
import time
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from device import device_name
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
global times
|
|
if rc == 0:
|
|
# print("连接成功,执行数据推送和本地存储")
|
|
subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/start_data_upload.sh'], shell=True)
|
|
times = 3
|
|
|
|
|
|
def on_connect_fail(client, userdata):
|
|
# print("失败,执行本地存储")
|
|
subprocess.Popen(['/usr/bin/bash /home/pi/lot_manager/bash/stop_data_upload.sh'], shell=True)
|
|
|
|
|
|
times = 3
|
|
if __name__ == '__main__':
|
|
while True:
|
|
try:
|
|
client = mqtt.Client(client_id=device_name)
|
|
client.username_pw_set("demo", "123456")
|
|
# Specify callback function
|
|
client.on_connect = on_connect
|
|
client.on_connect_fail = on_connect_fail
|
|
# Establish a connection
|
|
client.connect('ceshi-mqtt.lihaink.cn', 1883)
|
|
# Publish a message
|
|
client.loop_forever()
|
|
except Exception as e:
|
|
print("等待5秒重新连接客户端")
|
|
time.sleep(5)
|
|
if times == 0:
|
|
on_connect_fail(None, None)
|
|
times -= 1 |