2023-12-02 10:53:31 +08:00
|
|
|
import serial
|
|
|
|
import time
|
|
|
|
import struct
|
|
|
|
import json
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
|
|
from api import add
|
|
|
|
from db.models.log_data_model import LOT_DATA
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
global times
|
|
|
|
if rc == 0:
|
2023-12-05 16:21:08 +08:00
|
|
|
print("连接成功,执行数据推送和本地存储")
|
2023-12-05 17:01:46 +08:00
|
|
|
subprocess.Popen(['/usr/bin/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):
|
|
|
|
print("失败,执行本地存储")
|
2023-12-05 17:01:46 +08:00
|
|
|
subprocess.Popen(['/usr/bin/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-05 17:00:32 +08:00
|
|
|
client = mqtt.Client()
|
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-05 17:04:37 +08:00
|
|
|
client.connect('127.0.0.1', 1883)
|
2023-12-05 16:21:08 +08:00
|
|
|
# Publish a message
|
|
|
|
client.loop_forever()
|
|
|
|
except Exception as e:
|
|
|
|
print("等待5秒重新连接客户端")
|
|
|
|
time.sleep(5)
|
|
|
|
if times != 0:
|
|
|
|
times -= 1
|
|
|
|
on_connect_fail(None, None)
|