main.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. PROJECT = "air780e_forwarder"
  2. VERSION = "1.0.0"
  3. log.setLevel("DEBUG")
  4. log.info("main", PROJECT, VERSION)
  5. sys = require "sys"
  6. sysplus = require "sysplus"
  7. require "sysplus"
  8. -- 添加硬狗防止程序卡死, 在支持的设备上启用这个功能
  9. if wdt then
  10. -- 初始化 watchdog 设置为 9s
  11. wdt.init(9000)
  12. -- 3s 喂一次狗
  13. sys.timerLoopStart(wdt.feed, 3000)
  14. end
  15. -- 设置 DNS
  16. socket.setDNS(nil, 1, "119.29.29.29")
  17. socket.setDNS(nil, 2, "223.5.5.5")
  18. -- 设置 SIM 自动恢复, 搜索小区信息间隔, 最大搜索时间
  19. mobile.setAuto(1000 * 10, 1000 * 60, 1000 * 5)
  20. -- POWERKEY
  21. local powerkey_timer = 0
  22. gpio.setup(
  23. 35,
  24. function()
  25. local powerkey_state = gpio.get(35)
  26. if powerkey_state == 0 then
  27. powerkey_timer = os.time()
  28. else
  29. if powerkey_timer == 0 then
  30. return
  31. end
  32. local time = os.time() - powerkey_timer
  33. if time >= 2 then
  34. log.info("POWERKEY_LONG_PRESS", time)
  35. sys.publish("POWERKEY_LONG_PRESS")
  36. else
  37. log.info("POWERKEY_SHORT_PRESS", time)
  38. sys.publish("POWERKEY_SHORT_PRESS")
  39. end
  40. powerkey_timer = 0
  41. end
  42. end,
  43. gpio.PULLUP,
  44. gpio.FALLING
  45. )
  46. config = require "config"
  47. util_netled = require "util_netled"
  48. util_mobile = require "util_mobile"
  49. util_location = require "util_location"
  50. util_notify = require "util_notify"
  51. -- 短信回调
  52. sms.setNewSmsCb(
  53. function(num, txt, metas)
  54. log.info("smsCallback", num, txt, metas and json.encode(metas) or "")
  55. util_netled.blink(200, 200, 1000)
  56. util_notify.send({txt, "", "发件人号码: " .. num, "#SMS"})
  57. end
  58. )
  59. sys.taskInit(
  60. function()
  61. -- 等待网络环境准备就绪
  62. sys.waitUntil("IP_READY")
  63. util_netled.blink(200, 200, 5000)
  64. -- 开机基站定位
  65. util_location.getCoord(
  66. function()
  67. log.info("publish", "COORD_INIT_DONE")
  68. sys.publish("COORD_INIT_DONE")
  69. end
  70. )
  71. sys.waitUntil("COORD_INIT_DONE", 1000 * 20)
  72. -- 开机通知
  73. util_notify.send("#BOOT")
  74. -- 定时查询流量
  75. if config.QUERY_TRAFFIC_INTERVAL and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then
  76. sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
  77. end
  78. -- 定时基站定位
  79. if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 10 then
  80. sys.timerLoopStart(util_location.getCoord, config.LOCATION_INTERVAL)
  81. end
  82. -- 电源键短按发送测试通知
  83. sys.subscribe(
  84. "POWERKEY_SHORT_PRESS",
  85. function()
  86. util_notify.send("#ALIVE")
  87. end
  88. )
  89. -- 电源键长按查询流量
  90. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  91. end
  92. )
  93. sys.run()