Browse Source

:bug: 修复未开启串口转发时, 也会初始化uart导致误触发收到错乱数据, fix #36

Mizore 1 year ago
parent
commit
107c1f2fad
2 changed files with 20 additions and 19 deletions
  1. 4 4
      script/config.lua
  2. 16 15
      script/main.lua

+ 4 - 4
script/config.lua

@@ -1,12 +1,12 @@
 return {
-    -- 角色类型 MASTER: 主机,可主动联网; SLAVE: 从机,不可主动联网,通过串口发送数据
-    -- ROLE = {"MASTER","SLAVE"}
-    ROLE = "MASTER",
-    --
     -- 通知类型, 支持配置多个
     -- NOTIFY_TYPE = {"custom_post", "telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify", "serial"},
     NOTIFY_TYPE = "custom_post",
     --
+    -- 角色类型, 用于区分主从机, 仅当使用串口转发 NOTIFY_TYPE = "serial" 时才需要配置
+    -- MASTER: 主机, 可主动联网; SLAVE: 从机, 不可主动联网, 通过串口发送数据
+    ROLE = "MASTER",
+    --
     -- custom_post 通知配置, 自定义 POST 请求, CUSTOM_POST_BODY_TABLE 中的 {msg} 会被替换为通知内容
     CUSTOM_POST_URL = "https://sctapi.ftqq.com/<SENDKEY>.send",
     CUSTOM_POST_CONTENT_TYPE = "application/json",

+ 16 - 15
script/main.lua

@@ -8,9 +8,6 @@ log.info("main", "开机原因", pm.lastReson())
 sys = require "sys"
 sysplus = require "sysplus"
 
--- 串口配置
-uart.setup(1, 115200, 8, 1, uart.NONE)
-
 -- 添加硬狗防止程序卡死
 wdt.init(9000)
 sys.timerLoopStart(wdt.feed, 3000)
@@ -60,18 +57,22 @@ util_mobile = require "util_mobile"
 util_location = require "util_location"
 util_notify = require "util_notify"
 
--- 串口接收回调
-uart.on(1, "receive", function(id, len)
-    local data = uart.read(id, len)
-    log.info("uart read:", id, len, data)
-    if config.ROLE == "MASTER" then
-        -- 主机, 通过队列发送数据
-        util_notify.add(data)
-    else
-        -- 从机, 通过串口发送数据
-        uart.write(1, data)
-    end
-end)
+if config.NOTIFY_TYPE == "serial" then
+    -- 串口配置
+    uart.setup(1, 115200, 8, 1, uart.NONE)
+    -- 串口接收回调
+    uart.on(1, "receive", function(id, len)
+        local data = uart.read(id, len)
+        log.info("uart read:", id, len, data)
+        if config.ROLE == "MASTER" then
+            -- 主机, 通过队列发送数据
+            util_notify.add(data)
+        else
+            -- 从机, 通过串口发送数据
+            uart.write(1, data)
+        end
+    end)
+end
 
 -- 短信接收回调
 sms.setNewSmsCb(function(sender_number, sms_content, m)