excloud_test.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. --[[
  2. @module excloud_test
  3. @summary excloud测试文件
  4. @version 1.0
  5. @date 2025.09.22
  6. @author 孟伟
  7. @usage
  8. 本demo演示的功能为:
  9. 本demo演示了excloud扩展库的完整使用流程,包括:
  10. 1. 设备连接与认证
  11. 2. 数据上报与接收
  12. 3. 运维日志管理
  13. 4. 文件上传功能
  14. 5. 心跳保活机制
  15. ]]
  16. -- 导入excloud库
  17. local excloud = require("excloud")
  18. -- 注册回调函数
  19. -- 注册回调函数
  20. function on_excloud_event(event, data)
  21. log.info("用户回调函数", event, json.encode(data))
  22. if event == "connect_result" then
  23. if data.success then
  24. log.info("连接成功")
  25. sys.publish("aircloud_connected")
  26. else
  27. log.info("连接失败: " .. (data.error or "未知错误"))
  28. end
  29. elseif event == "auth_result" then
  30. if data.success then
  31. log.info("认证成功")
  32. else
  33. log.info("认证失败: " .. data.message)
  34. end
  35. elseif event == "message" then
  36. log.info("收到消息, 流水号: " .. data.header.sequence_num)
  37. -- 处理服务器下发的消息
  38. for _, tlv in ipairs(data.tlvs) do
  39. log.info("TLV字段", "含义:", tlv.field, "类型:", tlv.type, "值:", tlv.value)
  40. if tlv.field == excloud.FIELD_MEANINGS.CONTROL_COMMAND then
  41. log.info("收到控制命令: " .. tostring(tlv.value))
  42. -- 处理控制命令并发送响应
  43. local response_ok, err_msg = excloud.send({
  44. {
  45. field_meaning = excloud.FIELD_MEANINGS.CONTROL_RESPONSE,
  46. data_type = excloud.DATA_TYPES.UNICODE,
  47. value = "命令执行成功"
  48. }
  49. }, false)
  50. if not response_ok then
  51. log.info("发送控制响应失败: " .. err_msg)
  52. end
  53. end
  54. end
  55. elseif event == "disconnect" then
  56. log.warn("与服务器断开连接")
  57. elseif event == "reconnect_failed" then
  58. log.info("重连失败,已尝试 " .. data.count .. " 次")
  59. elseif event == "send_result" then
  60. if data.success then
  61. log.info("发送成功,流水号: " .. data.sequence_num)
  62. else
  63. log.info("发送失败: " .. data.error_msg)
  64. end
  65. elseif event == "mtn_log_upload_start" then
  66. log.info("运维日志上传开始", "文件数量:", data.file_count)
  67. elseif event == "mtn_log_upload_progress" then
  68. log.info("运维日志上传进度",
  69. "当前文件:", data.current_file,
  70. "总数:", data.total_files,
  71. "文件名:", data.file_name,
  72. "状态:", data.status)
  73. elseif event == "mtn_log_upload_complete" then
  74. log.info("运维日志上传完成",
  75. "成功:", data.success_count,
  76. "失败:", data.failed_count,
  77. "总计:", data.total_files)
  78. end
  79. end
  80. -- 注册回调
  81. excloud.on(on_excloud_event)
  82. -- 主任务函数
  83. function excloud_task_func()
  84. -- 如果当前时间点设置的默认网卡还没有连接成功,一直在这里循环等待
  85. while not socket.adapter(socket.dft()) do
  86. log.warn("excloud_task_func", "wait IP_READY", socket.dft())
  87. -- 在此处阻塞等待默认网卡连接成功的消息"IP_READY"
  88. -- 或者等待1秒超时退出阻塞等待状态;
  89. -- 注意:此处的1000毫秒超时不要修改的更长;
  90. -- 因为当使用exnetif.set_priority_order配置多个网卡连接外网的优先级时,会隐式的修改默认使用的网卡
  91. -- 当exnetif.set_priority_order的调用时序和此处的socket.adapter(socket.dft())判断时序有可能不匹配
  92. -- 此处的1秒,能够保证,即使时序不匹配,也能1秒钟退出阻塞状态,再去判断socket.adapter(socket.dft())
  93. sys.waitUntil("IP_READY", 1000)
  94. end
  95. -- -- 配置excloud参数
  96. local ok, err_msg = excloud.setup({
  97. use_getip = true, -- 使用getip服务
  98. device_type = 1, -- 4G设备
  99. auth_key = "VmhtOb81EgZau6YyuuZJzwF6oUNGCbXi",
  100. transport = "tcp", -- 使用TCP传输
  101. auto_reconnect = true, -- 自动重连
  102. reconnect_interval = 10, -- 重连间隔(秒)
  103. max_reconnect = 5, -- 最大重连次数
  104. mtn_log_enabled = true, -- 启用运维日志
  105. mtn_log_blocks = 1, -- 日志文件块数
  106. mtn_log_write_way = excloud.MTN_LOG_CACHE_WRITE -- 缓存写入方式
  107. })
  108. --不使用getip服务,注意把use_getip设置为false
  109. -- local ok, err_msg = excloud.setup({
  110. -- use_getip = false, -- 不使用getip服务
  111. -- device_type = 1, -- 设备类型: 4G
  112. -- host = "112.125.89.8", -- 服务器地址
  113. -- port = 32585, -- 服务器端口
  114. -- auth_key = "VmhtOb81EgZau6YyuuZJzwF6oUNGCbXi", -- 鉴权密钥
  115. -- transport = "tcp", -- 使用TCP传输
  116. -- auto_reconnect = true, -- 自动重连
  117. -- reconnect_interval = 10, -- 重连间隔(秒)
  118. -- max_reconnect = 5, -- 最大重连次数
  119. -- mtn_log_enabled = true -- 启用运维日志
  120. -- })
  121. -- 配置excloud参数,虚拟设备链接
  122. -- local ok, err_msg = excloud.setup({
  123. -- use_getip = true, --使用getip服务
  124. -- device_type = 9,
  125. -- auth_key = "VmhtOb81EgZau6YyuuZJzwF6oUNGCbXi",
  126. -- virtual_phone_number = "15893470522", -- 11位手机号
  127. -- virtual_serial_num = 1, -- 序列号(0-999)
  128. -- transport = "tcp", -- 由于mqtt链接需要使用imei,虚拟设备没有,所以只能使用TCP传输
  129. -- mtn_log_enabled = true
  130. -- })
  131. if not ok then
  132. log.info("初始化失败: " .. err_msg)
  133. return
  134. end
  135. log.info("excloud初始化成功")
  136. -- 开启excloud服务
  137. local ok, err_msg = excloud.open()
  138. if not ok then
  139. log.info("开启excloud服务失败: " .. err_msg)
  140. return
  141. end
  142. log.info("excloud服务已开启")
  143. -- 启动自动心跳,默认5分钟一次的心跳
  144. excloud.start_heartbeat()
  145. log.info("自动心跳已启动")
  146. -- 启动3分钟一次的心跳,可配置自定义内容
  147. -- excloud.start_heartbeat(180, {
  148. -- { field_meaning = excloud.FIELD_MEANINGS.TIMESTAMP,
  149. -- data_type = excloud.DATA_TYPES.INTEGER,
  150. -- value = os.time() }
  151. -- })
  152. -- 停止自动心跳
  153. --excloud.stop_heartbeat()
  154. -- 记录启动日志
  155. --excloud.mtn_log("system", "设备启动完成", "version", "1.0.0")
  156. -- 主循环:定期上报数据
  157. while true do
  158. -- 每30秒上报一次数据
  159. sys.wait(30000)
  160. -- 检查连接状态
  161. local status = excloud.status()
  162. if not status.is_connected then
  163. log.warn("设备未连接,跳过数据上报")
  164. else
  165. -- 上报基础状态数据
  166. local ok, err_msg = excloud.send({
  167. {
  168. field_meaning = excloud.FIELD_MEANINGS.SIGNAL_STRENGTH_4G,
  169. data_type = excloud.DATA_TYPES.INTEGER,
  170. value = 22 -- 信号强度
  171. },
  172. {
  173. field_meaning = excloud.FIELD_MEANINGS.SIM_ICCID,
  174. data_type = excloud.DATA_TYPES.ASCII,
  175. value = "89860118801012345678" -- SIM卡ICCID
  176. },
  177. {
  178. field_meaning = excloud.FIELD_MEANINGS.TIMESTAMP,
  179. data_type = excloud.DATA_TYPES.INTEGER,
  180. value = os.time()
  181. }
  182. }, false)
  183. if ok then
  184. log.info("基础数据上报成功")
  185. else
  186. log.error("基础数据上报失败:", err_msg)
  187. end
  188. end
  189. end
  190. end
  191. -- 启动主任务
  192. sys.taskInit(excloud_task_func)
  193. --上传图片示例
  194. function upload_image_fun()
  195. -- 等待连接建立
  196. sys.waitUntil("aircloud_connected", 10000)
  197. -- 上传图片
  198. log.info("开始上传图片")
  199. if not excloud.status().is_connected then
  200. log.info("设备未连接,跳过图片上传")
  201. return
  202. end
  203. if io.exists("/luadb/test.jpg") then
  204. local ok, err = excloud.upload_image("/luadb/test.jpg", "test.jpg")
  205. if ok then
  206. log.info("图片上传成功")
  207. else
  208. log.error("图片上传失败:", err)
  209. end
  210. else
  211. log.warn("测试图片文件不存在")
  212. end
  213. end
  214. sys.taskInit(upload_image_fun)
  215. -- 运维日志测试示例
  216. function mtnlog_test_task()
  217. local test_count = 0
  218. while true do
  219. test_count = test_count + 1
  220. excloud.mtn_log("info", "mtn_test", test_count)
  221. -- 每30秒记录一次
  222. sys.wait(1000)
  223. end
  224. end
  225. sys.taskInit(mtnlog_test_task)