update.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. --- 模块功能:远程升级
  2. -- @module ntp
  3. -- @author 朱天华
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.11.14
  7. require "misc"
  8. require "utils"
  9. require "socket"
  10. require "log"
  11. local sbyte, ssub = string.byte, string.sub
  12. module(..., package.seeall)
  13. -- 重试次数
  14. local RETRY = 3
  15. -- 升级包保存路径
  16. local UPD_PATH = "/luazip/update.bin"
  17. --- 自动连接服务器,检查升级,下载升级包s
  18. -- @return 无
  19. -- @usage update.run()
  20. uptate_ok = 0
  21. function run()
  22. sys.taskInit(function()
  23. local cnt = 1
  24. for cnt = 1, RETRY do
  25. while not socket.isReady() do sys.waitUntil('IP_READY_IND') end
  26. local url = "/update/firmware_upgrade?imei=" .. misc.getimei() .. "&device_key=" .. misc.getsn()
  27. .. "&firmware_name=" .. _G.PROJECT .. "_" .. rtos.get_version() .. "&version=" .. _G.VERSION
  28. -- local code, head, body = http.request("GET", "http://www.yfzhwlw.com" .. url, 5000)
  29. local code, head, body = http.request("GET", "http://120.27.222.26" .. url, 5000)
  30. log.error("####################update http code is:",code)
  31. if code == "200" then
  32. io.writefile(UPD_PATH, body)
  33. uptate_ok = code
  34. break
  35. else
  36. uptate_ok = code
  37. break
  38. end
  39. end
  40. log.error("file_exists:",file_exists(UPD_PATH))
  41. log.error("length_of_file:",length_of_file(UPD_PATH))
  42. end)
  43. end
  44. -- 获得文件长度
  45. -- 代码如下:
  46. function length_of_file(filename)
  47. local fh = assert(io.open(filename, "rb"))
  48. local len = assert(fh:seek("end"))
  49. fh:close()
  50. return len
  51. end
  52. -- 判断文件是否存在
  53. -- 复制代码 代码如下:
  54. function file_exists(path)
  55. local file = io.open(path, "rb")
  56. if file then file:close() end
  57. return file ~= nil
  58. end