1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- module(..., package.seeall)
- require "mqtt"
- require "sim"
- require "net"
- require "misc"
- require "pins"
- require "nvm"
- require"mqtt_recieve"
- -- require"mqtt_send"
- local host, port = nvm.get("mqttIp") or "develop.rltest.cn", nvm.get("mqttPort") or "1883"
- local username = nvm.get("mqttUser") or "rl517"
- local password = nvm.get("mqttPassword") or "rlian2022"
- --mqttc 连接状态
- local isConnected = false
- local isWaitReconnect = false
- --ota 升级检测是否结束
- local isOtaCheckEnd = false
- --等待ota升级检测结束
- -- sys.subscribe("ota_update_check_end", function(data)
- -- isOtaCheckEnd = true
- -- end)
- --启动MQTT客户端任务
- sys.taskInit(
- function()
- local retryConnectCnt = 0
- while true do
- if not socket.isReady() then
- retryConnectCnt = 0
- --等待网络环境准备就绪,超时时间是5分钟
- log.info("socket 1 is not ready")
- sys.wait(1000)
- sys.waitUntil("IP_READY_IND",300000)
- end
-
- if socket.isReady() then
- local imei = misc.getImei()
- --创建一个MQTT客户端
- local mqttClient = mqtt.client(imei,300,username,password)
- --阻塞执行MQTT CONNECT动作,直至成功
- --如果使用ssl连接,打开mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"}),根据自己的需求配置
- --mqttClient:connect("lbsmqtt.airm2m.com",1884,"tcp_ssl",{caCert="ca.crt"})
- if mqttClient:connect(host,port,"tcp") then
- log.info("imei:", imei)
- retryConnectCnt = 0
- isConnected = true
- --订阅主题
- local topics = {
- ["SHEGCL/IntelligenTool/UpdateToolInfo/"..imei] = 0,
- ["SHEGCL/IntelligenTool/UploadWorkRecordMA/"..imei]=0,
- ["SHEGCL/IntelligenTool/UploadWorkRecordFA/"..imei]=0,
- ["SHEGCL/IntelligenTool/UploadWorkRecordSimple/"..imei]=0,
- ["SHEGCL/IntelligenTool/Heart/"..imei]=0
- }
- if mqttClient:subscribe(topics) then
- --循环处理接收和发送的数据
- while true do
- -- if not mqtt_send.proc(mqttClient) then log.error("mqttTask.mqtt_send proc error") break end
- if not mqtt_recieve.proc(mqttClient) then log.error("mqttTask.mqtt_recieve.proc error") break end
-
- -- local a = mqtt_send.proc(mqttClient)
- -- log.info("send proc res", a)
- end
- end
- isConnected = false
- else
- retryConnectCnt = retryConnectCnt+1
- end
- --断开MQTT连接
- mqttClient:disconnect()
- if retryConnectCnt>=5 then link.shut() retryConnectCnt=0 end
- sys.wait(5000)
- else
- --进入飞行模式,20秒之后,退出飞行模式
- net.switchFly(true)
- sys.wait(20000)
- net.switchFly(false)
- end
- end
- end
- )
|