main.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "antbot_lua"
  3. VERSION = "1.0.0"
  4. log.info("main", PROJECT, VERSION)
  5. _G.sys = require("sys")
  6. -- 引入 ebike 应用示例模块
  7. local bot_app_ebike = require("bot_app_ebike")
  8. -- antbot.app_sta_get 固定返回值类型
  9. local bot_msg_type = {
  10. BOT_MSG_UNAVAILABLE = 0,
  11. BOT_MSG_INIT_FAILED = 1,
  12. BOT_MSG_INIT_SUCESS = 2,
  13. BOT_MSG_REG_FAILED = 3,
  14. BOT_MSG_REG_SUCESS = 4,
  15. BOT_MSG_PUB_FAILED = 5,
  16. BOT_MSG_PUB_SUCESS = 6,
  17. BOT_MSG_DATA_VERIFY_FAILED = 7
  18. }
  19. -- 用户资产数据上报
  20. function bot_app_asset_data_report()
  21. local bot_user_data = bot_app_ebike.user_asset_data_get()
  22. if #bot_user_data > BOT_USER_DATA_STRING_MAX_SIZE then
  23. log.error("bot_user_data length out of range")
  24. return
  25. end
  26. log.debug("bot start publishing bot data")
  27. local ret = antbot.data_publish(bot_user_data, #bot_user_data)
  28. if ret < 0 then
  29. log.error("It was fail to publish bot data, ret: -0x", string.format("%x", -ret))
  30. return
  31. end
  32. log.info("successfully published bot data, cnt:", ret)
  33. end
  34. sys.taskInit(function()
  35. sys.wait(3000)
  36. local version = antbot.version_get();
  37. log.debug("antbot.version_get: ", version)
  38. log.debug("antbot initialization")
  39. local ret = antbot.init()
  40. if ret ~= 0 then
  41. log.error("antbot initialization failed.")
  42. return
  43. end
  44. log.debug("antbot config_set")
  45. ret = antbot.config_set(BOT_CONFIG_TOKEN)
  46. if ret ~= 0 then
  47. log.error("antbot config_set failed.")
  48. return
  49. end
  50. local asset_config = bot_app_ebike.user_asset_config_get()
  51. local reg_retry_count = 0;
  52. local bot_sta = bot_msg_type.BOT_MSG_UNAVAILABLE
  53. local bot_msg = bot_msg_type.BOT_MSG_UNAVAILABLE
  54. local count = 1
  55. while 1 do
  56. log.debug("antbot app_sta_get")
  57. if bot_sta ~= antbot.app_sta_get() then
  58. bot_sta = antbot.app_sta_get()
  59. bot_msg = bot_sta
  60. end
  61. log.info("luatos", "hi", count, os.date())
  62. log.info("lua", rtos.meminfo()) -- lua内存
  63. log.info("sys", rtos.meminfo("sys")) -- sys内存
  64. count = count + 1
  65. if bot_msg == bot_msg_type.BOT_MSG_INIT_SUCESS then
  66. log.debug("bot init success")
  67. if antbot.asset_status_get(asset_config.id) == 1 then
  68. log.info("asset already register")
  69. bot_msg = bot_msg_type.BOT_MSG_REG_SUCESS
  70. else
  71. reg_retry_count = 0
  72. antbot.asset_register(asset_config.id, asset_config.type, asset_config.adv)
  73. bot_msg = bot_msg_type.BOT_MSG_UNAVAILABLE;
  74. -- 注册间隔需要大于2s
  75. -- sys.wait(2 * 1000)
  76. end
  77. elseif bot_msg == bot_msg_type.BOT_MSG_REG_FAILED then
  78. log.debug("bot register fail")
  79. sys.wait(10 * 1000)
  80. if reg_retry_count < BOT_REG_RETRY_COUNT_MAX then
  81. reg_retry_count = reg_retry_count + 1
  82. antbot.asset_register(asset_config.id, asset_config.type, asset_config.adv)
  83. log.info("msg notify BOT_MSG_REG_FAILED retry_count: ", reg_retry_count)
  84. end
  85. elseif bot_msg == bot_msg_type.BOT_MSG_REG_SUCESS or
  86. bot_msg == bot_msg_type.BOT_MSG_PUB_SUCESS or
  87. bot_msg == bot_msg_type.BOT_MSG_PUB_FAILED then
  88. log.debug("bot register success or pub success or pub failed")
  89. bot_app_asset_data_report();
  90. sys.wait(20 * 1000);
  91. else
  92. sys.wait(1 * 1000)
  93. end
  94. end
  95. end)
  96. sys.run()