修改推送消息为串口,去掉联网获取基站和位置

This commit is contained in:
meng 2024-03-09 21:39:10 +08:00
parent af39f6004e
commit a5522897a3
4 changed files with 124 additions and 354 deletions

View File

@ -1,52 +1,6 @@
return { return {
-- 通知类型, 支持配置多个 NOTIFY_TYPE = "uart",
-- NOTIFY_TYPE = {"telegram", "pushdeer", "bark", "dingtalk", "feishu", "wecom", "pushover", "inotify", "next-smtp-proxy", "gotify"}, UART_ID = 1,
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 = "",
-- --
-- 定时查询流量间隔, 单位毫秒, 设置为 0 关闭 (建议检查 util_mobile.lua 文件中运营商号码和查询代码是否正确, 以免发错短信导致扣费, 收到查询结果短信发送通知会消耗流量) -- 定时查询流量间隔, 单位毫秒, 设置为 0 关闭 (建议检查 util_mobile.lua 文件中运营商号码和查询代码是否正确, 以免发错短信导致扣费, 收到查询结果短信发送通知会消耗流量)
QUERY_TRAFFIC_INTERVAL = 0, QUERY_TRAFFIC_INTERVAL = 0,

View File

@ -21,7 +21,7 @@ socket.setDNS(nil, 2, "223.5.5.5")
mobile.setAuto(1000 * 10) mobile.setAuto(1000 * 10)
-- 开启 IPv6 -- 开启 IPv6
mobile.ipv6(true) -- mobile.ipv6(true)
-- POWERKEY -- POWERKEY
local button_last_press_time, button_last_release_time = 0, 0 local button_last_press_time, button_last_release_time = 0, 0
@ -56,10 +56,10 @@ gpio.setup(
-- 加载模块 -- 加载模块
config = require "config" config = require "config"
util_http = require "util_http" -- util_http = require "util_http"
util_netled = require "util_netled" util_netled = require "util_netled"
util_mobile = require "util_mobile" util_mobile = require "util_mobile"
util_location = require "util_location" -- util_location = require "util_location"
util_notify = require "util_notify" util_notify = require "util_notify"
-- 短信接收回调 -- 短信接收回调
@ -76,15 +76,16 @@ sms.setNewSmsCb(
sms.send(receiver_number, sms_content_to_be_sent) sms.send(receiver_number, sms_content_to_be_sent)
is_sms_ctrl = true is_sms_ctrl = true
end end
local my_number = mobile.number(mobile.simid())
-- 发送通知 -- 发送通知
util_notify.add( util_notify.add(
{ {
"#" .. string.gsub(my_number,"+86","",1) .. "收到短信来自" .. sender_number,
"",
sms_content, sms_content,
"", "",
"发件号码: " .. sender_number,
"发件时间: " .. time, "发件时间: " .. time,
"#SMS" .. (is_sms_ctrl and " #CTRL" or "") "短信状态: " .. (is_sms_ctrl and "控制" or "正常")
} }
) )
end end
@ -99,7 +100,7 @@ sys.taskInit(
-- 开机通知 -- 开机通知
if config.BOOT_NOTIFY then if config.BOOT_NOTIFY then
util_notify.add("#BOOT") util_notify.add("#开机通知")
end end
-- 定时查询流量 -- 定时查询流量
@ -107,10 +108,10 @@ sys.taskInit(
sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL) sys.timerLoopStart(util_mobile.queryTraffic, config.QUERY_TRAFFIC_INTERVAL)
end end
-- 定时基站定位 -- -- 定时基站定位
if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then -- if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then
sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30) -- sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30)
end -- end
-- 电源键短按发送测试通知 -- 电源键短按发送测试通知
sys.subscribe( sys.subscribe(

View File

@ -25,15 +25,15 @@ end
-- @param lat 纬度 -- @param lat 纬度
-- @param lng 经度 -- @param lng 经度
-- @return 地图链接 or "" -- @return 地图链接 or ""
local function getMapLink(lat, lng) -- local function getMapLink(lat, lng)
lat, lng = lat or 0, lng or 0 -- lat, lng = lat or 0, lng or 0
local map_link = "" -- local map_link = ""
if lat ~= 0 and lng ~= 0 then -- 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 -- map_link = "http://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
end -- end
log.debug("util_location.getMapLink", map_link) -- log.debug("util_location.getMapLink", map_link)
return map_link -- return map_link
end -- end
--- 格式化基站信息 --- 格式化基站信息
-- @param cell_info_raw 基站信息 -- @param cell_info_raw 基站信息
@ -112,23 +112,23 @@ function util_location.refresh(timeout, is_refresh_cell_info_disabled)
return lat, lng return lat, lng
end end
local cellocation = function(cell_info_formatted) -- local cellocation = function(cell_info_formatted)
local lbs_api = "http://api.cellocation.com:83/loc/?output=json&cl=" .. 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) -- local code, headers, body = util_http.fetch(timeout, "GET", lbs_api)
log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body) -- log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body)
if code ~= 200 or body == nil or body == "" then -- if code ~= 200 or body == nil or body == "" then
return -- return
end -- end
local lbs_data = json.decode(body) or {} -- local lbs_data = json.decode(body) or {}
local errcode, lat, lng = lbs_data.errcode, lbs_data.lat, lbs_data.lon -- 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 -- if errcode ~= 0 or lat == nil or lng == nil or lat == "0.0" or lng == "0.0" then
return -- return
end -- end
return lat, lng -- return lat, lng
end -- end
sys.taskInit( sys.taskInit(
function() function()
@ -143,15 +143,15 @@ function util_location.refresh(timeout, is_refresh_cell_info_disabled)
return return
end end
local lat, lng -- local lat, lng
if api_type == "openluat" then -- if api_type == "openluat" then
lat, lng = openluat(cell_info_formatted) -- lat, lng = openluat(cell_info_formatted)
elseif api_type == "cellocation" then -- elseif api_type == "cellocation" then
lat, lng = cellocation(cell_info_formatted) -- lat, lng = cellocation(cell_info_formatted)
end -- end
if lat and lng then -- if lat and lng then
cache.lbs_data = {lat, lng} -- cache.lbs_data = {lat, lng}
end -- end
end end
) )
end end

View File

@ -14,259 +14,74 @@ local function urlencodeTab(params)
end end
local notify = { local notify = {
-- 发送到 telegram -- 发送到 串口
["telegram"] = function(msg) ["uart"] = function(msg)
if config.TELEGRAM_PROXY_API == nil or config.TELEGRAM_PROXY_API == "" then if config.UART_ID == nil or config.UART_ID == "" then
log.error("util_notify", "未配置 `config.TELEGRAM_PROXY_API`") log.error("util_notify", "未配置 `config.UART_ID`")
return return
end end
--初始化
local header = { -- 最常用115200 8N1
["content-type"] = "text/plain", uart.setup(config.UART_ID, 115200, 8, 1, uart.NONE)
["x-disable-web-page-preview"] = "1", log.info("util_notify", "发送到串口", config.UART_ID, msg)
["x-chat-id"] = config.TELEGRAM_CHAT_ID or "", -- 写入可见字符串
["x-token"] = config.TELEGRAM_TOKEN or "" local res_body = uart.write(config.UART_ID, msg)
} return 200, '', res_body
log.info("util_notify", "POST", config.TELEGRAM_PROXY_API)
return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
end, end,
-- 发送到 gotify -- -- 发送到 dingtalk
["gotify"] = function(msg) -- ["dingtalk"] = function(msg)
if config.GOTIFY_API == nil or config.GOTIFY_API == "" then -- if config.DINGTALK_WEBHOOK == nil or config.DINGTALK_WEBHOOK == "" then
log.error("util_notify", "未配置 `config.GOTIFY_API`") -- log.error("util_notify", "未配置 `config.DINGTALK_WEBHOOK`")
return -- return
end -- end
if config.GOTIFY_TOKEN == nil or config.GOTIFY_TOKEN == "" then
log.error("util_notify", "未配置 `config.GOTIFY_TOKEN`")
return
end
local url = config.GOTIFY_API .. "/message?token=" .. config.GOTIFY_TOKEN -- local header = {
local header = { -- ["Content-Type"] = "application/json; charset=utf-8"
["Content-Type"] = "application/json; charset=utf-8" -- }
} -- local body = {
local body = { -- msgtype = "text",
title = config.GOTIFY_TITLE, -- text = {
message = msg, -- content = msg
priority = config.GOTIFY_PRIORITY -- }
} -- }
local json_data = json.encode(body) -- local json_data = json.encode(body)
json_data = string.gsub(json_data, "\\b", "\\n") -- -- LuatOS Bug, json.encode 会将 \n 转换为 \b
-- json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify", "POST", config.GOTIFY_API) -- log.info("util_notify", "POST", config.DINGTALK_WEBHOOK)
return util_http.fetch(nil, "POST", url, header, json_data) -- return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
end, -- end,
-- 发送到 pushdeer -- -- 发送到 wecom
["pushdeer"] = function(msg) -- ["wecom"] = function(msg)
if config.PUSHDEER_API == nil or config.PUSHDEER_API == "" then -- if config.WECOM_WEBHOOK == nil or config.WECOM_WEBHOOK == "" then
log.error("util_notify", "未配置 `config.PUSHDEER_API`") -- log.error("util_notify", "未配置 `config.WECOM_WEBHOOK`")
return -- return
end -- end
if config.PUSHDEER_KEY == nil or config.PUSHDEER_KEY == "" then
log.error("util_notify", "未配置 `config.PUSHDEER_KEY`")
return
end
local header = { -- local header = {
["Content-Type"] = "application/x-www-form-urlencoded" -- ["Content-Type"] = "application/json; charset=utf-8"
} -- }
local body = { -- local body = {
pushkey = config.PUSHDEER_KEY or "", -- msgtype = "text",
type = "text", -- text = {
text = msg -- 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) -- log.info("util_notify", "POST", config.WECOM_WEBHOOK)
return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body)) -- return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
end, -- 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
} }
local function append() local function append()
local msg = "\n" 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 if number then
msg = msg .. "\n本机号码: " .. number msg = msg .. "\n本机号码: " .. number
end end
@ -295,34 +110,34 @@ local function append()
msg = msg .. "\n信号: " .. rsrp .. "dBm" msg = msg .. "\n信号: " .. rsrp .. "dBm"
end end
-- 频段 -- -- 频段
local band = util_mobile.getBand() -- local band = util_mobile.getBand()
if band >= 0 then -- if band >= 0 then
msg = msg .. "\n频段: B" .. band -- msg = msg .. "\n频段: B" .. band
end -- end
-- 流量统计 -- 流量统计
local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic() -- local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB -- uplinkB = uplinkGB * 1024 * 1024 * 1024 + uplinkB
downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB -- downlinkB = downlinkGB * 1024 * 1024 * 1024 + downlinkB
local function formatBytes(bytes) -- local function formatBytes(bytes)
if bytes < 1024 then -- if bytes < 1024 then
return bytes .. "B" -- return bytes .. "B"
elseif bytes < 1024 * 1024 then -- elseif bytes < 1024 * 1024 then
return string.format("%.2fKB", bytes / 1024) -- return string.format("%.2fKB", bytes / 1024)
elseif bytes < 1024 * 1024 * 1024 then -- elseif bytes < 1024 * 1024 * 1024 then
return string.format("%.2fMB", bytes / 1024 / 1024) -- return string.format("%.2fMB", bytes / 1024 / 1024)
else -- else
return string.format("%.2fGB", bytes / 1024 / 1024 / 1024) -- return string.format("%.2fGB", bytes / 1024 / 1024 / 1024)
end -- end
end -- end
-- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB) -- msg = msg .. "\n流量: ↑" .. formatBytes(uplinkB) .. " ↓" .. formatBytes(downlinkB)
-- 位置 -- 位置
local _, _, map_link = util_location.get() -- local _, _, map_link = util_location.get()
if map_link ~= "" then -- if map_link ~= "" then
msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行 -- msg = msg .. "\n位置: " .. map_link -- 这里使用 U+00a0 防止换行
end -- end
return msg return msg
end end