From a5522897a351128be4adb434b26bceaf8ba89f18 Mon Sep 17 00:00:00 2001 From: meng Date: Sat, 9 Mar 2024 21:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A8=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=B8=BA=E4=B8=B2=E5=8F=A3=EF=BC=8C=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E8=81=94=E7=BD=91=E8=8E=B7=E5=8F=96=E5=9F=BA=E7=AB=99=E5=92=8C?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/config.lua | 50 +----- script/main.lua | 23 +-- script/util_location.lua | 64 ++++---- script/util_notify.lua | 341 +++++++++------------------------------ 4 files changed, 124 insertions(+), 354 deletions(-) diff --git a/script/config.lua b/script/config.lua index 2094105..3314e3c 100644 --- a/script/config.lua +++ b/script/config.lua @@ -1,52 +1,6 @@ return { - -- 通知类型, 支持配置多个 - -- NOTIFY_TYPE = {"telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify"}, - NOTIFY_TYPE = "pushdeer", - -- - -- telegram 通知配置, https://github.com/0wQ/telegram-notify - TELEGRAM_PROXY_API = "", - TELEGRAM_TOKEN = "", - TELEGRAM_CHAT_ID = "", - -- - -- pushdeer 通知配置, https://www.pushdeer.com/ - PUSHDEER_API = "https://api2.pushdeer.com/message/push", - PUSHDEER_KEY = "", - -- - -- bark 通知配置, https://github.com/Finb/Bark - BARK_API = "https://api.day.app", - BARK_KEY = "", - -- - -- dingtalk 通知配置, https://open.dingtalk.com/document/robots/custom-robot-access - DINGTALK_WEBHOOK = "", - -- - -- feishu 通知配置, https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN - FEISHU_WEBHOOK = "", - -- - -- wecom 通知配置, https://developer.work.weixin.qq.com/document/path/91770 - WECOM_WEBHOOK = "", - -- - -- pushover 通知配置, https://pushover.net/api - PUSHOVER_API_TOKEN = "", - PUSHOVER_USER_KEY = "", - -- - -- inotify 通知配置, https://github.com/xpnas/Inotify 或者使用合宙提供的 https://push.luatos.org - INOTIFY_API = "https://push.luatos.org/XXXXXX.send", - -- - -- next-smtp-proxy 通知配置, https://github.com/0wQ/next-smtp-proxy - NEXT_SMTP_PROXY_API = "", - NEXT_SMTP_PROXY_USER = "", - NEXT_SMTP_PROXY_PASSWORD = "", - NEXT_SMTP_PROXY_HOST = "smtp-mail.outlook.com", - NEXT_SMTP_PROXY_PORT = 587, - NEXT_SMTP_PROXY_FORM_NAME = "Air780E", - NEXT_SMTP_PROXY_TO_EMAIL = "", - NEXT_SMTP_PROXY_SUBJECT = "来自 Air780E 的通知", - -- - -- gotify 通知配置, https://gotify.net/ - GOTIFY_API = "", - GOTIFY_TITLE = "Air780E", - GOTIFY_PRIORITY = 8, - GOTIFY_TOKEN = "", + NOTIFY_TYPE = "uart", + UART_ID = 1, -- -- 定时查询流量间隔, 单位毫秒, 设置为 0 关闭 (建议检查 util_mobile.lua 文件中运营商号码和查询代码是否正确, 以免发错短信导致扣费, 收到查询结果短信发送通知会消耗流量) QUERY_TRAFFIC_INTERVAL = 0, diff --git a/script/main.lua b/script/main.lua index 60412b8..7f61e72 100644 --- a/script/main.lua +++ b/script/main.lua @@ -21,7 +21,7 @@ socket.setDNS(nil, 2, "223.5.5.5") mobile.setAuto(1000 * 10) -- 开启 IPv6 -mobile.ipv6(true) +-- mobile.ipv6(true) -- POWERKEY local button_last_press_time, button_last_release_time = 0, 0 @@ -56,10 +56,10 @@ gpio.setup( -- 加载模块 config = require "config" -util_http = require "util_http" +-- util_http = require "util_http" util_netled = require "util_netled" util_mobile = require "util_mobile" -util_location = require "util_location" +-- util_location = require "util_location" util_notify = require "util_notify" -- 短信接收回调 @@ -76,15 +76,16 @@ sms.setNewSmsCb( sms.send(receiver_number, sms_content_to_be_sent) is_sms_ctrl = true end - + local my_number = mobile.number(mobile.simid()) -- 发送通知 util_notify.add( { + "#" .. string.gsub(my_number,"+86","",1) .. "收到短信来自" .. sender_number, + "", sms_content, "", - "发件号码: " .. sender_number, "发件时间: " .. time, - "#SMS" .. (is_sms_ctrl and " #CTRL" or "") + "短信状态: " .. (is_sms_ctrl and "控制" or "正常") } ) end @@ -99,7 +100,7 @@ sys.taskInit( -- 开机通知 if config.BOOT_NOTIFY then - util_notify.add("#BOOT") + util_notify.add("#开机通知") end -- 定时查询流量 @@ -107,10 +108,10 @@ sys.taskInit( 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 + -- -- 定时基站定位 + -- if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then + -- sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30) + -- end -- 电源键短按发送测试通知 sys.subscribe( diff --git a/script/util_location.lua b/script/util_location.lua index 69531a7..63e8820 100644 --- a/script/util_location.lua +++ b/script/util_location.lua @@ -25,15 +25,15 @@ end -- @param lat 纬度 -- @param lng 经度 -- @return 地图链接 or "" -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 - log.debug("util_location.getMapLink", map_link) - return map_link -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 +-- log.debug("util_location.getMapLink", map_link) +-- return map_link +-- end --- 格式化基站信息 -- @param cell_info_raw 基站信息 @@ -112,23 +112,23 @@ function util_location.refresh(timeout, is_refresh_cell_info_disabled) return lat, lng end - local cellocation = function(cell_info_formatted) - local lbs_api = "http://api.cellocation.com:83/loc/?output=json&cl=" .. cell_info_formatted - local code, headers, body = util_http.fetch(timeout, "GET", lbs_api) - log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body) + -- local cellocation = function(cell_info_formatted) + -- local lbs_api = "http://api.cellocation.com:83/loc/?output=json&cl=" .. cell_info_formatted + -- local code, headers, body = util_http.fetch(timeout, "GET", lbs_api) + -- log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body) - if code ~= 200 or body == nil or body == "" then - return - end + -- if code ~= 200 or body == nil or body == "" then + -- return + -- end - local lbs_data = json.decode(body) or {} - local errcode, lat, lng = lbs_data.errcode, lbs_data.lat, lbs_data.lon - if errcode ~= 0 or lat == nil or lng == nil or lat == "0.0" or lng == "0.0" then - return - end + -- local lbs_data = json.decode(body) or {} + -- local errcode, lat, lng = lbs_data.errcode, lbs_data.lat, lbs_data.lon + -- if errcode ~= 0 or lat == nil or lng == nil or lat == "0.0" or lng == "0.0" then + -- return + -- end - return lat, lng - end + -- return lat, lng + -- end sys.taskInit( function() @@ -143,15 +143,15 @@ function util_location.refresh(timeout, is_refresh_cell_info_disabled) return end - local lat, lng - if api_type == "openluat" then - lat, lng = openluat(cell_info_formatted) - elseif api_type == "cellocation" then - lat, lng = cellocation(cell_info_formatted) - end - if lat and lng then - cache.lbs_data = {lat, lng} - end + -- local lat, lng + -- if api_type == "openluat" then + -- lat, lng = openluat(cell_info_formatted) + -- elseif api_type == "cellocation" then + -- lat, lng = cellocation(cell_info_formatted) + -- end + -- if lat and lng then + -- cache.lbs_data = {lat, lng} + -- end end ) end diff --git a/script/util_notify.lua b/script/util_notify.lua index 92dc589..26923e2 100644 --- a/script/util_notify.lua +++ b/script/util_notify.lua @@ -14,259 +14,74 @@ local function urlencodeTab(params) end local notify = { - -- 发送到 telegram - ["telegram"] = function(msg) - if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then - log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`") + -- 发送到 串口 + ["uart"] = function(msg) + if config.UART_ID == nil or config.UART_ID == "" then + log.error("util_notify", "未配置 `config.UART_ID`") return end - - local header = { - ["content-type"] = "text/plain", - ["x-disable-web-page-preview"] = "1", - ["x-chat-id"] = config.TELEGRAM_CHAT_ID or "", - ["x-token"] = config.TELEGRAM_TOKEN or "" - } - - log.info("util_notify", "POST", config.TELEGRAM_PROXY_API) - return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg) + --初始化 + -- 最常用115200 8N1 + uart.setup(config.UART_ID, 115200, 8, 1, uart.NONE) + log.info("util_notify", "发送到串口", config.UART_ID, msg) + -- 写入可见字符串 + local res_body = uart.write(config.UART_ID, msg) + return 200, '', res_body end, - -- 发送到 gotify - ["gotify"] = function(msg) - if config.GOTIFY_API == nil or config.GOTIFY_API == "" then - log.error("util_notify", "未配置 `config.GOTIFY_API`") - return - end - if config.GOTIFY_TOKEN == nil or config.GOTIFY_TOKEN == "" then - log.error("util_notify", "未配置 `config.GOTIFY_TOKEN`") - return - end + -- -- 发送到 dingtalk + -- ["dingtalk"] = function(msg) + -- if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then + -- log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`") + -- return + -- 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 json_data = json.encode(body) - json_data = string.gsub(json_data, "\\b", "\\n") + -- 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") - log.info("util_notify", "POST", config.GOTIFY_API) - return util_http.fetch(nil, "POST", url, header, json_data) - end, - -- 发送到 pushdeer - ["pushdeer"] = function(msg) - if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then - log.error("util_notify", "未配置 `config.PUSHDEER_API`") - return - end - if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then - log.error("util_notify", "未配置 `config.PUSHDEER_KEY`") - return - end + -- log.info("util_notify", "POST", config.DINGTALK_WEBHOOK) + -- return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data) + -- end, + -- -- 发送到 wecom + -- ["wecom"] = function(msg) + -- if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then + -- log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`") + -- 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/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") - log.info("util_notify", "POST", config.PUSHDEER_API) - return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body)) - end, - -- 发送到 bark - ["bark"] = function(msg) - if config.BARK_API == nil or config.BARK_API == "" then - log.error("util_notify", "未配置 `config.BARK_API`") - return - end - if config.BARK_KEY == nil or config.BARK_KEY == "" then - log.error("util_notify", "未配置 `config.BARK_KEY`") - return - end - - 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) - return util_http.fetch(nil, "POST", url, header, urlencodeTab(body)) - end, - -- 发送到 dingtalk - ["dingtalk"] = function(msg) - if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then - log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`") - return - end - - 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") - - log.info("util_notify", "POST", config.DINGTALK_WEBHOOK) - return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data) - end, - -- 发送到 feishu - ["feishu"] = function(msg) - if config.FEISHU_WEBHOOK == nil or config.FEISHU_WEBHOOK == "" then - log.error("util_notify", "未配置 `config.FEISHU_WEBHOOK`") - return - end - - 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") - - log.info("util_notify", "POST", config.FEISHU_WEBHOOK) - return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data) - end, - -- 发送到 wecom - ["wecom"] = function(msg) - if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then - log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`") - return - end - - 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") - - log.info("util_notify", "POST", config.WECOM_WEBHOOK) - return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data) - end, - -- 发送到 pushover - ["pushover"] = function(msg) - if config.PUSHOVER_API_TOKEN == nil or config.PUSHOVER_API_TOKEN == "" then - log.error("util_notify", "未配置 `config.PUSHOVER_API_TOKEN`") - return - end - if config.PUSHOVER_USER_KEY == nil or config.PUSHOVER_USER_KEY == "" then - log.error("util_notify", "未配置 `config.PUSHOVER_USER_KEY`") - 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 json_data = json.encode(body) - -- LuatOS Bug, json.encode 会将 \n 转换为 \b - json_data = string.gsub(json_data, "\\b", "\\n") - - local url = "https://api.pushover.net/1/messages.json" - - log.info("util_notify", "POST", url) - return util_http.fetch(nil, "POST", url, header, json_data) - end, - -- 发送到 inotify - ["inotify"] = function(msg) - if config.INOTIFY_API == nil or config.INOTIFY_API == "" then - log.error("util_notify", "未配置 `config.INOTIFY_API`") - return - end - if not config.INOTIFY_API:endsWith(".send") then - log.error("util_notify", "`config.INOTIFY_API` 必须以 `.send` 结尾") - return - end - - local url = config.INOTIFY_API .. "/" .. string.urlEncode(msg) - - log.info("util_notify", "GET", url) - return util_http.fetch(nil, "GET", url) - end, - -- 发送到 next-smtp-proxy - ["next-smtp-proxy"] = function(msg) - if config.NEXT_SMTP_PROXY_API == nil or config.NEXT_SMTP_PROXY_API == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_API`") - return - end - if config.NEXT_SMTP_PROXY_USER == nil or config.NEXT_SMTP_PROXY_USER == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_USER`") - return - end - if config.NEXT_SMTP_PROXY_PASSWORD == nil or config.NEXT_SMTP_PROXY_PASSWORD == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PASSWORD`") - return - end - if config.NEXT_SMTP_PROXY_HOST == nil or config.NEXT_SMTP_PROXY_HOST == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_HOST`") - return - end - if config.NEXT_SMTP_PROXY_PORT == nil or config.NEXT_SMTP_PROXY_PORT == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_PORT`") - return - end - if config.NEXT_SMTP_PROXY_TO_EMAIL == nil or config.NEXT_SMTP_PROXY_TO_EMAIL == "" then - log.error("util_notify", "未配置 `config.NEXT_SMTP_PROXY_TO_EMAIL`") - return - end - - local header = { - ["Content-Type"] = "application/x-www-form-urlencoded" - } - local body = { - user = config.NEXT_SMTP_PROXY_USER, - password = config.NEXT_SMTP_PROXY_PASSWORD, - host = config.NEXT_SMTP_PROXY_HOST, - port = config.NEXT_SMTP_PROXY_PORT, - form_name = config.NEXT_SMTP_PROXY_FORM_NAME, - to_email = config.NEXT_SMTP_PROXY_TO_EMAIL, - subject = config.NEXT_SMTP_PROXY_SUBJECT, - text = msg - } - - 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 + -- log.info("util_notify", "POST", config.WECOM_WEBHOOK) + -- return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data) + -- end, } local function append() local msg = "\n" -- 本机号码 - local number = mobile.number(mobile.simid()) + local my_number = mobile.number(mobile.simid()) + local number = string.gsub(my_number,"+86","",1) if number then msg = msg .. "\n本机号码: " .. number end @@ -295,34 +110,34 @@ local function append() msg = msg .. "\n信号: " .. rsrp .. "dBm" end - -- 频段 - local band = util_mobile.getBand() - if band >= 0 then - msg = msg .. "\n频段: B" .. band - end + -- -- 频段 + -- local band = util_mobile.getBand() + -- if band >= 0 then + -- msg = msg .. "\n频段: B" .. band + -- end -- 流量统计 - local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic() - uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB - downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB - local function formatBytes(bytes) - if bytes < 1024 then - return bytes .. "B" - elseif bytes < 1024 * 1024 then - return string.format("%.2fKB", bytes / 1024) - elseif bytes < 1024 * 1024 * 1024 then - return string.format("%.2fMB", bytes / 1024 / 1024) - else - return string.format("%.2fGB", bytes / 1024 / 1024 / 1024) - end - end + -- local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic() + -- uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB + -- downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB + -- local function formatBytes(bytes) + -- if bytes < 1024 then + -- return bytes .. "B" + -- elseif bytes < 1024 * 1024 then + -- return string.format("%.2fKB", bytes / 1024) + -- elseif bytes < 1024 * 1024 * 1024 then + -- return string.format("%.2fMB", bytes / 1024 / 1024) + -- else + -- return string.format("%.2fGB", bytes / 1024 / 1024 / 1024) + -- end + -- end -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB) -- 位置 - local _, _, map_link = util_location.get() - if map_link ~= "" then - msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行 - end + -- local _, _, map_link = util_location.get() + -- if map_link ~= "" then + -- msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行 + -- end return msg end