main.lua 4.7 KB

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