main.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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)
  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. -- 加载模块
  51. config = require "config"
  52. util_http = require "util_http"
  53. util_netled = require "util_netled"
  54. util_mobile = require "util_mobile"
  55. util_location = require "util_location"
  56. util_notify = require "util_notify"
  57. -- 短信回调
  58. sms.setNewSmsCb(
  59. function(num, txt, metas)
  60. log.info("smsCallback", num, txt, metas and json.encode(metas) or "")
  61. util_netled.blink(50, 50, 5000)
  62. util_notify.add({txt, "", "发件人号码: " .. num, "#SMS"})
  63. end
  64. )
  65. sys.taskInit(
  66. function()
  67. -- 等待网络环境准备就绪
  68. sys.waitUntil("IP_READY")
  69. util_netled.init()
  70. -- 开机通知
  71. if config.BOOT_NOTIFY then
  72. util_notify.add("#BOOT")
  73. end
  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 * 30 then
  80. sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30)
  81. end
  82. -- 电源键短按发送测试通知
  83. sys.subscribe(
  84. "POWERKEY_SHORT_PRESS",
  85. function()
  86. util_notify.add("#ALIVE")
  87. end
  88. )
  89. -- 电源键长按查询流量
  90. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  91. end
  92. )
  93. sys.run()