MessagemqttOutMsg.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --- 模块功能:微消息队列_MQTT客户端数据发送处理
  2. -- @author openLuat
  3. -- @module mqtt.mqttOutMsg
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2022.08.31
  7. module(...,package.seeall)
  8. --数据发送的消息队列
  9. local msgQueue = {}
  10. local function insertMsg(topic,payload,qos,user)
  11. if MessagemqttTask.isReady() then
  12. table.insert(msgQueue,{t=topic,p=payload,q=qos,user=user})
  13. sys.publish("APP_SOCKET_SEND_DATA")
  14. end
  15. end
  16. -------发布测试------
  17. local function pubQos0TestCb(result)
  18. log.info("mqttOutMsg.pubQos0TestCb",result)
  19. if result then sys.timerStart(pubQos0Test,10000) end
  20. end
  21. function pubQos0Test()
  22. insertMsg("luatceshi/ceshiwanglong","12345",0,{cb=pubQos0TestCb}) -----换成自己的主题,或者注释掉,不然报错
  23. end
  24. -------发布测试------
  25. --- 初始化“MQTT客户端数据发送”
  26. -- @return 无
  27. -- @usage mqttOutMsg.init()
  28. function init()
  29. pubQos0Test()
  30. end
  31. --- 去初始化“MQTT客户端数据发送”
  32. -- @return 无
  33. -- @usage mqttOutMsg.unInit()
  34. function unInit()
  35. while #msgQueue>0 do
  36. local outMsg = table.remove(msgQueue,1)
  37. if outMsg.user and outMsg.user.cb then outMsg.user.cb(false,outMsg.user.para) end
  38. end
  39. end
  40. --- MQTT客户端数据发送处理
  41. -- @param mqttClient,MQTT客户端对象
  42. -- @return 处理成功返回true,处理出错返回false
  43. -- @usage mqttOutMsg.proc(mqttClient)
  44. function proc(mqttClient)
  45. --log.info("mqttOutMsg.proc1",#msgQueue)
  46. while #msgQueue>0 do
  47. local outMsg = table.remove(msgQueue,1)
  48. log.info("mqttOutMsg.proc before publish",outMsg.p)
  49. local result = mqttClient:publish(outMsg.t,outMsg.p,outMsg.q)
  50. --log.info("mqttOutMsg.proc after publish",result)
  51. if outMsg.user and outMsg.user.cb then outMsg.user.cb(result,outMsg.user.para) end
  52. if not result then return end
  53. end
  54. return true
  55. end