mqtt_recieve.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. module(...,package.seeall)
  2. local DIR_PATH = {
  3. Users = "/sdcard0/common",
  4. Wrench = "/sdcard0/common",
  5. WorkingParts = "/sdcard0/common",
  6. WorkingPosition = "/sdcard0/common",
  7. WorkPlan = "/sdcard0/common",
  8. File = "/sdcard0/log",
  9. Wind = "/sdcard0/common"
  10. }
  11. local ALLOWCUSTOM_FILE = {
  12. Fan = 1,
  13. WorkingParts = 1,
  14. WorkingPosition = 1
  15. }
  16. -- 根据操作写文件
  17. function operateFs(op, cntType, cntVersion, content )
  18. -- body
  19. local opRes, count, desc = "",fileRes
  20. if op == "add" then
  21. -- body
  22. if DIR_PATH[cntType] then
  23. -- body
  24. if ALLOWCUSTOM_FILE[cntType] then
  25. -- body
  26. delBaseInfo(cntType, 0, DIR_PATH[cntType])
  27. end
  28. opRes, desc = saveBaseInfo(cntType, content, DIR_PATH[cntType] )
  29. else
  30. if ALLOWCUSTOM_FILE[cntType] then
  31. -- body
  32. delBaseInfo(cntType, 0, "/sdcard0/"..content.wnum)
  33. end
  34. if cntType == "WorkRecord" then
  35. -- body
  36. opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
  37. else
  38. opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
  39. end
  40. end
  41. end
  42. if op == "update" then
  43. -- body
  44. if DIR_PATH[cntType] then
  45. -- body
  46. opRes,count, desc = updateBaseInfo(cntType, content, DIR_PATH[cntType] )
  47. if not opRes or desc:sub(1, 29) == "this data not found where id=" then
  48. -- body
  49. opRes, desc = saveBaseInfo(cntType, content, DIR_PATH[cntType] )
  50. end
  51. else
  52. if cntType == "WorkRecord" then
  53. -- body
  54. opRes,count, desc = updateBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
  55. else
  56. opRes,count, desc = updateBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
  57. end
  58. if not opRes or desc:sub(1, 29) == "this data not found where id=" then
  59. -- body
  60. if cntType == "WorkRecord" then
  61. -- body
  62. opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
  63. else
  64. opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
  65. end
  66. end
  67. end
  68. end
  69. if op == "delete" then
  70. -- body
  71. if DIR_PATH[cntType] then
  72. -- body
  73. opRes,count, desc = delBaseInfo(cntType, content.id, DIR_PATH[cntType] )
  74. else
  75. if cntType == "WorkRecord" then
  76. -- body
  77. opRes,count, desc = delBaseInfo(cntType, content.id, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
  78. else
  79. opRes,count, desc = delBaseInfo(cntType, content.id, "/sdcard0/"..content.wnum )
  80. end
  81. end
  82. end
  83. if op == "directive" then
  84. if cntType == "File" then
  85. -- body
  86. if content.file_name == "/sdcard0/log/0" then
  87. -- body
  88. opRes = logModuel.readfile(nil)
  89. else
  90. opRes = logModuel.readfile(content.file_name)
  91. end
  92. end
  93. if cntType == "SysCMD" then
  94. -- body
  95. if content.cmd_content == "Reboot" then
  96. -- body
  97. opRes = true
  98. sys.restart("Sever send reboot CMD! ")
  99. end
  100. if content.cmd_content == "FormatSD" then
  101. -- body
  102. local fg = io.format(io.SDCARD)
  103. log.info("format res", fg)
  104. if fg == 1 then
  105. -- body
  106. nvm.set("localCntVersion", "0")
  107. sys.restart("Server send Format SDcard CMD! ")
  108. else
  109. opRes = false
  110. desc = "格式化SDcard失败"
  111. end
  112. end
  113. end
  114. if cntType == "Config" then
  115. -- body
  116. for k,v in pairs(content) do
  117. nvm.set(k, v)
  118. end
  119. opRes = true
  120. sys.restart("Sever send config update! ")
  121. end
  122. end
  123. local localVersion = nvm.get("localCntVersion")
  124. if cntVersion > localVersion then
  125. -- body
  126. nvm.set("localCntVersion", cntVersion)
  127. log.info("nvm set success, value:", cntVersion)
  128. end
  129. -- log.info("optype:",op,"cntType:", cntType, "opRes:",opRes, "count:",count, "desc:",desc)
  130. if not opRes then
  131. -- body
  132. if not desc then
  133. -- body
  134. desc = ""
  135. end
  136. logModuel.debug_log("mqtt_recieve operate fs failed! desc:"..desc..",optype:"..op..",cntVersion:"..cntVersion)
  137. end
  138. return opRes,desc
  139. end
  140. --- MQTT客户端数据接收处理
  141. -- @param mqttClient,MQTT客户端对象
  142. -- @return 处理成功返回true,处理出错返回false
  143. -- @usage mqttInMsg.proc(mqttClient)
  144. function proc(mqttClient)
  145. local result,data,param
  146. while true do
  147. result, data, param = mqttClient:receive(30000,"LOCAL_PUB_MSG")
  148. --接收到数据
  149. -- opType = "update"时,若不存在,则 add
  150. if result then
  151. local resData = json.decode(data.payload)
  152. local ack = {
  153. Imei = misc.getImei(),
  154. CntVersion = "",
  155. Succ = "",
  156. Extra = {}
  157. }
  158. --{"Extra":{},"Succ":"1","Imei":"863488052331447","CntVersion":"1659751519989"}
  159. local extrTable = {}
  160. local hasFile = false
  161. for k,v in pairs(resData) do
  162. local opRes, desc = operateFs(v.OpType, v.CntType, v.CntVersion, v.Content)
  163. if not opRes then
  164. -- body
  165. ack.Succ = "-1"
  166. table.insert(extrTable, "error_version:"..v.CntVersion..",OpType:"..v.OpType..",CntType:"..v.CntType..",desc:"..desc)
  167. end
  168. if v.OpType =="directive" and v.CntType == "File" then
  169. -- body
  170. table.insert(extrTable, "file_name:"..(v.Content.file_name or "unknown_file" )..",content:"..opRes)
  171. hasFile = true
  172. end
  173. end
  174. if ack.Succ == "" then
  175. -- body
  176. ack.Succ = "1"
  177. end
  178. ack.CntVersion = nvm.get("localCntVersion")
  179. ack.Extra = extrTable
  180. local mqttSendContent = ""
  181. if hasFile then
  182. -- body
  183. mqttSendContent = '{"Imei":' .. misc.getImei() ..',' .. '"CntVersion":'.. ack.CntVersion .. ',' .. '"Succ":'.. ack.Succ .. ','.. '"Extra":'.. table.concat(extrTable) .. '}'
  184. else
  185. mqttSendContent = json.encode(ack)
  186. end
  187. -- log.info("json temple:", att)
  188. -- mqttClient:publish("SHEGCL/IntelligenTool/Ack", json.encode(ack))
  189. mqttClient:publish("SHEGCL/IntelligenTool/Ack", mqttSendContent)
  190. elseif data == "LOCAL_PUB_MSG" then
  191. if type(param) == "string" then
  192. -- body
  193. param = json.decode(param)
  194. if param.topic and param.data then
  195. -- body
  196. local upData = param.data
  197. upData.PublishVersion = nvm.get("localCntVersion")
  198. upData.IMEI = misc.getImei()
  199. mqttClient:publish(param.topic, json.encode(upData))
  200. end
  201. else
  202. log.error("mqtt_recieve.proc", "param type invalid!! param:",param)
  203. end
  204. elseif data == "timeout" then
  205. log.info("mqtt_recieve.proc", "mqtt wait receive timeout!")
  206. else
  207. break
  208. end
  209. end
  210. return result or data=="timeout" or data=="LOCAL_PUB_MSG"
  211. end