otaTask.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --- 模块功能:远程升级功能测试(使用Luat iot平台).
  2. -- 开机后立即执行一次升级功能,仅执行一次,如果升级包下载成功,自动重启
  3. -- @author openLuat
  4. -- @module update.testUpdate1
  5. -- @license MIT
  6. -- @copyright openLuat
  7. -- @release 2018.03.27
  8. module(...,package.seeall)
  9. require"nvm"
  10. local otaUrl = nvm.get("otaUrl")
  11. local otaCheckInterval = nvm.get("otaCheckInterval")
  12. --[[
  13. 使用Luat物联云平台的升级服务器时,按照如下步骤操作
  14. 1、在main.lua中定义PRODUCT_KEY变量
  15. 2、加载update模块 require"update"
  16. 3、调用update.request()即可
  17. ]]
  18. require"update"
  19. local function otaUpdateCb(result)
  20. if result then
  21. log.info("otaTask.otaUpdateCb","ota update download success, start reboot...")
  22. sys.restart("UPDATE_DOWNLOAD_SUCCESS")
  23. else
  24. log.error("otaTask.otaUpdateCb","ota update download failed!")
  25. end
  26. end
  27. sys.subscribe("device_config_init_result", function ( )
  28. log.info("otaTask.subscribe.device_config_init_result", "otaUrl:", otaUrl, "otaCheckInterval:", otaCheckInterval)
  29. if otaUrl and otaCheckInterval and otaCheckInterval > 0 then
  30. update.request(otaUpdateCb,otaUrl,otaCheckInterval * 1000)
  31. elseif otaUrl and not otaCheckInterval then
  32. update.request(otaUpdateCb,otaUrl)
  33. elseif not otaUrl and otaCheckInterval and otaCheckInterval > 0 then
  34. update.request(otaUpdateCb, nil, otaCheckInterval * 1000)
  35. else
  36. update.request()
  37. end
  38. end)
  39. sys.subscribe("device_command_ota_start", function ( )
  40. log.info("otaTask.subscribe.device_command_ota_start", "otaUrl:", otaUrl, "otaCheckInterval:", otaCheckInterval)
  41. if otaUrl and otaCheckInterval and otaCheckInterval > 0 then
  42. update.request(otaUpdateCb,otaUrl,otaCheckInterval * 1000)
  43. elseif otaUrl and not otaCheckInterval then
  44. update.request(otaUpdateCb,otaUrl)
  45. elseif not otaUrl and otaCheckInterval and otaCheckInterval > 0 then
  46. update.request(otaUpdateCb, nil, otaCheckInterval * 1000)
  47. else
  48. update.request()
  49. end
  50. end)
  51. sys.timerLoopStart(log.info,5000,"otaTask.timerLoopStart",rtos.get_version(),_G.VERSION)