main.lua 4.8 KB

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