main.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(sender_number, sms_content, m)
  60. local time = string.format("%d/%02d/%02d %02d:%02d:%02d", m.year + 2000, m.mon, m.day, m.hour, m.min, m.sec)
  61. log.info("smsCallback", time, sender_number, sms_content)
  62. -- 短信控制
  63. local is_sms_ctrl = false
  64. local receiver_number, sms_content_to_be_sent = sms_content:match("^SMS,(+?%d+),(.+)$")
  65. receiver_number, sms_content_to_be_sent = receiver_number or "", sms_content_to_be_sent or ""
  66. if sms_content_to_be_sent ~= "" and receiver_number ~= "" and #receiver_number >= 5 and #receiver_number <= 20 then
  67. sms.send(receiver_number, sms_content_to_be_sent)
  68. is_sms_ctrl = true
  69. end
  70. -- 发送通知
  71. util_notify.add(
  72. {
  73. sms_content,
  74. "",
  75. "发件号码: " .. sender_number,
  76. "发件时间: " .. time,
  77. "#SMS" .. (is_sms_ctrl and " #CTRL" or "")
  78. }
  79. )
  80. end
  81. )
  82. sys.taskInit(
  83. function()
  84. -- 等待网络环境准备就绪
  85. sys.waitUntil("IP_READY")
  86. util_netled.init()
  87. -- 开机通知
  88. if config.BOOT_NOTIFY then
  89. util_notify.add("#BOOT")
  90. end
  91. -- 定时查询流量
  92. if config.QUERY_TRAFFIC_INTERVAL and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then
  93. sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
  94. end
  95. -- 定时基站定位
  96. if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then
  97. sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30)
  98. end
  99. -- 电源键短按发送测试通知
  100. sys.subscribe(
  101. "POWERKEY_SHORT_PRESS",
  102. function()
  103. util_notify.add("#ALIVE")
  104. end
  105. )
  106. -- 电源键长按查询流量
  107. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  108. end
  109. )
  110. sys.run()