main.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 button_last_press_time, button_last_release_time = 0, 0
  22. gpio.setup(
  23. 35,
  24. function()
  25. local current_time = mcu.ticks()
  26. -- 按下
  27. if gpio.get(35) == 0 then
  28. button_last_press_time = current_time -- 记录最后一次按下时间
  29. return
  30. end
  31. -- 释放
  32. if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放
  33. return
  34. end
  35. if current_time - button_last_release_time < 250 then -- 防止连按
  36. return
  37. end
  38. local duration = current_time - button_last_press_time -- 按键持续时间
  39. button_last_release_time = current_time -- 记录最后一次释放时间
  40. if duration > 2000 then
  41. log.debug("EVENT.POWERKEY_LONG_PRESS", duration)
  42. sys.publish("POWERKEY_LONG_PRESS", duration)
  43. elseif duration > 50 then
  44. log.debug("EVENT.POWERKEY_SHORT_PRESS", duration)
  45. sys.publish("POWERKEY_SHORT_PRESS", duration)
  46. end
  47. end,
  48. gpio.PULLUP
  49. )
  50. config = require "config"
  51. util_netled = require "util_netled"
  52. util_mobile = require "util_mobile"
  53. util_location = require "util_location"
  54. util_notify = require "util_notify"
  55. -- 短信回调
  56. sms.setNewSmsCb(
  57. function(num, txt, metas)
  58. log.info("smsCallback", num, txt, metas and json.encode(metas) or "")
  59. util_netled.blink(50, 50, 5000)
  60. util_notify.send({txt, "", "发件人号码: " .. num, "#SMS"})
  61. end
  62. )
  63. sys.taskInit(
  64. function()
  65. -- 等待网络环境准备就绪
  66. sys.waitUntil("IP_READY")
  67. util_netled.init()
  68. -- 开机基站定位
  69. util_location.getCoord(
  70. function()
  71. log.info("publish", "COORD_INIT_DONE")
  72. sys.publish("COORD_INIT_DONE")
  73. end
  74. )
  75. sys.waitUntil("COORD_INIT_DONE", 1000 * 20)
  76. -- 开机通知
  77. if config.BOOT_NOTIFY then
  78. util_notify.send("#BOOT")
  79. end
  80. -- 定时查询流量
  81. if config.QUERY_TRAFFIC_INTERVAL and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then
  82. sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
  83. end
  84. -- 定时基站定位
  85. if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 10 then
  86. sys.timerLoopStart(util_location.getCoord, config.LOCATION_INTERVAL)
  87. end
  88. -- 电源键短按发送测试通知
  89. sys.subscribe(
  90. "POWERKEY_SHORT_PRESS",
  91. function()
  92. util_notify.send("#ALIVE")
  93. end
  94. )
  95. -- 电源键长按查询流量
  96. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  97. end
  98. )
  99. sys.run()