PROJECT = "air780e_forwarder" VERSION = "1.0.0" log.setLevel("DEBUG") log.info("main", PROJECT, VERSION) log.info("main", "开机原因", pm.lastReson()) sys = require "sys" sysplus = require "sysplus" require "sysplus" -- 添加硬狗防止程序卡死 wdt.init(9000) sys.timerLoopStart(wdt.feed, 3000) -- 设置 SIM 自动恢复(单位: 毫秒), 搜索小区信息间隔(单位: 毫秒), 最大搜索时间(单位: 秒) mobile.setAuto(1000 * 10) -- POWERKEY local button_last_press_time, button_last_release_time = 0, 0 gpio.setup( 10, function() local current_time = mcu.ticks() -- 按下 if gpio.get(10) == gpio.LOW then button_last_press_time = current_time -- 记录最后一次按下时间 return end -- 释放 if button_last_press_time == 0 then -- 开机前已经按下, 开机后释放 return end if current_time - button_last_release_time < 250 then -- 防止连按 return end local duration = current_time - button_last_press_time -- 按键持续时间 button_last_release_time = current_time -- 记录最后一次释放时间 if duration > 2000 then log.debug("EVENT.POWERKEY_LONG_PRESS", duration) sys.publish("POWERKEY_LONG_PRESS", duration) elseif duration > 50 then log.debug("EVENT.POWERKEY_SHORT_PRESS", duration) sys.publish("POWERKEY_SHORT_PRESS", duration) end end, gpio.PULLUP ) -- 加载模块 config = require "config" -- util_http = require "util_http" util_netled = require "util_netled" util_mobile = require "util_mobile" -- util_location = require "util_location" util_notify = require "util_notify" -- 传输状态机 GPIO1 默认低 内部下拉 gpio.setup(config.STATUS_GPIO, nil, gpio.PULLDOWN) -- 短信接收回调 sms.setNewSmsCb( function(sender_number, sms_content, m) local time = string.format("%d/%02d/%02d %02d:%02d:%02d", m.year + 2000, m.mon, m.day, m.hour, m.min, m.sec) log.info("smsCallback", time, sender_number, sms_content) -- 短信控制 local is_sms_ctrl = false local receiver_number, sms_content_to_be_sent = sms_content:match("^SMS,(+?%d+),(.+)$") receiver_number, sms_content_to_be_sent = receiver_number or "", sms_content_to_be_sent or "" if sms_content_to_be_sent ~= "" and receiver_number ~= "" and #receiver_number >= 5 and #receiver_number <= 20 then sms.send(receiver_number, sms_content_to_be_sent) is_sms_ctrl = true end -- local my_number = mobile.number(mobile.simid()) -- 发送通知 util_notify.add( { from = sender_number, sms = sms_content, from_time = time, sms_type = (is_sms_ctrl and "控制" or "正常"), type = "sms" } ) end ) old_ststus = "" sys.taskInit( function() util_netled.init() -- 开机通知 if config.BOOT_NOTIFY then util_notify.add( { sms = "设备开机", type = "boot" } ) end -- 等待网络环境准备就绪 -- sys.waitUntil("IP_READY") sys.subscribe("IP_READY", function() log.info("main", "IP_READY") end) -- 定时查询流量 -- if config.QUERY_TRAFFIC_INTERVAL and config.QUERY_TRAFFIC_INTERVAL >= 1000 * 60 then -- sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL) -- end -- -- 定时基站定位 -- if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then -- sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30) -- end -- 断网后会发一次这个消息 sys.subscribe("SIM_IND", function(status, value) -- log.info("上次状态", old_ststus,"当前状态",status,old_ststus ~= string.gsub(status, "%s+", "")) if old_ststus ~= string.gsub(status, "%s+", "") then old_ststus = string.gsub(status, "%s+", "") -- status的取值有: -- RDY SIM卡就绪, value为nil -- NORDY 无SIM卡, value为nil -- SIM_PIN 需要输入PIN, value为nil -- GET_NUMBER 获取到电话号码(不一定有值), value为nil -- SIM_WC SIM卡的写入次数统计,掉电归0, value为统计值 if status == "RDY" then log.info("main", "SIM卡就绪") elseif status == "NORDY" then log.info("main", "无SIM卡") util_notify.add( { sms = "设备没安装SIM卡", type = "boot" } ) elseif status == "SIM_PIN" then log.info("main", "需要输入PIN") util_notify.add( { sms = "设备SIM卡需要输入PIN码解锁", type = "boot" } ) end end end) -- 电源键短按发送测试通知 sys.subscribe( "POWERKEY_SHORT_PRESS", function() log.info("main", "POWERKEY_SHORT_PRESS") util_notify.add( { from = "15264925507", sms = "本地测试短信通知并没有实际收到短信", from_time = "2024-03-14 00:00:00", sms_type = "正常", type = "sms" } ) end ) -- 电源键长按查询流量 sys.subscribe( "POWERKEY_LONG_PRESS", function() log.info("main", "POWERKEY_LONG_PRESS") util_notify.add( { from = "15264925507", sms = "长按本地测试短信通知并没有实际收到短信", from_time = "2024-03-14 00:00:00", sms_type = "正常", type = "sms" } ) end ) -- 开启低功耗模式 if config.LOW_POWER_MODE then sys.wait(1000 * 15) log.warn("main", "即将关闭 usb 电源, 如需查看日志请在配置中关闭低功耗模式") sys.wait(1000 * 5) -- gpio.setup(23, nil) -- gpio.close(33) pm.power(pm.USB, false) -- 关闭 USB pm.power(pm.GPS, false) pm.power(pm.GPS_ANT, false) pm.power(pm.DAC_EN, false) pm.force(pm.LIGHT) -- 进入休眠 end end ) sys.run()