Browse Source

Merge pull request #27 from FSJohn/main

增加主从模式和串口转发
Mizore 2 years ago
parent
commit
6dafa80fda
4 changed files with 54 additions and 6 deletions
  1. 1 0
      README.md
  2. 6 2
      script/config.lua
  3. 16 0
      script/main.lua
  4. 31 4
      script/util_notify.lua

+ 1 - 0
README.md

@@ -22,6 +22,7 @@
 - [x] 低功耗模式 (使用 IoT Power 测量, 开发板待机 30min 平均电流 2.5mA)
 - [x] 使用消息队列, 经测试同时发送几百条通知, 不会卡死
 - [x] 通知发送失败, 自动重发
+- [x] 支持主从模式,一主对多从,从机通过串口转发消息,主机接受消息后转发到通知服务
 
 ## :hammer: Usage
 

+ 6 - 2
script/config.lua

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

+ 16 - 0
script/main.lua

@@ -9,6 +9,9 @@ sys = require "sys"
 sysplus = require "sysplus"
 require "sysplus"
 
+-- 串口配置
+uart.setup(1, 115200, 8, 1, uart.NONE)
+
 -- 添加硬狗防止程序卡死
 wdt.init(9000)
 sys.timerLoopStart(wdt.feed, 3000)
@@ -62,6 +65,19 @@ 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)
+
 -- 短信接收回调
 sms.setNewSmsCb(
     function(sender_number, sms_content, m)

+ 31 - 4
script/util_notify.lua

@@ -307,6 +307,14 @@ local notify = {
 
         log.info("util_notify", "POST", config.NEXT_SMTP_PROXY_API)
         return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
+    end,
+    -- 发送到 serial
+    ["serial"] = function(msg)
+        uart.write(1, msg)
+        log.info("util_notify", msg)
+        log.info("util_notify", "消息已转发到串口")
+        sys.wait(1000)
+        return 200
     end
 }
 
@@ -399,7 +407,14 @@ function util_notify.send(msg, channel)
     end
 
     -- 通知内容追加更多信息
-    if config.NOTIFY_APPEND_MORE_INFO then
+    -- 若已经包含则不再追加
+    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
 
@@ -444,16 +459,23 @@ function util_notify.add(msg, channels)
     end
     sys.publish("NEW_MSG")
     log.debug("util_notify.add", "添加到消息队列, 当前队列长度:", #msg_queue)
+    log.debug("util_notify.add", "添加到消息队列的内容:", msg)
 end
 
 -- 轮询消息队列
 -- 发送成功则从消息队列中删除
 -- 发送失败则等待下次轮询
 local function poll()
-    local item, result
+    local item, result, roaming
     while true do
         -- 消息队列非空, 且网络已注册
-        if next(msg_queue) ~= nil and mobile.status() == 1 then
+        if next(msg_queue) ~= nil and (mobile.status() == 1 or mobile.status() == 5) then
+            -- 判断是否漫游
+            if mobile.status() == 5 then
+                roaming = true
+            else
+                roaming = false
+            end
             log.debug("util_notify.poll", "轮询消息队列中, 当前队列长度:", #msg_queue)
 
             item = msg_queue[1]
@@ -462,7 +484,12 @@ local function poll()
             if item.retry > (config.NOTIFY_RETRY_MAX or 100) then
                 log.error("util_notify.poll", "超过最大重发次数", "msg:", item.msg)
             else
-                result = util_notify.send(item.msg, item.channel)
+                if roaming then
+                    log.debug("util_notify.poll", "当前设备处于漫游状态只使用 Serial 通信方式")
+                    result = util_notify.send(item.msg, 'serial')
+                else
+                    result = util_notify.send(item.msg, item.channel)
+                end
                 item.retry = item.retry + 1
 
                 if not result then