ソースを参照

:art: Format code

Mizore 2 年 前
コミット
a8af2742b3
7 ファイル変更212 行追加354 行削除
  1. 38 52
      script/lib_smtp.lua
  2. 78 102
      script/main.lua
  3. 7 7
      script/util_http.lua
  4. 11 27
      script/util_location.lua
  5. 10 10
      script/util_mobile.lua
  6. 29 39
      script/util_netled.lua
  7. 39 117
      script/util_notify.lua

+ 38 - 52
script/lib_smtp.lua

@@ -21,15 +21,10 @@ end
 -- @param content string, 需要转义的内容
 -- @return string, 转义后的内容
 local function escapeDot(content)
-    return content:gsub(
-        "(.-\r\n)",
-        function(line)
-            if line:sub(1, 1) == "." then
-                line = "." .. line
-            end
-            return line
-        end
-    )
+    return content:gsub("(.-\r\n)", function(line)
+        if line:sub(1, 1) == "." then line = "." .. line end
+        return line
+    end)
 end
 
 --- 接收到数据时的处理函数
@@ -44,41 +39,39 @@ local function recvHandler(netc, rxbuf, socket_id, current_command)
     -- 如果返回非 2xx 或 3xx 状态码, 则断开连接
     if not rx_str:match("^[23]%d%d") then
         log.error("lib_smtp", socket_id, "服务器返回错误状态码, 断开连接, 请检查日志")
-        sys.publish(socket_id .. "_disconnect", {success = false, message = "服务器返回错误状态码", is_retry = false})
+        sys.publish(socket_id .. "_disconnect", { success = false, message = "服务器返回错误状态码", is_retry = false })
         return
     end
 
     if current_command == nil then
         log.info("lib_smtp", socket_id, "全部发送完成")
-        sys.publish(socket_id .. "_disconnect", {success = true, message = "发送成功", is_retry = false})
+        sys.publish(socket_id .. "_disconnect", { success = true, message = "发送成功", is_retry = false })
         return
     end
 
     -- 分包发送
     local index = 1
-    sys.taskInit(
-        function()
-            while index <= #current_command do
-                local packet = current_command:sub(index, index + lib_smtp.packet_size - 1)
-                socket.tx(netc, packet)
-                log.info("lib_smtp", socket_id, "->", logFormat(packet))
-                index = index + lib_smtp.packet_size
-                sys.wait(100)
-            end
+    sys.taskInit(function()
+        while index <= #current_command do
+            local packet = current_command:sub(index, index + lib_smtp.packet_size - 1)
+            socket.tx(netc, packet)
+            log.info("lib_smtp", socket_id, "->", logFormat(packet))
+            index = index + lib_smtp.packet_size
+            sys.wait(100)
         end
-    )
+    end)
 end
 
 local function validateParameters(smtp_config)
     -- 配置参数验证规则
     local validation_rules = {
-        {field = "host", type = "string", required = true},
-        {field = "port", type = "number", required = true},
-        {field = "username", type = "string", required = true},
-        {field = "password", type = "string", required = true},
-        {field = "mail_from", type = "string", required = true},
-        {field = "mail_to", type = "string", required = true},
-        {field = "tls_enable", type = "boolean", required = false}
+        { field = "host", type = "string", required = true },
+        { field = "port", type = "number", required = true },
+        { field = "username", type = "string", required = true },
+        { field = "password", type = "string", required = true },
+        { field = "mail_from", type = "string", required = true },
+        { field = "mail_to", type = "string", required = true },
+        { field = "tls_enable", type = "boolean", required = false },
     }
     local result = true
     for _, rule in ipairs(validation_rules) do
@@ -113,12 +106,10 @@ function lib_smtp.send(body, subject, smtp_config)
     -- 参数验证
     if type(smtp_config) ~= "table" then
         log.error("lib_smtp", "`smtp_config` 应为 table 类型")
-        return {success = false, message = "参数错误", is_retry = false}
+        return { success = false, message = "参数错误", is_retry = false }
     end
     local valid = validateParameters(smtp_config)
-    if not valid then
-        return {success = false, message = "参数错误", is_retry = false}
-    end
+    if not valid then return { success = false, message = "参数错误", is_retry = false } end
 
     subject = type(subject) == "string" and subject or ""
     body = type(body) == "string" and escapeDot(body) or ""
@@ -133,19 +124,16 @@ function lib_smtp.send(body, subject, smtp_config)
         "MAIL FROM: <" .. smtp_config.mail_from .. ">\r\n",
         "RCPT TO: <" .. smtp_config.mail_to .. ">\r\n",
         "DATA\r\n",
-        table.concat(
-            {
-                "From: " .. smtp_config.mail_from,
-                "To: " .. smtp_config.mail_to,
-                "Subject: " .. subject,
-                "Content-Type: text/plain; charset=UTF-8",
-                "",
-                body,
-                ".",
-                ""
-            },
-            "\r\n"
-        )
+        table.concat({
+            "From: " .. smtp_config.mail_from,
+            "To: " .. smtp_config.mail_to,
+            "Subject: " .. subject,
+            "Content-Type: text/plain; charset=UTF-8",
+            "",
+            body,
+            ".",
+            "",
+        }, "\r\n"),
     }
     local current_command_index = 1
     local function getNextCommand()
@@ -157,7 +145,7 @@ function lib_smtp.send(body, subject, smtp_config)
     -- socket 回调
     local function netCB(netc, event, param)
         if param ~= 0 then
-            sys.publish(socket_id .. "_disconnect", {success = false, message = "param~=0", is_retry = true})
+            sys.publish(socket_id .. "_disconnect", { success = false, message = "param~=0", is_retry = true })
             return
         end
         if event == socket.LINK then
@@ -167,15 +155,13 @@ function lib_smtp.send(body, subject, smtp_config)
         elseif event == socket.EVENT then
             socket.rx(netc, rxbuf)
             socket.wait(netc)
-            if rxbuf:used() > 0 then
-                recvHandler(netc, rxbuf, socket_id, getNextCommand())
-            end
+            if rxbuf:used() > 0 then recvHandler(netc, rxbuf, socket_id, getNextCommand()) end
             rxbuf:del()
         elseif event == socket.TX_OK then
             socket.wait(netc)
         elseif event == socket.CLOSE then
             log.info("lib_smtp", socket_id, "CLOSED")
-            sys.publish(socket_id .. "_disconnect", {success = false, message = "服务器断开连接", is_retry = true})
+            sys.publish(socket_id .. "_disconnect", { success = false, message = "服务器断开连接", is_retry = true })
         end
     end
 
@@ -187,7 +173,7 @@ function lib_smtp.send(body, subject, smtp_config)
     local is_connect_success = socket.connect(netc, smtp_config.host, smtp_config.port)
     if not is_connect_success then
         socket.close(netc)
-        return {success = false, message = "未知错误", is_retry = true}
+        return { success = false, message = "未知错误", is_retry = true }
     end
     -- 等待发送结果
     local is_send_success, send_result = sys.waitUntil(socket_id .. "_disconnect", lib_smtp.timeout)
@@ -196,7 +182,7 @@ function lib_smtp.send(body, subject, smtp_config)
         return send_result
     else
         log.error("lib_smtp", socket_id, "发送超时")
-        return {success = false, message = "发送超时", is_retry = true}
+        return { success = false, message = "发送超时", is_retry = true }
     end
 end
 

+ 78 - 102
script/main.lua

@@ -7,7 +7,6 @@ log.info("main", "开机原因", pm.lastReson())
 
 sys = require "sys"
 sysplus = require "sysplus"
-require "sysplus"
 
 -- 串口配置
 uart.setup(1, 115200, 8, 1, uart.NONE)
@@ -28,34 +27,30 @@ mobile.ipv6(true)
 
 -- POWERKEY
 local button_last_press_time, button_last_release_time = 0, 0
-gpio.setup(
-    35,
-    function()
-        local current_time = mcu.ticks()
-        -- 按下
-        if gpio.get(35) == 0 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
-)
+gpio.setup(35, function()
+    local current_time = mcu.ticks()
+    -- 按下
+    if gpio.get(35) == 0 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"
@@ -79,80 +74,61 @@ uart.on(1, "receive", function(id, len)
 end)
 
 -- 短信接收回调
-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
-
-        -- 发送通知
-        util_notify.add(
-            {
-                sms_content,
-                "",
-                "发件号码: " .. sender_number,
-                "发件时间: " .. time,
-                "#SMS" .. (is_sms_ctrl and " #CTRL" or "")
-            }
-        )
+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
-)
-
-sys.taskInit(
-    function()
-        -- 等待网络环境准备就绪
-        sys.waitUntil("IP_READY")
-
-        util_netled.init()
-
-        -- 开机通知
-        if config.BOOT_NOTIFY then
-            sys.timerStart(util_notify.add, 1000 * 5, "#BOOT")
-        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
-            util_location.refresh(nil, true)
-            sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL)
-        end
-
-        -- 电源键短按发送测试通知
-        sys.subscribe(
-            "POWERKEY_SHORT_PRESS",
-            function()
-                util_notify.add("#ALIVE")
-            end
-        )
-        -- 电源键长按查询流量
-        sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
-
-        -- 开启低功耗模式
-        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
+
+    -- 发送通知
+    util_notify.add({ sms_content, "", "发件号码: " .. sender_number, "发件时间: " .. time, "#SMS" .. (is_sms_ctrl and " #CTRL" or "") })
+end)
+
+sys.taskInit(function()
+    -- 等待网络环境准备就绪
+    sys.waitUntil("IP_READY")
+
+    util_netled.init()
+
+    -- 开机通知
+    if config.BOOT_NOTIFY then sys.timerStart(util_notify.add, 1000 * 5, "#BOOT") 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
+        util_location.refresh(nil, true)
+        sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL)
     end
-)
+
+    -- 电源键短按发送测试通知
+    sys.subscribe("POWERKEY_SHORT_PRESS", function() util_notify.add("#ALIVE") end)
+    -- 电源键长按查询流量
+    sys.subscribe("POWERKEY_LONG_PRESS", util_mobile.queryTraffic)
+
+    -- 开启低功耗模式
+    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()

+ 7 - 7
script/util_http.lua

@@ -12,8 +12,10 @@ local http_running_count = 0
 -- @param headers 请求头
 -- @param body 请求体
 function util_http.fetch(timeout, method, url, headers, body)
+    collectgarbage("collect")
+
     timeout = timeout or 1000 * 25
-    local opts = {timeout = timeout}
+    local opts = { timeout = timeout }
 
     http_count = http_count + 1
     http_running_count = http_running_count + 1
@@ -27,14 +29,12 @@ function util_http.fetch(timeout, method, url, headers, body)
     res_code, res_headers, res_body = http.request(method, url, headers, body, opts).wait()
     log.debug("util_http.fetch", "请求结束", "id:", id, "code:", res_code)
 
-    if res_code == -8 then
-        log.warn("util_http.fetch", "请求超时", "id:", id)
-    end
+    if res_code == -8 then log.warn("util_http.fetch", "请求超时", "id:", id) end
 
     http_running_count = http_running_count - 1
-    if http_running_count == 0 then
-        util_netled.blink()
-    end
+    if http_running_count == 0 then util_netled.blink() end
+
+    collectgarbage("collect")
 
     return res_code, res_headers, res_body
 end

+ 11 - 27
script/util_location.lua

@@ -3,12 +3,7 @@ local util_location = {}
 PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
 local lbsLoc = require("lbsLoc")
 
-local cache = {
-    lbs_data = {
-        lat = 0,
-        lng = 0
-    }
-}
+local cache = { lbs_data = { lat = 0, lng = 0 } }
 
 --- 格式化经纬度 (保留小数点后 6 位, 去除末尾的 0)
 -- @param value 经纬度
@@ -26,9 +21,7 @@ end
 local function getMapLink(lat, lng)
     lat, lng = lat or 0, lng or 0
     local map_link = ""
-    if lat ~= 0 and lng ~= 0 then
-        map_link = "http://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
-    end
+    if lat ~= 0 and lng ~= 0 then map_link = "http://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng end
     log.debug("util_location.getMapLink", map_link)
     return map_link
 end
@@ -37,9 +30,7 @@ end
 local function getLocCb(result, lat, lng, addr, time, locType)
     log.info("util_location.getLocCb", "result,lat,lng,time,locType:", result, lat, lng, time:toHex(), locType)
     -- 获取经纬度成功, 坐标系WGS84
-    if result == 0 then
-        cache.lbs_data = {lat, lng}
-    end
+    if result == 0 then cache.lbs_data = { lat, lng } end
 end
 
 --- 刷新基站信息
@@ -59,17 +50,13 @@ end
 
 --- 刷新基站定位信息
 -- @param timeout 超时时间(单位: 毫秒)
-function util_location.refresh(timeout, is_refresh_cell_info_disabled)
+function util_location.refresh(timeout)
     timeout = type(timeout) == "number" and timeout or nil
 
-    sys.taskInit(
-        function()
-            if not is_refresh_cell_info_disabled then
-                refreshCellInfo()
-            end
-            lbsLoc.request(getLocCb, nil, timeout, nil, "bs.air32.cn")
-        end
-    )
+    sys.taskInit(function()
+        refreshCellInfo()
+        lbsLoc.request(getLocCb, nil, timeout, nil, "bs.air32.cn")
+    end)
 end
 
 --- 获取位置信息
@@ -82,11 +69,8 @@ function util_location.get()
     return lat, lng, getMapLink(lat, lng)
 end
 
-sys.subscribe(
-    "CELL_INFO_UPDATE",
-    function()
-        log.debug("EVENT.CELL_INFO_UPDATE")
-    end
-)
+sys.taskInit(refreshCellInfo)
+
+sys.subscribe("CELL_INFO_UPDATE", function() log.debug("EVENT.CELL_INFO_UPDATE") end)
 
 return util_location

+ 10 - 10
script/util_mobile.lua

@@ -3,19 +3,19 @@ local util_mobile = {}
 -- 运营商数据
 local oper_data = {
     -- 中国移动
-    ["46000"] = {"CM", "中国移动", {"10086", "CXLL"}},
-    ["46002"] = {"CM", "中国移动", {"10086", "CXLL"}},
-    ["46007"] = {"CM", "中国移动", {"10086", "CXLL"}},
+    ["46000"] = { "CM", "中国移动", { "10086", "CXLL" } },
+    ["46002"] = { "CM", "中国移动", { "10086", "CXLL" } },
+    ["46007"] = { "CM", "中国移动", { "10086", "CXLL" } },
     -- 中国联通
-    ["46001"] = {"CU", "中国联通", {"10010", "2082"}},
-    ["46006"] = {"CU", "中国联通", {"10010", "2082"}},
-    ["46009"] = {"CU", "中国联通", {"10010", "2082"}},
+    ["46001"] = { "CU", "中国联通", { "10010", "2082" } },
+    ["46006"] = { "CU", "中国联通", { "10010", "2082" } },
+    ["46009"] = { "CU", "中国联通", { "10010", "2082" } },
     -- 中国电信
-    ["46003"] = {"CT", "中国电信", {"10001", "108"}},
-    ["46005"] = {"CT", "中国电信", {"10001", "108"}},
-    ["46011"] = {"CT", "中国电信", {"10001", "108"}},
+    ["46003"] = { "CT", "中国电信", { "10001", "108" } },
+    ["46005"] = { "CT", "中国电信", { "10001", "108" } },
+    ["46011"] = { "CT", "中国电信", { "10001", "108" } },
     -- 中国广电
-    ["46015"] = {"CB", "中国广电"}
+    ["46015"] = { "CB", "中国广电" },
 }
 
 --- 获取 MCC 和 MNC

+ 29 - 39
script/util_netled.lua

@@ -9,63 +9,53 @@ local netled_interval = netled_default_interval
 local netled_inited = false
 
 -- 开机时呼吸灯效果
-sys.taskInit(
-    function()
-        local nums = {0, 1, 2, 4, 6, 12, 16, 21, 27, 34, 42, 51, 61, 72, 85, 100, 100}
-        local len = #nums
-        while true do
-            for i = 1, len, 1 do
-                pwm.open(4, 1000, nums[i])
-                result = sys.waitUntil("NET_LED_INIT", 25)
-                if result then
-                    pwm.close(4)
-                    return
-                end
+sys.taskInit(function()
+    local nums = { 0, 1, 2, 4, 6, 12, 16, 21, 27, 34, 42, 51, 61, 72, 85, 100, 100 }
+    local len = #nums
+    while true do
+        for i = 1, len, 1 do
+            pwm.open(4, 1000, nums[i])
+            result = sys.waitUntil("NET_LED_INIT", 25)
+            if result then
+                pwm.close(4)
+                return
             end
-            for i = len, 1, -1 do
-                pwm.open(4, 1000, nums[i])
-                result = sys.waitUntil("NET_LED_INIT", 25)
-                if result then
-                    pwm.close(4)
-                    return
-                end
+        end
+        for i = len, 1, -1 do
+            pwm.open(4, 1000, nums[i])
+            result = sys.waitUntil("NET_LED_INIT", 25)
+            if result then
+                pwm.close(4)
+                return
             end
         end
     end
-)
+end)
 
 -- 注册网络后开始闪烁
 function util_netled.init()
-    if netled_inited then
-        return
-    end
+    if netled_inited then return end
     netled_inited = true
     sys.publish("NET_LED_INIT")
 
-    sys.taskInit(
-        function()
-            local netled = gpio.setup(27, 0, gpio.PULLUP)
-            while true do
-                netled(1)
-                sys.waitUntil("NET_LED_UPDATE", netled_duration)
-                netled(0)
-                sys.waitUntil("NET_LED_UPDATE", netled_interval)
-            end
+    sys.taskInit(function()
+        local netled = gpio.setup(27, 0, gpio.PULLUP)
+        while true do
+            netled(1)
+            sys.waitUntil("NET_LED_UPDATE", netled_duration)
+            netled(0)
+            sys.waitUntil("NET_LED_UPDATE", netled_interval)
         end
-    )
+    end)
 end
 
 function util_netled.blink(duration, interval, restore)
-    if duration == netled_duration and interval == netled_interval then
-        return
-    end
+    if duration == netled_duration and interval == netled_interval then return end
     netled_duration = duration or netled_default_duration
     netled_interval = interval or netled_default_interval
     log.debug("EVENT.NET_LED_UPDATE", duration, interval, restore)
     sys.publish("NET_LED_UPDATE")
-    if restore then
-        sys.timerStart(util_netled.blink, restore)
-    end
+    if restore then sys.timerStart(util_netled.blink, restore) end
 end
 
 return util_netled

+ 39 - 117
script/util_notify.lua

@@ -27,9 +27,7 @@ local notify = {
             return
         end
 
-        local header = {
-            ["content-type"] = config.CUSTOM_POST_CONTENT_TYPE
-        }
+        local header = { ["content-type"] = config.CUSTOM_POST_CONTENT_TYPE }
 
         local body = json.decode(json.encode(config.CUSTOM_POST_BODY_TABLE))
         -- 遍历并替换其中的变量
@@ -67,14 +65,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["content-type"] = "application/json"
-        }
-        local body = {
-            ["chat_id"] = config.TELEGRAM_CHAT_ID,
-            ["disable_web_page_preview"] = true,
-            ["text"] = msg
-        }
+        local header = { ["content-type"] = "application/json" }
+        local body = { ["chat_id"] = config.TELEGRAM_CHAT_ID, ["disable_web_page_preview"] = true, ["text"] = msg }
         local json_data = json.encode(body)
         json_data = string.gsub(json_data, "\\b", "\\n")
 
@@ -93,14 +85,8 @@ local notify = {
         end
 
         local url = config.GOTIFY_API .. "/message?token=" .. config.GOTIFY_TOKEN
-        local header = {
-            ["Content-Type"] = "application/json; charset=utf-8"
-        }
-        local body = {
-            title = config.GOTIFY_TITLE,
-            message = msg,
-            priority = config.GOTIFY_PRIORITY
-        }
+        local header = { ["Content-Type"] = "application/json; charset=utf-8" }
+        local body = { title = config.GOTIFY_TITLE, message = msg, priority = config.GOTIFY_PRIORITY }
         local json_data = json.encode(body)
         json_data = string.gsub(json_data, "\\b", "\\n")
 
@@ -118,14 +104,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/x-www-form-urlencoded"
-        }
-        local body = {
-            pushkey = config.PUSHDEER_KEY or "",
-            type = "text",
-            text = msg
-        }
+        local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
+        local body = { pushkey = config.PUSHDEER_KEY or "", type = "text", text = msg }
 
         log.info("util_notify", "POST", config.PUSHDEER_API)
         return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
@@ -141,12 +121,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/x-www-form-urlencoded"
-        }
-        local body = {
-            body = msg
-        }
+        local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
+        local body = { body = msg }
         local url = config.BARK_API .. "/" .. config.BARK_KEY
 
         log.info("util_notify", "POST", url)
@@ -159,15 +135,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/json; charset=utf-8"
-        }
-        local body = {
-            msgtype = "text",
-            text = {
-                content = msg
-            }
-        }
+        local header = { ["Content-Type"] = "application/json; charset=utf-8" }
+        local body = { msgtype = "text", text = { content = msg } }
         local json_data = json.encode(body)
         -- LuatOS Bug, json.encode 会将 \n 转换为 \b
         json_data = string.gsub(json_data, "\\b", "\\n")
@@ -182,15 +151,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/json; charset=utf-8"
-        }
-        local body = {
-            msg_type = "text",
-            content = {
-                text = msg
-            }
-        }
+        local header = { ["Content-Type"] = "application/json; charset=utf-8" }
+        local body = { msg_type = "text", content = { text = msg } }
         local json_data = json.encode(body)
         -- LuatOS Bug, json.encode 会将 \n 转换为 \b
         json_data = string.gsub(json_data, "\\b", "\\n")
@@ -205,15 +167,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/json; charset=utf-8"
-        }
-        local body = {
-            msgtype = "text",
-            text = {
-                content = msg
-            }
-        }
+        local header = { ["Content-Type"] = "application/json; charset=utf-8" }
+        local body = { msgtype = "text", text = { content = msg } }
         local json_data = json.encode(body)
         -- LuatOS Bug, json.encode 会将 \n 转换为 \b
         json_data = string.gsub(json_data, "\\b", "\\n")
@@ -232,14 +187,8 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/json; charset=utf-8"
-        }
-        local body = {
-            token = config.PUSHOVER_API_TOKEN,
-            user = config.PUSHOVER_USER_KEY,
-            message = msg
-        }
+        local header = { ["Content-Type"] = "application/json; charset=utf-8" }
+        local body = { token = config.PUSHOVER_API_TOKEN, user = config.PUSHOVER_USER_KEY, message = msg }
 
         local json_data = json.encode(body)
         -- LuatOS Bug, json.encode 会将 \n 转换为 \b
@@ -293,9 +242,7 @@ local notify = {
             return
         end
 
-        local header = {
-            ["Content-Type"] = "application/x-www-form-urlencoded"
-        }
+        local header = { ["Content-Type"] = "application/x-www-form-urlencoded" }
         local body = {
             user = config.NEXT_SMTP_PROXY_USER,
             password = config.NEXT_SMTP_PROXY_PASSWORD,
@@ -304,7 +251,7 @@ local notify = {
             form_name = config.NEXT_SMTP_PROXY_FORM_NAME,
             to_email = config.NEXT_SMTP_PROXY_TO_EMAIL,
             subject = config.NEXT_SMTP_PROXY_SUBJECT,
-            text = msg
+            text = msg,
         }
 
         log.info("util_notify", "POST", config.NEXT_SMTP_PROXY_API)
@@ -318,23 +265,18 @@ local notify = {
             password = config.SMTP_PASSWORD,
             mail_from = config.SMTP_MAIL_FROM,
             mail_to = config.SMTP_MAIL_TO,
-            tls_enable = config.SMTP_TLS_ENABLE
+            tls_enable = config.SMTP_TLS_ENABLE,
         }
         local result = lib_smtp.send(msg, config.SMTP_MAIL_SUBJECT, smtp_config)
         log.info("util_notify", "SMTP", result.success, result.message, result.is_retry)
-        if result.success then
-            return 200, nil, result.message
-        end
-        if result.is_retry then
-            return 500, nil, result.message
-        end
+        if result.success then return 200, nil, result.message end
+        if result.is_retry then return 500, nil, result.message end
         return 400, nil, result.message
     end,
     -- 发送到 serial
     ["serial"] = function(msg)
         uart.write(1, msg)
-        log.info("util_notify", msg)
-        log.info("util_notify", "消息已转发到串口")
+        log.info("util_notify", "serial", "消息已转发到串口")
         sys.wait(1000)
         return 200
     end,
@@ -345,9 +287,7 @@ local function append()
 
     -- 本机号码
     local number = mobile.number(mobile.simid())
-    if number then
-        msg = msg .. "\n本机号码: " .. number
-    end
+    if number then msg = msg .. "\n本机号码: " .. number end
 
     -- 开机时长
     local ms = mcu.ticks()
@@ -357,27 +297,19 @@ local function append()
     seconds = seconds % 60
     minutes = minutes % 60
     local boot_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)
-    if ms >= 0 then
-        msg = msg .. "\n开机时长: " .. boot_time
-    end
+    if ms >= 0 then msg = msg .. "\n开机时长: " .. boot_time end
 
     -- 运营商
     local oper = util_mobile.getOper(true)
-    if oper ~= "" then
-        msg = msg .. "\n运营商: " .. oper
-    end
+    if oper ~= "" then msg = msg .. "\n运营商: " .. oper end
 
     -- 信号
     local rsrp = mobile.rsrp()
-    if rsrp ~= 0 then
-        msg = msg .. "\n信号: " .. rsrp .. "dBm"
-    end
+    if rsrp ~= 0 then msg = msg .. "\n信号: " .. rsrp .. "dBm" end
 
     -- 频段
     local band = util_mobile.getBand()
-    if band >= 0 then
-        msg = msg .. "\n频段: B" .. band
-    end
+    if band >= 0 then msg = msg .. "\n频段: B" .. band end
 
     -- 流量统计
     local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
@@ -428,18 +360,6 @@ function util_notify.send(msg, channel)
         return true
     end
 
-    -- 通知内容追加更多信息
-    -- 若已经包含则不再追加
-    local isappend = true
-    if string.find(msg,"本机号码:") and string.find(msg,"开机时长:") then
-        log.info("util_notify.send", "不追加更多信息")
-        isappend = false
-    end
-
-    if config.NOTIFY_APPEND_MORE_INFO and isappend then
-        msg = msg .. append()
-    end
-
     -- 发送通知
     local code, headers, body = notify[channel](msg)
     if code == nil then
@@ -466,22 +386,24 @@ end
 -- @param msg 消息内容
 -- @param channels 通知渠道
 function util_notify.add(msg, channels)
-    if type(msg) == "table" then
-        msg = table.concat(msg, "\n")
+    if type(msg) == "table" then msg = table.concat(msg, "\n") end
+
+    -- 通知内容追加更多信息, 若已经包含则不再追加
+    local is_append = true
+    if string.find(msg, "本机号码:") and string.find(msg, "开机时长:") then
+        log.info("util_notify.send", "不追加更多信息")
+        is_append = false
     end
+    if config.NOTIFY_APPEND_MORE_INFO and is_append then msg = msg .. append() end
 
     channels = channels or config.NOTIFY_TYPE
 
-    if type(channels) ~= "table" then
-        channels = {channels}
-    end
+    if type(channels) ~= "table" then channels = { channels } end
 
-    for _, channel in ipairs(channels) do
-        table.insert(msg_queue, {channel = channel, msg = msg, retry = 0})
-    end
+    for _, channel in ipairs(channels) do table.insert(msg_queue, { channel = channel, msg = msg, retry = 0 }) end
     sys.publish("NEW_MSG")
     log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
-    log.debug("util_notify.add", "添加到消息队列的内容:", msg)
+    log.debug("util_notify.add", "添加到消息队列的内容:", msg:gsub("\r", "\\r"):gsub("\n", "\\n"))
 end
 
 -- 轮询消息队列