local air780_helper = { -- 是否正在读取串口数据 } air780_helper.reading = false air780_helper.cs_index = 1 local sys = require("sys") local utils = require("utils") local config = require("config") local util_notify = require("util_notify") local uart_timeout = 100 -- 使用UART 1接口与Air780E通信 -- ESP32 <--> Air780E -- 02(UART1_TX) <--> 31(UART1_RXD) -- 03(UART1_RX) <--> 30(UART1_TXD) local uart_setup_result = uart.setup(config.UART_ID, 115200, 8, 1) if uart_setup_result ~= 0 then log.error("air780_helper", "UART初始化失败,返回值:"..uart_setup_result..",ESP32将重启") sys.wait(1000) rtos.reboot() else log.info("air780_helper", "UART初始化成功") end -- 串口读缓冲区 local send_queue = {} -- 注册串口接收事件回调 uart.on(config.UART_ID, "receive", function(id, length) air780_helper.reading = true local s = "" repeat -- log.error("air780_helper", "开始设备串口") s = uart.read(id, length) if #s > 0 then table.insert(send_queue, s) sys.timerStart(sys.publish, config.UART_TIMEOUT, "UART_READY") -- sys.publish("UART_READY") end until s == "" end) sys.subscribe("UART_READY", function() led_helper.shut_working_led() led_helper.blink_status_led(config.LED_BLINK_DURATION.initializing) -- 拼接所有收到的数据 local data = table.concat(send_queue) log.info("air780_helper", '读取短信完成',data) -- 读取完成后清空缓冲区 utils.clear_table(send_queue) air780_helper.reading = false log.info("air780_helper", "去NEW_MSG处理消息类型和内容") sys.publish("NEW_MSG",data) -- while #data > 0 do -- air780_helper.reading = false -- sys.publish(config.UART_NEW_MSG,data) -- end end) -- 收到串口新消息 添加到发送队列 sys.subscribe("NEW_MSG", function(data) if data == nil then return end if #data == 0 then return end local msg = json.decode(data) if utils.table_len(msg) <= 0 then return end if msg == nil then return end local numbers = fskv.kv_get("numbers") -- log.info("air780_helper", "测试numbers", json.encode(numbers),type(numbers)) local cs_index = air780_helper.cs_index - 1 if utils.is_empty(msg.sms) == false then air780_helper.reading = false msg.index = tostring(cs_index) -- 是否屏蔽自检信息 local disable_boot = fskv.kv_get("disable_boot") if disable_boot == 1 and msg.type == "boot" then return end -- 如果没有读到号码就是用设置的号码 if utils.is_empty(msg.to) then if fskv.kv_get("numbers") then if numbers[cs_index] ~= nil then msg.to = numbers[cs_index] end end end if fskv.kv_get("hostname") then msg.hostname = fskv.kv_get("hostname") else msg.hostname = "weiguo_" .. wlan.getMac() end if utils.is_empty(msg.from) and msg.type == "boot" then msg.from = nil msg.sms = "收到设备通知:" .. msg.sms msg.index = nil end util_notify.add(msg) utils.clear_table(msg) msg = nil end end) -- 发送AT指令 function air780_helper.send_at_command(command) uart.write(config.UART_ID, command) uart.write(config.UART_ID, "\r\n") log.debug("air780_helper", "发送AT指令\""..command.."\"") end -- 发送AT指令并等待指定topic function air780_helper.send_at_command_and_wait(command, topic_listen_to, timeout) while true do air780_helper.send_at_command(command) local is_successful, r1, r2, r3 = sys.waitUntil(topic_listen_to, timeout or 1000) if is_successful then return r1, r2, r3 end end end return air780_helper