ctwing.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --- 模块功能:ctwing平台
  2. -- @module ctwingiot
  3. -- @author Dozingfiretruck
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2021.2.1
  7. module(...,package.seeall)
  8. require "ntp"
  9. require "misc"
  10. require "mqtt"
  11. require "utils"
  12. require "patch"
  13. require "socket"
  14. require "http"
  15. require "common"
  16. local DevicetId = "15027217123456789"
  17. local DeviceSecret = "jnwnpBJvZGL5JDQKtnDjhGVn34gQ1a6uUKdhYYfAHSw"
  18. local ctwing_mqttClient
  19. --[[
  20. 函数名:getDeviceName
  21. 功能 :获取设备名称
  22. 参数 :无
  23. 返回值:设备名称
  24. ]]
  25. local function getDeviceName()
  26. --默认使用设备的IMEI作为设备名称,用户可以根据项目需求自行修改
  27. return misc.getImei()
  28. end
  29. --mqtt订阅主题,根据自己需要修改
  30. local ctwing_iot_subscribetopic = {
  31. ["test"]=0
  32. }
  33. function ctwingiot_publish()
  34. --sys.publish("APP_SOCKET_SEND_DATA")
  35. --mqtt发布主题根据自己需要修改
  36. --ctwing_mqttClient:publish("$thing/up/property/"..ProductId.."/"..getDeviceName(), "publish from luat mqtt client", 0)
  37. local body = {
  38. pci=-32768,
  39. rsrp=-32768,
  40. cell_id=-2147483648,
  41. sinr=-32768,
  42. ecl=-33333,
  43. }
  44. local body_json = json.encode(body)
  45. ctwing_mqttClient:publish("signal_report", body_json, 0)
  46. end
  47. -- 无网络重启时间,飞行模式启动时间
  48. local rstTim, flyTim = 600000, 300000
  49. local mqtt_ready = false
  50. --- MQTT连接是否处于激活状态
  51. -- @return 激活状态返回true,非激活状态返回false
  52. -- @usage mqttTask.isReady()
  53. function isReady()
  54. return mqtt_ready
  55. end
  56. --- MQTT客户端数据接收处理
  57. -- @param mqttClient,MQTT客户端对象
  58. -- @return 处理成功返回true,处理出错返回false
  59. -- @usage mqttInMsg.proc(mqttClient)
  60. local function proc(mqttClient)
  61. local result,data
  62. while true do
  63. result,data = mqttClient:receive(120000,"APP_SOCKET_SEND_DATA")
  64. --接收到数据
  65. if result then
  66. log.info("mqttInMsg.proc",data.topic,string.toHex(data.payload))
  67. --TODO:根据需求自行处理data.payload
  68. else
  69. break
  70. end
  71. end
  72. return result or data=="timeout" or data=="APP_SOCKET_SEND_DATA"
  73. end
  74. local function ctwing_iot()
  75. while true do
  76. if not socket.isReady() and not sys.waitUntil("IP_READY_IND", rstTim) then sys.restart("网络初始化失败!") end
  77. --创建一个MQTT客户端
  78. ctwing_mqttClient = mqtt.client(DevicetId,300,"123456789",DeviceSecret)
  79. --阻塞执行MQTT CONNECT动作,直至成功
  80. while not ctwing_mqttClient:connect("mqtt.ctwing.cn",1883,"tcp",nil,2000) do sys.wait(2000) end
  81. log.info("mqtt连接成功")
  82. --订阅主题
  83. if ctwing_mqttClient:subscribe(ctwing_iot_subscribetopic, nil) then
  84. log.info("mqtt订阅成功")
  85. --循环处理接收和发送的数据
  86. while true do
  87. mqtt_ready = true
  88. if not proc(ctwing_mqttClient) then log.error("mqttTask.mqttInMsg.proc error") break end
  89. end
  90. else
  91. log.info("mqtt订阅失败")
  92. end
  93. mqtt_ready = false
  94. --断开MQTT连接
  95. ctwing_mqttClient:disconnect()
  96. end
  97. end
  98. local function iot()
  99. ntp.timeSync()
  100. if not socket.isReady() and not sys.waitUntil("IP_READY_IND", rstTim) then sys.restart("网络初始化失败!") end
  101. while not ntp.isEnd() do sys.wait(1000) end
  102. ctwing_iot()
  103. end
  104. net.switchFly(false)
  105. -- NTP同步失败强制重启
  106. local tid = sys.timerStart(function()
  107. net.switchFly(true)
  108. sys.timerStart(net.switchFly, 5000, false)
  109. end, flyTim)
  110. sys.subscribe("IP_READY_IND", function()
  111. sys.timerStop(tid)
  112. log.info("---------------------- 网络注册已成功 ----------------------")
  113. end)
  114. sys.taskInit(iot)