main.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. -- 设置电平输出 3.3V
  12. -- pm.ioVol(pm.IOVOL_ALL_GPIO, 3300)
  13. -- 设置 DNS
  14. socket.setDNS(nil, 1, "119.29.29.29")
  15. socket.setDNS(nil, 2, "223.5.5.5")
  16. -- SIM 自动恢复, 周期性获取小区信息, 网络遇到严重故障时尝试自动恢复等功能
  17. mobile.setAuto(10000, 300000, 8, true, 120000)
  18. -- 开启 IPv6
  19. -- mobile.ipv6(true)
  20. -- 初始化 fskv
  21. log.info("main", "fskv.init", fskv.init())
  22. -- POWERKEY
  23. local rtos_bsp = rtos.bsp()
  24. local pin_table = { ["EC618"] = 35, ["EC718P"] = 46 }
  25. local powerkey_pin = pin_table[rtos_bsp]
  26. if powerkey_pin then
  27. local button_last_press_time, button_last_release_time = 0, 0
  28. gpio.setup(powerkey_pin, function()
  29. local current_time = mcu.ticks()
  30. -- 按下
  31. if gpio.get(powerkey_pin) == 0 then
  32. button_last_press_time = current_time -- 记录最后一次按下时间
  33. return
  34. end
  35. -- 释放
  36. if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放
  37. return
  38. end
  39. if current_time - button_last_release_time < 250 then -- 防止连按
  40. return
  41. end
  42. local duration = current_time - button_last_press_time -- 按键持续时间
  43. button_last_release_time = current_time -- 记录最后一次释放时间
  44. if duration > 2000 then
  45. log.debug("EVENT.POWERKEY_LONG_PRESS", duration)
  46. sys.publish("POWERKEY_LONG_PRESS", duration)
  47. elseif duration > 50 then
  48. log.debug("EVENT.POWERKEY_SHORT_PRESS", duration)
  49. sys.publish("POWERKEY_SHORT_PRESS", duration)
  50. end
  51. end, gpio.PULLUP)
  52. end
  53. -- 加载模块
  54. config = require "config"
  55. util_http = require "util_http"
  56. util_netled = require "util_netled"
  57. util_mobile = require "util_mobile"
  58. util_location = require "util_location"
  59. util_notify = require "util_notify"
  60. -- 由于 NOTIFY_TYPE 支持多个配置, 需按照包含来判断
  61. local containsValue = function(t, value)
  62. if t == value then return true end
  63. if type(t) ~= "table" then return false end
  64. for k, v in pairs(t) do if v == value then return true end end
  65. return false
  66. end
  67. if containsValue(config.NOTIFY_TYPE, "serial") then
  68. -- 串口配置
  69. uart.setup(1, 115200, 8, 1, uart.NONE)
  70. -- 串口接收回调
  71. uart.on(1, "receive", function(id, len)
  72. local data = uart.read(id, len)
  73. log.info("uart read:", id, len, data)
  74. if config.ROLE == "MASTER" then
  75. -- 主机, 通过队列发送数据
  76. util_notify.add(data)
  77. else
  78. -- 从机, 通过串口发送数据
  79. uart.write(1, data)
  80. end
  81. end)
  82. end
  83. -- 短信接收回调
  84. sms.setNewSmsCb(function(sender_number, sms_content, m)
  85. local time = string.format("%d/%02d/%02d %02d:%02d:%02d", m.year + 2000, m.mon, m.day, m.hour, m.min, m.sec)
  86. log.info("smsCallback", time, sender_number, sms_content)
  87. -- 短信控制
  88. local is_sms_ctrl = false
  89. local receiver_number, sms_content_to_be_sent = sms_content:match("^SMS,(+?%d+),(.+)$")
  90. receiver_number, sms_content_to_be_sent = receiver_number or "", sms_content_to_be_sent or ""
  91. if sms_content_to_be_sent ~= "" and receiver_number ~= "" and #receiver_number >= 5 and #receiver_number <= 20 then
  92. sms.send(receiver_number, sms_content_to_be_sent)
  93. is_sms_ctrl = true
  94. end
  95. -- 发送通知
  96. util_notify.add({ sms_content, "", "发件号码: " .. sender_number, "发件时间: " .. time, "#SMS" .. (is_sms_ctrl and " #CTRL" or "") })
  97. end)
  98. sys.taskInit(function()
  99. -- 等待网络环境准备就绪
  100. sys.waitUntil("IP_READY", 1000 * 60 * 5)
  101. util_netled.init()
  102. -- 开机通知
  103. if config.BOOT_NOTIFY then
  104. sys.timerStart(util_notify.add, 1000 * 5, "#BOOT_" .. pm.lastReson())
  105. end
  106. -- 定时同步时间
  107. if os.time() < 1714500000 then
  108. socket.sntp()
  109. end
  110. if type(config.SNTP_INTERVAL) == "number" and config.SNTP_INTERVAL >= 1000 * 60 then
  111. sys.timerLoopStart(socket.sntp, config.SNTP_INTERVAL)
  112. end
  113. -- 定时查询流量
  114. if type(config.QUERY_TRAFFIC_INTERVAL) == "number" and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then
  115. sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
  116. end
  117. -- 定时基站定位
  118. if type(config.LOCATION_INTERVAL) == "number" and config.LOCATION_INTERVAL >= 1000 * 60 then
  119. util_location.refresh(nil, true)
  120. sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL)
  121. end
  122. -- 定时上报
  123. if type(config.REPORT_INTERVAL) == "number" and config.REPORT_INTERVAL >= 1000 * 60 then
  124. sys.timerLoopStart(function() util_notify.add("#ALIVE_REPORT") end, config.REPORT_INTERVAL)
  125. end
  126. -- 电源键短按发送测试通知
  127. sys.subscribe("POWERKEY_SHORT_PRESS", function() util_notify.add("#ALIVE") end)
  128. -- 电源键长按查询流量
  129. sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
  130. end)
  131. sys.taskInit(function()
  132. if type(config.PIN_CODE) ~= "string" or config.PIN_CODE == "" then
  133. return
  134. end
  135. -- 开机等待 5 秒仍未联网, 再进行 pin 验证
  136. if not sys.waitUntil("IP_READY", 1000 * 5) then
  137. util_mobile.pinVerify(config.PIN_CODE)
  138. end
  139. end)
  140. -- 定时开关飞行模式
  141. if type(config.FLYMODE_INTERVAL) == "number" and config.FLYMODE_INTERVAL >= 1000 * 60 then
  142. sys.timerLoopStart(function()
  143. mobile.flymode(0, true)
  144. mobile.flymode(0, false)
  145. end, config.FLYMODE_INTERVAL)
  146. end
  147. -- 通话相关
  148. local is_calling = false
  149. sys.subscribe("CC_IND", function(status)
  150. if cc == nil then return end
  151. if status == "INCOMINGCALL" then
  152. -- 来电事件, 期间会重复触发
  153. if is_calling then return end
  154. is_calling = true
  155. log.info("cc_status", "INCOMINGCALL", "来电事件", cc.lastNum())
  156. -- 发送通知
  157. util_notify.add({ "来电号码: " .. cc.lastNum(), "来电时间: " .. os.date("%Y-%m-%d %H:%M:%S"), "#CALL #CALL_IN" })
  158. return
  159. end
  160. if status == "DISCONNECTED" then
  161. -- 挂断事件
  162. is_calling = false
  163. log.info("cc_status", "DISCONNECTED", "挂断事件", cc.lastNum())
  164. -- 发送通知
  165. util_notify.add({ "来电号码: " .. cc.lastNum(), "挂断时间: " .. os.date("%Y-%m-%d %H:%M:%S"), "#CALL #CALL_DISCONNECTED" })
  166. return
  167. end
  168. log.info("cc_status", status)
  169. end)
  170. sys.run()