| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import _thread
- import time
- import utils
- from config import getConfig
- from httpServer import startHttpServer,closeHttpServer
- # 硬重启
- import machine
- def taskCheck():
- """
- 定时检查连接
- :return:
- """
- time_sleep = 10
- count = 0
- config = getConfig()
- while True:
- if getConfig().runTask:
- try:
- if utils.getStatus():
- print('WiFi 正常', utils.getWifiConnectInfo())
- if getConfig().mqttStatus :
- print('MQTT 正常', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic)
- if getConfig().mqtt.clientId is "":
- print("设备未绑定")
- utils.send_ping()
- else:
- # 有变化就上报一次
- if config != getConfig():
- print("上报数据")
- utils.reportConfig()
- config = getConfig()
- else:
- utils.send_ping()
- count = 0
- else:
- print('MQTT 未连接', getConfig().mqtt.host, getConfig().mqtt.port,getConfig().mqtt.subTopic)
- utils.connectMQTT(time_sleep)
- else:
- print('WiFi 未连接')
- if utils.autoConnectWifi():
- utils.connectMQTT(time_sleep)
- # 没有客户的id表示没被绑定
- if not utils.getStatus() or not getConfig().mqttStatus or getConfig().mqtt.clientId is "":
- count += 1
- # 连接失败开启热点
- utils.openAp()
- startHttpServer()
- else:
- closeHttpServer()
- utils.closeAp()
- if count > 60:
- machine.reset()
- except Exception as e:
- print(f"连接失败:未知错误 - {e}")
- time.sleep(time_sleep)
- try:
- _thread.start_new_thread(taskCheck, ())
- except:
- machine.reset()
|