main.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. PROJECT = "air780e_forwarder"
  2. VERSION = "1.0.0"
  3. log.setLevel("DEBUG")
  4. log.info("main", PROJECT, VERSION)
  5. log.info("main", "开机原因", pm.lastReson())
  6. sys = require "sys"
  7. sysplus = require "sysplus"
  8. require "sysplus"
  9. -- 添加硬狗防止程序卡死
  10. wdt.init(9000)
  11. sys.timerLoopStart(wdt.feed, 3000)
  12. -- 设置 DNS
  13. socket.setDNS(nil, 1, "119.29.29.29")
  14. socket.setDNS(nil, 2, "223.5.5.5")
  15. -- 设置 SIM 自动恢复(单位: 毫秒), 搜索小区信息间隔(单位: 毫秒), 最大搜索时间(单位: 秒)
  16. mobile.setAuto(1000 * 10)
  17. -- POWERKEY
  18. local button_last_press_time, button_last_release_time = 0, 0
  19. gpio.setup(
  20. 35,
  21. function()
  22. local current_time = mcu.ticks()
  23. -- 按下
  24. if gpio.get(35) == 0 then
  25. button_last_press_time = current_time -- 记录最后一次按下时间
  26. return
  27. end
  28. -- 释放
  29. if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放
  30. return
  31. end
  32. if current_time - button_last_release_time < 250 then -- 防止连按
  33. return
  34. end
  35. local duration = current_time - button_last_press_time -- 按键持续时间
  36. button_last_release_time = current_time -- 记录最后一次释放时间
  37. if duration > 2000 then
  38. log.debug("EVENT.POWERKEY_LONG_PRESS", duration)
  39. sys.publish("POWERKEY_LONG_PRESS", duration)
  40. elseif duration > 50 then
  41. log.debug("EVENT.POWERKEY_SHORT_PRESS", duration)
  42. sys.publish("POWERKEY_SHORT_PRESS", duration)
  43. end
  44. end,
  45. gpio.PULLUP
  46. )
  47. -- 加载模块
  48. config = require "config"
  49. util_http = require "util_http"
  50. util_netled = require "util_netled"
  51. util_mobile = require "util_mobile"
  52. util_location = require "util_location"
  53. util_notify = require "util_notify"
  54. -- 短信接收回调
  55. sms.setNewSmsCb(
  56. function(sender_number, sms_content, m)
  57. local time = string.format("%d/%02d/%02d %02d:%02d:%02d", m.year + 2000, m.mon, m.day, m.hour, m.min, m.sec)
  58. log.info("smsCallback", time, sender_number, sms_content)
  59. -- 短信控制
  60. local is_sms_ctrl = false
  61. local receiver_number, sms_content_to_be_sent = sms_content:match("^SMS,(+?%d+),(.+)$")
  62. receiver_number, sms_content_to_be_sent = receiver_number or "", sms_content_to_be_sent or ""
  63. if sms_content_to_be_sent ~= "" and receiver_number ~= "" and #receiver_number >= 5 and #receiver_number <= 20 then
  64. sms.send(receiver_number, sms_content_to_be_sent)
  65. is_sms_ctrl = true
  66. end
  67. -- 发送通知
  68. util_notify.add(
  69. {
  70. sms_content,
  71. "",
  72. "发件号码: " .. sender_number,
  73. "发件时间: " .. time,
  74. "#SMS" .. (is_sms_ctrl and " #CTRL" or "")
  75. }
  76. )
  77. end
  78. )
  79. sys.taskInit(
  80. function()
  81. -- 等待网络环境准备就绪
  82. sys.waitUntil("IP_READY")
  83. util_netled.init()
  84. -- 开机通知
  85. if config.BOOT_NOTIFY then
  86. util_notify.add("#BOOT")
  87. end
  88. -- 定时查询流量
  89. if config.QUERY_TRAFFIC_INTERVAL and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then
  90. sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
  91. end
  92. -- 定时基站定位
  93. if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then
  94. sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30)
  95. end
  96. -- 电源键短按发送测试通知
  97. sys.subscribe(
  98. "POWERKEY_SHORT_PRESS",
  99. function()
  100. util_notify.add("#ALIVE")
  101. end
  102. )
  103. -- 电源键长按查询流量
  104. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  105. -- 开启低功耗模式
  106. if config.LOW_POWER_MODE then
  107. sys.wait(1000 * 15)
  108. log.warn("main", "即将关闭 usb 电源, 如需查看日志请在配置中关闭低功耗模式")
  109. sys.wait(1000 * 5)
  110. -- 关闭 USB
  111. pm.power(pm.USB, false)
  112. pm.power(pm.GPS, false)
  113. pm.power(pm.GPS_ANT, false)
  114. pm.power(pm.DAC_EN, false)
  115. -- 休眠
  116. pm.force(pm.LIGHT)
  117. end
  118. end
  119. )
  120. sys.run()